src/gl/axesprogram.cpp

Wed, 18 Mar 2020 17:11:23 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Wed, 18 Mar 2020 17:11:23 +0200
changeset 85
40e2940605a3
parent 74
6b51e7b7c459
child 102
9f435f66bd0c
child 118
8e1c9f18ae15
permissions
-rw-r--r--

added a matrix editing widget

69
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
1 /*
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
2 * LDForge: LDraw parts authoring CAD
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
3 * Copyright (C) 2020 Teemu Piippo
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
4 *
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
5 * This program is free software: you can redistribute it and/or modify
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
6 * it under the terms of the GNU General Public License as published by
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
7 * the Free Software Foundation, either version 3 of the License, or
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
8 * (at your option) any later version.
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
9 *
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
10 * This program is distributed in the hope that it will be useful,
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
13 * GNU General Public License for more details.
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
14 *
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
17 */
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
18
70
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 69
diff changeset
19 #include "axesprogram.h"
69
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
20
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
21 const char vertexShaderSource[] = R"(
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
22 #version 330 core
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
23
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
24 layout (location = 0) in vec3 in_position;
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
25 layout (location = 1) in vec3 in_color;
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
26 uniform mat4 view;
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
27 uniform mat4 projection;
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
28 uniform mat4 model;
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
29 smooth out vec3 ex_color;
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
30
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
31 void main()
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
32 {
70
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 69
diff changeset
33 gl_Position = projection * view * model * vec4(in_position, 1.0);
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 69
diff changeset
34 ex_color = in_color;
69
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
35 }
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
36 )";
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
37
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
38 const char fragmentShaderSource[] = R"(
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
39 #version 330 core
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
40
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
41 out vec4 color;
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
42 smooth in vec3 ex_color;
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
43
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
44 void main(void)
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
45 {
70
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 69
diff changeset
46 color = vec4(ex_color, 1);
69
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
47 }
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
48 )";
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
49
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
50 namespace
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
51 {
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
52 struct AxesVertex
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
53 {
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
54 glm::vec3 position;
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
55 glm::vec3 color;
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
56 };
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
57 }
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
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
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
60 {
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
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
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
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
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
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
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
73 };
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
74
70
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 69
diff changeset
75 const char* AxesProgram::vertexShaderSource() const
69
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
76 {
70
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 69
diff changeset
77 return ::vertexShaderSource;
69
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
78 }
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
79
70
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 69
diff changeset
80 const char* AxesProgram::fragmentShaderSource() const
69
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
81 {
70
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 69
diff changeset
82 return ::fragmentShaderSource;
69
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
83 }
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
84
70
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 69
diff changeset
85 const void* AxesProgram::vertexData() const
69
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
86 {
70
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 69
diff changeset
87 return ::data;
69
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
88 }
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
89
70
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 69
diff changeset
90 GLenum AxesProgram::drawMode() const
69
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
91 {
70
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 69
diff changeset
92 return GL_LINES;
69
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
93 }
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
94
70
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 69
diff changeset
95 int AxesProgram::vertexSize() const
69
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
96 {
70
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 69
diff changeset
97 return sizeof data[0];
69
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
98 }
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
99
70
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 69
diff changeset
100 int AxesProgram::vertexCount() const
69
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
101 {
70
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 69
diff changeset
102 return countof(data);
69
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
103 }
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
104
70
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 69
diff changeset
105 void AxesProgram::setupVertexArrays()
69
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
106 {
70
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 69
diff changeset
107 for (int i : {0, 1})
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 69
diff changeset
108 {
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 69
diff changeset
109 this->program->enableAttributeArray(i);
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 69
diff changeset
110 }
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 69
diff changeset
111 const int stride = this->vertexSize();
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 69
diff changeset
112 this->program->setAttributeBuffer(0, GL_FLOAT, offsetof(AxesVertex, position), 3, stride);
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 69
diff changeset
113 this->program->setAttributeBuffer(1, GL_FLOAT, offsetof(AxesVertex, color), 3, stride);
69
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
114 }

mercurial