Rewrote the objl library and did some conversion helper classes

This commit is contained in:
Ben
2019-02-23 12:02:28 +00:00
parent a170621eea
commit cf7b72df2a
7 changed files with 285 additions and 8 deletions

View File

@@ -46,7 +46,8 @@ include_directories(${executable}
)
file(GLOB SourceFiles
${SrcDIR}/*
${SrcDIR}/*
${SrcDIR}/util/*
)
add_executable(${executable} ${SourceFiles})

View File

@@ -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<Vertex>& _Vertices, std::vector<unsigned int>& _Indices);
// Mesh Name
std::string MeshName;
// Vertex List
std::vector<Vertex> Vertices;
// Index List
std::vector<unsigned int> 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<std::string> &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 <class T>
inline const T & getElement(const std::vector<T> &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<Mesh> LoadedMeshes;
// Loaded Vertex Objects
std::vector<Vertex> LoadedVertices;
// Loaded Index Positions
std::vector<unsigned int> LoadedIndices;
// Loaded Material Objects
std::vector<Material> LoadedMaterials;
private:
// Generate vertices from a list of positions,
// tcoords, normals and a face line
void GenVerticesFromRawOBJ(std::vector<Vertex>& oVerts,
const std::vector<Vector3>& iPositions,
const std::vector<Vector2>& iTCoords,
const std::vector<Vector3>& iNormals,
std::string icurline);
// Triangulate a list of vertices into a face by printing
// inducies corresponding with triangles within it
void VertexTriangluation(std::vector<unsigned int>& oIndices,
const std::vector<Vertex>& 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;
}
};
}
}
#endif

View File

@@ -7,6 +7,7 @@
// #include <glm/gtc/type_ptr.hpp>
// Custom includes
#define OBJL_DEFINITION
#define LOGGER_DEFINITION
#include <logger.h>

View File

@@ -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
}

View File

@@ -4,14 +4,14 @@
#include <string>
#include <vector>
#include <OBJLoader.h>
#include <glad/glad.hpp>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <OBJLoader.h>
#include "./util/util.h"
class Shader;

View File

@@ -1,3 +1,30 @@
#include "util.h"
void OBJLtoGLM(std::vector<objl::Vertex>& inVertArr,
std::vector<glm::vec3>& outVert,
std::vector<glm::vec3>& outNorm,
std::vector<glm::vec2>& 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<unsigned int>& inIndices,
std::vector<GLuint>& outIndices) {
for (int i = 0; i < inIndices.size(); i++) {
outIndices.push_back(inIndices[i]);
}
}

View File

@@ -1,9 +1,17 @@
#ifndef SMHENGINE_SRC_UTIL_UTIL_H_
#define SMHENGINE_SRC_UTIL_UTUL_H_
template <typename T, typename P>
void OBJLtoGLM(T& inVec, P& outVec) {
#include <glad/glad.hpp>
#include <glm/glm.hpp>
#include <vector>
#include <OBJLoader.h>
}
void OBJLtoGLM(std::vector<objl::Vertex>& inVertArr,
std::vector<glm::vec3>& outVert,
std::vector<glm::vec3>& outNorm,
std::vector<glm::vec2>& outTexCoord);
void UintToGLuint(std::vector<unsigned int>& inIndices,
std::vector<GLuint>& outIndices);
#endif