From 3fea21a2d8f2aa67fd212837fe7e09e4f29ad515 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Tue, 14 Mar 2023 19:16:57 +0000 Subject: Support creating a super texture from fragments Currently makes wild assumptions about vertices and doesn't actually populate the texture, it's just grey --- assetFactory/factoryMesh.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'assetFactory/factoryMesh.cpp') diff --git a/assetFactory/factoryMesh.cpp b/assetFactory/factoryMesh.cpp index 1665b90..c47e80e 100644 --- a/assetFactory/factoryMesh.cpp +++ b/assetFactory/factoryMesh.cpp @@ -2,13 +2,10 @@ #include "collections.hpp" #include "gfx/models/vertex.hpp" #include "modelFactoryMesh.h" -#include Mesh::Ptr FactoryMesh::createMesh() const { - constexpr glm::vec2 NullUV {}; - ModelFactoryMesh mesh; for (const auto & use : uses) { use->createMesh(mesh, 1); @@ -23,7 +20,8 @@ FactoryMesh::createMesh() const const auto smooth = mesh.property(mesh.smoothFaceProperty, face); const auto colour = mesh.color(face); for (const auto & vertex : mesh.fv_range(face)) { - vertices.emplace_back(mesh.point(vertex), NullUV, + const auto textureUV = mesh.texcoord2D(vertex); + vertices.emplace_back(mesh.point(vertex), textureUV, smooth ? mesh.property(mesh.vertex_normals_pph(), vertex) : mesh.property(mesh.face_normals_pph(), face), colour); -- cgit v1.2.3 From f412b04cc367168b1a6f921dfa1171a17e1353fc Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 18 Mar 2023 02:12:21 +0000 Subject: Use indices instead of triangulation --- assetFactory/factoryMesh.cpp | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) (limited to 'assetFactory/factoryMesh.cpp') diff --git a/assetFactory/factoryMesh.cpp b/assetFactory/factoryMesh.cpp index c47e80e..59b390f 100644 --- a/assetFactory/factoryMesh.cpp +++ b/assetFactory/factoryMesh.cpp @@ -10,24 +10,31 @@ FactoryMesh::createMesh() const for (const auto & use : uses) { use->createMesh(mesh, 1); } - mesh.garbage_collection(); - mesh.triangulate(); mesh.update_face_normals(); mesh.update_vertex_normals(); std::vector vertices; + std::vector indices; for (const auto & face : mesh.faces()) { const auto smooth = mesh.property(mesh.smoothFaceProperty, face); const auto colour = mesh.color(face); - for (const auto & vertex : mesh.fv_range(face)) { + auto vrange = mesh.fv_range(face); + const unsigned int start = static_cast(vertices.size()); + for (const auto & vertex : vrange) { const auto textureUV = mesh.texcoord2D(vertex); vertices.emplace_back(mesh.point(vertex), textureUV, smooth ? mesh.property(mesh.vertex_normals_pph(), vertex) : mesh.property(mesh.face_normals_pph(), face), colour); } + const auto vcount = std::distance(vrange.begin(), vrange.end()); + for (unsigned int i = 2; i < vcount; i++) { + indices.push_back(start); + indices.push_back(start + i - 1); + indices.push_back(start + i); + } } - return std::make_shared(vertices, vectorOfN(vertices.size())); + return std::make_shared(vertices, indices); } bool -- cgit v1.2.3 From 51f0f60bd24a3d7ed77cf713f846bc49c2f1aed5 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 18 Mar 2023 02:44:00 +0000 Subject: Use halfedge for texture coordinates More unique than vertex as it is also per face, allowing for a different texture on adjacent faces --- assetFactory/factoryMesh.cpp | 9 +++++---- assetFactory/modelFactoryMesh.h | 3 ++- assetFactory/style.cpp | 4 ++-- 3 files changed, 9 insertions(+), 7 deletions(-) (limited to 'assetFactory/factoryMesh.cpp') diff --git a/assetFactory/factoryMesh.cpp b/assetFactory/factoryMesh.cpp index 59b390f..6cca388 100644 --- a/assetFactory/factoryMesh.cpp +++ b/assetFactory/factoryMesh.cpp @@ -18,16 +18,17 @@ FactoryMesh::createMesh() const for (const auto & face : mesh.faces()) { const auto smooth = mesh.property(mesh.smoothFaceProperty, face); const auto colour = mesh.color(face); - auto vrange = mesh.fv_range(face); + auto hrange = mesh.fh_range(face); const unsigned int start = static_cast(vertices.size()); - for (const auto & vertex : vrange) { - const auto textureUV = mesh.texcoord2D(vertex); + for (const auto & heh : hrange) { + const auto & vertex = mesh.to_vertex_handle(heh); + const auto textureUV = mesh.texcoord2D(heh); vertices.emplace_back(mesh.point(vertex), textureUV, smooth ? mesh.property(mesh.vertex_normals_pph(), vertex) : mesh.property(mesh.face_normals_pph(), face), colour); } - const auto vcount = std::distance(vrange.begin(), vrange.end()); + const auto vcount = std::distance(hrange.begin(), hrange.end()); for (unsigned int i = 2; i < vcount; i++) { indices.push_back(start); indices.push_back(start + i - 1); diff --git a/assetFactory/modelFactoryMesh.h b/assetFactory/modelFactoryMesh.h index 493ce1b..a862dc1 100644 --- a/assetFactory/modelFactoryMesh.h +++ b/assetFactory/modelFactoryMesh.h @@ -35,7 +35,8 @@ namespace OpenMesh { struct ModelFactoryTraits : public OpenMesh::DefaultTraits { FaceAttributes(OpenMesh::Attributes::Normal | OpenMesh::Attributes::Status | OpenMesh::Attributes::Color); EdgeAttributes(OpenMesh::Attributes::Status); - VertexAttributes(OpenMesh::Attributes::Normal | OpenMesh::Attributes::Status | OpenMesh::Attributes::TexCoord2D); + VertexAttributes(OpenMesh::Attributes::Normal | OpenMesh::Attributes::Status); + HalfedgeAttributes(OpenMesh::Attributes::TexCoord2D); using Point = glm::vec3; using Normal = glm::vec3; using Color = glm::vec4; diff --git a/assetFactory/style.cpp b/assetFactory/style.cpp index 8aecc71..d1d4adc 100644 --- a/assetFactory/style.cpp +++ b/assetFactory/style.cpp @@ -30,8 +30,8 @@ Style::applyStyle( auto coords = mf->getTextureCoords(texture); auto coord = coords.begin(); // Wild assumption that face is a quad and the texture should apply linearly - for (const auto & vh : mesh.fv_range(face)) { - mesh.set_texcoord2D(vh, *coord++); + for (const auto & heh : mesh.fh_range(face)) { + mesh.set_texcoord2D(heh, *coord++); } } } -- cgit v1.2.3 From 40cc01f79c6308b2e4b64fab73cb27d4221a648e Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 19 Mar 2023 23:32:47 +0000 Subject: Dedupe vertices during asset factory mesh build This calls Vertex::== far too many times, but it's not (yet) enough to be a problem --- assetFactory/factoryMesh.cpp | 38 +++++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 15 deletions(-) (limited to 'assetFactory/factoryMesh.cpp') diff --git a/assetFactory/factoryMesh.cpp b/assetFactory/factoryMesh.cpp index 6cca388..8869efd 100644 --- a/assetFactory/factoryMesh.cpp +++ b/assetFactory/factoryMesh.cpp @@ -16,23 +16,31 @@ FactoryMesh::createMesh() const std::vector vertices; std::vector indices; for (const auto & face : mesh.faces()) { - const auto smooth = mesh.property(mesh.smoothFaceProperty, face); - const auto colour = mesh.color(face); - auto hrange = mesh.fh_range(face); - const unsigned int start = static_cast(vertices.size()); - for (const auto & heh : hrange) { + const auto & smooth = mesh.property(mesh.smoothFaceProperty, face); + const auto & colour = mesh.color(face); + + std::vector faceIndices; + for (const auto & heh : mesh.fh_range(face)) { const auto & vertex = mesh.to_vertex_handle(heh); - const auto textureUV = mesh.texcoord2D(heh); - vertices.emplace_back(mesh.point(vertex), textureUV, - smooth ? mesh.property(mesh.vertex_normals_pph(), vertex) - : mesh.property(mesh.face_normals_pph(), face), - colour); + const auto & textureUV = mesh.texcoord2D(heh); + const auto & point = mesh.point(vertex); + const auto & normal = smooth ? mesh.property(mesh.vertex_normals_pph(), vertex) + : mesh.property(mesh.face_normals_pph(), face); + Vertex outVertex {point, textureUV, normal, colour}; + if (const auto existingItr = std::find(vertices.rbegin(), vertices.rend(), outVertex); + existingItr != vertices.rend()) { + faceIndices.push_back(static_cast(std::distance(existingItr, vertices.rend()) - 1)); + } + else { + faceIndices.push_back(static_cast(vertices.size())); + vertices.emplace_back(outVertex); + } } - const auto vcount = std::distance(hrange.begin(), hrange.end()); - for (unsigned int i = 2; i < vcount; i++) { - indices.push_back(start); - indices.push_back(start + i - 1); - indices.push_back(start + i); + + for (unsigned int i = 2; i < faceIndices.size(); i++) { + indices.push_back(faceIndices[0]); + indices.push_back(faceIndices[i - 1]); + indices.push_back(faceIndices[i]); } } return std::make_shared(vertices, indices); -- cgit v1.2.3