From 482d155dde7ffa86f3782a026f0a18171605793d Mon Sep 17 00:00:00 2001 From: alemuntoni Date: Mon, 27 Jan 2025 10:00:24 +0100 Subject: [PATCH] [mesh] TexCoord component uses TexCoordIndexed class --- tests/core/006-load-save-mesh-obj/main.cpp | 15 ++++++++++ tests/core/008-load-save-mesh-ply/main.cpp | 30 +++++++++++++++++++ vclib/core/include/vclib/load_save/obj/load.h | 13 ++++---- vclib/core/include/vclib/load_save/obj/save.h | 15 ++++++++-- .../vclib/load_save/ply/detail/vertex.h | 16 ++++++++++ .../include/vclib/mesh/components/tex_coord.h | 10 +++---- 6 files changed, 87 insertions(+), 12 deletions(-) diff --git a/tests/core/006-load-save-mesh-obj/main.cpp b/tests/core/006-load-save-mesh-obj/main.cpp index 17d9348b8..656d1d6b4 100644 --- a/tests/core/006-load-save-mesh-obj/main.cpp +++ b/tests/core/006-load-save-mesh-obj/main.cpp @@ -125,6 +125,21 @@ TEMPLATE_TEST_CASE( REQUIRE(tm.faceNumber() == 12); } + SECTION("TriMesh - Wedge TextureDouble") + { + TriMesh tm; + vcl::loadObj(tm, VCLIB_EXAMPLE_MESHES_PATH "/TextureDouble.obj"); + REQUIRE(tm.vertexNumber() == 8); + REQUIRE(tm.faceNumber() == 4); + REQUIRE(tm.textureNumber() == 2); + REQUIRE(tm.isPerFaceWedgeTexCoordsEnabled()); + for(const auto& f : tm.faces()) + { + // first two faces have texture index 0, the other two have index 1 + REQUIRE(f.textureIndex() == f.index() / 2); + } + } + SECTION("PolyMesh - PolyCube") { PolyMesh pm; diff --git a/tests/core/008-load-save-mesh-ply/main.cpp b/tests/core/008-load-save-mesh-ply/main.cpp index 895f81ebf..28e181a65 100644 --- a/tests/core/008-load-save-mesh-ply/main.cpp +++ b/tests/core/008-load-save-mesh-ply/main.cpp @@ -151,6 +151,36 @@ TEMPLATE_TEST_CASE( REQUIRE(tm.faceNumber() == 12); } + SECTION("TriMesh - VertTextureDouble") + { + TriMesh tm; + vcl::loadPly(tm, VCLIB_EXAMPLE_MESHES_PATH "/VertTextureDouble.ply"); + REQUIRE(tm.vertexNumber() == 8); + REQUIRE(tm.faceNumber() == 4); + REQUIRE(tm.textureNumber() == 2); + REQUIRE(tm.isPerVertexTexCoordEnabled()); + for(const auto& v : tm.vertices()) + { + // first four vertices have index 0, the other four have index 1 + REQUIRE(v.texCoord().index() == v.index() / 4); + } + } + + SECTION("TriMesh - Wedge TextureDouble") + { + TriMesh tm; + vcl::loadPly(tm, VCLIB_EXAMPLE_MESHES_PATH "/TextureDouble.ply"); + REQUIRE(tm.vertexNumber() == 8); + REQUIRE(tm.faceNumber() == 4); + REQUIRE(tm.textureNumber() == 2); + REQUIRE(tm.isPerFaceWedgeTexCoordsEnabled()); + for(const auto& f : tm.faces()) + { + // first two faces have texture index 0, the other two have index 1 + REQUIRE(f.textureIndex() == f.index() / 2); + } + } + SECTION("PolyMesh - PolyCube") { PolyMesh pm; diff --git a/vclib/core/include/vclib/load_save/obj/load.h b/vclib/core/include/vclib/load_save/obj/load.h index a16dd86f8..eb1766017 100644 --- a/vclib/core/include/vclib/load_save/obj/load.h +++ b/vclib/core/include/vclib/load_save/obj/load.h @@ -263,7 +263,7 @@ void readObjFace( MeshType& m, MeshInfo& loadedInfo, const Tokenizer& tokens, - const std::vector& wedgeTexCoords, + const std::vector& wedgeTexCoords, const ObjMaterial& currentMaterial, const LoadSettings& settings) { @@ -380,7 +380,7 @@ void readObjFace( std::to_string(fid)); } f.wedgeTexCoord(i) = - wedgeTexCoords[wids[i]] + ((vcl::TexCoordd)wedgeTexCoords[wids[i]]) .cast(); if (currentMaterial.hasTexture) { @@ -409,7 +409,7 @@ void readObjFace( // set the wedge texcoord in the same position of // the vertex f.wedgeTexCoord(i) = - wedgeTexCoords[wids[pos]] + ((vcl::TexCoordd)wedgeTexCoords[wids[pos]]) .cast(); if (currentMaterial.hasTexture) { @@ -507,7 +507,7 @@ void loadObj( uint vn = 0; // number of vertex normals read // save array of texcoords, that are stored later (into wedges when loading // faces or into vertices as a fallback) - std::vector texCoords; + std::vector texCoords; // map of materials loaded std::map materialMap; @@ -588,10 +588,13 @@ void loadObj( HasPerFaceWedgeTexCoords) { if (header == "vt") { // save the texcoord for later - TexCoordd tf; + TexCoordIndexedd tf; for (uint i = 0; i < 2; ++i) { tf[i] = io::readDouble(token); } + if (currentMaterial.hasTexture) { + tf.index() = currentMaterial.mapId; + } texCoords.push_back(tf); } } diff --git a/vclib/core/include/vclib/load_save/obj/save.h b/vclib/core/include/vclib/load_save/obj/save.h index a10d08533..4814283a6 100644 --- a/vclib/core/include/vclib/load_save/obj/save.h +++ b/vclib/core/include/vclib/load_save/obj/save.h @@ -40,7 +40,10 @@ namespace vcl { namespace detail { template -ObjMaterial objMaterialFromVertex(const VertexType& v, const MeshInfo& fi) +ObjMaterial objMaterialFromVertex( + const VertexType& v, + const MeshType& m, + const MeshInfo& fi) { ObjMaterial mat; if constexpr (HasPerVertexColor) { @@ -51,6 +54,14 @@ ObjMaterial objMaterialFromVertex(const VertexType& v, const MeshInfo& fi) mat.Kd.z() = v.color().blueF(); } } + if constexpr(HasPerVertexTexCoord) { + if (fi.hasVertexTexCoords()) { + mat.hasTexture = true; + if constexpr (HasTexturePaths) { + mat.map_Kd = m.texturePath(v.texCoord().index()); + } + } + } return mat; } @@ -117,7 +128,7 @@ void writeElementObjMaterial( if constexpr (EL_IS_VERTEX) { mat = objMaterialFromVertex( - e, fi); + e, m, fi); } if constexpr (EL_IS_FACE) { mat = objMaterialFromFace(e, m, fi); diff --git a/vclib/core/include/vclib/load_save/ply/detail/vertex.h b/vclib/core/include/vclib/load_save/ply/detail/vertex.h index e57620b80..26d8540f6 100644 --- a/vclib/core/include/vclib/load_save/ply/detail/vertex.h +++ b/vclib/core/include/vclib/load_save/ply/detail/vertex.h @@ -91,6 +91,15 @@ void readPlyVertexProperty( } } } + if (p.name == ply::texnumber) { + if constexpr (HasPerVertexTexCoord) { + if (isPerVertexTexCoordAvailable(mesh)) { + v.texCoord().index() = + io::readPrimitiveType(file, p.type, end); + hasBeenRead = true; + } + } + } if (p.name == ply::unknown) { if constexpr (HasPerVertexCustomComponents) { if (mesh.hasPerVertexCustomComponent(p.unknownPropertyName)) { @@ -193,6 +202,13 @@ void writePlyVertices( hasBeenWritten = true; } } + if (p.name == ply::texnumber) { + if constexpr (HasPerVertexTexCoord) { + io::writeProperty( + file, v.texCoord().index(), p.type, format); + hasBeenWritten = true; + } + } if (p.name == ply::unknown) { if constexpr (HasPerVertexCustomComponents) { if (mesh.hasPerVertexCustomComponent( diff --git a/vclib/core/include/vclib/mesh/components/tex_coord.h b/vclib/core/include/vclib/mesh/components/tex_coord.h index 5ef4e00a8..aee0feef0 100644 --- a/vclib/core/include/vclib/mesh/components/tex_coord.h +++ b/vclib/core/include/vclib/mesh/components/tex_coord.h @@ -26,7 +26,7 @@ #include "bases/component.h" #include -#include +#include namespace vcl::comp { @@ -34,7 +34,7 @@ namespace vcl::comp { * @brief The TexCoord class represents a component that stores a texture * coordinate. * - * It exposes a vcl::TexCoord object, that stores a texture coordinate and an + * It exposes a vcl::TexCoordIndexed object, that stores a texture coordinate and an * id of the texture to use. * * For example, if you have a Vertex Element `v` with the TexCoord component, @@ -59,7 +59,7 @@ class TexCoord : public Component< TexCoord, CompId::TEX_COORD, - vcl::TexCoord, + vcl::TexCoordIndexed, ParentElemType, !std::is_same_v, OPT> @@ -67,7 +67,7 @@ class TexCoord : using Base = Component< TexCoord, CompId::TEX_COORD, - vcl::TexCoord, + vcl::TexCoordIndexed, ParentElemType, !std::is_same_v, OPT>; @@ -76,7 +76,7 @@ class TexCoord : /** * @brief Expose the type of the TexCoord. */ - using TexCoordType = vcl::TexCoord; + using TexCoordType = vcl::TexCoordIndexed; /* Constructors */