src/gl/gridprogram.cpp

changeset 55
cb81ecb5fb23
parent 54
a4055f67b9c7
child 64
f99d52b1646b
equal deleted inserted replaced
54:a4055f67b9c7 55:cb81ecb5fb23
40 const char fragmentShaderSource[] = R"( 40 const char fragmentShaderSource[] = R"(
41 #version 330 core 41 #version 330 core
42 42
43 out vec4 color; 43 out vec4 color;
44 smooth in vec2 ex_uv; 44 smooth in vec2 ex_uv;
45 const vec4 lineColor = vec4(1.0, 1.0, 1.0, 0.75); 45 uniform vec4 gridColor;
46 const float pi = 3.14159265f; 46 const float pi = 3.14159265f;
47 47
48 void main(void) 48 void main(void)
49 { 49 {
50 float dx = fract(ex_uv.y / 0.0001f); 50 float dx = fract(ex_uv.y / 0.0001f);
56 /* fade the grid towards extreme co-ordinates */ 56 /* fade the grid towards extreme co-ordinates */
57 d = (1.0f - 20 * max(abs(ex_uv.x), abs(ex_uv.y))) * d; 57 d = (1.0f - 20 * max(abs(ex_uv.x), abs(ex_uv.y))) * d;
58 /* add dashes */ 58 /* add dashes */
59 d *= (1 + cos(ex_uv.y / 0.00001f * pi)) * 0.5f; 59 d *= (1 + cos(ex_uv.y / 0.00001f * pi)) * 0.5f;
60 d *= (1 + cos(ex_uv.x / 0.00001f * pi)) * 0.5f; 60 d *= (1 + cos(ex_uv.x / 0.00001f * pi)) * 0.5f;
61 color = vec4(lineColor.xyz, d); 61 color = vec4(gridColor.xyz, gridColor.w * d);
62 } 62 }
63 )"; 63 )";
64 64
65 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}};
66 66
87 this->buffer.allocate(data, countof(data) * sizeof data[0]); 87 this->buffer.allocate(data, countof(data) * sizeof data[0]);
88 this->vertexArrayObject.create(); 88 this->vertexArrayObject.create();
89 this->vertexArrayObject.bind(); 89 this->vertexArrayObject.bind();
90 this->program->enableAttributeArray(0); 90 this->program->enableAttributeArray(0);
91 this->program->setAttributeBuffer(0, GL_FLOAT, 0, 2, 0); 91 this->program->setAttributeBuffer(0, GL_FLOAT, 0, 2, 0);
92 this->program->setUniformVector("gridColor", this->gridColor);
92 this->vertexArrayObject.release(); 93 this->vertexArrayObject.release();
93 this->buffer.release(); 94 this->buffer.release();
94 this->program->release(); 95 this->program->release();
95 this->checkForGLErrors(); 96 this->checkForGLErrors();
96 } 97 }
121 this->program->setUniformMatrix("model", newModelMatrix); 122 this->program->setUniformMatrix("model", newModelMatrix);
122 this->program->release(); 123 this->program->release();
123 this->checkForGLErrors(); 124 this->checkForGLErrors();
124 } 125 }
125 126
127 void GridProgram::setGridColor(const QColor& newGridColor)
128 {
129 const glm::vec4 vec = gl::colorToVector4(newGridColor);
130 if (this->isInitialized)
131 {
132 this->program->bind();
133 this->program->setUniformVector("gridColor", vec);
134 this->program->release();
135 this->checkForGLErrors();
136 }
137 else
138 {
139 this->gridColor = vec;
140 }
141 }
142
126 void GridProgram::draw() 143 void GridProgram::draw()
127 { 144 {
128 this->program->bind(); 145 this->program->bind();
129 this->vertexArrayObject.bind(); 146 this->vertexArrayObject.bind();
130 glDrawArrays(GL_QUADS, 0, countof(data)); 147 glDrawArrays(GL_QUADS, 0, countof(data));

mercurial