src/gl/gridprogram.cpp

changeset 156
65b75beed7e0
parent 155
13713fadbf14
child 167
c1ff4f107569
equal deleted inserted replaced
155:13713fadbf14 156:65b75beed7e0
25 layout (location = 0) in vec2 in_position; 25 layout (location = 0) in vec2 in_position;
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(1000, 0, 0, 0), vec4(0, 1000, 0, 0), vec4(0, 0, 1000, 0), vec4(0, 0, 0, 1));
31 uniform mat4 grid; 30 uniform mat4 grid;
32 31
33 void main() 32 void main()
34 { 33 {
35 gl_Position = projection * view * model * grid * stretch * vec4(in_position, 0.0, 1.0); 34 gl_Position = projection * view * model * grid * vec4(in_position, 0.0, 1.0);
36 ex_uv = in_position; 35 ex_uv = in_position;
37 } 36 }
38 )"; 37 )";
39 38
40 const char fragmentShaderSource[] = R"( 39 const char fragmentShaderSource[] = R"(
41 #version 330 core 40 #version 330 core
42 41
43 out vec4 color; 42 out vec4 color;
44 smooth in vec2 ex_uv; 43 smooth in vec2 ex_uv;
45 uniform vec4 gridColor; 44 uniform vec4 gridColor;
46 const float pi = 3.14159265f;
47 45
48 void main(void) 46 void main(void)
49 { 47 {
50 float dx = fract(ex_uv.y / 0.001f); 48 float dx = fract(ex_uv.y);
51 float dy = fract(ex_uv.x / 0.001f); 49 float dy = fract(ex_uv.x);
52 /* compute distance to nearest unit line */
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.02 - d, 100);
56 /* fade the grid towards extreme co-ordinates */ 50 /* fade the grid towards extreme co-ordinates */
57 d = (1.0f - 20 * max(abs(ex_uv.x), abs(ex_uv.y))) * d; 51 float d = (1.0f - 0.015 * max(abs(ex_uv.x), abs(ex_uv.y)));
58 /* add dashes */
59 d *= (1 + pow(cos((ex_uv.y / 0.0001f) * pi), 10)) * 0.5f;
60 d *= (1 + pow(cos((ex_uv.x / 0.0001f) * pi), 10)) * 0.5f;
61 color = vec4(gridColor.xyz, gridColor.w * d); 52 color = vec4(gridColor.xyz, gridColor.w * d);
62 } 53 }
63 )"; 54 )";
64 55
65 static const glm::vec2 data[] = {{-1, -1}, {-1, 1}, {1, 1}, {1, -1}}; 56 GridProgram::GridProgram(QObject *parent) :
57 AbstractBasicShaderProgram{parent}
58 {
59 this->gridData.reserve(8004);
60 for (int i = -1000; i < 1000; i += 1)
61 {
62 this->gridData.push_back({i, -1000});
63 this->gridData.push_back({i, 1000});
64 }
65 for (int i = -1000; i < 1000; i += 1)
66 {
67 this->gridData.push_back({-1000, i});
68 this->gridData.push_back({1000, i});
69 }
70 }
66 71
67 void GridProgram::setGridMatrix(const glm::mat4& newGridMatrix) 72 void GridProgram::setGridMatrix(const glm::mat4& newGridMatrix)
68 { 73 {
69 this->setMatrix("grid", newGridMatrix); 74 this->setMatrix("grid", newGridMatrix);
70 } 75 }
95 return ::fragmentShaderSource; 100 return ::fragmentShaderSource;
96 } 101 }
97 102
98 const void* GridProgram::vertexData() const 103 const void* GridProgram::vertexData() const
99 { 104 {
100 return ::data; 105 return this->gridData.data();
101 } 106 }
102 107
103 int GridProgram::vertexSize() const 108 int GridProgram::vertexSize() const
104 { 109 {
105 return sizeof data[0]; 110 return sizeof this->gridData[0];
106 } 111 }
107 112
108 int GridProgram::vertexCount() const 113 int GridProgram::vertexCount() const
109 { 114 {
110 return glm::countof(data); 115 return this->gridData.size();
111 } 116 }
112 117
113 void GridProgram::setupVertexArrays() 118 void GridProgram::setupVertexArrays()
114 { 119 {
115 this->program->enableAttributeArray(0); 120 this->program->enableAttributeArray(0);
117 this->program->setUniformVector("gridColor", this->gridColor); 122 this->program->setUniformVector("gridColor", this->gridColor);
118 } 123 }
119 124
120 GLenum GridProgram::drawMode() const 125 GLenum GridProgram::drawMode() const
121 { 126 {
122 return GL_QUADS; 127 return GL_LINES;
123 } 128 }
124 129
125 QOpenGLBuffer::UsagePattern GridProgram::usagePattern() const 130 QOpenGLBuffer::UsagePattern GridProgram::usagePattern() const
126 { 131 {
127 return QOpenGLBuffer::StaticDraw; 132 return QOpenGLBuffer::StaticDraw;

mercurial