16 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
17 */ |
17 */ |
18 |
18 |
19 #include "gridprogram.h" |
19 #include "gridprogram.h" |
20 |
20 |
21 const char vertexShaderSource[] = R"( |
21 constexpr char vertexShaderSource[] = R"( |
22 #version 330 core |
22 #version 330 core |
23 |
23 |
24 layout (location = 0) in vec2 in_position; |
24 layout (location = 0) in vec2 in_position; |
25 uniform mat4 view; |
25 uniform mat4 mvp; |
26 uniform mat4 projection; |
|
27 uniform mat4 model; |
|
28 smooth out vec2 ex_uv; |
26 smooth out vec2 ex_uv; |
29 uniform mat4 grid; |
27 uniform mat4 grid; |
30 |
28 |
31 void main() |
29 void main() |
32 { |
30 { |
33 gl_Position = projection * view * model * grid * vec4(in_position, 0.0, 1.0); |
31 gl_Position = mvp * grid * vec4(in_position, 0.0, 1.0); |
34 ex_uv = in_position; |
32 ex_uv = in_position; |
35 } |
33 } |
36 )"; |
34 )"; |
37 |
35 |
38 const char fragmentShaderSource[] = R"( |
36 constexpr char fragmentShaderSource[] = R"( |
39 #version 330 core |
37 #version 330 core |
40 |
38 |
41 out vec4 color; |
39 out vec4 color; |
42 smooth in vec2 ex_uv; |
40 smooth in vec2 ex_uv; |
43 uniform vec4 gridColor; |
41 uniform vec4 gridColor; |
50 float d = (1.0f - 0.015 * max(abs(ex_uv.x), abs(ex_uv.y))); |
48 float d = (1.0f - 0.015 * max(abs(ex_uv.x), abs(ex_uv.y))); |
51 color = vec4(gridColor.xyz, gridColor.w * d); |
49 color = vec4(gridColor.xyz, gridColor.w * d); |
52 } |
50 } |
53 )"; |
51 )"; |
54 |
52 |
55 GridProgram::GridProgram(QObject *parent) : |
53 template<int extent> |
56 AbstractBasicShaderProgram{parent} |
54 constexpr auto calcGridData() |
57 { |
55 { |
58 constexpr int extent = 50; |
56 std::array<glm::vec2, 8 * extent + 4> result; |
59 this->gridData.reserve(8 * extent + 4); |
57 int ix = 0; |
60 for (int i = -extent; i <= extent; i += 1) |
58 for (int i = -extent; i <= extent; i += 1) { |
61 { |
59 result[ix++] = {i, -extent}; |
62 this->gridData.push_back({i, -extent}); |
60 result[ix++] = {i, extent}; |
63 this->gridData.push_back({i, extent}); |
|
64 } |
61 } |
65 for (int i = -extent; i <= extent; i += 1) |
62 for (int i = -extent; i <= extent; i += 1) { |
66 { |
63 result[ix++] = {-extent, i}; |
67 this->gridData.push_back({-extent, i}); |
64 result[ix++] = {extent, i}; |
68 this->gridData.push_back({extent, i}); |
65 } |
|
66 return result; |
|
67 } |
|
68 |
|
69 void GridLayer::setGridMatrix(const glm::mat4& newGridMatrix) |
|
70 { |
|
71 this->shader.setUniformMatrix("grid", newGridMatrix); |
|
72 } |
|
73 |
|
74 void GridLayer::setGridColor(const QColor& newGridColor) |
|
75 { |
|
76 this->gridColor = gl::colorToVector4(newGridColor); |
|
77 if (this->isInitialized) { |
|
78 this->shader.setUniformVector("gridColor", this->gridColor); |
69 } |
79 } |
70 } |
80 } |
71 |
81 |
72 void GridProgram::setGridMatrix(const glm::mat4& newGridMatrix) |
82 void GridLayer::initializeGL() |
73 { |
83 { |
74 this->setMatrix("grid", newGridMatrix); |
84 this->shader.initialize( |
|
85 ::vertexShaderSource, |
|
86 ::fragmentShaderSource, |
|
87 QOpenGLBuffer::StaticDraw, |
|
88 { |
|
89 GLAttributeSpec{ |
|
90 .type = GL_FLOAT, |
|
91 .offset = 0, |
|
92 .tuplesize = 2, |
|
93 .stride = 0, |
|
94 }, |
|
95 } |
|
96 ); |
|
97 this->isInitialized = true; |
|
98 constexpr auto data = calcGridData<50>(); |
|
99 this->shader.setUniformVector("gridColor", this->gridColor); |
|
100 this->setGridMatrix({{1, 0, 0, 0}, {0, 0, 1, 0}, {0, 1, 0, 0}, {0, 0, 0, 1}}); |
|
101 this->shader.bufferData(data.data(), data.size(), sizeof data[0]); |
75 } |
102 } |
76 |
103 |
77 void GridProgram::setGridColor(const QColor& newGridColor) |
104 void GridLayer::paintGL() |
78 { |
105 { |
79 const glm::vec4 vec = gl::colorToVector4(newGridColor); |
106 glLineWidth(1); |
80 if (this->isInitialized) |
107 glEnable(GL_BLEND); |
81 { |
108 glLineStipple(1, 0x8888); |
82 this->program->bind(); |
109 glEnable(GL_LINE_STIPPLE); |
83 this->program->setUniformVector("gridColor", vec); |
110 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
84 this->program->release(); |
111 this->shader.draw(GL_LINES); |
85 this->checkForGLErrors(); |
112 glDisable(GL_BLEND); |
86 } |
113 glDisable(GL_LINE_STIPPLE); |
87 else |
|
88 { |
|
89 this->gridColor = vec; |
|
90 } |
|
91 } |
114 } |
92 |
115 |
93 const char* GridProgram::vertexShaderSource() const |
116 void GridLayer::mvpMatrixChanged(const glm::mat4& mvpMatrix) |
94 { |
117 { |
95 return ::vertexShaderSource; |
118 this->shader.setMvpMatrix(mvpMatrix); |
96 } |
119 } |
97 |
|
98 const char* GridProgram::fragmentShaderSource() const |
|
99 { |
|
100 return ::fragmentShaderSource; |
|
101 } |
|
102 |
|
103 const void* GridProgram::vertexData() const |
|
104 { |
|
105 return this->gridData.data(); |
|
106 } |
|
107 |
|
108 int GridProgram::vertexSize() const |
|
109 { |
|
110 return sizeof this->gridData[0]; |
|
111 } |
|
112 |
|
113 int GridProgram::vertexCount() const |
|
114 { |
|
115 return this->gridData.size(); |
|
116 } |
|
117 |
|
118 void GridProgram::setupVertexArrays() |
|
119 { |
|
120 this->program->enableAttributeArray(0); |
|
121 this->program->setAttributeBuffer(0, GL_FLOAT, 0, 2, 0); |
|
122 this->program->setUniformVector("gridColor", this->gridColor); |
|
123 } |
|
124 |
|
125 GLenum GridProgram::drawMode() const |
|
126 { |
|
127 return GL_LINES; |
|
128 } |
|
129 |
|
130 QOpenGLBuffer::UsagePattern GridProgram::usagePattern() const |
|
131 { |
|
132 return QOpenGLBuffer::StaticDraw; |
|
133 } |
|