src/gl/gridprogram.cpp

changeset 54
a4055f67b9c7
parent 53
3af627f7a40f
child 55
cb81ecb5fb23
equal deleted inserted replaced
53:3af627f7a40f 54:a4055f67b9c7
26 uniform mat4 view; 26 uniform mat4 view;
27 uniform mat4 projection; 27 uniform mat4 projection;
28 uniform mat4 model; 28 uniform mat4 model;
29 smooth out vec2 ex_uv; 29 smooth out vec2 ex_uv;
30 const mat4 stretch = mat4(vec4(10000, 0, 0, 0), vec4(0, 10000, 0, 0), vec4(0, 0, 10000, 0), vec4(0, 0, 0, 1)); 30 const mat4 stretch = mat4(vec4(10000, 0, 0, 0), vec4(0, 10000, 0, 0), vec4(0, 0, 10000, 0), vec4(0, 0, 0, 1));
31 const mat4 gridmatrix = mat4(vec4(1, 0, 0, 0), vec4(0, 1, 0, 0), vec4(0, 0, 1, 0), vec4(0, 0, 0, 1));
31 32
32 void main() 33 void main()
33 { 34 {
34 gl_Position = projection * view * model * stretch * vec4(in_position, 0.0, 1.0); 35 gl_Position = projection * view * model * gridmatrix * stretch * vec4(in_position, 0.0, 1.0);
35 ex_uv = in_position; 36 ex_uv = in_position;
36 } 37 }
37 )"; 38 )";
38 39
39 const char fragmentShaderSource[] = R"( 40 const char fragmentShaderSource[] = R"(
40 #version 330 core 41 #version 330 core
41 42
42 out vec4 color; 43 out vec4 color;
43 smooth in vec2 ex_uv; 44 smooth in vec2 ex_uv;
44 const vec4 lineColor = vec4(1.0, 1.0, 1.0, 0.75); 45 const vec4 lineColor = vec4(1.0, 1.0, 1.0, 0.75);
46 const float pi = 3.14159265f;
45 47
46 void main(void) 48 void main(void)
47 { 49 {
48 if(fract(ex_uv.x / 0.001f) < 0.01f || fract(ex_uv.y / 0.001f) < 0.01f) 50 float dx = fract(ex_uv.y / 0.0001f);
49 color = lineColor; 51 float dy = fract(ex_uv.x / 0.0001f);
50 else 52 /* compute distance to nearest unit line */
51 color = vec4(0); 53 float d = min(min(min(dy, dx), 1 - dy), 1 - dx);
54 /* use an extreme sigmoid to bring out the grid shape */
55 d = pow(1 - d, 50);
56 /* fade the grid towards extreme co-ordinates */
57 d = (1.0f - 20 * max(abs(ex_uv.x), abs(ex_uv.y))) * d;
58 /* add dashes */
59 d *= (1 + cos(ex_uv.y / 0.00001f * pi)) * 0.5f;
60 d *= (1 + cos(ex_uv.x / 0.00001f * pi)) * 0.5f;
61 color = vec4(lineColor.xyz, d);
52 } 62 }
53 )"; 63 )";
54 64
55 static const glm::vec2 data[] = {{-1, -1}, {-1, 1}, {1, 1}, {1, -1}}; 65 static const glm::vec2 data[] = {{-1, -1}, {-1, 1}, {1, 1}, {1, -1}};
56 66

mercurial