src/gl/compiler.cpp

Tue, 14 Jun 2022 20:44:07 +0300

author
Teemu Piippo <teemu.s.piippo@gmail.com>
date
Tue, 14 Jun 2022 20:44:07 +0300
changeset 220
a6d937f8c0ac
parent 211
b27b90fb993f
child 250
2837b549e616
permissions
-rw-r--r--

Further reduce settings.h

19
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
1 /*
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
2 * LDForge: LDraw parts authoring CAD
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
3 * Copyright (C) 2013 - 2018 Teemu Piippo
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
4 *
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
5 * This program is free software: you can redistribute it and/or modify
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
6 * it under the terms of the GNU General Public License as published by
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
7 * the Free Software Foundation, either version 3 of the License, or
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
8 * (at your option) any later version.
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
9 *
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
10 * This program is distributed in the hope that it will be useful,
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
13 * GNU General Public License for more details.
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
14 *
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
17 */
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
18
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
19 #define GL_GLEXT_PROTOTYPES
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
20 #include <GL/glu.h>
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
21 #include <GL/glext.h>
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
22 #include <QMessageBox>
19
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
23 #include "gl/compiler.h"
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
24 #include "documentmanager.h"
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
25 #include "invert.h"
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
26 #include "ring.h"
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
27
27
c57fb7a5ffa3 commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
28 static const char* vertexShaderSource = R"(
c57fb7a5ffa3 commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
29 #version 330 core
c57fb7a5ffa3 commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
30
c57fb7a5ffa3 commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
31 layout(location=0) in vec3 position;
c57fb7a5ffa3 commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
32 layout(location=1) in vec4 color;
34
1de2b8d64e9f added some sort of lighting
Teemu Piippo <teemu@hecknology.net>
parents: 33
diff changeset
33 layout(location=2) in vec3 normal;
49
d56cc7387dad wrote the id color in terms of the id value in the shader now that I can get the id to the shader properly
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
34 layout(location=3) in int id;
51
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 50
diff changeset
35 layout(location=4) in int selected;
27
c57fb7a5ffa3 commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
36 out vec4 vColor;
34
1de2b8d64e9f added some sort of lighting
Teemu Piippo <teemu@hecknology.net>
parents: 33
diff changeset
37 out vec3 vFragPos;
1de2b8d64e9f added some sort of lighting
Teemu Piippo <teemu@hecknology.net>
parents: 33
diff changeset
38 out vec3 vNormal;
1de2b8d64e9f added some sort of lighting
Teemu Piippo <teemu@hecknology.net>
parents: 33
diff changeset
39 uniform mat4 modelMatrix;
1de2b8d64e9f added some sort of lighting
Teemu Piippo <teemu@hecknology.net>
parents: 33
diff changeset
40 uniform mat4 viewMatrix;
1de2b8d64e9f added some sort of lighting
Teemu Piippo <teemu@hecknology.net>
parents: 33
diff changeset
41 uniform mat4 projectionMatrix;
37
90443fb328ea wired renderstyle up
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
42 uniform int fragmentStyle;
48
3c10f0e2fbe0 added selection highlighting
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
43 uniform vec3 selectedColor;
3c10f0e2fbe0 added selection highlighting
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
44 uniform int highlighted;
37
90443fb328ea wired renderstyle up
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
45
90443fb328ea wired renderstyle up
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
46 const int FRAGSTYLE_Normal = 0;
90443fb328ea wired renderstyle up
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
47 const int FRAGSTYLE_BfcGreen = 1;
90443fb328ea wired renderstyle up
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
48 const int FRAGSTYLE_BfcRed = 2;
90443fb328ea wired renderstyle up
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
49 const int FRAGSTYLE_Random = 3;
46
98645c8e7704 added the pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
50 const int FRAGSTYLE_Id = 4;
119
Teemu Piippo <teemu@hecknology.net>
parents: 115
diff changeset
51 const int FRAGSTYLE_Black = 5;
27
c57fb7a5ffa3 commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
52
c57fb7a5ffa3 commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
53 void main()
c57fb7a5ffa3 commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
54 {
34
1de2b8d64e9f added some sort of lighting
Teemu Piippo <teemu@hecknology.net>
parents: 33
diff changeset
55 mat3 normalMatrix = transpose(inverse(mat3(modelMatrix)));
1de2b8d64e9f added some sort of lighting
Teemu Piippo <teemu@hecknology.net>
parents: 33
diff changeset
56 vNormal = normalize(normalMatrix * normal);
48
3c10f0e2fbe0 added selection highlighting
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
57 if (fragmentStyle == FRAGSTYLE_Id)
37
90443fb328ea wired renderstyle up
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
58 {
49
d56cc7387dad wrote the id color in terms of the id value in the shader now that I can get the id to the shader properly
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
59 /* Calculate a color based from this index. This method caters for
d56cc7387dad wrote the id color in terms of the id value in the shader now that I can get the id to the shader properly
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
60 * 16777216 objects. I don't think that will be exceeded anytime soon.
d56cc7387dad wrote the id color in terms of the id value in the shader now that I can get the id to the shader properly
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
61 */
d56cc7387dad wrote the id color in terms of the id value in the shader now that I can get the id to the shader properly
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
62 int r = (id / 0x10000) % 0x100;
d56cc7387dad wrote the id color in terms of the id value in the shader now that I can get the id to the shader properly
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
63 int g = (id / 0x100) % 0x100;
d56cc7387dad wrote the id color in terms of the id value in the shader now that I can get the id to the shader properly
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
64 int b = id % 0x100;
d56cc7387dad wrote the id color in terms of the id value in the shader now that I can get the id to the shader properly
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
65 vColor = vec4(r / 255.0, g / 255.0, b / 255.0, 1.0);
46
98645c8e7704 added the pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
66 }
51
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 50
diff changeset
67 else if (selected == 1)
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 50
diff changeset
68 {
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 50
diff changeset
69 vColor = vec4(selectedColor, 1.0);
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 50
diff changeset
70 }
37
90443fb328ea wired renderstyle up
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
71 else
90443fb328ea wired renderstyle up
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
72 {
48
3c10f0e2fbe0 added selection highlighting
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
73 if (fragmentStyle == FRAGSTYLE_BfcGreen)
3c10f0e2fbe0 added selection highlighting
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
74 {
3c10f0e2fbe0 added selection highlighting
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
75 vColor = vec4(0.2, 0.9, 0.2, 1.0);
3c10f0e2fbe0 added selection highlighting
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
76 }
3c10f0e2fbe0 added selection highlighting
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
77 else if (fragmentStyle == FRAGSTYLE_BfcRed)
3c10f0e2fbe0 added selection highlighting
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
78 {
3c10f0e2fbe0 added selection highlighting
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
79 vColor = vec4(0.9, 0.2, 0.2, 1.0);
3c10f0e2fbe0 added selection highlighting
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
80 }
119
Teemu Piippo <teemu@hecknology.net>
parents: 115
diff changeset
81 else if (fragmentStyle == FRAGSTYLE_Black)
Teemu Piippo <teemu@hecknology.net>
parents: 115
diff changeset
82 {
Teemu Piippo <teemu@hecknology.net>
parents: 115
diff changeset
83 vColor = vec4(0.0, 0.0, 0.0, 1.0);
Teemu Piippo <teemu@hecknology.net>
parents: 115
diff changeset
84 }
48
3c10f0e2fbe0 added selection highlighting
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
85 else
3c10f0e2fbe0 added selection highlighting
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
86 {
3c10f0e2fbe0 added selection highlighting
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
87 vColor = color;
3c10f0e2fbe0 added selection highlighting
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
88 }
3c10f0e2fbe0 added selection highlighting
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
89 if (highlighted == id)
3c10f0e2fbe0 added selection highlighting
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
90 {
3c10f0e2fbe0 added selection highlighting
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
91 vColor = (vColor + vec4(selectedColor, 1.0) * 0.6) / 1.6;
3c10f0e2fbe0 added selection highlighting
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
92 }
37
90443fb328ea wired renderstyle up
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
93 }
48
3c10f0e2fbe0 added selection highlighting
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
94
34
1de2b8d64e9f added some sort of lighting
Teemu Piippo <teemu@hecknology.net>
parents: 33
diff changeset
95 vFragPos = vec3(modelMatrix * vec4(position, 1.0));
1de2b8d64e9f added some sort of lighting
Teemu Piippo <teemu@hecknology.net>
parents: 33
diff changeset
96 gl_Position = projectionMatrix * viewMatrix * vec4(vFragPos, 1.0);
27
c57fb7a5ffa3 commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
97 }
c57fb7a5ffa3 commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
98 )";
c57fb7a5ffa3 commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
99
c57fb7a5ffa3 commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
100 static const char* fragmentShaderSource = R"(
c57fb7a5ffa3 commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
101 #version 330 core
c57fb7a5ffa3 commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
102
c57fb7a5ffa3 commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
103 in vec4 vColor;
34
1de2b8d64e9f added some sort of lighting
Teemu Piippo <teemu@hecknology.net>
parents: 33
diff changeset
104 in vec3 vFragPos;
1de2b8d64e9f added some sort of lighting
Teemu Piippo <teemu@hecknology.net>
parents: 33
diff changeset
105 in vec3 vNormal;
27
c57fb7a5ffa3 commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
106 out vec4 fColor;
34
1de2b8d64e9f added some sort of lighting
Teemu Piippo <teemu@hecknology.net>
parents: 33
diff changeset
107 const vec3 lightPos = vec3(0.5, 0.5, 0.5);
1de2b8d64e9f added some sort of lighting
Teemu Piippo <teemu@hecknology.net>
parents: 33
diff changeset
108 const vec4 lightColor = vec4(1.0, 1.0, 1.0, 1.0);
1de2b8d64e9f added some sort of lighting
Teemu Piippo <teemu@hecknology.net>
parents: 33
diff changeset
109 const float ambientStrength = 0.7;
46
98645c8e7704 added the pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
110 uniform bool useLighting;
27
c57fb7a5ffa3 commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
111
c57fb7a5ffa3 commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
112 void main()
c57fb7a5ffa3 commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
113 {
46
98645c8e7704 added the pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
114 if (useLighting)
98645c8e7704 added the pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
115 {
98645c8e7704 added the pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
116 vec4 ambient = ambientStrength * lightColor;
98645c8e7704 added the pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
117 vec3 lightDirection = normalize(lightPos - vFragPos);
98645c8e7704 added the pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
118 vec4 diffuse = max(dot(vNormal, lightDirection), 0.0) * lightColor;
98645c8e7704 added the pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
119 fColor = (ambient + diffuse) * vColor;
98645c8e7704 added the pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
120 }
98645c8e7704 added the pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
121 else
98645c8e7704 added the pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
122 {
98645c8e7704 added the pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
123 fColor = vColor;
98645c8e7704 added the pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
124 }
27
c57fb7a5ffa3 commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
125 }
c57fb7a5ffa3 commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
126 )";
c57fb7a5ffa3 commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
127
211
b27b90fb993f Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 210
diff changeset
128 template<typename Fn>
b27b90fb993f Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 210
diff changeset
129 constexpr void pointsToRender(const PolygonElement& element, Fn func)
b27b90fb993f Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 210
diff changeset
130 {
b27b90fb993f Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 210
diff changeset
131 visitPolygon<void>(
b27b90fb993f Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 210
diff changeset
132 [&func](const LineSegment& edge)
b27b90fb993f Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 210
diff changeset
133 {
b27b90fb993f Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 210
diff changeset
134 func(edge.p1, glm::vec3{});
b27b90fb993f Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 210
diff changeset
135 func(edge.p2, glm::vec3{});
b27b90fb993f Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 210
diff changeset
136 },
b27b90fb993f Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 210
diff changeset
137 [&func](const Triangle& tri)
b27b90fb993f Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 210
diff changeset
138 {
b27b90fb993f Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 210
diff changeset
139 func(tri.p1, normalVector({tri.p3, tri.p1, tri.p2}));
b27b90fb993f Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 210
diff changeset
140 func(tri.p2, normalVector({tri.p1, tri.p2, tri.p3}));
b27b90fb993f Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 210
diff changeset
141 func(tri.p3, normalVector({tri.p2, tri.p3, tri.p1}));
b27b90fb993f Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 210
diff changeset
142 },
b27b90fb993f Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 210
diff changeset
143 [&func](const Quadrilateral& quad)
b27b90fb993f Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 210
diff changeset
144 {
b27b90fb993f Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 210
diff changeset
145 func(quad.p1, normalVector({quad.p4, quad.p1, quad.p2}));
b27b90fb993f Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 210
diff changeset
146 func(quad.p2, normalVector({quad.p1, quad.p2, quad.p3}));
b27b90fb993f Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 210
diff changeset
147 func(quad.p3, normalVector({quad.p2, quad.p3, quad.p4}));
b27b90fb993f Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 210
diff changeset
148 func(quad.p4, normalVector({quad.p3, quad.p4, quad.p1}));
b27b90fb993f Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 210
diff changeset
149 },
b27b90fb993f Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 210
diff changeset
150 [&func](const ConditionalEdge& cedge)
b27b90fb993f Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 210
diff changeset
151 {
b27b90fb993f Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 210
diff changeset
152 func(cedge.p1, glm::vec3{});
b27b90fb993f Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 210
diff changeset
153 func(cedge.p2, glm::vec3{});
b27b90fb993f Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 210
diff changeset
154 },
b27b90fb993f Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 210
diff changeset
155 element);
b27b90fb993f Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 210
diff changeset
156 }
b27b90fb993f Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 210
diff changeset
157
53
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
158 void gl::buildShaders(
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
159 QOpenGLShaderProgram* shaderProgram,
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
160 const char* vertexShaderSource,
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
161 const char* fragmentShaderSource)
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
162 {
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
163 shaderProgram->create();
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
164 const bool vertexShaderCompiled = shaderProgram->addShaderFromSourceCode(QOpenGLShader::Vertex, vertexShaderSource);
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
165 QString log;
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
166 if (not vertexShaderCompiled)
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
167 {
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
168 log += "\n" + QObject::tr("Vertex shader:") + "\n" + shaderProgram->log();
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
169 }
115
ed884a2fb009 fix too long lines
Teemu Piippo <teemu@hecknology.net>
parents: 80
diff changeset
170 const bool fragmentShaderCompiled = shaderProgram->addShaderFromSourceCode(
ed884a2fb009 fix too long lines
Teemu Piippo <teemu@hecknology.net>
parents: 80
diff changeset
171 QOpenGLShader::Fragment,
ed884a2fb009 fix too long lines
Teemu Piippo <teemu@hecknology.net>
parents: 80
diff changeset
172 fragmentShaderSource);
53
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
173 if (not fragmentShaderCompiled)
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
174 {
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
175 log += "\n" + QObject::tr("Fragment shader:") + "\n" + shaderProgram->log();
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
176 }
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
177 if (not vertexShaderCompiled or not fragmentShaderCompiled)
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
178 {
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
179 QMessageBox::critical(
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
180 nullptr,
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
181 QObject::tr("Shader compile error"),
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
182 QObject::tr("Could not compile shaders.") + "\n" + log);
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
183 std::exit(-1);
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
184 }
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
185 const bool linkSuccessful = shaderProgram->link();
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
186 if (not linkSuccessful)
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
187 {
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
188 QMessageBox::critical(
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
189 nullptr,
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
190 QObject::tr("Shader link error"),
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
191 QObject::tr("Could not link shaders: %1").arg(shaderProgram->log())
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
192 );
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
193 }
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
194 }
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
195
189
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
196 void gl::initializeModelShaders(gl::ModelShaders *modelShaders)
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
197 {
211
b27b90fb993f Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 210
diff changeset
198 if (not modelShaders->initialized) {
b27b90fb993f Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 210
diff changeset
199 for (auto& shader : modelShaders->shaderObjects) {
189
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
200 shader.program = std::make_unique<QOpenGLShaderProgram>();
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
201 gl::buildShaders(shader.program.get(), ::vertexShaderSource, ::fragmentShaderSource);
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
202 shader.program->bind();
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
203 shader.buffer.create();
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
204 shader.buffer.bind();
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
205 shader.buffer.setUsagePattern(QOpenGLBuffer::DynamicDraw);
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
206 shader.vertexArray.create();
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
207 shader.vertexArray.bind();
211
b27b90fb993f Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 210
diff changeset
208 for (int k : {0, 1, 2, 3, 4}) {
189
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
209 shader.program->enableAttributeArray(k);
48
3c10f0e2fbe0 added selection highlighting
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
210 }
189
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
211 using Vertex = ModelShaders::Vertex;
80
764381756899 simplification
Teemu Piippo <teemu@hecknology.net>
parents: 78
diff changeset
212 constexpr int stride = sizeof(Vertex);
189
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
213 shader.program->setAttributeBuffer(0, GL_FLOAT, offsetof(Vertex, position), 3, stride);
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
214 shader.program->setAttributeBuffer(1, GL_FLOAT, offsetof(Vertex, color), 4, stride);
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
215 shader.program->setAttributeBuffer(2, GL_FLOAT, offsetof(Vertex, normal), 3, stride);
80
764381756899 simplification
Teemu Piippo <teemu@hecknology.net>
parents: 78
diff changeset
216 glVertexAttribIPointer(3, 1, GL_INT, stride, reinterpret_cast<void*>(offsetof(Vertex, id)));
764381756899 simplification
Teemu Piippo <teemu@hecknology.net>
parents: 78
diff changeset
217 glVertexAttribIPointer(4, 1, GL_INT, stride, reinterpret_cast<void*>(offsetof(Vertex, selected)));
189
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
218 shader.vertexArray.release();
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
219 shader.buffer.release();
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
220 shader.program->release();
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
221 }
189
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
222 modelShaders->initialized = true;
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
223 }
19
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
224 }
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
225
210
232e7634cc8a more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 205
diff changeset
226 static constexpr gl::ArrayClass classifyPolygon(const PolygonElement& element)
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
227 {
210
232e7634cc8a more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 205
diff changeset
228 return visitPolygon<gl::ArrayClass>(
232e7634cc8a more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 205
diff changeset
229 [](const LineSegment&) { return gl::ArrayClass::Lines; },
232e7634cc8a more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 205
diff changeset
230 [](const Triangle&) { return gl::ArrayClass::Triangles; },
232e7634cc8a more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 205
diff changeset
231 [](const Quadrilateral&) { return gl::ArrayClass::Quads; },
232e7634cc8a more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 205
diff changeset
232 [](const ConditionalEdge&) { return gl::ArrayClass::ConditionalLines; },
232e7634cc8a more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 205
diff changeset
233 element);
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
234 }
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
235
189
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
236 template<typename Fn>
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
237 void iterateModelPolygons(Model* model, DocumentManager* context, Fn&& fn)
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
238 {
211
b27b90fb993f Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 210
diff changeset
239 PolygonCache* cache = findPolygonCacheForModel(model, context);
b27b90fb993f Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 210
diff changeset
240 if (cache != nullptr) {
b27b90fb993f Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 210
diff changeset
241 const PolygonCache::vector_type* polygons = getCachedPolygons(cache, model, context);
b27b90fb993f Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 210
diff changeset
242 for (const WithId<PolygonElement>& polygon : *polygons) {
b27b90fb993f Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 210
diff changeset
243 fn(polygon);
189
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
244 }
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
245 }
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
246 }
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
247
189
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
248 static QColor getColorForPolygon(
210
232e7634cc8a more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 205
diff changeset
249 const PolygonElement& polygon,
189
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
250 const gl::RenderPreferences& preferences,
205
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
251 const ColorTable& colorTable)
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
252 {
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
253 QColor color;
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
254 // For normal colors, use the polygon's color.
205
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
255 if (polygon.color == MAIN_COLOR)
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
256 {
39
caac957e9834 Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents: 37
diff changeset
257 color = preferences.mainColor;
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
258 }
205
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
259 else if (polygon.color == EDGE_COLOR)
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
260 {
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
261 // Edge color is black, unless we have a dark background, in which case lines need to be bright.
43
08dc62e03a6d made edges white in dark backgrounds
Teemu Piippo <teemu@hecknology.net>
parents: 39
diff changeset
262 color = luma(preferences.backgroundColor) > (40.0 / 256.0) ? Qt::black : Qt::white;
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
263 }
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
264 else
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
265 {
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
266 // Not main or edge color, use the polygon's color as is.
205
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
267 color = colorFace(polygon.color, colorTable).value_or(Qt::black);
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
268 }
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
269 return color;
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
270 }
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
271
189
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
272 /**
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
273 * @brief Computes the minimum bounding box for a model
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
274 */
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
275 BoundingBox gl::boundingBoxForModel(Model* model, DocumentManager* context)
22
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
276 {
189
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
277 BoundingBox result = emptyBoundingBox;
210
232e7634cc8a more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 205
diff changeset
278 iterateModelPolygons(model, context, [&](const PolygonElement& polygon)
189
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
279 {
211
b27b90fb993f Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 210
diff changeset
280 visitPoints([&result](const glm::vec3& p) {
b27b90fb993f Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 210
diff changeset
281 addPointToBox(result, p);
b27b90fb993f Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 210
diff changeset
282 }, polygon);
189
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
283 });
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
284 return result;
22
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
285 }
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
286
189
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
287 /**
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
288 * @brief gl::build Creates GL vertices for objects in the model and buffers them to shaders.
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
289 */
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
290 void gl::build(
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
291 gl::ModelShaders* shaders,
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
292 Model* model,
205
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
293 const ColorTable& colorTable,
189
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
294 DocumentManager* context,
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
295 const gl::RenderPreferences& preferences)
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
296 {
189
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
297 for (gl::ModelShaders::ShaderObject& shader : shaders->shaderObjects) {
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
298 shader.cachedData.clear();
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
299 }
211
b27b90fb993f Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 210
diff changeset
300 iterateModelPolygons(model, context, [&](const WithId<PolygonElement>& polygon)
189
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
301 {
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
302 const int index = static_cast<int>(classifyPolygon(polygon));
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
303 std::vector<gl::ModelShaders::Vertex>& vertexBuffer = shaders->shaderObjects[index].cachedData;
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
304 const QColor color = getColorForPolygon(polygon, preferences, colorTable);
211
b27b90fb993f Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 210
diff changeset
305 pointsToRender(polygon, [&](const glm::vec3& point, const glm::vec3& normal){
189
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
306 gl::ModelShaders::Vertex& vertex = vertexBuffer.emplace_back();
211
b27b90fb993f Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 210
diff changeset
307 vertex.position = point;
b27b90fb993f Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 210
diff changeset
308 vertex.normal = normal;
189
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
309 vertex.color = glm::vec4{color.redF(), color.greenF(), color.blueF(), color.alphaF()};
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
310 vertex.id = polygon.id.value;
211
b27b90fb993f Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 210
diff changeset
311 });
189
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
312 });
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
313 for (gl::ModelShaders::ShaderObject& shader : shaders->shaderObjects)
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
314 {
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
315 shader.vertexCount = shader.cachedData.size();
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
316 shader.buffer.bind();
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
317 const int bytes = static_cast<int>(shader.cachedData.size() * sizeof shader.cachedData[0]);
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
318 shader.buffer.allocate(shader.cachedData.data(), bytes);
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
319 shader.buffer.release();
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
320 }
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
321 }
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
322
200
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 193
diff changeset
323 ModelId gl::idFromColor(const std::array<GLubyte, 3>& data)
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
324 {
189
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
325 return {data[0] * std::int32_t{0x10000} + data[1] * std::int32_t{0x100} + data[2]};
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
326 }
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
327
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
328 void gl::bindModelShaderVertexArray(gl::ModelShaders* shaders, gl::ArrayClass arrayClass)
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
329 {
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
330 ModelShaders::ShaderObject& shaderObject = shaders->shaderObjects[static_cast<int>(arrayClass)];
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
331 shaderObject.vertexArray.bind();
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
332 shaderObject.program->bind();
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
333 }
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
334
189
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
335 void gl::releaseModelShaderVertexArray(gl::ModelShaders* shaders, gl::ArrayClass arrayClass)
51
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 50
diff changeset
336 {
189
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
337 ModelShaders::ShaderObject& shaderObject = shaders->shaderObjects[static_cast<int>(arrayClass)];
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
338 shaderObject.program->release();
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
339 shaderObject.vertexArray.release();
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
340 }
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
341
200
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 193
diff changeset
342 void gl::setModelShaderSelectedObjects(gl::ModelShaders* shaders, const QSet<ModelId> &ids)
189
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
343 {
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
344 for (ModelShaders::ShaderObject& object : shaders->shaderObjects)
51
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 50
diff changeset
345 {
189
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
346 std::vector<ModelShaders::Vertex>& vector = object.cachedData;
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
347 for (ModelShaders::Vertex& vertex : vector)
51
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 50
diff changeset
348 {
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 50
diff changeset
349 vertex.selected = (ids.contains({vertex.id})) ? 1 : 0;
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 50
diff changeset
350 }
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 50
diff changeset
351 const GLsizeiptr size = static_cast<int>(vector.size() * sizeof vector[0]);
80
764381756899 simplification
Teemu Piippo <teemu@hecknology.net>
parents: 78
diff changeset
352 object.buffer.bind();
51
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 50
diff changeset
353 glBufferSubData(GL_ARRAY_BUFFER, 0, size, vector.data());
80
764381756899 simplification
Teemu Piippo <teemu@hecknology.net>
parents: 78
diff changeset
354 object.buffer.release();
51
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 50
diff changeset
355 }
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 50
diff changeset
356 }
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 50
diff changeset
357
189
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
358 std::size_t gl::vertexCount(const gl::ModelShaders* shaders, const gl::ArrayClass arrayClass)
28
c92c1daf735f modelview matrix set up
Teemu Piippo <teemu@hecknology.net>
parents: 27
diff changeset
359 {
189
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
360 return shaders->shaderObjects[static_cast<int>(arrayClass)].vertexCount;
28
c92c1daf735f modelview matrix set up
Teemu Piippo <teemu@hecknology.net>
parents: 27
diff changeset
361 }

mercurial