src/gl/compiler.cpp

Wed, 22 Jan 2020 00:23:29 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Wed, 22 Jan 2020 00:23:29 +0200
changeset 26
3a9e761e4faa
parent 22
6da867fa5429
child 27
c57fb7a5ffa3
permissions
-rw-r--r--

at least VAOs work now

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
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
28 gl::Compiler::Compiler(const ColorTable& colorTable, QObject* parent) :
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
29 QObject{parent},
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
30 colorTable{colorTable}
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
31 {
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
32 }
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
33
19
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
34 gl::Compiler::~Compiler()
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
35 {
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
36 if (this->initialized)
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
37 {
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
38 for (int arrayId = 0; arrayId < gl::NUM_ARRAY_CLASSES; arrayId += 1)
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
39 {
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
40 glDeleteProgram(this->glObjects[arrayId].program);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
41 glDeleteShader(this->glObjects[arrayId].vertexShader);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
42 glDeleteProgram(this->glObjects[arrayId].fragmentShader);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
43 }
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
44 }
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
45 }
19
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
46
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
47 void gl::Compiler::initialize()
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
48 {
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
49 if (not this->initialized)
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
50 {
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
51 this->initializeOpenGLFunctions();
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
52 for (int arrayId = 0; arrayId < gl::NUM_ARRAY_CLASSES; arrayId += 1)
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
53 {
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
54 QOpenGLVertexArrayObject& vao = this->vertexArrays[arrayId];
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
55 vao.create();
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
56 vao.bind();
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
57 glGenBuffers(gl::numVboSubclasses, &this->storedVbo[gl::numVboSubclasses * arrayId]);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
58 this->buildShaders(arrayId);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
59 vao.release();
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
60 }
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
61 //glGenBuffers(countof(this->storedVbo), &this->storedVbo[0]);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
62 this->initialized = true;
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 }
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
65
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
66 void gl::Compiler::build(Model* model, DocumentManager* context)
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
67 {
22
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
68 this->boundingBox = {};
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
69 std::vector<GLfloat> vboData[gl::numVbos];
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
70 const std::vector<gl::Polygon> polygons = model->getPolygons(context);
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
71 for (const gl::Polygon& polygon : polygons)
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
72 {
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
73 this->buildPolygon(polygon, vboData);
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
74 }
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
75 /*
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
76 for (int index = 0; index < gl::numVbos; index += 1)
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
77 {
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
78 this->upload(index, vboData[index]);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
79 }*/
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
80 for (int arrayId = 0; arrayId < gl::NUM_ARRAY_CLASSES; arrayId += 1)
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
81 {
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
82 this->vertexArrays[arrayId].bind();
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
83 for (int i = 0; i < gl::numVboSubclasses; i += 1)
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
84 {
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
85 const int vboIndex = gl::vboIndex({static_cast<gl::ArrayClass>(arrayId), static_cast<gl::VboSubclass>(i)});
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
86 this->upload(vboIndex, vboData[vboIndex]);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
87 }
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
88 this->vertexArrays[arrayId].release();
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
89 }
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
90 }
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
91
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
92 gl::ArrayClass classifyPolygon(const gl::Polygon& polygon)
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
93 {
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
94 switch (polygon.type)
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
95 {
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
96 case gl::Polygon::EdgeLine:
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
97 return gl::ArrayClass::Lines;
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
98 case gl::Polygon::Triangle:
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
99 return gl::ArrayClass::Triangles;
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
100 case gl::Polygon::Quadrilateral:
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
101 return gl::ArrayClass::Quads;
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
102 case gl::Polygon::ConditionalEdge:
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
103 return gl::ArrayClass::ConditionalLines;
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
104 }
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
105 return gl::ArrayClass::Lines;
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
106 }
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
107
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
108 [[maybe_unused]]
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
109 static QColor colorFromId(linetypes::Id id)
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
110 {
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
111 // Calculate a color based from this index. This method caters for
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
112 // 16777216 objects. I don't think that will be exceeded anytime soon.
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
113 const int r = (id.value / 0x10000) % 0x100;
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
114 const int g = (id.value / 0x100) % 0x100;
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
115 const int b = id.value % 0x100;
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
116 return {r, g, b};
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
117 }
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
118
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
119 void gl::Compiler::buildPolygon(gl::Polygon polygon, std::vector<GLfloat>* vboData)
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
120 {
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
121 const gl::ArrayClass vboClass = classifyPolygon(polygon);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
122 std::vector<GLfloat>& vertexBuffer = vboData[gl::vboIndex({vboClass, gl::VboSubclass::VertexData})];
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
123 std::vector<GLfloat>& normalsBuffer = vboData[gl::vboIndex({vboClass, gl::VboSubclass::Normals})];
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
124 auto vertexRing = iter::ring(polygon.vertices, polygon.numPolygonVertices());
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
125 reserveMore(normalsBuffer, polygon.numPolygonVertices() * 3_z);
22
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
126 for (unsigned int i = 0; i < polygon.numPolygonVertices(); i += 1)
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
127 {
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
128 const Point3D& v1 = vertexRing[i - 1];
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
129 const Point3D& v2 = vertexRing[i];
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
130 const Point3D& v3 = vertexRing[i + 1];
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
131 const QVector3D normal = QVector3D::crossProduct(v3 - v2, v1 - v2).normalized();
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
132 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
133 normalsBuffer.push_back(coord);
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
134 }
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
135 reserveMore(vertexBuffer, polygon.numPolygonVertices() * 7);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
136 const QColor color = this->getColorForPolygon(polygon);
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
137 // 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
138 for (unsigned int i = 0; i < polygon.numPolygonVertices(); i += 1)
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
139 {
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
140 Point3D& point = polygon.vertices[i];
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
141 polygon.vertices[i].y = -polygon.vertices[i].y;
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
142 polygon.vertices[i].z = -polygon.vertices[i].z;
22
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
143 this->boundingBox.consider(polygon.vertices[i]);
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
144 vertexBuffer.push_back(static_cast<GLfloat>(point.x));
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
145 vertexBuffer.push_back(static_cast<GLfloat>(point.y));
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
146 vertexBuffer.push_back(static_cast<GLfloat>(point.z));
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
147 vertexBuffer.push_back(static_cast<GLfloat>(color.redF()));
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
148 vertexBuffer.push_back(static_cast<GLfloat>(color.greenF()));
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
149 vertexBuffer.push_back(static_cast<GLfloat>(color.blueF()));
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
150 vertexBuffer.push_back(static_cast<GLfloat>(color.alphaF()));
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
151 }
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
152 }
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
153
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
154 QColor gl::Compiler::getColorForPolygon(const gl::Polygon& polygon)
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
155 {
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
156 QColor color;
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
157 // For normal colors, use the polygon's color.
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
158 if (polygon.color == colors::main)
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
159 {
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
160 color = {255, 255, 64}; // mainColorRepresentation();
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
161 }
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
162 else if (polygon.color == colors::edge)
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
163 {
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
164 // 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
165 color = Qt::black; //luma(config::backgroundColor()) > 40 ? Qt::black : Qt::white;
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
166 }
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
167 else
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
168 {
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
169 // 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
170 color = this->colorTable[polygon.color].faceColor;
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
171 }
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
172 return color;
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
173 }
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
174
22
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
175 Point3D gl::Compiler::modelCenter() const
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
176 {
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
177 return boxCenter(this->boundingBox);
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
178 }
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
179
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
180 double gl::Compiler::modelDistance() const
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
181 {
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
182 return longestMeasure(this->boundingBox);
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
183 }
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
184
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
185 void gl::Compiler::bindVertexArray(gl::ArrayClass arrayClass)
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
186 {
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
187 this->vertexArrays[static_cast<int>(arrayClass)].bind();
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
188 glUseProgram(this->glObjects[static_cast<int>(arrayClass)].program);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
189 }
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
190
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
191 void gl::Compiler::releaseVertexArray(gl::ArrayClass arrayClass)
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
192 {
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
193 this->vertexArrays[static_cast<int>(arrayClass)].release();
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
194 }
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
195
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
196 void gl::Compiler::buildShaders(int arrayId)
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
197 {
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
198 /*
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
199 this->glObjects[arrayId].vertexShader = glCreateShader(GL_VERTEX_SHADER);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
200 glShaderSource(this->glObjects[arrayId].vertexShader, 1, &vertexShaderSource, nullptr);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
201 glCompileShader(this->glObjects[arrayId].vertexShader);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
202 this->glObjects[arrayId].fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
203 glShaderSource(this->glObjects[arrayId].fragmentShader, 1, &fragmentShaderSource, nullptr);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
204 glCompileShader(this->glObjects[arrayId].fragmentShader);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
205 for (auto&& pair : {
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
206 std::make_pair(this->glObjects[arrayId].vertexShader, tr("vertex shader")),
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
207 std::make_pair(this->glObjects[arrayId].fragmentShader, tr("fragment shader")),
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
208 })
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
209 {
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
210 GLint status;
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
211 glGetShaderiv(this->glObjects[arrayId].fragmentShader, GL_COMPILE_STATUS, &status);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
212 if (status != GL_TRUE)
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
213 {
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
214 char compileLog[512];
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
215 glGetShaderInfoLog(pair.first, countof(compileLog), nullptr, compileLog);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
216 QMessageBox::critical(nullptr, tr("Shader compile error"), tr("Unable to compile the %1. Compile log:\n\n%2").arg(pair.second).arg(compileLog));
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
217 abort();
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
218 }
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
219 }
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
220 this->glObjects[arrayId].program = glCreateProgram();
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
221 glAttachShader(this->glObjects[arrayId].program, this->glObjects[arrayId].vertexShader);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
222 glAttachShader(this->glObjects[arrayId].program, this->glObjects[arrayId].fragmentShader);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
223 glLinkProgram(this->glObjects[arrayId].program);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
224 glUseProgram(this->glObjects[arrayId].program);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
225 const std::size_t size = gl::FLOATS_PER_VERTEX * sizeof(GLfloat);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
226 const GLuint posAttrib = static_cast<GLuint>(glGetAttribLocation(this->glObjects[arrayId].program, "position"));
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
227 glEnableVertexAttribArray(posAttrib);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
228 glVertexAttribPointer(posAttrib, 3, GL_FLOAT, GL_FALSE, size, gl::offset(0));
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
229 const GLuint colAttrib = static_cast<GLuint>(glGetAttribLocation(this->glObjects[arrayId].program, "color"));
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
230 glEnableVertexAttribArray(colAttrib);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
231 glVertexAttribPointer(colAttrib, 4, GL_FLOAT, GL_FALSE, size, gl::offset(3 * sizeof(GLfloat)));
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
232 */
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
233 }
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
234
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
235 ///
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
236 /// \brief Uploads data to a vbo
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
237 /// \param vboAddress Which vbo to upload to
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
238 /// \param data Data to upload to vbo
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
239 ///
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
240 void gl::Compiler::upload(const int vboIndex, const std::vector<GLfloat>& data)
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
241 {
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
242 glBindBuffer(GL_ARRAY_BUFFER, this->storedVbo[vboIndex]);
22
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
243 glBufferData(GL_ARRAY_BUFFER, static_cast<GLsizeiptr>(data.size() * sizeof data[0]), data.data(), GL_STATIC_DRAW);
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
244 this->storedVboSizes[vboIndex] = data.size();
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 GLuint gl::Compiler::vbo(const VboAddress vboAddress) const
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
248 {
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
249 return this->storedVbo[gl::vboIndex(vboAddress)];
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
250 }
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
251
22
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
252 std::size_t gl::Compiler::vboSize(const VboAddress vboAddress) const
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
253 {
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
254 return this->storedVboSizes[gl::vboIndex(vboAddress)];
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
255 }
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
256
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
257 int gl::vboIndex(const VboAddress vboAddress)
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
258 {
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
259 return static_cast<std::uint8_t>(vboAddress.vboClass) * gl::numVboSubclasses
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
260 + static_cast<std::uint8_t>(vboAddress.vboSubclass);
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
261 }

mercurial