src/gl/compiler.cpp

changeset 374
75efc3ba5a56
parent 338
719b909a7d2b
child 376
3cef3b016330
equal deleted inserted replaced
373:e34d6a30b96d 374:75efc3ba5a56
112 } 112 }
113 } 113 }
114 )"; 114 )";
115 115
116 template<typename Fn> 116 template<typename Fn>
117 constexpr void pointsToRender(const PolygonElement& element, Fn func) 117 constexpr void pointsToRender(const PlainPolygonElement& element, Fn func)
118 { 118 {
119 visitPolygon<void>( 119 if (const LineSegment* edge = std::get_if<LineSegment>(&element))
120 [&func](const LineSegment& edge) 120 {
121 { 121 func(edge->p1, glm::vec3{});
122 func(edge.p1, glm::vec3{}); 122 func(edge->p2, glm::vec3{});
123 func(edge.p2, glm::vec3{}); 123 }
124 }, 124 else if (std::holds_alternative<Triangle>(element))
125 [&func](const Triangle& tri) 125 {
126 { 126 const Triangle& tri = std::get<Triangle>(element);
127 func(tri.p1, normalVector({tri.p3, tri.p1, tri.p2})); 127 func(tri.p1, normalVector({tri.p3, tri.p1, tri.p2}));
128 func(tri.p2, normalVector({tri.p1, tri.p2, tri.p3})); 128 func(tri.p2, normalVector(tri));
129 func(tri.p3, normalVector({tri.p2, tri.p3, tri.p1})); 129 func(tri.p3, normalVector({tri.p2, tri.p3, tri.p1}));
130 }, 130 }
131 [&func](const Quadrilateral& quad) 131 else if (std::holds_alternative<Quadrilateral>(element))
132 { 132 {
133 func(quad.p1, normalVector({quad.p4, quad.p1, quad.p2})); 133 const Quadrilateral& quad = std::get<Quadrilateral>(element);
134 func(quad.p2, normalVector({quad.p1, quad.p2, quad.p3})); 134 func(quad.p1, normalVector({quad.p4, quad.p1, quad.p2}));
135 func(quad.p3, normalVector({quad.p2, quad.p3, quad.p4})); 135 func(quad.p2, normalVector({quad.p1, quad.p2, quad.p3}));
136 func(quad.p4, normalVector({quad.p3, quad.p4, quad.p1})); 136 func(quad.p3, normalVector({quad.p2, quad.p3, quad.p4}));
137 }, 137 func(quad.p4, normalVector({quad.p3, quad.p4, quad.p1}));
138 [&func](const ConditionalEdge& cedge) 138 }
139 { 139 else
140 func(cedge.p1, glm::vec3{}); 140 {
141 func(cedge.p2, glm::vec3{}); 141 const ConditionalEdge& conditional_edge = std::get<ConditionalEdge>(element);
142 }, 142 func(conditional_edge.p1, glm::vec3{});
143 element); 143 func(conditional_edge.p2, glm::vec3{});
144 }
144 } 145 }
145 146
146 void gl::buildShaders( 147 void gl::buildShaders(
147 QOpenGLShaderProgram* shaderProgram, 148 QOpenGLShaderProgram* shaderProgram,
148 const char* vertexShaderSource, 149 const char* vertexShaderSource,
209 } 210 }
210 modelShaders->initialized = true; 211 modelShaders->initialized = true;
211 } 212 }
212 } 213 }
213 214
214 static constexpr gl::ArrayClass classifyPolygon(const PolygonElement& element) 215 static constexpr gl::ArrayClass classifyPolygon(const PlainPolygonElement& element)
215 { 216 {
216 return visitPolygon<gl::ArrayClass>( 217 return visitPolygon<gl::ArrayClass>(
217 [](const LineSegment&) { return gl::ArrayClass::Lines; }, 218 [](const LineSegment&) { return gl::ArrayClass::Lines; },
218 [](const Triangle&) { return gl::ArrayClass::Triangles; }, 219 [](const Triangle&) { return gl::ArrayClass::Triangles; },
219 [](const Quadrilateral&) { return gl::ArrayClass::Quads; }, 220 [](const Quadrilateral&) { return gl::ArrayClass::Quads; },
265 BoundingBox result = emptyBoundingBox; 266 BoundingBox result = emptyBoundingBox;
266 iterateModelPolygons(model, context, [&](const PolygonElement& polygon) 267 iterateModelPolygons(model, context, [&](const PolygonElement& polygon)
267 { 268 {
268 visitPoints([&result](const glm::vec3& p) { 269 visitPoints([&result](const glm::vec3& p) {
269 addPointToBox(result, p); 270 addPointToBox(result, p);
270 }, polygon); 271 }, polygon.element);
271 }); 272 });
272 return result; 273 return result;
273 } 274 }
274 275
275 /** 276 /**
285 for (gl::ModelShaders::ShaderObject& shader : shaders->shaderObjects) { 286 for (gl::ModelShaders::ShaderObject& shader : shaders->shaderObjects) {
286 shader.cachedData.clear(); 287 shader.cachedData.clear();
287 } 288 }
288 iterateModelPolygons(model, context, [&](const WithId<PolygonElement>& polygon) 289 iterateModelPolygons(model, context, [&](const WithId<PolygonElement>& polygon)
289 { 290 {
290 const int index = static_cast<int>(classifyPolygon(polygon)); 291 const int index = static_cast<int>(classifyPolygon(polygon.element));
291 std::vector<gl::ModelShaders::Vertex>& vertexBuffer = shaders->shaderObjects[index].cachedData; 292 std::vector<gl::ModelShaders::Vertex>& vertexBuffer = shaders->shaderObjects[index].cachedData;
292 const QColor color = getColorForPolygon(polygon, preferences, colorTable); 293 const QColor color = getColorForPolygon(polygon, preferences, colorTable);
293 pointsToRender(polygon, [&](const glm::vec3& point, const glm::vec3& normal){ 294 pointsToRender(polygon.element, [&](const glm::vec3& point, const glm::vec3& normal){
294 gl::ModelShaders::Vertex& vertex = vertexBuffer.emplace_back(); 295 gl::ModelShaders::Vertex& vertex = vertexBuffer.emplace_back();
295 vertex.position = point; 296 vertex.position = point;
296 vertex.normal = normal; 297 vertex.normal = normal;
297 vertex.color = glm::vec4{color.redF(), color.greenF(), color.blueF(), color.alphaF()}; 298 vertex.color = glm::vec4{color.redF(), color.greenF(), color.blueF(), color.alphaF()};
298 vertex.id = polygon.linenumber; 299 vertex.id = polygon.linenumber;

mercurial