From cf7b72df2a4758ab116799f33a4f26e7a106bb2a Mon Sep 17 00:00:00 2001 From: Ben Date: Sat, 23 Feb 2019 12:02:28 +0000 Subject: [PATCH] Rewrote the objl library and did some conversion helper classes --- CMakeLists.txt | 3 +- include/OBJLoader.h | 242 +++++++++++++++++++++++++++++++++++++++++++- src/main.cpp | 1 + src/mesh.cpp | 2 +- src/mesh.h | 4 +- src/util/util.cpp | 27 +++++ src/util/util.h | 14 ++- 7 files changed, 285 insertions(+), 8 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a628c27..b5db556 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -46,7 +46,8 @@ include_directories(${executable} ) file(GLOB SourceFiles - ${SrcDIR}/* + ${SrcDIR}/* + ${SrcDIR}/util/* ) add_executable(${executable} ${SourceFiles}) diff --git a/include/OBJLoader.h b/include/OBJLoader.h index db84ae1..bd69b9e 100644 --- a/include/OBJLoader.h +++ b/include/OBJLoader.h @@ -24,6 +24,244 @@ // // Description: The namespace that holds eveyrthing that // is needed and used for the OBJ Model Loader +namespace objl +{ + // Structure: Vector2 + // + // Description: A 2D Vector that Holds Positional Data + struct Vector2 + { + // Default Constructor + Vector2(); + // Variable Set Constructor + Vector2(float X_, float Y_); + // Bool Equals Operator Overload + bool operator==(const Vector2& other); + // Bool Not Equals Operator Overload + bool operator!=(const Vector2& other); + // Addition Operator Overload + Vector2 operator+(const Vector2& right); + // Subtraction Operator Overload + Vector2 operator-(const Vector2& right); + // Float Multiplication Operator Overload + Vector2 operator*(const float& other); + + // Positional Variables + float X; + float Y; + }; + + // Structure: Vector3 + // + // Description: A 3D Vector that Holds Positional Data + struct Vector3 + { + // Default Constructor + Vector3(); + // Variable Set Constructor + Vector3(float X_, float Y_, float Z_); + // Bool Equals Operator Overload + bool operator==(const Vector3& other); + // Bool Not Equals Operator Overload + bool operator!=(const Vector3& other); + // Addition Operator Overload + Vector3 operator+(const Vector3& right); + // Subtraction Operator Overload + Vector3 operator-(const Vector3& right); + // Float Multiplication Operator Overload + Vector3 operator*(const float& other); + // Float Division Operator Overload + Vector3 operator/(const float& other); + + // Positional Variables + float X; + float Y; + float Z; + }; + + // Structure: Vertex + // + // Description: Model Vertex object that holds + // a Position, Normal, and Texture Coordinate + struct Vertex + { + // Position Vector + Vector3 Position; + + // Normal Vector + Vector3 Normal; + + // Texture Coordinate Vector + Vector2 TextureCoordinate; + }; + + struct Material + { + Material(); + + // Material Name + std::string name; + // Ambient Color + Vector3 Ka; + // Diffuse Color + Vector3 Kd; + // Specular Color + Vector3 Ks; + // Specular Exponent + float Ns; + // Optical Density + float Ni; + // Dissolve + float d; + // Illumination + int illum; + // Ambient Texture Map + std::string map_Ka; + // Diffuse Texture Map + std::string map_Kd; + // Specular Texture Map + std::string map_Ks; + // Specular Hightlight Map + std::string map_Ns; + // Alpha Texture Map + std::string map_d; + // Bump Map + std::string map_bump; + }; + + // Structure: Mesh + // + // Description: A Simple Mesh Object that holds + // a name, a vertex list, and an index list + struct Mesh + { + // Default Constructor + Mesh(); + // Variable Set Constructor + Mesh(std::vector& _Vertices, std::vector& _Indices); + // Mesh Name + std::string MeshName; + // Vertex List + std::vector Vertices; + // Index List + std::vector Indices; + + // Material + Material MeshMaterial; + }; + + // Namespace: Math + // + // Description: The namespace that holds all of the math + // functions need for OBJL + namespace math + { + // Vector3 Cross Product + Vector3 CrossV3(const Vector3 a, const Vector3 b); + + // Vector3 Magnitude Calculation + float MagnitudeV3(const Vector3 in); + + // Vector3 DotProduct + float DotV3(const Vector3 a, const Vector3 b); + + // Angle between 2 Vector3 Objects + float AngleBetweenV3(const Vector3 a, const Vector3 b); + + // Projection Calculation of a onto b + Vector3 ProjV3(const Vector3 a, const Vector3 b); + } + + // Namespace: Algorithm + // + // Description: The namespace that holds all of the + // Algorithms needed for OBJL + namespace algorithm + { + // Vector3 Multiplication Opertor Overload + Vector3 operator*(const float& left, const Vector3& right); + + // A test to see if P1 is on the same side as P2 of a line segment ab + bool SameSide(Vector3 p1, Vector3 p2, Vector3 a, Vector3 b); + + // Generate a cross produect normal for a triangle + Vector3 GenTriNormal(Vector3 t1, Vector3 t2, Vector3 t3); + + // Check to see if a Vector3 Point is within a 3 Vector3 Triangle + bool inTriangle(Vector3 point, Vector3 tri1, Vector3 tri2, Vector3 tri3); + + // Split a String into a string array at a given token + inline void split(const std::string &in, + std::vector &out, + std::string token); + + // Get tail of string after first token and possibly following spaces + inline std::string tail(const std::string &in); + + // Get first token of string + inline std::string firstToken(const std::string &in); + + // Get element at given index position + template + inline const T & getElement(const std::vector &elements, std::string &index) + { + int idx = std::stoi(index); + if (idx < 0) + idx = int(elements.size()) + idx; + else + idx--; + return elements[idx]; + } + } + + // Class: Loader + // + // Description: The OBJ Model Loader + class Loader + { + public: + // Default Constructor + Loader(); + ~Loader(); + + // Load a file into the loader + // + // If file is loaded return true + // + // If the file is unable to be found + // or unable to be loaded return false + bool LoadFile(std::string Path); + // Loaded Mesh Objects + std::vector LoadedMeshes; + // Loaded Vertex Objects + std::vector LoadedVertices; + // Loaded Index Positions + std::vector LoadedIndices; + // Loaded Material Objects + std::vector LoadedMaterials; + + private: + // Generate vertices from a list of positions, + // tcoords, normals and a face line + void GenVerticesFromRawOBJ(std::vector& oVerts, + const std::vector& iPositions, + const std::vector& iTCoords, + const std::vector& iNormals, + std::string icurline); + + // Triangulate a list of vertices into a face by printing + // inducies corresponding with triangles within it + void VertexTriangluation(std::vector& oIndices, + const std::vector& iVerts); + + // Load Materials from .mtl file + bool LoadMaterials(std::string path); + }; +} + +#ifdef OBJL_IMPLIMENTATION +#undef OBJL_IMPLIMENTATION + namespace objl { // Structure: Vector2 @@ -1164,4 +1402,6 @@ namespace objl return true; } }; -} \ No newline at end of file +} + +#endif diff --git a/src/main.cpp b/src/main.cpp index 5569285..9fbb004 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7,6 +7,7 @@ // #include // Custom includes +#define OBJL_DEFINITION #define LOGGER_DEFINITION #include diff --git a/src/mesh.cpp b/src/mesh.cpp index 68f897e..056ff8f 100644 --- a/src/mesh.cpp +++ b/src/mesh.cpp @@ -9,7 +9,7 @@ Mesh::Mesh(std::string objPath) { } Mesh::Mesh(objl::Mesh objMesh) { - OBJLtoGLM(objMesh.Vertices, vertices); + OBJLtoGLM(objMesh.Vertices, vertices, normals, texCoords); // objMesh.Vertices includes normals, positions and texcoords // it must convert them to the neccesary GLM shit } diff --git a/src/mesh.h b/src/mesh.h index d720180..ccb5ab1 100644 --- a/src/mesh.h +++ b/src/mesh.h @@ -4,14 +4,14 @@ #include #include -#include - #include #include #include #include +#include + #include "./util/util.h" class Shader; diff --git a/src/util/util.cpp b/src/util/util.cpp index a3dec3b..2f19b09 100644 --- a/src/util/util.cpp +++ b/src/util/util.cpp @@ -1,3 +1,30 @@ #include "util.h" +void OBJLtoGLM(std::vector& inVertArr, + std::vector& outVert, + std::vector& outNorm, + std::vector& outTexCoord) { + for (int i = 0; i < inVertArr.size(); i++) { + + glm::vec3 tempVert {inVertArr[i].Position.X, inVertArr[i].Position.Y, inVertArr[i].Position.Z}; + outVert.push_back(tempVert); + + glm::vec3 tempNorm {inVertArr[i].Normal.X, inVertArr[i].Normal.Y, inVertArr[i].Normal.Z}; + outNorm.push_back(tempNorm); + + glm::vec2 tempTexCoord {inVertArr[i].TextureCoordinate.X, inVertArr[i].TextureCoordinate.Y}; + outTexCoord.push_back(tempTexCoord); + + } + +} + +void UintToGLuint(std::vector& inIndices, + std::vector& outIndices) { + + for (int i = 0; i < inIndices.size(); i++) { + outIndices.push_back(inIndices[i]); + } + +} diff --git a/src/util/util.h b/src/util/util.h index fb46053..fb39b5b 100644 --- a/src/util/util.h +++ b/src/util/util.h @@ -1,9 +1,17 @@ #ifndef SMHENGINE_SRC_UTIL_UTIL_H_ #define SMHENGINE_SRC_UTIL_UTUL_H_ -template -void OBJLtoGLM(T& inVec, P& outVec) { +#include +#include +#include +#include -} +void OBJLtoGLM(std::vector& inVertArr, + std::vector& outVert, + std::vector& outNorm, + std::vector& outTexCoord); + +void UintToGLuint(std::vector& inIndices, + std::vector& outIndices); #endif