src/gl/compiler.cpp

Wed, 22 Jan 2020 22:43:28 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Wed, 22 Jan 2020 22:43:28 +0200
changeset 29
4cc6b582fde8
parent 28
c92c1daf735f
child 30
1536f23cfab7
permissions
-rw-r--r--

added FindGLM.cmake

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;
c57fb7a5ffa3 commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
33 out vec4 vColor;
c57fb7a5ffa3 commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
34 uniform mat4 CameraTransformation;
c57fb7a5ffa3 commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
35
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 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
37 {
c57fb7a5ffa3 commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
38 vColor = color;
28
c92c1daf735f modelview matrix set up
Teemu Piippo <teemu@hecknology.net>
parents: 27
diff changeset
39 gl_Position = CameraTransformation * vec4(position, 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
40 }
c57fb7a5ffa3 commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
41 )";
c57fb7a5ffa3 commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
42
c57fb7a5ffa3 commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
43 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
44 #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
45
c57fb7a5ffa3 commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
46 in vec4 vColor;
c57fb7a5ffa3 commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
47 out vec4 fColor;
c57fb7a5ffa3 commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
48
c57fb7a5ffa3 commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
49 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
50 {
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 fColor = vColor;
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 )";
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
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
55 gl::Compiler::Compiler(const ColorTable& colorTable, QObject* parent) :
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
56 QObject{parent},
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
57 colorTable{colorTable}
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
58 {
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
59 }
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
60
19
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
61 gl::Compiler::~Compiler()
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
62 {
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
63 }
19
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
64
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
65 void gl::Compiler::initialize()
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
66 {
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
67 if (not this->initialized)
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
68 {
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
69 this->initializeOpenGLFunctions();
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
70 for (int i = 0; i < gl::NUM_ARRAY_CLASSES; i += 1)
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
71 {
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
72 auto& object = this->glObjects[i];
c57fb7a5ffa3 commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
73 object.program = new QOpenGLShaderProgram;
c57fb7a5ffa3 commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
74 object.program->create();
28
c92c1daf735f modelview matrix set up
Teemu Piippo <teemu@hecknology.net>
parents: 27
diff changeset
75 const bool vertexShaderCompiled = object.program->addShaderFromSourceCode(QOpenGLShader::Vertex, ::vertexShaderSource);
c92c1daf735f modelview matrix set up
Teemu Piippo <teemu@hecknology.net>
parents: 27
diff changeset
76 QString log;
c92c1daf735f modelview matrix set up
Teemu Piippo <teemu@hecknology.net>
parents: 27
diff changeset
77 if (not vertexShaderCompiled)
c92c1daf735f modelview matrix set up
Teemu Piippo <teemu@hecknology.net>
parents: 27
diff changeset
78 {
c92c1daf735f modelview matrix set up
Teemu Piippo <teemu@hecknology.net>
parents: 27
diff changeset
79 log += "\n" + tr("Vertex shader:") + "\n" + object.program->log();
c92c1daf735f modelview matrix set up
Teemu Piippo <teemu@hecknology.net>
parents: 27
diff changeset
80 }
c92c1daf735f modelview matrix set up
Teemu Piippo <teemu@hecknology.net>
parents: 27
diff changeset
81 const bool fragmentShaderCompiled = object.program->addShaderFromSourceCode(QOpenGLShader::Fragment, ::fragmentShaderSource);
c92c1daf735f modelview matrix set up
Teemu Piippo <teemu@hecknology.net>
parents: 27
diff changeset
82 if (not fragmentShaderCompiled)
c92c1daf735f modelview matrix set up
Teemu Piippo <teemu@hecknology.net>
parents: 27
diff changeset
83 {
c92c1daf735f modelview matrix set up
Teemu Piippo <teemu@hecknology.net>
parents: 27
diff changeset
84 log += "\n" + tr("Fragment shader:") + "\n" + object.program->log();
c92c1daf735f modelview matrix set up
Teemu Piippo <teemu@hecknology.net>
parents: 27
diff changeset
85 }
c92c1daf735f modelview matrix set up
Teemu Piippo <teemu@hecknology.net>
parents: 27
diff changeset
86 if (not vertexShaderCompiled or not fragmentShaderCompiled)
c92c1daf735f modelview matrix set up
Teemu Piippo <teemu@hecknology.net>
parents: 27
diff changeset
87 {
c92c1daf735f modelview matrix set up
Teemu Piippo <teemu@hecknology.net>
parents: 27
diff changeset
88 QMessageBox::critical(nullptr, tr("Shader compile error"), tr("Could not compile shaders.") + "\n" + log);
c92c1daf735f modelview matrix set up
Teemu Piippo <teemu@hecknology.net>
parents: 27
diff changeset
89 std::exit(-1);
c92c1daf735f modelview matrix set up
Teemu Piippo <teemu@hecknology.net>
parents: 27
diff changeset
90 }
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
91 object.program->link();
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 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
93 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
94 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
95 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
96 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
97 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
98 object.program->enableAttributeArray(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
99 object.program->enableAttributeArray(1);
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 object.program->setAttributeBuffer(0, GL_FLOAT, 0, 3);
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 object.program->setAttributeBuffer(1, GL_FLOAT, 3, 4);
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 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
103 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
104 object.program->release();
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
105 }
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
106 this->initialized = true;
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
107 }
19
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
108 }
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
109
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
110 void gl::Compiler::build(Model* model, DocumentManager* context)
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
111 {
22
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
112 this->boundingBox = {};
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
113 std::vector<GLfloat> vboData[gl::NUM_ARRAY_CLASSES];
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
114 const std::vector<gl::Polygon> polygons = model->getPolygons(context);
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
115 for (const gl::Polygon& polygon : polygons)
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
116 {
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
117 this->buildPolygon(polygon, vboData);
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
118 }
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
119 for (int arrayId = 0; arrayId < gl::NUM_ARRAY_CLASSES; arrayId += 1)
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
120 {
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
121 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
122 auto& vector = vboData[arrayId];
c57fb7a5ffa3 commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
123 this->storedVboSizes[arrayId] = vector.size();
c57fb7a5ffa3 commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
124 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
125 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
126 buffer.release();
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
127 }
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
128 }
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
129
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
130 gl::ArrayClass classifyPolygon(const gl::Polygon& polygon)
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
131 {
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
132 switch (polygon.type)
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
133 {
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
134 case gl::Polygon::EdgeLine:
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
135 return gl::ArrayClass::Lines;
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
136 case gl::Polygon::Triangle:
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
137 return gl::ArrayClass::Triangles;
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
138 case gl::Polygon::Quadrilateral:
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
139 return gl::ArrayClass::Quads;
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
140 case gl::Polygon::ConditionalEdge:
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
141 return gl::ArrayClass::ConditionalLines;
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
142 }
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
143 return gl::ArrayClass::Lines;
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
144 }
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
145
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
146 [[maybe_unused]]
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
147 static QColor colorFromId(linetypes::Id id)
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
148 {
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
149 // Calculate a color based from this index. This method caters for
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
150 // 16777216 objects. I don't think that will be exceeded anytime soon.
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
151 const int r = (id.value / 0x10000) % 0x100;
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
152 const int g = (id.value / 0x100) % 0x100;
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
153 const int b = id.value % 0x100;
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
154 return {r, g, b};
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
155 }
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
156
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
157 void gl::Compiler::buildPolygon(gl::Polygon polygon, std::vector<GLfloat>* vboData)
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
158 {
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
159 const gl::ArrayClass vboClass = classifyPolygon(polygon);
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
160 //std::vector<GLfloat>& vertexBuffer = vboData[gl::vboIndex({vboClass, gl::VboSubclass::VertexData})];
c57fb7a5ffa3 commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
161 std::vector<GLfloat>& vertexBuffer = vboData[static_cast<int>(vboClass)];
c57fb7a5ffa3 commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
162 /*
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
163 std::vector<GLfloat>& normalsBuffer = vboData[gl::vboIndex({vboClass, gl::VboSubclass::Normals})];
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
164 auto vertexRing = iter::ring(polygon.vertices, polygon.numPolygonVertices());
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
165 reserveMore(normalsBuffer, polygon.numPolygonVertices() * 3_z);
22
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
166 for (unsigned int i = 0; i < polygon.numPolygonVertices(); i += 1)
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
167 {
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
168 const Point3D& v1 = vertexRing[i - 1];
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
169 const Point3D& v2 = vertexRing[i];
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
170 const Point3D& v3 = vertexRing[i + 1];
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
171 const QVector3D normal = QVector3D::crossProduct(v3 - v2, v1 - v2).normalized();
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
172 for (const GLfloat coord : {normal.x(), normal.y(), normal.z()})
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
173 normalsBuffer.push_back(coord);
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
174 }
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
175 */
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
176 reserveMore(vertexBuffer, polygon.numPolygonVertices() * 7);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
177 const QColor color = this->getColorForPolygon(polygon);
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
178 // Transform vertices so that they're suitable for GL rendering
22
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
179 for (unsigned int i = 0; i < polygon.numPolygonVertices(); i += 1)
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
180 {
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
181 Point3D& point = polygon.vertices[i];
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
182 polygon.vertices[i].y = -polygon.vertices[i].y;
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
183 polygon.vertices[i].z = -polygon.vertices[i].z;
22
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
184 this->boundingBox.consider(polygon.vertices[i]);
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
185 vertexBuffer.push_back(static_cast<GLfloat>(point.x));
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
186 vertexBuffer.push_back(static_cast<GLfloat>(point.y));
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
187 vertexBuffer.push_back(static_cast<GLfloat>(point.z));
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
188 vertexBuffer.push_back(static_cast<GLfloat>(color.redF()));
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
189 vertexBuffer.push_back(static_cast<GLfloat>(color.greenF()));
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
190 vertexBuffer.push_back(static_cast<GLfloat>(color.blueF()));
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
191 vertexBuffer.push_back(static_cast<GLfloat>(color.alphaF()));
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
192 }
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
193 }
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
194
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
195 QColor gl::Compiler::getColorForPolygon(const gl::Polygon& polygon)
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
196 {
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
197 QColor color;
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
198 // For normal colors, use the polygon's color.
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
199 if (polygon.color == colors::main)
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
200 {
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
201 color = {255, 255, 64}; // mainColorRepresentation();
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
202 }
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
203 else if (polygon.color == colors::edge)
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
204 {
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
205 // Edge color is black, unless we have a dark background, in which case lines need to be bright.
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
206 color = Qt::black; //luma(config::backgroundColor()) > 40 ? Qt::black : Qt::white;
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
207 }
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
208 else
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
209 {
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
210 // 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
211 color = this->colorTable[polygon.color].faceColor;
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
212 }
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
213 return color;
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
214 }
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
215
22
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
216 Point3D gl::Compiler::modelCenter() const
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
217 {
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
218 return boxCenter(this->boundingBox);
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
219 }
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
220
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
221 double gl::Compiler::modelDistance() const
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
222 {
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
223 return longestMeasure(this->boundingBox);
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
224 }
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
225
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
226 void gl::Compiler::bindVertexArray(gl::ArrayClass arrayClass)
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
227 {
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
228 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
229 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
230 object.program->bind();
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
231 }
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
232
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
233 void gl::Compiler::releaseVertexArray(gl::ArrayClass arrayClass)
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
234 {
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
235 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
236 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
237 object.vertexArray.release();
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
238 }
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
239
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
240 std::size_t gl::Compiler::vboSize(const gl::ArrayClass arrayClass) const
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
241 {
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
242 return this->storedVboSizes[static_cast<int>(arrayClass)];
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
243 }
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
244
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
245 int gl::vboIndex(const VboAddress vboAddress)
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
246 {
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
247 return static_cast<std::uint8_t>(vboAddress.vboClass) * gl::numVboSubclasses
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
248 + static_cast<std::uint8_t>(vboAddress.vboSubclass);
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
249 }
28
c92c1daf735f modelview matrix set up
Teemu Piippo <teemu@hecknology.net>
parents: 27
diff changeset
250
c92c1daf735f modelview matrix set up
Teemu Piippo <teemu@hecknology.net>
parents: 27
diff changeset
251 QMatrix4x4 gl::toQMatrix(const glm::mat4& matrix)
c92c1daf735f modelview matrix set up
Teemu Piippo <teemu@hecknology.net>
parents: 27
diff changeset
252 {
c92c1daf735f modelview matrix set up
Teemu Piippo <teemu@hecknology.net>
parents: 27
diff changeset
253 return {
c92c1daf735f modelview matrix set up
Teemu Piippo <teemu@hecknology.net>
parents: 27
diff changeset
254 matrix[0][0],
c92c1daf735f modelview matrix set up
Teemu Piippo <teemu@hecknology.net>
parents: 27
diff changeset
255 matrix[0][1],
c92c1daf735f modelview matrix set up
Teemu Piippo <teemu@hecknology.net>
parents: 27
diff changeset
256 matrix[0][2],
c92c1daf735f modelview matrix set up
Teemu Piippo <teemu@hecknology.net>
parents: 27
diff changeset
257 matrix[0][3],
c92c1daf735f modelview matrix set up
Teemu Piippo <teemu@hecknology.net>
parents: 27
diff changeset
258 matrix[1][0],
c92c1daf735f modelview matrix set up
Teemu Piippo <teemu@hecknology.net>
parents: 27
diff changeset
259 matrix[1][1],
c92c1daf735f modelview matrix set up
Teemu Piippo <teemu@hecknology.net>
parents: 27
diff changeset
260 matrix[1][2],
c92c1daf735f modelview matrix set up
Teemu Piippo <teemu@hecknology.net>
parents: 27
diff changeset
261 matrix[1][3],
c92c1daf735f modelview matrix set up
Teemu Piippo <teemu@hecknology.net>
parents: 27
diff changeset
262 matrix[2][0],
c92c1daf735f modelview matrix set up
Teemu Piippo <teemu@hecknology.net>
parents: 27
diff changeset
263 matrix[2][1],
c92c1daf735f modelview matrix set up
Teemu Piippo <teemu@hecknology.net>
parents: 27
diff changeset
264 matrix[2][2],
c92c1daf735f modelview matrix set up
Teemu Piippo <teemu@hecknology.net>
parents: 27
diff changeset
265 matrix[2][3],
c92c1daf735f modelview matrix set up
Teemu Piippo <teemu@hecknology.net>
parents: 27
diff changeset
266 matrix[3][0],
c92c1daf735f modelview matrix set up
Teemu Piippo <teemu@hecknology.net>
parents: 27
diff changeset
267 matrix[3][1],
c92c1daf735f modelview matrix set up
Teemu Piippo <teemu@hecknology.net>
parents: 27
diff changeset
268 matrix[3][2],
c92c1daf735f modelview matrix set up
Teemu Piippo <teemu@hecknology.net>
parents: 27
diff changeset
269 matrix[3][3],
c92c1daf735f modelview matrix set up
Teemu Piippo <teemu@hecknology.net>
parents: 27
diff changeset
270 };
c92c1daf735f modelview matrix set up
Teemu Piippo <teemu@hecknology.net>
parents: 27
diff changeset
271 }

mercurial