src/gl/compiler.cpp

Sun, 19 Jan 2020 02:54:48 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Sun, 19 Jan 2020 02:54:48 +0200
changeset 22
6da867fa5429
parent 21
0133e565e072
child 26
3a9e761e4faa
permissions
-rw-r--r--

commit work on GL rendering

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 }
22
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
43 this->boundingBox = {};
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
44 std::vector<GLfloat> vboData[gl::numVbos];
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
45 const std::vector<gl::Polygon> polygons = model->getPolygons(context);
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
46 for (const gl::Polygon& polygon : polygons)
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
47 {
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
48 this->buildPolygon(polygon, vboData);
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
49 }
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
50 for (int i = 0; i < gl::numVbos; i += 1)
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
51 {
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
52 this->upload(i, vboData[i]);
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
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
56 gl::VboClass classifyPolygon(const gl::Polygon& polygon)
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
57 {
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
58 switch (polygon.type)
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
59 {
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
60 case gl::Polygon::EdgeLine:
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
61 return gl::VboClass::Lines;
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
62 case gl::Polygon::Triangle:
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
63 return gl::VboClass::Triangles;
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
64 case gl::Polygon::Quadrilateral:
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
65 return gl::VboClass::Quads;
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
66 case gl::Polygon::ConditionalEdge:
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
67 return gl::VboClass::ConditionalLines;
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
68 }
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
69 return gl::VboClass::Lines;
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
70 }
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
71
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
72 QColor colorFromId(linetypes::Id id)
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
73 {
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
74 // Calculate a color based from this index. This method caters for
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
75 // 16777216 objects. I don't think that will be exceeded anytime soon.
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
76 const int r = (id.value / 0x10000) % 0x100;
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
77 const int g = (id.value / 0x100) % 0x100;
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
78 const int b = id.value % 0x100;
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
79 return {r, g, b};
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
80 }
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
81
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
82 void writeVertex(std::vector<GLfloat>& data, const Point3D& point)
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
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
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
87 }
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
88
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
89 void gl::Compiler::buildPolygon(gl::Polygon polygon, std::vector<GLfloat>* vboData)
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
90 {
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
91 const gl::VboClass vboClass = classifyPolygon(polygon);
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
92 auto vboBuffer = [&](VboSubclass subclass) -> std::vector<GLfloat>&
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
93 {
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
94 return vboData[gl::vboIndex({vboClass, subclass})];
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
95 };
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
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
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
98 {
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
99 const Point3D& v1 = vertexRing[i - 1];
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
100 const Point3D& v2 = vertexRing[i];
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
101 const Point3D& v3 = vertexRing[i + 1];
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
102 const QVector3D normal = QVector3D::crossProduct(v3 - v2, v1 - v2).normalized();
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
103 std::vector<GLfloat>& data = vboBuffer(VboSubclass::Normals);
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
104 for (const GLfloat coord : {normal.x(), normal.y(), normal.z()})
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
105 data.push_back(coord);
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
106 }
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
107 vboBuffer(VboSubclass::Surfaces).reserve(vboBuffer(VboSubclass::Surfaces).size() + polygon.numPolygonVertices());
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
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
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
110 {
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
111 polygon.vertices[i].y = -polygon.vertices[i].y;
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
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
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
114 writeVertex(vboBuffer(VboSubclass::Surfaces), polygon.vertices[i]);
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
115 }
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
116 this->writeColor(vboData, polygon, VboSubclass::RegularColors);
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
117 this->writeColor(vboData, polygon, VboSubclass::PickColors);
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
118 this->writeColor(vboData, polygon, VboSubclass::BfcFrontColors);
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
119 this->writeColor(vboData, polygon, VboSubclass::BfcBackColors);
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
120 }
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
121
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
122 QColor gl::Compiler::getColorForPolygon(const gl::Polygon& polygon, VboSubclass subclass)
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
123 {
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
124 QColor color;
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
125
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
126 switch (subclass)
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
127 {
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
128 case VboSubclass::Surfaces:
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
129 case VboSubclass::Normals:
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
130 case VboSubclass::InvertedNormals:
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
131 // 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
132 return {};
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
133 case VboSubclass::BfcFrontColors:
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
134 // Use the constant green color for BFC front colors
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
135 return {64, 192, 80};
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
136 case VboSubclass::BfcBackColors:
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
137 // Use the constant red color for BFC back colors
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
138 return {208, 64, 64};
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
139 case VboSubclass::PickColors:
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
140 // For the picking scene, use unique picking colors provided by the model.
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
141 return colorFromId(polygon.id);
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
142 case VboSubclass::RandomColors:
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
143 // 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
144 color = {255, 64, 255}; // polygonOwner->randomColor();
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
145 break;
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
146 case VboSubclass::RegularColors:
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
147 // For normal colors, use the polygon's color.
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
148 if (polygon.color == colors::main)
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
149 {
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
150 color = {255, 255, 64}; // mainColorRepresentation();
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
151 }
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
152 else if (polygon.color == colors::edge)
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
153 {
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
154 // 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
155 color = Qt::black; //luma(config::backgroundColor()) > 40 ? Qt::black : Qt::white;
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
156 }
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
157 else
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
158 {
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
159 // Not main or edge color, use the polygon's color as is.
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
160 color = {255, 255, 64}; //polygon.color.faceColor();
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
161 }
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
162 break;
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
163 }
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
164
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
165 if (color.isValid())
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
166 {
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
167 // We may wish to apply blending on the color to indicate selection or highlight.
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
168 /*
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
169 const double blendAlpha = 0.0;
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
170 if (blendAlpha != 0.0)
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
171 {
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
172 QColor selectedColor = config::selectColorBlend();
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
173 double denominator = blendAlpha + 1.0;
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
174 color.setRed((color.red() + (selectedColor.red() * blendAlpha)) / denominator);
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
175 color.setGreen((color.green() + (selectedColor.green() * blendAlpha)) / denominator);
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
176 color.setBlue((color.blue() + (selectedColor.blue() * blendAlpha)) / denominator);
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
177 }
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
178 */
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
179 }
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
180 else
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
181 {
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
182 if (polygon.numPolygonVertices() == 2)
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
183 color = Qt::black;
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
184 else
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
185 color = {255, 255, 64};
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
186 }
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
187
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
188 return color;
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
189 }
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
190
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
191 void gl::Compiler::writeColor(std::vector<GLfloat>* data, const gl::Polygon& polygon, VboSubclass subclass)
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
192 {
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
193 std::vector<GLfloat>& buffer = data[gl::vboIndex({classifyPolygon(polygon), subclass})];
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
194 const QColor color = this->getColorForPolygon(polygon, subclass);
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
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
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
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
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
202 }
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
203 }
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
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
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
215 void gl::Compiler::initializeVbo()
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
216 {
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
217 this->initializeOpenGLFunctions();
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
218 glGenBuffers(countof(this->storedVbo), &this->storedVbo[0]);
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
219 this->initialized = true;
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
220 }
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
221
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
222 ///
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
223 /// \brief Uploads data to a vbo
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
224 /// \param vboAddress Which vbo to upload to
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
225 /// \param data Data to upload to vbo
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
226 ///
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
227 void gl::Compiler::upload(const int vboIndex, const std::vector<GLfloat>& data)
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
228 {
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
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
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
231 glBindBuffer(GL_ARRAY_BUFFER, 0);
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
232 this->storedVboSizes[vboIndex] = data.size();
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 GLuint gl::Compiler::vbo(const VboAddress vboAddress) const
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
236 {
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
237 return this->storedVbo[gl::vboIndex(vboAddress)];
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
238 }
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
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
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
241 {
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
242 return this->storedVboSizes[gl::vboIndex(vboAddress)];
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 }

mercurial