diff --git a/resources/shaders/phong.vert b/resources/shaders/phong.vert index e59af72..87e4784 100644 --- a/resources/shaders/phong.vert +++ b/resources/shaders/phong.vert @@ -1,21 +1,22 @@ #version 330 -in vec3 position; -in vec3 normal; +layout (location = 0) in vec3 position; +layout (location = 1)in vec3 normal; +layout (location = 2) in vec3 texCoord; out vec3 Normal; out vec3 FragPos; -out vec4 FragPosLightSpace; +// out vec4 FragPosLightSpace; uniform mat4 model; uniform mat4 view; uniform mat4 proj; -uniform mat4 lightSpaceMatrix; +// uniform mat4 lightSpaceMatrix; void main() { gl_Position = proj * view * model * vec4(position, 1.0); FragPos = vec3(model * vec4(position, 1.0)); - FragPosLightSpace = lightSpaceMatrix * vec4(FragPos, 1.0); + // FragPosLightSpace = lightSpaceMatrix * vec4(FragPos, 1.0); Normal = mat3(model) * normal; } diff --git a/src/main.cpp b/src/main.cpp index bedf5c6..6f42c32 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7,7 +7,6 @@ // #include // Custom includes -#define TINYOBJLOADER_IMPLEMENTATION #define LOGGER_DEFINITION #include diff --git a/src/material.cpp b/src/material.cpp index 34d92c3..84d70be 100644 --- a/src/material.cpp +++ b/src/material.cpp @@ -5,39 +5,39 @@ Material::Material() { } Material::Material(std::string objPath) { - Logger logger; - objl::Loader loader; - bool canLoad = loader.LoadFile(objPath); + // Logger logger; + // objl::Loader loader; + // bool canLoad = loader.LoadFile(objPath); - if (!canLoad) { - logger << LOGGER_ERROR << "Cannot load material '" << objPath << "'" << LOGGER_ENDL; - return; - } + // if (!canLoad) { + // logger << LOGGER_ERROR << "Cannot load material '" << objPath << "'" << LOGGER_ENDL; + // return; + // } - loadFromObj(loader.LoadedMeshes[0]); + // loadFromObj(loader.LoadedMeshes[0]); } -Material::Material(objl::Mesh objMesh) { - loadFromObj(objMesh); -} +// Material::Material(objl::Mesh objMesh) { +// loadFromObj(objMesh); +// } -void Material::loadFromObj(objl::Mesh objMesh) { - loadFromMat(objMesh.MeshMaterial); -} +// void Material::loadFromObj(objl::Mesh objMesh) { +// loadFromMat(objMesh.MeshMaterial); +// } -void Material::loadFromMat(objl::Material mat) { - this->name = mat.name; - OBJLVec3toGLM(mat.Ka, this->Ka); - OBJLVec3toGLM(mat.Kd, this->Kd); - OBJLVec3toGLM(mat.Ks, this->Ks); - this->Ns = mat.Ns; - this->Ni = mat.Ni; - this->d = mat.d; - this->illum = mat.illum; - this->map_Ka = mat.map_Ka; - this->map_Kd = mat.map_Kd; - this->map_Ks = mat.map_Ks; - this->map_Ns = mat.map_Ns; - this->map_d = mat.map_d; - this->map_bump = mat.map_bump; -} +// void Material::loadFromMat(objl::Material mat) { +// this->name = mat.name; +// OBJLVec3toGLM(mat.Ka, this->Ka); +// OBJLVec3toGLM(mat.Kd, this->Kd); +// OBJLVec3toGLM(mat.Ks, this->Ks); +// this->Ns = mat.Ns; +// this->Ni = mat.Ni; +// this->d = mat.d; +// this->illum = mat.illum; +// this->map_Ka = mat.map_Ka; +// this->map_Kd = mat.map_Kd; +// this->map_Ks = mat.map_Ks; +// this->map_Ns = mat.map_Ns; +// this->map_d = mat.map_d; +// this->map_bump = mat.map_bump; +// } diff --git a/src/material.h b/src/material.h index 35bff28..d56541f 100644 --- a/src/material.h +++ b/src/material.h @@ -10,8 +10,6 @@ #include #include -#include - #include #include "./util/util.h" @@ -19,10 +17,10 @@ class Material { public: Material(); Material(std::string objPath); - Material(objl::Mesh objMesh); + // Material(objl::Mesh objMesh); - void loadFromObj(objl::Mesh objMesh); - void loadFromMat(objl::Material mat); + // void loadFromObj(objl::Mesh objMesh); + // void loadFromMat(objl::Material mat); // Material Name std::string name; diff --git a/src/mesh.cpp b/src/mesh.cpp index a793f97..49f9fb5 100644 --- a/src/mesh.cpp +++ b/src/mesh.cpp @@ -13,12 +13,12 @@ Mesh::Mesh(std::string objPath) { bool canLoad = tinyobj::LoadObj(&mesh.attrib, &mesh.shapes, &mesh.materials, &warn, &err, objPath.c_str()); if (!err.empty() || !canLoad) { - logger << LOGGER_ERROR << "Cannot load obj '" << objPath << "' :" << err << LOGGER_ENDL; + logger << LOGGER_ERROR << "Cannot load obj '" << objPath << "': " << err << LOGGER_ENDL; return; } if (!warn.empty()) { - logger << LOGGER_WARN << "Warning from obj loader while loading obj '" << objPath << "' :" << warn << LOGGER_ENDL; + logger << LOGGER_WARN << "Warning from obj loader while loading obj '" << objPath << "': " << warn << LOGGER_ENDL; } logger << LOGGER_INFO << "Loaded: " << objPath << LOGGER_ENDL; @@ -31,8 +31,8 @@ Mesh::Mesh(ObjLMesh mesh) { } void Mesh::loadFromObj(ObjLMesh mesh) { - OBJLtoGLM(mesh, vertices, normals, texCoords); - OBJLtoIndices(mesh, indices); + OBJLtoGLM(mesh, vertices, normals, texCoords, indices); + // OBJLtoIndices(mesh, indices); //Logger logger; //for (int i = 0; i < 100; i++) { @@ -78,7 +78,7 @@ void Mesh::setup() { glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 0, (const void*)((vertices.size() + texCoords.size()) * sizeof(glm::vec3))); - logger << LOGGER_INFO << "Mesh " << name << " setup" << LOGGER_ENDL; + logger << LOGGER_INFO << "Mesh setup" << LOGGER_ENDL; glBindVertexArray(0); } diff --git a/src/mesh.h b/src/mesh.h index bf6150b..5a7e3a5 100644 --- a/src/mesh.h +++ b/src/mesh.h @@ -26,7 +26,6 @@ struct Vertex { glm::vec2 texCoord; }; - // For easy passing around of the tinyobj // mesh structures struct ObjLMesh { diff --git a/src/model.h b/src/model.h index 5e7bc39..de4e748 100644 --- a/src/model.h +++ b/src/model.h @@ -10,8 +10,6 @@ #include #include -#include - #include #include "mesh.h" diff --git a/src/tiny_obj_loader.cpp b/src/tiny_obj_loader.cpp new file mode 100644 index 0000000..48da3da --- /dev/null +++ b/src/tiny_obj_loader.cpp @@ -0,0 +1,2 @@ +#define TINYOBJLOADER_IMPLEMENTATION // define this in only *one* .cc +#include "tiny_obj_loader.h" diff --git a/src/util/util.cpp b/src/util/util.cpp index 0d72e4d..b8755ba 100644 --- a/src/util/util.cpp +++ b/src/util/util.cpp @@ -1,36 +1,67 @@ #include "util.h" +#include + #include +#include "../mesh.h" + + void OBJLtoGLM(ObjLMesh& mesh, std::vector& outVert, std::vector& outNorm, std::vector& outTexCoord, std::vector& outIndices) { + std::unordered_map uniqueVertices {{{1.0f, 1.0f, 1.0f}, 1}}; + for (const auto &shape : mesh.shapes) { for (const auto& index : shape.mesh.indices) { - outVert.push_back({ + glm::vec3 vertex { mesh.attrib.vertices[3 * index.vertex_index + 0], mesh.attrib.vertices[3 * index.vertex_index + 1], mesh.attrib.vertices[3 * index.vertex_index + 2] - }); + }; - outNorm.push_back({ - mesh.attrib.normals[0], - mesh.attrib.normals[0], - mesh.attrib.normals[0] - }); + if (uniqueVertices.count(vertex) == 0) { + uniqueVertices[vertex] = static_cast(outVert.size()); + outVert.push_back(vertex); + } - outTexCoord.push_back({ - mesh.attrib.texcoords[2 * index.texcoord_index + 0], - mesh.attrib.texcoords[2 * index.texcoord_index + 1], - 0.0f - }); + // outNorm.push_back({ + // mesh.attrib.normals[3 * index.normal_index + 0], + // mesh.attrib.normals[3 * index.normal_index + 1], + // mesh.attrib.normals[3 * index.normal_index + 2] + // }); + + // outTexCoord.push_back({ + // mesh.attrib.texcoords[2 * index.texcoord_index + 0], + // mesh.attrib.texcoords[2 * index.texcoord_index + 1], + // 0.0f + // }); + + outIndices.push_back(index.vertex_index); } } + ComputeNormals(outNorm, outVert, outIndices); +} + +void ComputeNormals(std::vector& normals, + std::vector& vertices, + std::vector& elements) { + + normals.resize(vertices.size(), glm::vec3(0.0, 0.0, 0.0)); + for (int i = 0; i < elements.size(); i += 3) { + GLushort ia = elements[i]; + GLushort ib = elements[i+1]; + GLushort ic = elements[i+2]; + glm::vec3 normal = glm::normalize(glm::cross( + glm::vec3(vertices[ib]) - glm::vec3(vertices[ia]), + glm::vec3(vertices[ic]) - glm::vec3(vertices[ia]))); + normals[ia] = normals[ib] = normals[ic] = normal; + } } void UintToGLuint(ObjLMesh& mesh, diff --git a/src/util/util.h b/src/util/util.h index b7bf0f7..036894a 100644 --- a/src/util/util.h +++ b/src/util/util.h @@ -2,16 +2,19 @@ #define SMHENGINE_SRC_UTIL_UTUL_H_ #include +// This is so that i can use +// glm::vec3 inside of an +// std::unordered_map as it's +// experimental for some reason +// smh +#define GLM_ENABLE_EXPERIMENTAL +#include #include #include #include -struct ObjLMesh { - tinyobj::attrib_t attrib; - std::vector shapes; - std::vector materials; -}; +struct ObjLMesh; void OBJLtoGLM(ObjLMesh& mesh, std::vector& outVert, @@ -19,6 +22,10 @@ void OBJLtoGLM(ObjLMesh& mesh, std::vector& outTexCoord, std::vector& outIndices); +void ComputeNormals(std::vector& normals, + std::vector& vertices, + std::vector& elements); + void UintToGLuint(ObjLMesh& mesh, std::vector& outIndices);