# HG changeset patch # User Teemu Piippo # Date 1654791087 -10800 # Node ID b27b90fb993f7629794e9b284a649d3097d3d559 # Parent 232e7634cc8a7d1050d6eb291ad8c5cfa8f6cd86 Now builds again diff -r 232e7634cc8a -r b27b90fb993f src/gl/compiler.cpp --- a/src/gl/compiler.cpp Thu Jun 09 13:32:55 2022 +0300 +++ b/src/gl/compiler.cpp Thu Jun 09 19:11:27 2022 +0300 @@ -125,6 +125,36 @@ } )"; +template +constexpr void pointsToRender(const PolygonElement& element, Fn func) +{ + visitPolygon( + [&func](const LineSegment& edge) + { + func(edge.p1, glm::vec3{}); + func(edge.p2, glm::vec3{}); + }, + [&func](const Triangle& tri) + { + func(tri.p1, normalVector({tri.p3, tri.p1, tri.p2})); + func(tri.p2, normalVector({tri.p1, tri.p2, tri.p3})); + func(tri.p3, normalVector({tri.p2, tri.p3, tri.p1})); + }, + [&func](const Quadrilateral& quad) + { + func(quad.p1, normalVector({quad.p4, quad.p1, quad.p2})); + func(quad.p2, normalVector({quad.p1, quad.p2, quad.p3})); + func(quad.p3, normalVector({quad.p2, quad.p3, quad.p4})); + func(quad.p4, normalVector({quad.p3, quad.p4, quad.p1})); + }, + [&func](const ConditionalEdge& cedge) + { + func(cedge.p1, glm::vec3{}); + func(cedge.p2, glm::vec3{}); + }, + element); +} + void gl::buildShaders( QOpenGLShaderProgram* shaderProgram, const char* vertexShaderSource, @@ -165,10 +195,8 @@ void gl::initializeModelShaders(gl::ModelShaders *modelShaders) { - if (not modelShaders->initialized) - { - for (auto& shader : modelShaders->shaderObjects) - { + if (not modelShaders->initialized) { + for (auto& shader : modelShaders->shaderObjects) { shader.program = std::make_unique(); gl::buildShaders(shader.program.get(), ::vertexShaderSource, ::fragmentShaderSource); shader.program->bind(); @@ -177,8 +205,7 @@ shader.buffer.setUsagePattern(QOpenGLBuffer::DynamicDraw); shader.vertexArray.create(); shader.vertexArray.bind(); - for (int k : {0, 1, 2, 3, 4}) - { + for (int k : {0, 1, 2, 3, 4}) { shader.program->enableAttributeArray(k); } using Vertex = ModelShaders::Vertex; @@ -209,16 +236,11 @@ template void iterateModelPolygons(Model* model, DocumentManager* context, Fn&& fn) { - std::optional modelId = context->findIdForModel(model); - if (modelId.has_value()) - { - PolygonCache* cache = context->getPolygonCacheForModel(modelId.value()); - if (cache != nullptr) - { - for (const WithId& polygon : getCachedPolygons(cache, model, context)) - { - fn(polygon); - } + PolygonCache* cache = findPolygonCacheForModel(model, context); + if (cache != nullptr) { + const PolygonCache::vector_type* polygons = getCachedPolygons(cache, model, context); + for (const WithId& polygon : *polygons) { + fn(polygon); } } } @@ -255,9 +277,9 @@ BoundingBox result = emptyBoundingBox; iterateModelPolygons(model, context, [&](const PolygonElement& polygon) { - visitPoints( - [&result](const glm::vec3& p) { addPointToBox(result, p); }, - polygon); + visitPoints([&result](const glm::vec3& p) { + addPointToBox(result, p); + }, polygon); }); return result; } @@ -275,24 +297,18 @@ for (gl::ModelShaders::ShaderObject& shader : shaders->shaderObjects) { shader.cachedData.clear(); } - iterateModelPolygons(model, context, [&](const PolygonElement& polygon) + iterateModelPolygons(model, context, [&](const WithId& polygon) { const int index = static_cast(classifyPolygon(polygon)); std::vector& vertexBuffer = shaders->shaderObjects[index].cachedData; - auto vertexRing = iter::ring(polygon.vertices, polygon.numPolygonVertices()); - reserveMore(vertexBuffer, polygon.numPolygonVertices()); const QColor color = getColorForPolygon(polygon, preferences, colorTable); - for (unsigned int i = 0; i < polygon.numPolygonVertices(); i += 1) - { - const glm::vec3& v1 = vertexRing[i - 1]; - const glm::vec3& v2 = vertexRing[i]; - const glm::vec3& v3 = vertexRing[i + 1]; + pointsToRender(polygon, [&](const glm::vec3& point, const glm::vec3& normal){ gl::ModelShaders::Vertex& vertex = vertexBuffer.emplace_back(); - vertex.position = polygon.vertices[i]; - vertex.normal = glm::normalize(glm::cross(v1 - v2, v3 - v2)); + vertex.position = point; + vertex.normal = normal; vertex.color = glm::vec4{color.redF(), color.greenF(), color.blueF(), color.alphaF()}; vertex.id = polygon.id.value; - } + }); }); for (gl::ModelShaders::ShaderObject& shader : shaders->shaderObjects) { diff -r 232e7634cc8a -r b27b90fb993f src/model.h --- a/src/model.h Thu Jun 09 13:32:55 2022 +0300 +++ b/src/model.h Thu Jun 09 19:11:27 2022 +0300 @@ -60,6 +60,12 @@ }; template +struct transfer_reference +{ + using type = const std::remove_reference_t&; +}; + +template struct transfer_reference { using type = std::remove_reference_t&&; @@ -88,7 +94,7 @@ ConditionalEdge>>; template -struct remove_color { using type = T; }; +struct remove_color {}; template struct remove_color> { using type = T; }; @@ -97,30 +103,43 @@ struct remove_color&> { using type = T&; }; template +struct remove_color&> { using type = const T&; }; + +template struct remove_color&&> { using type = T&&; }; template using remove_color_t = typename remove_color::type; +static_assert(std::is_same_v>, Triangle>); +static_assert(std::is_same_v&>, Triangle&>); +static_assert(std::is_same_v&>, const Triangle&>); + template constexpr remove_color_t extract_colored(T&& x) { return static_cast>(x); } -template -constexpr auto visitPolygon( - std::function)> f1, - std::function)> f2, - std::function)> f3, - std::function)> f4, - T&& element) +template +constexpr auto visitPolygon(Fn1&& f1, Fn2&& f2, Fn3&& f3, Fn4&& f4, T&& element) { - return std::visit(overloaded{f1, f2, f3, f4}, extract_colored(element)); + if (std::holds_alternative(element)) { + return f1(std::get(element)); + } + else if (std::holds_alternative(element)) { + return f2(std::get(element)); + } + else if (std::holds_alternative(element)) { + return f3(std::get(element)); + } + else { + return f4(std::get(element)); + } } template -constexpr void visitPoints(T&& element, Fn func) +constexpr void visitPoints(Fn&& func, T&& element) { visitPolygon( [&func](transfer_reference_t edge) diff -r 232e7634cc8a -r b27b90fb993f src/polygoncache.cpp --- a/src/polygoncache.cpp Thu Jun 09 13:32:55 2022 +0300 +++ b/src/polygoncache.cpp Thu Jun 09 19:11:27 2022 +0300 @@ -11,12 +11,23 @@ PolygonElement element, const glm::mat4& transform) { - visitPoints(element, [&transform](glm::vec3& p) { + visitPoints([&transform](glm::vec3& p) { p = transform * glm::vec4{p, 1}; - }); + }, element); return element; } +PolygonCache* findPolygonCacheForModel(Model* model, DocumentManager* context) +{ + std::optional modelId = context->findIdForModel(model); + if (modelId.has_value()) { + return context->getPolygonCacheForModel(modelId.value()); + } + else { + return nullptr; + } +} + static std::vector> getPolygonsAt( const Model* model, GetPolygonsContext* context) @@ -39,24 +50,20 @@ [&](const Colored& cedge) { result.push_back({{cedge, cedge.color}, id}); }, - [&](const Colored& ref) { + [&result, &id, context](const Colored& ref) { Model* dependency = context->documents->findDependencyByName(context->modelId, ref.name); PolygonCache* cache = nullptr; - if (dependency != nullptr) - { - const auto dependencyModelId = context->documents->findIdForModel(dependency); - if (dependencyModelId.has_value()) { - cache = context->documents->getPolygonCacheForModel(dependencyModelId.value()); - } + if (dependency != nullptr) { + cache = findPolygonCacheForModel(dependency, context->documents); } if (cache != nullptr) { const bool needInverting = glm::determinant(ref.transformation) < 0; - const auto& modelPolygons = getCachedPolygons( + const PolygonCache::vector_type* modelPolygons = getCachedPolygons( cache, dependency, context->documents); - result.reserve(result.size() + modelPolygons.size()); - for (WithId polygon : modelPolygons) { + reserveMore(result, modelPolygons->size()); + for (WithId polygon : *modelPolygons) { polygon = {transformed(polygon, ref.transformation), polygon.id}; if (needInverting != ref.inverted) { gl::invert(polygon); @@ -81,7 +88,7 @@ * @param documents Documents to use to resolve subfile references * @return vector of GL polygons */ -const std::vector> &getCachedPolygons( +const PolygonCache::vector_type* getCachedPolygons( PolygonCache *cache, Model *model, DocumentManager *documents) @@ -97,5 +104,5 @@ } cache->needRecache = false; } - return cache->cachedPolygons; + return &cache->cachedPolygons; } diff -r 232e7634cc8a -r b27b90fb993f src/polygoncache.h --- a/src/polygoncache.h Thu Jun 09 13:32:55 2022 +0300 +++ b/src/polygoncache.h Thu Jun 09 19:11:27 2022 +0300 @@ -17,11 +17,13 @@ struct PolygonCache { - std::vector> cachedPolygons; + using vector_type = std::vector>; + vector_type cachedPolygons; bool needRecache = true; }; -const std::vector>& getCachedPolygons( +const PolygonCache::vector_type* getCachedPolygons( PolygonCache* cache, Model* model, class DocumentManager* documents); +PolygonCache* findPolygonCacheForModel(Model* model, DocumentManager* context);