Wed, 28 Jul 2021 08:23:09 +0300
update
src/gl/common.h | file | annotate | diff | comparison | revisions | |
src/gl/compiler.cpp | file | annotate | diff | comparison | revisions | |
src/gl/partrenderer.cpp | file | annotate | diff | comparison | revisions | |
src/gl/vertexprogram.cpp | file | annotate | diff | comparison | revisions | |
src/gl/vertexprogram.h | file | annotate | diff | comparison | revisions | |
src/ui/canvas.cpp | file | annotate | diff | comparison | revisions | |
src/vertexmap.cpp | file | annotate | diff | comparison | revisions | |
src/vertexmap.h | file | annotate | diff | comparison | revisions |
--- a/src/gl/common.h Tue Jul 27 16:29:00 2021 +0300 +++ b/src/gl/common.h Wed Jul 28 08:23:09 2021 +0300 @@ -189,7 +189,9 @@ // Use a different colour for each object RandomColors, // Render so that the colour of an object has an one to one correspondence with its id - PickScene + PickScene, + // Render a scene where vertices can be picked + VertexPickScene, }; // Different ways to render fragments. @@ -206,6 +208,8 @@ RandomColors = 3, // Use a colour that codes the object's id Id = 4, + // Render everything black + Black = 5, }; // User options for rendering
--- a/src/gl/compiler.cpp Tue Jul 27 16:29:00 2021 +0300 +++ b/src/gl/compiler.cpp Wed Jul 28 08:23:09 2021 +0300 @@ -48,6 +48,7 @@ const int FRAGSTYLE_BfcRed = 2; const int FRAGSTYLE_Random = 3; const int FRAGSTYLE_Id = 4; +const int FRAGSTYLE_Black = 5; void main() { @@ -77,6 +78,10 @@ { vColor = vec4(0.9, 0.2, 0.2, 1.0); } + else if (fragmentStyle == FRAGSTYLE_Black) + { + vColor = vec4(0.0, 0.0, 0.0, 1.0); + } else { vColor = color;
--- a/src/gl/partrenderer.cpp Tue Jul 27 16:29:00 2021 +0300 +++ b/src/gl/partrenderer.cpp Wed Jul 28 08:23:09 2021 +0300 @@ -178,6 +178,11 @@ this->setFragmentStyle(gl::FragmentStyle::Id); this->renderAllArrays(); break; + case gl::RenderStyle::VertexPickScene: + glLineWidth(1.0f); + this->setFragmentStyle(gl::FragmentStyle::Black); + this->renderAllArrays(); + break; case gl::RenderStyle::Wireframe: glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); this->setFragmentStyle(gl::FragmentStyle::Normal);
--- a/src/gl/vertexprogram.cpp Tue Jul 27 16:29:00 2021 +0300 +++ b/src/gl/vertexprogram.cpp Wed Jul 28 08:23:09 2021 +0300 @@ -3,7 +3,8 @@ static const char vertexShaderSource[] = R"( #version 330 core - +const int FRAGSTYLE_Normal = 0; +const int FRAGSTYLE_Id = 1; layout (location = 0) in vec3 in_position; layout (location = 1) in vec3 in_color; uniform mat4 view; @@ -30,8 +31,18 @@ } )"; -static constexpr int VERTICES_PER_OBJECT = 16; +static constexpr glm::vec3 o = {0, 0, 0}; +static constexpr glm::vec3 c1 = {1, -1, 1}; +static constexpr glm::vec3 c2 = {1, -1, -1}; +static constexpr glm::vec3 c3 = {-1, -1, -1}; +static constexpr glm::vec3 c4 = {-1, -1, 1}; +static constexpr glm::vec3 markerGeometry[] = { + o, c1, c2, + o, c2, c3, + o, c3, c4, + o, c4, c1, +}; VertexProgram::VertexProgram(QObject *parent) : AbstractBasicShaderProgram{parent} @@ -76,7 +87,7 @@ GLenum VertexProgram::drawMode() const { - return GL_LINES; + return GL_TRIANGLES; } QOpenGLBuffer::UsagePattern VertexProgram::usagePattern() const @@ -84,6 +95,11 @@ return QOpenGLBuffer::DynamicDraw; } +void VertexProgram::setFragmentStyle(const FragmentStyle newFragmentStyle) +{ + this->fragmentStyle = newFragmentStyle; +} + void VertexProgram::build(const Document *document) { constexpr float size = 1; @@ -91,23 +107,11 @@ this->data.clear(); document->applyToVertices([&](const glm::vec3& vertex, const std::set<ldraw::id_t>&) { - reserveMore(this->data, VERTICES_PER_OBJECT); - this->data.push_back({vertex, color}); - this->data.push_back({vertex + glm::vec3{size, -size, size}, color}); - this->data.push_back({vertex, color}); - this->data.push_back({vertex + glm::vec3{size, -size, -size}, color}); - this->data.push_back({vertex, color}); - this->data.push_back({vertex + glm::vec3{-size, -size, -size}, color}); - this->data.push_back({vertex, color}); - this->data.push_back({vertex + glm::vec3{-size, -size, size}, color}); - this->data.push_back({vertex + glm::vec3{size, -size, size}, color}); - this->data.push_back({vertex + glm::vec3{size, -size, -size}, color}); - this->data.push_back({vertex + glm::vec3{size, -size, -size}, color}); - this->data.push_back({vertex + glm::vec3{-size, -size, -size}, color}); - this->data.push_back({vertex + glm::vec3{-size, -size, -size}, color}); - this->data.push_back({vertex + glm::vec3{-size, -size, size}, color}); - this->data.push_back({vertex + glm::vec3{-size, -size, size}, color}); - this->data.push_back({vertex + glm::vec3{size, -size, size}, color}); + reserveMore(this->data, ::countof(::markerGeometry)); + for (const glm::vec3& point : ::markerGeometry) + { + this->data.push_back({vertex + point * size, color}); + } }); this->buffer.bind(); this->buffer.allocate(this->vertexData(), this->vertexCount() * this->vertexSize());
--- a/src/gl/vertexprogram.h Tue Jul 27 16:29:00 2021 +0300 +++ b/src/gl/vertexprogram.h Wed Jul 28 08:23:09 2021 +0300 @@ -6,6 +6,11 @@ class VertexProgram : public AbstractBasicShaderProgram { public: + enum FragmentStyle + { + Normal, + Id + }; struct Vertex { glm::vec3 position; @@ -22,8 +27,10 @@ void setupVertexArrays() override; GLenum drawMode() const override; QOpenGLBuffer::UsagePattern usagePattern() const override; + void setFragmentStyle(FragmentStyle newFragmentStyle); private: std::vector<Vertex> data; + FragmentStyle fragmentStyle = Normal; }; #endif // VERTEXPROGRAM_H
--- a/src/ui/canvas.cpp Tue Jul 27 16:29:00 2021 +0300 +++ b/src/ui/canvas.cpp Wed Jul 28 08:23:09 2021 +0300 @@ -161,6 +161,16 @@ this->axesProgram->draw(); glDisable(GL_LINE_SMOOTH); } + // Render vertices + { + glLineWidth(2); + glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); + glEnable(GL_LINE_SMOOTH); + glHint(GL_LINE_SMOOTH_HINT, GL_NICEST); + this->vertexProgram->draw(); + glDisable(GL_LINE_SMOOTH); + glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); + } // Render grid { glEnable(GL_BLEND); @@ -168,14 +178,6 @@ this->gridProgram->draw(); glDisable(GL_BLEND); } - // Render vertices - { - glLineWidth(2); - glEnable(GL_LINE_SMOOTH); - glHint(GL_LINE_SMOOTH_HINT, GL_NICEST); - this->vertexProgram->draw(); - glDisable(GL_LINE_SMOOTH); - } if (this->worldPosition.has_value()) { QPainter painter{this};
--- a/src/vertexmap.cpp Tue Jul 27 16:29:00 2021 +0300 +++ b/src/vertexmap.cpp Wed Jul 28 08:23:09 2021 +0300 @@ -29,6 +29,7 @@ { this->map.clear(); this->vertices.clear(); + this->vertexHashes.clear(); this->model->apply<ldraw::Object>([&](const ldraw::Object* object) { for (int i = 0; i < object->numPoints(); i += 1) @@ -36,7 +37,11 @@ const glm::vec3& point = object->getPoint(i); const unsigned int hash = qHash(point); this->map[hash].insert(object->id); - this->vertices[hash] = point; + if (not this->vertexHashes.contains(hash)) + { + this->vertexHashes.insert(hash); + this->vertices.push_back(point); + } } }); Q_EMIT this->verticesChanged(); @@ -48,9 +53,9 @@ */ void VertexMap::apply(ApplyFunction fn) const { - for (auto it = this->map.cbegin(); it != this->map.cend(); ++it) + for (unsigned int i = 0; i < this->vertices.size(); i += 1) { - const glm::vec3& point = this->vertices.at(it->first); - fn(point, it->second); + const glm::vec3& point = this->vertices[i]; + fn(point, this->map.at(qHash(point))); } }
--- a/src/vertexmap.h Tue Jul 27 16:29:00 2021 +0300 +++ b/src/vertexmap.h Wed Jul 28 08:23:09 2021 +0300 @@ -19,7 +19,8 @@ Q_SIGNAL void verticesChanged(); private: const Model* const model; - std::map<unsigned int, glm::vec3> vertices; + QSet<unsigned int> vertexHashes; + std::vector<glm::vec3> vertices; std::map<unsigned int, std::set<ldraw::id_t>> map; };