Sun, 19 Jan 2020 02:54:48 +0200
commit work on GL rendering
19 | 1 | /* |
2 | * LDForge: LDraw parts authoring CAD | |
3 | * Copyright (C) 2013 - 2018 Teemu Piippo | |
4 | * | |
5 | * This program is free software: you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License as published by | |
7 | * the Free Software Foundation, either version 3 of the License, or | |
8 | * (at your option) any later version. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
17 | */ | |
18 | ||
19 | #define GL_GLEXT_PROTOTYPES | |
20 | #include <GL/glu.h> | |
21 | #include <GL/glext.h> | |
22 | #include "gl/compiler.h" | |
23 | #include "documentmanager.h" | |
24 | #include "invert.h" | |
25 | #include "ring.h" | |
26 | ||
21 | 27 | gl::Compiler::Compiler(QObject* parent) : |
28 | QObject{parent} | |
29 | { | |
30 | } | |
31 | ||
19 | 32 | gl::Compiler::~Compiler() |
33 | { | |
34 | ||
35 | } | |
21 | 36 | |
37 | void gl::Compiler::build(Model* model, DocumentManager* context) | |
38 | { | |
39 | if (not this->initialized) | |
40 | { | |
41 | this->initializeVbo(); | |
42 | } | |
22
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
43 | this->boundingBox = {}; |
21 | 44 | std::vector<GLfloat> vboData[gl::numVbos]; |
45 | const std::vector<gl::Polygon> polygons = model->getPolygons(context); | |
46 | for (const gl::Polygon& polygon : polygons) | |
47 | { | |
48 | this->buildPolygon(polygon, vboData); | |
49 | } | |
50 | for (int i = 0; i < gl::numVbos; i += 1) | |
51 | { | |
52 | this->upload(i, vboData[i]); | |
53 | } | |
54 | } | |
55 | ||
56 | gl::VboClass classifyPolygon(const gl::Polygon& polygon) | |
57 | { | |
58 | switch (polygon.type) | |
59 | { | |
60 | case gl::Polygon::EdgeLine: | |
61 | return gl::VboClass::Lines; | |
62 | case gl::Polygon::Triangle: | |
63 | return gl::VboClass::Triangles; | |
64 | case gl::Polygon::Quadrilateral: | |
65 | return gl::VboClass::Quads; | |
66 | case gl::Polygon::ConditionalEdge: | |
67 | return gl::VboClass::ConditionalLines; | |
68 | } | |
69 | return gl::VboClass::Lines; | |
70 | } | |
71 | ||
72 | QColor colorFromId(linetypes::Id id) | |
73 | { | |
74 | // Calculate a color based from this index. This method caters for | |
75 | // 16777216 objects. I don't think that will be exceeded anytime soon. | |
76 | const int r = (id.value / 0x10000) % 0x100; | |
77 | const int g = (id.value / 0x100) % 0x100; | |
78 | const int b = id.value % 0x100; | |
79 | return {r, g, b}; | |
80 | } | |
81 | ||
82 | void writeVertex(std::vector<GLfloat>& data, const Point3D& point) | |
83 | { | |
22
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
84 | data.push_back(static_cast<GLfloat>(point.x)); |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
85 | data.push_back(static_cast<GLfloat>(point.y)); |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
86 | data.push_back(static_cast<GLfloat>(point.z)); |
21 | 87 | } |
88 | ||
89 | void gl::Compiler::buildPolygon(gl::Polygon polygon, std::vector<GLfloat>* vboData) | |
90 | { | |
91 | const gl::VboClass vboClass = classifyPolygon(polygon); | |
92 | auto vboBuffer = [&](VboSubclass subclass) -> std::vector<GLfloat>& | |
93 | { | |
94 | return vboData[gl::vboIndex({vboClass, subclass})]; | |
95 | }; | |
96 | auto vertexRing = iter::ring(polygon.vertices, polygon.numPolygonVertices()); | |
22
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
97 | for (unsigned int i = 0; i < polygon.numPolygonVertices(); i += 1) |
21 | 98 | { |
99 | const Point3D& v1 = vertexRing[i - 1]; | |
100 | const Point3D& v2 = vertexRing[i]; | |
101 | const Point3D& v3 = vertexRing[i + 1]; | |
102 | const QVector3D normal = QVector3D::crossProduct(v3 - v2, v1 - v2).normalized(); | |
103 | std::vector<GLfloat>& data = vboBuffer(VboSubclass::Normals); | |
104 | for (const GLfloat coord : {normal.x(), normal.y(), normal.z()}) | |
105 | data.push_back(coord); | |
106 | } | |
107 | vboBuffer(VboSubclass::Surfaces).reserve(vboBuffer(VboSubclass::Surfaces).size() + polygon.numPolygonVertices()); | |
108 | // 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
|
109 | for (unsigned int i = 0; i < polygon.numPolygonVertices(); i += 1) |
21 | 110 | { |
111 | polygon.vertices[i].y = -polygon.vertices[i].y; | |
112 | polygon.vertices[i].z = -polygon.vertices[i].z; | |
22
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
113 | this->boundingBox.consider(polygon.vertices[i]); |
21 | 114 | writeVertex(vboBuffer(VboSubclass::Surfaces), polygon.vertices[i]); |
115 | } | |
116 | this->writeColor(vboData, polygon, VboSubclass::RegularColors); | |
117 | this->writeColor(vboData, polygon, VboSubclass::PickColors); | |
118 | this->writeColor(vboData, polygon, VboSubclass::BfcFrontColors); | |
119 | this->writeColor(vboData, polygon, VboSubclass::BfcBackColors); | |
120 | } | |
121 | ||
122 | QColor gl::Compiler::getColorForPolygon(const gl::Polygon& polygon, VboSubclass subclass) | |
123 | { | |
124 | QColor color; | |
125 | ||
126 | switch (subclass) | |
127 | { | |
128 | case VboSubclass::Surfaces: | |
129 | case VboSubclass::Normals: | |
130 | case VboSubclass::InvertedNormals: | |
131 | // Surface and normal VBOs contain vertex data, not colors. So we can't return anything meaningful. | |
132 | return {}; | |
133 | case VboSubclass::BfcFrontColors: | |
134 | // Use the constant green color for BFC front colors | |
135 | return {64, 192, 80}; | |
136 | case VboSubclass::BfcBackColors: | |
137 | // Use the constant red color for BFC back colors | |
138 | return {208, 64, 64}; | |
139 | case VboSubclass::PickColors: | |
140 | // For the picking scene, use unique picking colors provided by the model. | |
141 | return colorFromId(polygon.id); | |
142 | case VboSubclass::RandomColors: | |
143 | // For the random color scene, the owner object has rolled up a random color. Use that. | |
144 | color = {255, 64, 255}; // polygonOwner->randomColor(); | |
145 | break; | |
146 | case VboSubclass::RegularColors: | |
147 | // For normal colors, use the polygon's color. | |
148 | if (polygon.color == colors::main) | |
149 | { | |
150 | color = {255, 255, 64}; // mainColorRepresentation(); | |
151 | } | |
152 | else if (polygon.color == colors::edge) | |
153 | { | |
154 | // Edge color is black, unless we have a dark background, in which case lines need to be bright. | |
155 | color = Qt::black; //luma(config::backgroundColor()) > 40 ? Qt::black : Qt::white; | |
156 | } | |
157 | else | |
158 | { | |
159 | // Not main or edge color, use the polygon's color as is. | |
160 | color = {255, 255, 64}; //polygon.color.faceColor(); | |
161 | } | |
162 | break; | |
163 | } | |
164 | ||
165 | if (color.isValid()) | |
166 | { | |
167 | // We may wish to apply blending on the color to indicate selection or highlight. | |
168 | /* | |
169 | const double blendAlpha = 0.0; | |
170 | if (blendAlpha != 0.0) | |
171 | { | |
172 | QColor selectedColor = config::selectColorBlend(); | |
173 | double denominator = blendAlpha + 1.0; | |
174 | color.setRed((color.red() + (selectedColor.red() * blendAlpha)) / denominator); | |
175 | color.setGreen((color.green() + (selectedColor.green() * blendAlpha)) / denominator); | |
176 | color.setBlue((color.blue() + (selectedColor.blue() * blendAlpha)) / denominator); | |
177 | } | |
178 | */ | |
179 | } | |
180 | else | |
181 | { | |
182 | if (polygon.numPolygonVertices() == 2) | |
183 | color = Qt::black; | |
184 | else | |
185 | color = {255, 255, 64}; | |
186 | } | |
187 | ||
188 | return color; | |
189 | } | |
190 | ||
191 | void gl::Compiler::writeColor(std::vector<GLfloat>* data, const gl::Polygon& polygon, VboSubclass subclass) | |
192 | { | |
193 | std::vector<GLfloat>& buffer = data[gl::vboIndex({classifyPolygon(polygon), subclass})]; | |
194 | const QColor color = this->getColorForPolygon(polygon, subclass); | |
195 | buffer.reserve(data->size() + 4 * polygon.numPolygonVertices()); | |
22
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
196 | for (unsigned int i = 0; i < polygon.numPolygonVertices(); i += 1) |
21 | 197 | { |
22
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
198 | buffer.push_back(static_cast<GLfloat>(color.redF())); |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
199 | buffer.push_back(static_cast<GLfloat>(color.greenF())); |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
200 | buffer.push_back(static_cast<GLfloat>(color.blueF())); |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
201 | buffer.push_back(static_cast<GLfloat>(color.alphaF())); |
21 | 202 | } |
203 | } | |
204 | ||
22
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
205 | Point3D gl::Compiler::modelCenter() const |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
206 | { |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
207 | return boxCenter(this->boundingBox); |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
208 | } |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
209 | |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
210 | double gl::Compiler::modelDistance() const |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
211 | { |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
212 | return longestMeasure(this->boundingBox); |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
213 | } |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
214 | |
21 | 215 | void gl::Compiler::initializeVbo() |
216 | { | |
217 | this->initializeOpenGLFunctions(); | |
218 | glGenBuffers(countof(this->storedVbo), &this->storedVbo[0]); | |
219 | this->initialized = true; | |
220 | } | |
221 | ||
222 | /// | |
223 | /// \brief Uploads data to a vbo | |
224 | /// \param vboAddress Which vbo to upload to | |
225 | /// \param data Data to upload to vbo | |
226 | /// | |
227 | void gl::Compiler::upload(const int vboIndex, const std::vector<GLfloat>& data) | |
228 | { | |
229 | glBindBuffer(GL_ARRAY_BUFFER, this->storedVbo[vboIndex]); | |
22
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
230 | glBufferData(GL_ARRAY_BUFFER, static_cast<GLsizeiptr>(data.size() * sizeof data[0]), data.data(), GL_STATIC_DRAW); |
21 | 231 | glBindBuffer(GL_ARRAY_BUFFER, 0); |
232 | this->storedVboSizes[vboIndex] = data.size(); | |
233 | } | |
234 | ||
235 | GLuint gl::Compiler::vbo(const VboAddress vboAddress) const | |
236 | { | |
237 | return this->storedVbo[gl::vboIndex(vboAddress)]; | |
238 | } | |
239 | ||
22
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
240 | std::size_t gl::Compiler::vboSize(const VboAddress vboAddress) const |
21 | 241 | { |
242 | return this->storedVboSizes[gl::vboIndex(vboAddress)]; | |
243 | } | |
244 | ||
245 | int gl::vboIndex(const VboAddress vboAddress) | |
246 | { | |
247 | return static_cast<std::uint8_t>(vboAddress.vboClass) * gl::numVboSubclasses | |
248 | + static_cast<std::uint8_t>(vboAddress.vboSubclass); | |
249 | } |