Mon, 09 Mar 2020 14:21:54 +0200
ldraw::Id is now templated for extra type safety
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 | // Based on https://stackoverflow.com/q/30842755 | |
22 | const char vertexShaderSource[] = R"( | |
23 | #version 330 core | |
24 | ||
25 | layout (location = 0) in vec3 in_position; | |
26 | layout (location = 1) in vec3 in_color; | |
27 | uniform mat4 view; | |
28 | uniform mat4 projection; | |
29 | uniform mat4 model; | |
30 | smooth out vec3 ex_color; | |
31 | ||
32 | void main() | |
33 | { | |
70 | 34 | gl_Position = projection * view * model * vec4(in_position, 1.0); |
35 | ex_color = in_color; | |
69 | 36 | } |
37 | )"; | |
38 | ||
39 | const char fragmentShaderSource[] = R"( | |
40 | #version 330 core | |
41 | ||
42 | out vec4 color; | |
43 | smooth in vec3 ex_color; | |
44 | ||
45 | void main(void) | |
46 | { | |
70 | 47 | color = vec4(ex_color, 1); |
69 | 48 | } |
49 | )"; | |
50 | ||
51 | namespace | |
52 | { | |
53 | struct AxesVertex | |
54 | { | |
55 | glm::vec3 position; | |
56 | glm::vec3 color; | |
57 | }; | |
58 | } | |
59 | ||
60 | static AxesVertex data[] = | |
61 | { | |
62 | AxesVertex{{-10000, 0, 0}, {1, 0, 0}}, | |
63 | AxesVertex{{10000, 0, 0}, {1, 0, 0}}, | |
64 | AxesVertex{{0, -10000, 0}, {0, 1, 0}}, | |
65 | AxesVertex{{0, 10000, 0}, {0, 1, 0}}, | |
66 | AxesVertex{{0, 0, -10000}, {0, 0, 1}}, | |
67 | AxesVertex{{0, 0, 10000}, {0, 0, 1}}, | |
68 | }; | |
69 | ||
70 | 70 | const char* AxesProgram::vertexShaderSource() const |
69 | 71 | { |
70 | 72 | return ::vertexShaderSource; |
69 | 73 | } |
74 | ||
70 | 75 | const char* AxesProgram::fragmentShaderSource() const |
69 | 76 | { |
70 | 77 | return ::fragmentShaderSource; |
69 | 78 | } |
79 | ||
70 | 80 | const void* AxesProgram::vertexData() const |
69 | 81 | { |
70 | 82 | return ::data; |
69 | 83 | } |
84 | ||
70 | 85 | GLenum AxesProgram::drawMode() const |
69 | 86 | { |
70 | 87 | return GL_LINES; |
69 | 88 | } |
89 | ||
70 | 90 | int AxesProgram::vertexSize() const |
69 | 91 | { |
70 | 92 | return sizeof data[0]; |
69 | 93 | } |
94 | ||
70 | 95 | int AxesProgram::vertexCount() const |
69 | 96 | { |
70 | 97 | return countof(data); |
69 | 98 | } |
99 | ||
70 | 100 | void AxesProgram::setupVertexArrays() |
69 | 101 | { |
70 | 102 | for (int i : {0, 1}) |
103 | { | |
104 | this->program->enableAttributeArray(i); | |
105 | } | |
106 | const int stride = this->vertexSize(); | |
107 | this->program->setAttributeBuffer(0, GL_FLOAT, offsetof(AxesVertex, position), 3, stride); | |
108 | this->program->setAttributeBuffer(1, GL_FLOAT, offsetof(AxesVertex, color), 3, stride); | |
69 | 109 | } |