src/gl/compiler.cpp

Wed, 01 Jan 2020 17:45:56 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Wed, 01 Jan 2020 17:45:56 +0200
changeset 21
0133e565e072
parent 19
ed9685f44ab3
child 22
6da867fa5429
permissions
-rw-r--r--

things

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>
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
22 #include "gl/compiler.h"
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
23 #include "documentmanager.h"
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
24 #include "invert.h"
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
25 #include "ring.h"
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
26
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
27 gl::Compiler::Compiler(QObject* parent) :
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
28 QObject{parent}
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
29 {
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
30 }
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
31
19
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
32 gl::Compiler::~Compiler()
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
33 {
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
34
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
35 }
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
36
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
37 void gl::Compiler::build(Model* model, DocumentManager* context)
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
38 {
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
39 if (not this->initialized)
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
40 {
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
41 this->initializeVbo();
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
42 }
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
43 std::vector<GLfloat> vboData[gl::numVbos];
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
44 const std::vector<gl::Polygon> polygons = model->getPolygons(context);
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
45 for (const gl::Polygon& polygon : polygons)
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
46 {
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
47 this->buildPolygon(polygon, vboData);
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
48 }
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
49 for (int i = 0; i < gl::numVbos; i += 1)
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
50 {
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
51 this->upload(i, vboData[i]);
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
52 }
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
53 }
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
54
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
55 gl::VboClass classifyPolygon(const gl::Polygon& polygon)
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
56 {
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
57 switch (polygon.type)
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
58 {
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
59 case gl::Polygon::EdgeLine:
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
60 return gl::VboClass::Lines;
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
61 case gl::Polygon::Triangle:
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
62 return gl::VboClass::Triangles;
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
63 case gl::Polygon::Quadrilateral:
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
64 return gl::VboClass::Quads;
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
65 case gl::Polygon::ConditionalEdge:
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
66 return gl::VboClass::ConditionalLines;
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
67 }
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
68 return gl::VboClass::Lines;
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
69 }
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
70
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
71 QColor colorFromId(linetypes::Id id)
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
72 {
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
73 // Calculate a color based from this index. This method caters for
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
74 // 16777216 objects. I don't think that will be exceeded anytime soon.
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
75 const int r = (id.value / 0x10000) % 0x100;
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
76 const int g = (id.value / 0x100) % 0x100;
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
77 const int b = id.value % 0x100;
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
78 return {r, g, b};
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
79 }
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
80
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
81 void writeVertex(std::vector<GLfloat>& data, const Point3D& point)
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
82 {
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
83 data.push_back(point.x);
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
84 data.push_back(point.y);
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
85 data.push_back(point.z);
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
86 }
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
87
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
88 void gl::Compiler::buildPolygon(gl::Polygon polygon, std::vector<GLfloat>* vboData)
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
89 {
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
90 const gl::VboClass vboClass = classifyPolygon(polygon);
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
91 auto vboBuffer = [&](VboSubclass subclass) -> std::vector<GLfloat>&
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
92 {
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
93 return vboData[gl::vboIndex({vboClass, subclass})];
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
94 };
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
95 auto vertexRing = iter::ring(polygon.vertices, polygon.numPolygonVertices());
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
96 for (int i = 0; i < polygon.numPolygonVertices(); i += 1)
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
97 {
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
98 const Point3D& v1 = vertexRing[i - 1];
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
99 const Point3D& v2 = vertexRing[i];
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
100 const Point3D& v3 = vertexRing[i + 1];
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
101 const QVector3D normal = QVector3D::crossProduct(v3 - v2, v1 - v2).normalized();
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
102 std::vector<GLfloat>& data = vboBuffer(VboSubclass::Normals);
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
103 for (const GLfloat coord : {normal.x(), normal.y(), normal.z()})
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
104 data.push_back(coord);
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
105 }
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
106 vboBuffer(VboSubclass::Surfaces).reserve(vboBuffer(VboSubclass::Surfaces).size() + polygon.numPolygonVertices());
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
107 // Transform vertices so that they're suitable for GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
108 for (int i = 0; i < polygon.numPolygonVertices(); i += 1)
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
109 {
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
110 polygon.vertices[i].y = -polygon.vertices[i].y;
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
111 polygon.vertices[i].z = -polygon.vertices[i].z;
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
112 #if 0
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
113 // Add these vertices to the bounding box (unless we're going to do it over
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
114 // from scratch afterwards anyway)
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
115 if (not this->needBoundingBoxRebuild)
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
116 {
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
117 this->boundingBox.consider(poly.vertices[i]);
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
118 }
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
119 #endif
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
120 writeVertex(vboBuffer(VboSubclass::Surfaces), polygon.vertices[i]);
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
121 }
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
122 this->writeColor(vboData, polygon, VboSubclass::RegularColors);
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
123 this->writeColor(vboData, polygon, VboSubclass::PickColors);
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
124 this->writeColor(vboData, polygon, VboSubclass::BfcFrontColors);
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
125 this->writeColor(vboData, polygon, VboSubclass::BfcBackColors);
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 QColor gl::Compiler::getColorForPolygon(const gl::Polygon& polygon, VboSubclass subclass)
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
129 {
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
130 QColor color;
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
131
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
132 switch (subclass)
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
133 {
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
134 case VboSubclass::Surfaces:
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
135 case VboSubclass::Normals:
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
136 case VboSubclass::InvertedNormals:
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
137 // Surface and normal VBOs contain vertex data, not colors. So we can't return anything meaningful.
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
138 return {};
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
139 case VboSubclass::BfcFrontColors:
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
140 // Use the constant green color for BFC front colors
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
141 return {64, 192, 80};
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
142 case VboSubclass::BfcBackColors:
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
143 // Use the constant red color for BFC back colors
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
144 return {208, 64, 64};
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
145 case VboSubclass::PickColors:
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
146 // For the picking scene, use unique picking colors provided by the model.
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
147 return colorFromId(polygon.id);
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
148 case VboSubclass::RandomColors:
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
149 // For the random color scene, the owner object has rolled up a random color. Use that.
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
150 color = {255, 64, 255}; // polygonOwner->randomColor();
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
151 break;
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
152 case VboSubclass::RegularColors:
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
153 // For normal colors, use the polygon's color.
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
154 if (polygon.color == colors::main)
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
155 {
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
156 color = {255, 255, 64}; // mainColorRepresentation();
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
157 }
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
158 else if (polygon.color == colors::edge)
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
159 {
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
160 // Edge color is black, unless we have a dark background, in which case lines need to be bright.
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
161 color = Qt::black; //luma(config::backgroundColor()) > 40 ? Qt::black : Qt::white;
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
162 }
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
163 else
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
164 {
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
165 // Not main or edge color, use the polygon's color as is.
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
166 color = {255, 255, 64}; //polygon.color.faceColor();
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
167 }
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
168 break;
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
169 }
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
170
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
171 if (color.isValid())
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
172 {
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
173 // We may wish to apply blending on the color to indicate selection or highlight.
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
174 /*
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
175 const double blendAlpha = 0.0;
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
176 if (blendAlpha != 0.0)
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
177 {
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
178 QColor selectedColor = config::selectColorBlend();
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
179 double denominator = blendAlpha + 1.0;
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
180 color.setRed((color.red() + (selectedColor.red() * blendAlpha)) / denominator);
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
181 color.setGreen((color.green() + (selectedColor.green() * blendAlpha)) / denominator);
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
182 color.setBlue((color.blue() + (selectedColor.blue() * blendAlpha)) / denominator);
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
183 }
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
184 */
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
185 }
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
186 else
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
187 {
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
188 if (polygon.numPolygonVertices() == 2)
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
189 color = Qt::black;
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
190 else
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
191 color = {255, 255, 64};
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 return color;
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
195 }
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
196
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
197 void gl::Compiler::writeColor(std::vector<GLfloat>* data, const gl::Polygon& polygon, VboSubclass subclass)
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
198 {
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
199 std::vector<GLfloat>& buffer = data[gl::vboIndex({classifyPolygon(polygon), subclass})];
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
200 const QColor color = this->getColorForPolygon(polygon, subclass);
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
201 buffer.reserve(data->size() + 4 * polygon.numPolygonVertices());
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
202 for (int i = 0; i < polygon.numPolygonVertices(); i += 1)
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
203 {
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
204 buffer.push_back(color.redF() / 255.0f);
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
205 buffer.push_back(color.greenF() / 255.0f);
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
206 buffer.push_back(color.blueF() / 255.0f);
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
207 buffer.push_back(color.alphaF() / 255.0f);
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
208 }
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
209 }
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
210
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
211 void gl::Compiler::initializeVbo()
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
212 {
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
213 this->initializeOpenGLFunctions();
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
214 glGenBuffers(countof(this->storedVbo), &this->storedVbo[0]);
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
215 this->initialized = true;
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
216 }
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
217
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
218 ///
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
219 /// \brief Uploads data to a vbo
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
220 /// \param vboAddress Which vbo to upload to
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
221 /// \param data Data to upload to vbo
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
222 ///
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
223 void gl::Compiler::upload(const int vboIndex, const std::vector<GLfloat>& data)
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
224 {
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
225 glBindBuffer(GL_ARRAY_BUFFER, this->storedVbo[vboIndex]);
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
226 glBufferData(GL_ARRAY_BUFFER, data.size() * sizeof data[0], data.data(), GL_STATIC_DRAW);
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
227 glBindBuffer(GL_ARRAY_BUFFER, 0);
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
228 this->storedVboSizes[vboIndex] = data.size();
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
229 }
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
230
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
231 GLuint gl::Compiler::vbo(const VboAddress vboAddress) const
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
232 {
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
233 return this->storedVbo[gl::vboIndex(vboAddress)];
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 int gl::Compiler::vboSize(const VboAddress vboAddress) const
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
237 {
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
238 return this->storedVboSizes[gl::vboIndex(vboAddress)];
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
239 }
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
240
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
241 int gl::vboIndex(const VboAddress vboAddress)
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
242 {
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
243 return static_cast<std::uint8_t>(vboAddress.vboClass) * gl::numVboSubclasses
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
244 + static_cast<std::uint8_t>(vboAddress.vboSubclass);
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
245 }

mercurial