src/gl/compiler.cpp

Sun, 16 May 2021 22:41:00 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Sun, 16 May 2021 22:41:00 +0300
changeset 103
94719518e310
parent 80
764381756899
child 115
ed884a2fb009
permissions
-rw-r--r--

update

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;
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
51
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 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
53 {
34
1de2b8d64e9f added some sort of lighting
Teemu Piippo <teemu@hecknology.net>
parents: 33
diff changeset
54 mat3 normalMatrix = transpose(inverse(mat3(modelMatrix)));
1de2b8d64e9f added some sort of lighting
Teemu Piippo <teemu@hecknology.net>
parents: 33
diff changeset
55 vNormal = normalize(normalMatrix * normal);
48
3c10f0e2fbe0 added selection highlighting
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
56 if (fragmentStyle == FRAGSTYLE_Id)
37
90443fb328ea wired renderstyle up
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
57 {
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
58 /* 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
59 * 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
60 */
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 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
62 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
63 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
64 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
65 }
51
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 50
diff changeset
66 else if (selected == 1)
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 50
diff changeset
67 {
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 50
diff changeset
68 vColor = vec4(selectedColor, 1.0);
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 50
diff changeset
69 }
37
90443fb328ea wired renderstyle up
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
70 else
90443fb328ea wired renderstyle up
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
71 {
48
3c10f0e2fbe0 added selection highlighting
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
72 if (fragmentStyle == FRAGSTYLE_BfcGreen)
3c10f0e2fbe0 added selection highlighting
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
73 {
3c10f0e2fbe0 added selection highlighting
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
74 vColor = vec4(0.2, 0.9, 0.2, 1.0);
3c10f0e2fbe0 added selection highlighting
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
75 }
3c10f0e2fbe0 added selection highlighting
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
76 else if (fragmentStyle == FRAGSTYLE_BfcRed)
3c10f0e2fbe0 added selection highlighting
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
77 {
3c10f0e2fbe0 added selection highlighting
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
78 vColor = vec4(0.9, 0.2, 0.2, 1.0);
3c10f0e2fbe0 added selection highlighting
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
79 }
3c10f0e2fbe0 added selection highlighting
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
80 else
3c10f0e2fbe0 added selection highlighting
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
81 {
3c10f0e2fbe0 added selection highlighting
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
82 vColor = color;
3c10f0e2fbe0 added selection highlighting
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
83 }
3c10f0e2fbe0 added selection highlighting
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
84 if (highlighted == id)
3c10f0e2fbe0 added selection highlighting
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
85 {
3c10f0e2fbe0 added selection highlighting
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
86 vColor = (vColor + vec4(selectedColor, 1.0) * 0.6) / 1.6;
3c10f0e2fbe0 added selection highlighting
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
87 }
37
90443fb328ea wired renderstyle up
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
88 }
48
3c10f0e2fbe0 added selection highlighting
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
89
34
1de2b8d64e9f added some sort of lighting
Teemu Piippo <teemu@hecknology.net>
parents: 33
diff changeset
90 vFragPos = vec3(modelMatrix * vec4(position, 1.0));
1de2b8d64e9f added some sort of lighting
Teemu Piippo <teemu@hecknology.net>
parents: 33
diff changeset
91 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
92 }
c57fb7a5ffa3 commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
93 )";
c57fb7a5ffa3 commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
94
c57fb7a5ffa3 commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
95 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
96 #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
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 in vec4 vColor;
34
1de2b8d64e9f added some sort of lighting
Teemu Piippo <teemu@hecknology.net>
parents: 33
diff changeset
99 in vec3 vFragPos;
1de2b8d64e9f added some sort of lighting
Teemu Piippo <teemu@hecknology.net>
parents: 33
diff changeset
100 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
101 out vec4 fColor;
34
1de2b8d64e9f added some sort of lighting
Teemu Piippo <teemu@hecknology.net>
parents: 33
diff changeset
102 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
103 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
104 const float ambientStrength = 0.7;
46
98645c8e7704 added the pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
105 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
106
c57fb7a5ffa3 commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
107 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
108 {
46
98645c8e7704 added the pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
109 if (useLighting)
98645c8e7704 added the pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
110 {
98645c8e7704 added the pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
111 vec4 ambient = ambientStrength * lightColor;
98645c8e7704 added the pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
112 vec3 lightDirection = normalize(lightPos - vFragPos);
98645c8e7704 added the pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
113 vec4 diffuse = max(dot(vNormal, lightDirection), 0.0) * lightColor;
98645c8e7704 added the pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
114 fColor = (ambient + diffuse) * vColor;
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 else
98645c8e7704 added the pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
117 {
98645c8e7704 added the pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
118 fColor = vColor;
98645c8e7704 added the pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
119 }
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
120 }
c57fb7a5ffa3 commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
121 )";
c57fb7a5ffa3 commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
122
35
98906a94732f renamed the linetypes namespace to ldraw namespace and added more structures to it
Teemu Piippo <teemu@hecknology.net>
parents: 34
diff changeset
123 gl::Compiler::Compiler(const ldraw::ColorTable& colorTable, QObject* parent) :
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
124 QObject{parent},
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
125 colorTable{colorTable}
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
126 {
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
127 }
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
128
19
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
129 gl::Compiler::~Compiler()
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
130 {
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
131 }
19
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
132
53
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
133 void gl::buildShaders(
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
134 QOpenGLShaderProgram* shaderProgram,
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
135 const char* vertexShaderSource,
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
136 const char* fragmentShaderSource)
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
137 {
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
138 shaderProgram->create();
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
139 const bool vertexShaderCompiled = shaderProgram->addShaderFromSourceCode(QOpenGLShader::Vertex, vertexShaderSource);
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
140 QString log;
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
141 if (not vertexShaderCompiled)
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
142 {
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
143 log += "\n" + QObject::tr("Vertex shader:") + "\n" + shaderProgram->log();
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
144 }
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
145 const bool fragmentShaderCompiled = shaderProgram->addShaderFromSourceCode(QOpenGLShader::Fragment, fragmentShaderSource);
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
146 if (not fragmentShaderCompiled)
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
147 {
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
148 log += "\n" + QObject::tr("Fragment shader:") + "\n" + shaderProgram->log();
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
149 }
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
150 if (not vertexShaderCompiled or not fragmentShaderCompiled)
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
151 {
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
152 QMessageBox::critical(
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
153 nullptr,
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
154 QObject::tr("Shader compile error"),
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
155 QObject::tr("Could not compile shaders.") + "\n" + log);
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
156 std::exit(-1);
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
157 }
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
158 const bool linkSuccessful = shaderProgram->link();
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
159 if (not linkSuccessful)
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
160 {
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
161 QMessageBox::critical(
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
162 nullptr,
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
163 QObject::tr("Shader link error"),
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
164 QObject::tr("Could not link shaders: %1").arg(shaderProgram->log())
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
165 );
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
166 }
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
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
169 void gl::Compiler::initialize()
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
170 {
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
171 if (not this->initialized)
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
172 {
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
173 this->initializeOpenGLFunctions();
80
764381756899 simplification
Teemu Piippo <teemu@hecknology.net>
parents: 78
diff changeset
174 for (auto& object : this->glObjects)
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
175 {
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
176 object.program = new QOpenGLShaderProgram;
53
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
177 gl::buildShaders(object.program, ::vertexShaderSource, ::fragmentShaderSource);
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
178 object.program->bind();
c57fb7a5ffa3 commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
179 object.buffer.create();
c57fb7a5ffa3 commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
180 object.buffer.bind();
c57fb7a5ffa3 commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
181 object.buffer.setUsagePattern(QOpenGLBuffer::DynamicDraw);
c57fb7a5ffa3 commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
182 object.vertexArray.create();
c57fb7a5ffa3 commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
183 object.vertexArray.bind();
51
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 50
diff changeset
184 for (int k : {0, 1, 2, 3, 4})
48
3c10f0e2fbe0 added selection highlighting
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
185 {
3c10f0e2fbe0 added selection highlighting
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
186 object.program->enableAttributeArray(k);
3c10f0e2fbe0 added selection highlighting
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
187 }
80
764381756899 simplification
Teemu Piippo <teemu@hecknology.net>
parents: 78
diff changeset
188 constexpr int stride = sizeof(Vertex);
764381756899 simplification
Teemu Piippo <teemu@hecknology.net>
parents: 78
diff changeset
189 object.program->setAttributeBuffer(0, GL_FLOAT, offsetof(Vertex, position), 3, stride);
764381756899 simplification
Teemu Piippo <teemu@hecknology.net>
parents: 78
diff changeset
190 object.program->setAttributeBuffer(1, GL_FLOAT, offsetof(Vertex, color), 4, stride);
764381756899 simplification
Teemu Piippo <teemu@hecknology.net>
parents: 78
diff changeset
191 object.program->setAttributeBuffer(2, GL_FLOAT, offsetof(Vertex, normal), 3, stride);
764381756899 simplification
Teemu Piippo <teemu@hecknology.net>
parents: 78
diff changeset
192 glVertexAttribIPointer(3, 1, GL_INT, stride, reinterpret_cast<void*>(offsetof(Vertex, id)));
764381756899 simplification
Teemu Piippo <teemu@hecknology.net>
parents: 78
diff changeset
193 glVertexAttribIPointer(4, 1, GL_INT, stride, reinterpret_cast<void*>(offsetof(Vertex, 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
194 object.vertexArray.release();
c57fb7a5ffa3 commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
195 object.buffer.release();
c57fb7a5ffa3 commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
196 object.program->release();
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
197 }
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
198 this->initialized = true;
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
199 }
19
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
200 }
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
201
39
caac957e9834 Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents: 37
diff changeset
202 void gl::Compiler::build(Model* model, DocumentManager* context, const gl::RenderPreferences& preferences)
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
203 {
22
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
204 this->boundingBox = {};
80
764381756899 simplification
Teemu Piippo <teemu@hecknology.net>
parents: 78
diff changeset
205 std::vector<Vertex> vboData[gl::NUM_POLYGON_TYPES];
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
206 const std::vector<gl::Polygon> polygons = model->getPolygons(context);
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
207 for (const gl::Polygon& polygon : polygons)
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
208 {
39
caac957e9834 Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents: 37
diff changeset
209 this->buildPolygon(polygon, vboData, preferences);
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
210 }
80
764381756899 simplification
Teemu Piippo <teemu@hecknology.net>
parents: 78
diff changeset
211 for (int arrayId = 0; arrayId < gl::NUM_POLYGON_TYPES; arrayId += 1)
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
212 {
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
213 auto& buffer = this->glObjects[arrayId].buffer;
c57fb7a5ffa3 commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
214 auto& vector = vboData[arrayId];
34
1de2b8d64e9f added some sort of lighting
Teemu Piippo <teemu@hecknology.net>
parents: 33
diff changeset
215 this->storedVertexCounts[arrayId] = vector.size();
51
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 50
diff changeset
216 this->glObjects[arrayId].cachedData = vector; // todo: get rid of this copy
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
217 buffer.bind();
c57fb7a5ffa3 commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
218 buffer.allocate(vector.data(), static_cast<int>(vector.size() * sizeof vector[0]));
c57fb7a5ffa3 commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
219 buffer.release();
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
220 }
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
221 }
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
222
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
223 gl::ArrayClass classifyPolygon(const gl::Polygon& polygon)
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
224 {
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
225 switch (polygon.type)
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
226 {
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
227 case gl::Polygon::EdgeLine:
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
228 return gl::ArrayClass::Lines;
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
229 case gl::Polygon::Triangle:
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
230 return gl::ArrayClass::Triangles;
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
231 case gl::Polygon::Quadrilateral:
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
232 return gl::ArrayClass::Quads;
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
233 case gl::Polygon::ConditionalEdge:
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
234 return gl::ArrayClass::ConditionalLines;
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
235 }
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
236 return gl::ArrayClass::Lines;
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
237 }
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
238
78
97c3ce5aa498 fixed signed vs unsigned nonsense in gl::Compiler::idFromColor
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
239 ldraw::id_t gl::Compiler::idFromColor(const std::array<GLubyte, 3>& data)
47
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents: 46
diff changeset
240 {
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents: 46
diff changeset
241 return {data[0] * std::int32_t{0x10000} + data[1] * std::int32_t{0x100} + data[2]};
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents: 46
diff changeset
242 }
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents: 46
diff changeset
243
39
caac957e9834 Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents: 37
diff changeset
244 void gl::Compiler::buildPolygon(
caac957e9834 Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents: 37
diff changeset
245 gl::Polygon polygon,
80
764381756899 simplification
Teemu Piippo <teemu@hecknology.net>
parents: 78
diff changeset
246 std::vector<Vertex>* vboData,
39
caac957e9834 Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents: 37
diff changeset
247 const gl::RenderPreferences& preferences)
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
248 {
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
249 const gl::ArrayClass vboClass = classifyPolygon(polygon);
80
764381756899 simplification
Teemu Piippo <teemu@hecknology.net>
parents: 78
diff changeset
250 std::vector<Vertex>& vertexBuffer = vboData[static_cast<int>(vboClass)];
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
251 auto vertexRing = iter::ring(polygon.vertices, polygon.numPolygonVertices());
34
1de2b8d64e9f added some sort of lighting
Teemu Piippo <teemu@hecknology.net>
parents: 33
diff changeset
252 reserveMore(vertexBuffer, polygon.numPolygonVertices());
39
caac957e9834 Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents: 37
diff changeset
253 const QColor color = this->getColorForPolygon(polygon, preferences);
22
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
254 for (unsigned int i = 0; i < polygon.numPolygonVertices(); i += 1)
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
255 {
33
4c41bfe2ec6e replaced matrix and vertex classes with glm
Teemu Piippo <teemu@hecknology.net>
parents: 30
diff changeset
256 const glm::vec3& v1 = vertexRing[i - 1];
4c41bfe2ec6e replaced matrix and vertex classes with glm
Teemu Piippo <teemu@hecknology.net>
parents: 30
diff changeset
257 const glm::vec3& v2 = vertexRing[i];
4c41bfe2ec6e replaced matrix and vertex classes with glm
Teemu Piippo <teemu@hecknology.net>
parents: 30
diff changeset
258 const glm::vec3& v3 = vertexRing[i + 1];
22
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
259 this->boundingBox.consider(polygon.vertices[i]);
80
764381756899 simplification
Teemu Piippo <teemu@hecknology.net>
parents: 78
diff changeset
260 Vertex& vertex = vertexBuffer.emplace_back();
34
1de2b8d64e9f added some sort of lighting
Teemu Piippo <teemu@hecknology.net>
parents: 33
diff changeset
261 vertex.position = polygon.vertices[i];
1de2b8d64e9f added some sort of lighting
Teemu Piippo <teemu@hecknology.net>
parents: 33
diff changeset
262 vertex.normal = glm::normalize(glm::cross(v1 - v2, v3 - v2));
1de2b8d64e9f added some sort of lighting
Teemu Piippo <teemu@hecknology.net>
parents: 33
diff changeset
263 vertex.color = glm::vec4{color.redF(), color.greenF(), color.blueF(), color.alphaF()};
48
3c10f0e2fbe0 added selection highlighting
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
264 vertex.id = polygon.id.value;
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
265 }
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
266 }
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
267
39
caac957e9834 Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents: 37
diff changeset
268 QColor gl::Compiler::getColorForPolygon(const gl::Polygon& polygon, const gl::RenderPreferences& preferences)
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
269 {
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
270 QColor color;
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
271 // For normal colors, use the polygon's color.
35
98906a94732f renamed the linetypes namespace to ldraw namespace and added more structures to it
Teemu Piippo <teemu@hecknology.net>
parents: 34
diff changeset
272 if (polygon.color == ldraw::mainColor)
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
273 {
39
caac957e9834 Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents: 37
diff changeset
274 color = preferences.mainColor;
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
275 }
35
98906a94732f renamed the linetypes namespace to ldraw namespace and added more structures to it
Teemu Piippo <teemu@hecknology.net>
parents: 34
diff changeset
276 else if (polygon.color == ldraw::edgeColor)
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
277 {
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
278 // 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
279 color = luma(preferences.backgroundColor) > (40.0 / 256.0) ? Qt::black : Qt::white;
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
280 }
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
281 else
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
282 {
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
283 // Not main or edge color, use the polygon's color as is.
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
284 color = this->colorTable[polygon.color].faceColor;
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
285 }
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
286 return color;
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
287 }
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
288
33
4c41bfe2ec6e replaced matrix and vertex classes with glm
Teemu Piippo <teemu@hecknology.net>
parents: 30
diff changeset
289 glm::vec3 gl::Compiler::modelCenter() const
22
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
290 {
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
291 return boxCenter(this->boundingBox);
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
292 }
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
293
51
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 50
diff changeset
294 double gl::Compiler::modelDistance() const
22
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
295 {
51
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 50
diff changeset
296 return static_cast<double>(longestMeasure(this->boundingBox));
22
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
297 }
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
298
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
299 void gl::Compiler::bindVertexArray(gl::ArrayClass arrayClass)
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
300 {
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
301 auto& object = this->glObjects[static_cast<int>(arrayClass)];
c57fb7a5ffa3 commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
302 object.vertexArray.bind();
c57fb7a5ffa3 commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
303 object.program->bind();
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
304 }
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
305
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
306 void gl::Compiler::releaseVertexArray(gl::ArrayClass arrayClass)
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
307 {
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
308 auto& object = this->glObjects[static_cast<int>(arrayClass)];
c57fb7a5ffa3 commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
309 object.program->release();
c57fb7a5ffa3 commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
310 object.vertexArray.release();
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
311 }
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
312
73
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
313 void gl::Compiler::setSelectedObjects(const QSet<ldraw::id_t> ids)
51
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 50
diff changeset
314 {
80
764381756899 simplification
Teemu Piippo <teemu@hecknology.net>
parents: 78
diff changeset
315 for (auto& object : this->glObjects)
51
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 50
diff changeset
316 {
80
764381756899 simplification
Teemu Piippo <teemu@hecknology.net>
parents: 78
diff changeset
317 std::vector<Vertex>& vector = object.cachedData;
764381756899 simplification
Teemu Piippo <teemu@hecknology.net>
parents: 78
diff changeset
318 for (Vertex& vertex : vector)
51
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 50
diff changeset
319 {
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 50
diff changeset
320 vertex.selected = (ids.contains({vertex.id})) ? 1 : 0;
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 50
diff changeset
321 }
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 50
diff changeset
322 const GLsizeiptr size = static_cast<int>(vector.size() * sizeof vector[0]);
80
764381756899 simplification
Teemu Piippo <teemu@hecknology.net>
parents: 78
diff changeset
323 object.buffer.bind();
51
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 50
diff changeset
324 glBufferSubData(GL_ARRAY_BUFFER, 0, size, vector.data());
80
764381756899 simplification
Teemu Piippo <teemu@hecknology.net>
parents: 78
diff changeset
325 object.buffer.release();
51
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 50
diff changeset
326 }
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 50
diff changeset
327 }
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 50
diff changeset
328
34
1de2b8d64e9f added some sort of lighting
Teemu Piippo <teemu@hecknology.net>
parents: 33
diff changeset
329 std::size_t gl::Compiler::vertexCount(const gl::ArrayClass arrayClass) const
28
c92c1daf735f modelview matrix set up
Teemu Piippo <teemu@hecknology.net>
parents: 27
diff changeset
330 {
34
1de2b8d64e9f added some sort of lighting
Teemu Piippo <teemu@hecknology.net>
parents: 33
diff changeset
331 return this->storedVertexCounts[static_cast<int>(arrayClass)];
28
c92c1daf735f modelview matrix set up
Teemu Piippo <teemu@hecknology.net>
parents: 27
diff changeset
332 }

mercurial