Basic model loading with tinyobjloader, still really broken though

This commit is contained in:
Ben
2019-02-28 15:49:47 +00:00
parent b5b443f47a
commit d44c0f0303
10 changed files with 101 additions and 66 deletions

View File

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

View File

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

View File

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

View File

@@ -10,8 +10,6 @@
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <OBJLoader.h>
#include <logger.h>
#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;

View File

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

View File

@@ -26,7 +26,6 @@ struct Vertex {
glm::vec2 texCoord;
};
// For easy passing around of the tinyobj
// mesh structures
struct ObjLMesh {

View File

@@ -10,8 +10,6 @@
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <OBJLoader.h>
#include <logger.h>
#include "mesh.h"

2
src/tiny_obj_loader.cpp Normal file
View File

@@ -0,0 +1,2 @@
#define TINYOBJLOADER_IMPLEMENTATION // define this in only *one* .cc
#include "tiny_obj_loader.h"

View File

@@ -1,36 +1,67 @@
#include "util.h"
#include <unordered_map>
#include <logger.h>
#include "../mesh.h"
void OBJLtoGLM(ObjLMesh& mesh,
std::vector<glm::vec3>& outVert,
std::vector<glm::vec3>& outNorm,
std::vector<glm::vec3>& outTexCoord,
std::vector<GLuint>& outIndices) {
std::unordered_map<glm::vec3, uint64_t> 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<uint32_t>(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<glm::vec3>& normals,
std::vector<glm::vec3>& vertices,
std::vector<GLuint>& 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,

View File

@@ -2,16 +2,19 @@
#define SMHENGINE_SRC_UTIL_UTUL_H_
#include <glad/glad.hpp>
// 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 <glm/gtx/hash.hpp>
#include <glm/glm.hpp>
#include <vector>
#include <tiny_obj_loader.h>
struct ObjLMesh {
tinyobj::attrib_t attrib;
std::vector<tinyobj::shape_t> shapes;
std::vector<tinyobj::material_t> materials;
};
struct ObjLMesh;
void OBJLtoGLM(ObjLMesh& mesh,
std::vector<glm::vec3>& outVert,
@@ -19,6 +22,10 @@ void OBJLtoGLM(ObjLMesh& mesh,
std::vector<glm::vec3>& outTexCoord,
std::vector<GLuint>& outIndices);
void ComputeNormals(std::vector<glm::vec3>& normals,
std::vector<glm::vec3>& vertices,
std::vector<GLuint>& elements);
void UintToGLuint(ObjLMesh& mesh,
std::vector<GLuint>& outIndices);