Sat, 05 Mar 2022 15:40:43 +0200
the grid now has less lines
69 | 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 | ||
70 | 19 | #include "axesprogram.h" |
69 | 20 | |
21 | const char vertexShaderSource[] = R"( | |
22 | #version 330 core | |
23 | ||
24 | layout (location = 0) in vec3 in_position; | |
25 | layout (location = 1) in vec3 in_color; | |
26 | uniform mat4 view; | |
27 | uniform mat4 projection; | |
28 | uniform mat4 model; | |
29 | smooth out vec3 ex_color; | |
30 | ||
31 | void main() | |
32 | { | |
70 | 33 | gl_Position = projection * view * model * vec4(in_position, 1.0); |
34 | ex_color = in_color; | |
69 | 35 | } |
36 | )"; | |
37 | ||
38 | const char fragmentShaderSource[] = R"( | |
39 | #version 330 core | |
40 | ||
41 | out vec4 color; | |
42 | smooth in vec3 ex_color; | |
43 | ||
44 | void main(void) | |
45 | { | |
70 | 46 | color = vec4(ex_color, 1); |
69 | 47 | } |
48 | )"; | |
49 | ||
50 | namespace | |
51 | { | |
52 | struct AxesVertex | |
53 | { | |
54 | glm::vec3 position; | |
55 | glm::vec3 color; | |
56 | }; | |
57 | } | |
58 | ||
74
6b51e7b7c459
negative axes are now drawn in darker color
Teemu Piippo <teemu@hecknology.net>
parents:
70
diff
changeset
|
59 | static const AxesVertex data[] = |
69 | 60 | { |
61 | AxesVertex{{10000, 0, 0}, {1, 0, 0}}, | |
74
6b51e7b7c459
negative axes are now drawn in darker color
Teemu Piippo <teemu@hecknology.net>
parents:
70
diff
changeset
|
62 | AxesVertex{{0, 0, 0}, {1, 0, 0}}, |
6b51e7b7c459
negative axes are now drawn in darker color
Teemu Piippo <teemu@hecknology.net>
parents:
70
diff
changeset
|
63 | AxesVertex{{-10000, 0, 0}, {0.5, 0, 0}}, |
6b51e7b7c459
negative axes are now drawn in darker color
Teemu Piippo <teemu@hecknology.net>
parents:
70
diff
changeset
|
64 | AxesVertex{{0, 0, 0}, {0.5, 0, 0}}, |
69 | 65 | AxesVertex{{0, 10000, 0}, {0, 1, 0}}, |
74
6b51e7b7c459
negative axes are now drawn in darker color
Teemu Piippo <teemu@hecknology.net>
parents:
70
diff
changeset
|
66 | AxesVertex{{0, 0, 0}, {0, 1, 0}}, |
6b51e7b7c459
negative axes are now drawn in darker color
Teemu Piippo <teemu@hecknology.net>
parents:
70
diff
changeset
|
67 | AxesVertex{{0, -10000, 0}, {0, 0.5, 0}}, |
6b51e7b7c459
negative axes are now drawn in darker color
Teemu Piippo <teemu@hecknology.net>
parents:
70
diff
changeset
|
68 | AxesVertex{{0, 0, 0}, {0, 0.5, 0}}, |
69 | 69 | AxesVertex{{0, 0, 10000}, {0, 0, 1}}, |
74
6b51e7b7c459
negative axes are now drawn in darker color
Teemu Piippo <teemu@hecknology.net>
parents:
70
diff
changeset
|
70 | AxesVertex{{0, 0, 0}, {0, 0, 1}}, |
6b51e7b7c459
negative axes are now drawn in darker color
Teemu Piippo <teemu@hecknology.net>
parents:
70
diff
changeset
|
71 | AxesVertex{{0, 0, -10000}, {0, 0, 0.5}}, |
6b51e7b7c459
negative axes are now drawn in darker color
Teemu Piippo <teemu@hecknology.net>
parents:
70
diff
changeset
|
72 | AxesVertex{{0, 0, 0}, {0, 0, 0.5}}, |
69 | 73 | }; |
74 | ||
70 | 75 | const char* AxesProgram::vertexShaderSource() const |
69 | 76 | { |
70 | 77 | return ::vertexShaderSource; |
69 | 78 | } |
79 | ||
70 | 80 | const char* AxesProgram::fragmentShaderSource() const |
69 | 81 | { |
70 | 82 | return ::fragmentShaderSource; |
69 | 83 | } |
84 | ||
70 | 85 | const void* AxesProgram::vertexData() const |
69 | 86 | { |
70 | 87 | return ::data; |
69 | 88 | } |
89 | ||
70 | 90 | GLenum AxesProgram::drawMode() const |
69 | 91 | { |
70 | 92 | return GL_LINES; |
69 | 93 | } |
94 | ||
70 | 95 | int AxesProgram::vertexSize() const |
69 | 96 | { |
70 | 97 | return sizeof data[0]; |
69 | 98 | } |
99 | ||
70 | 100 | int AxesProgram::vertexCount() const |
69 | 101 | { |
70 | 102 | return countof(data); |
69 | 103 | } |
104 | ||
70 | 105 | void AxesProgram::setupVertexArrays() |
69 | 106 | { |
70 | 107 | for (int i : {0, 1}) |
108 | { | |
109 | this->program->enableAttributeArray(i); | |
110 | } | |
111 | const int stride = this->vertexSize(); | |
112 | this->program->setAttributeBuffer(0, GL_FLOAT, offsetof(AxesVertex, position), 3, stride); | |
113 | this->program->setAttributeBuffer(1, GL_FLOAT, offsetof(AxesVertex, color), 3, stride); | |
69 | 114 | } |
118 | 115 | |
116 | QOpenGLBuffer::UsagePattern AxesProgram::usagePattern() const | |
117 | { | |
118 | return QOpenGLBuffer::StaticDraw; | |
119 | } |