Sat, 05 Mar 2022 00:51:23 +0200
improve grid rendering
| 53 | 1 | /* |
| 2 | * LDForge: LDraw parts authoring CAD | |
| 3 | * Copyright (C) 2020 Teemu Piippo | |
| 4 | * | |
| 5 | * This program is free software: you can redistribute it and/or modify | |
| 6 | * it under the terms of the GNU General Public License as published by | |
| 7 | * the Free Software Foundation, either version 3 of the License, or | |
| 8 | * (at your option) any later version. | |
| 9 | * | |
| 10 | * This program is distributed in the hope that it will be useful, | |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | * GNU General Public License for more details. | |
| 14 | * | |
| 15 | * You should have received a copy of the GNU General Public License | |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 17 | */ | |
| 18 | ||
| 19 | #include "gridprogram.h" | |
| 20 | ||
| 21 | // Based on https://stackoverflow.com/q/30842755 | |
| 22 | const char vertexShaderSource[] = R"( | |
| 23 | #version 330 core | |
| 24 | ||
| 25 | layout (location = 0) in vec2 in_position; | |
| 26 | uniform mat4 view; | |
| 27 | uniform mat4 projection; | |
| 28 | uniform mat4 model; | |
| 29 | smooth out vec2 ex_uv; | |
|
64
f99d52b1646b
grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
30 | uniform mat4 grid; |
| 53 | 31 | |
| 32 | void main() | |
| 33 | { | |
|
156
65b75beed7e0
render the grid with lines
Teemu Piippo <teemu@hecknology.net>
parents:
155
diff
changeset
|
34 | gl_Position = projection * view * model * grid * vec4(in_position, 0.0, 1.0); |
| 53 | 35 | ex_uv = in_position; |
| 36 | } | |
| 37 | )"; | |
| 38 | ||
| 39 | const char fragmentShaderSource[] = R"( | |
| 40 | #version 330 core | |
| 41 | ||
| 42 | out vec4 color; | |
| 43 | smooth in vec2 ex_uv; | |
| 55 | 44 | uniform vec4 gridColor; |
| 53 | 45 | |
| 46 | void main(void) | |
| 47 | { | |
|
156
65b75beed7e0
render the grid with lines
Teemu Piippo <teemu@hecknology.net>
parents:
155
diff
changeset
|
48 | float dx = fract(ex_uv.y); |
|
65b75beed7e0
render the grid with lines
Teemu Piippo <teemu@hecknology.net>
parents:
155
diff
changeset
|
49 | float dy = fract(ex_uv.x); |
|
54
a4055f67b9c7
made the grid look nicer
Teemu Piippo <teemu@hecknology.net>
parents:
53
diff
changeset
|
50 | /* fade the grid towards extreme co-ordinates */ |
|
156
65b75beed7e0
render the grid with lines
Teemu Piippo <teemu@hecknology.net>
parents:
155
diff
changeset
|
51 | float d = (1.0f - 0.015 * max(abs(ex_uv.x), abs(ex_uv.y))); |
| 55 | 52 | color = vec4(gridColor.xyz, gridColor.w * d); |
| 53 | 53 | } |
| 54 | )"; | |
| 55 | ||
|
156
65b75beed7e0
render the grid with lines
Teemu Piippo <teemu@hecknology.net>
parents:
155
diff
changeset
|
56 | GridProgram::GridProgram(QObject *parent) : |
|
65b75beed7e0
render the grid with lines
Teemu Piippo <teemu@hecknology.net>
parents:
155
diff
changeset
|
57 | AbstractBasicShaderProgram{parent} |
|
65b75beed7e0
render the grid with lines
Teemu Piippo <teemu@hecknology.net>
parents:
155
diff
changeset
|
58 | { |
|
65b75beed7e0
render the grid with lines
Teemu Piippo <teemu@hecknology.net>
parents:
155
diff
changeset
|
59 | this->gridData.reserve(8004); |
|
65b75beed7e0
render the grid with lines
Teemu Piippo <teemu@hecknology.net>
parents:
155
diff
changeset
|
60 | for (int i = -1000; i < 1000; i += 1) |
|
65b75beed7e0
render the grid with lines
Teemu Piippo <teemu@hecknology.net>
parents:
155
diff
changeset
|
61 | { |
|
65b75beed7e0
render the grid with lines
Teemu Piippo <teemu@hecknology.net>
parents:
155
diff
changeset
|
62 | this->gridData.push_back({i, -1000}); |
|
65b75beed7e0
render the grid with lines
Teemu Piippo <teemu@hecknology.net>
parents:
155
diff
changeset
|
63 | this->gridData.push_back({i, 1000}); |
|
65b75beed7e0
render the grid with lines
Teemu Piippo <teemu@hecknology.net>
parents:
155
diff
changeset
|
64 | } |
|
65b75beed7e0
render the grid with lines
Teemu Piippo <teemu@hecknology.net>
parents:
155
diff
changeset
|
65 | for (int i = -1000; i < 1000; i += 1) |
|
65b75beed7e0
render the grid with lines
Teemu Piippo <teemu@hecknology.net>
parents:
155
diff
changeset
|
66 | { |
|
65b75beed7e0
render the grid with lines
Teemu Piippo <teemu@hecknology.net>
parents:
155
diff
changeset
|
67 | this->gridData.push_back({-1000, i}); |
|
65b75beed7e0
render the grid with lines
Teemu Piippo <teemu@hecknology.net>
parents:
155
diff
changeset
|
68 | this->gridData.push_back({1000, i}); |
|
65b75beed7e0
render the grid with lines
Teemu Piippo <teemu@hecknology.net>
parents:
155
diff
changeset
|
69 | } |
|
65b75beed7e0
render the grid with lines
Teemu Piippo <teemu@hecknology.net>
parents:
155
diff
changeset
|
70 | } |
| 53 | 71 | |
|
64
f99d52b1646b
grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
72 | void GridProgram::setGridMatrix(const glm::mat4& newGridMatrix) |
|
f99d52b1646b
grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
73 | { |
|
f99d52b1646b
grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
74 | this->setMatrix("grid", newGridMatrix); |
|
f99d52b1646b
grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
75 | } |
|
f99d52b1646b
grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
76 | |
| 55 | 77 | void GridProgram::setGridColor(const QColor& newGridColor) |
| 78 | { | |
| 79 | const glm::vec4 vec = gl::colorToVector4(newGridColor); | |
| 80 | if (this->isInitialized) | |
| 81 | { | |
| 82 | this->program->bind(); | |
| 83 | this->program->setUniformVector("gridColor", vec); | |
| 84 | this->program->release(); | |
| 85 | this->checkForGLErrors(); | |
| 86 | } | |
| 87 | else | |
| 88 | { | |
| 89 | this->gridColor = vec; | |
| 90 | } | |
| 91 | } | |
| 92 | ||
| 70 | 93 | const char* GridProgram::vertexShaderSource() const |
| 94 | { | |
| 95 | return ::vertexShaderSource; | |
| 96 | } | |
| 97 | ||
| 98 | const char* GridProgram::fragmentShaderSource() const | |
| 53 | 99 | { |
| 70 | 100 | return ::fragmentShaderSource; |
| 101 | } | |
| 102 | ||
| 103 | const void* GridProgram::vertexData() const | |
| 104 | { | |
|
156
65b75beed7e0
render the grid with lines
Teemu Piippo <teemu@hecknology.net>
parents:
155
diff
changeset
|
105 | return this->gridData.data(); |
| 53 | 106 | } |
| 107 | ||
| 70 | 108 | int GridProgram::vertexSize() const |
| 53 | 109 | { |
|
156
65b75beed7e0
render the grid with lines
Teemu Piippo <teemu@hecknology.net>
parents:
155
diff
changeset
|
110 | return sizeof this->gridData[0]; |
| 70 | 111 | } |
| 112 | ||
| 113 | int GridProgram::vertexCount() const | |
| 114 | { | |
|
156
65b75beed7e0
render the grid with lines
Teemu Piippo <teemu@hecknology.net>
parents:
155
diff
changeset
|
115 | return this->gridData.size(); |
| 53 | 116 | } |
| 117 | ||
| 70 | 118 | void GridProgram::setupVertexArrays() |
| 53 | 119 | { |
| 70 | 120 | this->program->enableAttributeArray(0); |
| 121 | this->program->setAttributeBuffer(0, GL_FLOAT, 0, 2, 0); | |
| 122 | this->program->setUniformVector("gridColor", this->gridColor); | |
| 53 | 123 | } |
| 70 | 124 | |
| 125 | GLenum GridProgram::drawMode() const | |
| 126 | { | |
|
156
65b75beed7e0
render the grid with lines
Teemu Piippo <teemu@hecknology.net>
parents:
155
diff
changeset
|
127 | return GL_LINES; |
| 70 | 128 | } |
| 118 | 129 | |
| 130 | QOpenGLBuffer::UsagePattern GridProgram::usagePattern() const | |
| 131 | { | |
| 132 | return QOpenGLBuffer::StaticDraw; | |
| 133 | } |