From ca7f846c6a60294fbe0989cacbd4fb3f02aecf15 Mon Sep 17 00:00:00 2001 From: Ben Date: Wed, 27 Feb 2019 16:07:49 +0000 Subject: [PATCH] Converting to GLM shit from tinyobjloader, having trouble with normals / indicies --- src/mesh.cpp | 5 ++--- src/util/util.cpp | 51 ++++++++++++++++++++++++++++------------------- src/util/util.h | 15 ++++++++++---- 3 files changed, 43 insertions(+), 28 deletions(-) diff --git a/src/mesh.cpp b/src/mesh.cpp index f468662..a793f97 100644 --- a/src/mesh.cpp +++ b/src/mesh.cpp @@ -31,9 +31,8 @@ Mesh::Mesh(ObjLMesh mesh) { } void Mesh::loadFromObj(ObjLMesh mesh) { - // OBJLtoGLM(objMesh.Vertices, vertices, normals, texCoords); - // indices = objMesh.Indices; - // name = objMesh.MeshName; + OBJLtoGLM(mesh, vertices, normals, texCoords); + OBJLtoIndices(mesh, indices); //Logger logger; //for (int i = 0; i < 100; i++) { diff --git a/src/util/util.cpp b/src/util/util.cpp index b003f62..0d72e4d 100644 --- a/src/util/util.cpp +++ b/src/util/util.cpp @@ -2,39 +2,48 @@ #include -void OBJLtoGLM(std::vector& inVertArr, +void OBJLtoGLM(ObjLMesh& mesh, std::vector& outVert, std::vector& outNorm, - std::vector& outTexCoord) { + std::vector& outTexCoord, + std::vector& outIndices) { - for (int i = 0; i < inVertArr.size() - 1; i++) { - - glm::vec3 tempVert {inVertArr[i].Position.X, inVertArr[i].Position.Y, inVertArr[i].Position.Z}; - outVert.push_back(tempVert); + for (const auto &shape : mesh.shapes) { + for (const auto& index : shape.mesh.indices) { - glm::vec3 tempNorm {inVertArr[i].Normal.X, inVertArr[i].Normal.Y, inVertArr[i].Normal.Z}; - outNorm.push_back(tempNorm); + outVert.push_back({ + mesh.attrib.vertices[3 * index.vertex_index + 0], + mesh.attrib.vertices[3 * index.vertex_index + 1], + mesh.attrib.vertices[3 * index.vertex_index + 2] + }); - glm::vec3 tempTexCoord {inVertArr[i].TextureCoordinate.X, inVertArr[i].TextureCoordinate.Y, 1.0f}; - outTexCoord.push_back(tempTexCoord); + outNorm.push_back({ + mesh.attrib.normals[0], + mesh.attrib.normals[0], + mesh.attrib.normals[0] + }); + outTexCoord.push_back({ + mesh.attrib.texcoords[2 * index.texcoord_index + 0], + mesh.attrib.texcoords[2 * index.texcoord_index + 1], + 0.0f + }); + } } } -void UintToGLuint(std::vector& inIndices, +void UintToGLuint(ObjLMesh& mesh, std::vector& outIndices) { - - for (int i = 0; i < inIndices.size(); i++) { - outIndices.push_back(inIndices[i]); - } - + } -void OBJLVec3toGLM(objl::Vector3& inVec, glm::vec3& outVec) { - outVec.x = inVec.X; - outVec.y = inVec.Y; - outVec.z = inVec.Z; -} +// void OBJLVec3toGLM(objl::Vector3& inVec, glm::vec3& outVec) { + +// outVec.x = inVec.X; +// outVec.y = inVec.Y; +// outVec.z = inVec.Z; + +// } diff --git a/src/util/util.h b/src/util/util.h index 6ac97db..b7bf0f7 100644 --- a/src/util/util.h +++ b/src/util/util.h @@ -7,14 +7,21 @@ #include -void OBJLtoGLM(std::vector& inVertArr, +struct ObjLMesh { + tinyobj::attrib_t attrib; + std::vector shapes; + std::vector materials; +}; + +void OBJLtoGLM(ObjLMesh& mesh, std::vector& outVert, std::vector& outNorm, - std::vector& outTexCoord); + std::vector& outTexCoord, + std::vector& outIndices); -void UintToGLuint(std::vector& inIndices, +void UintToGLuint(ObjLMesh& mesh, std::vector& outIndices); -void OBJLVec3toGLM(objl::Vector3& inVec, glm::vec3& outVec); +// void OBJLVec3toGLM(objl::Vector3& inVec, glm::vec3& outVec); #endif