From 14b0b9a155d9108cb61bbb6031e80f820bc6c1a2 Mon Sep 17 00:00:00 2001 From: Benjamin Kyd Date: Sun, 24 Feb 2019 18:51:11 +0000 Subject: [PATCH] i think i broke it further --- include/object.h | 33 ++++++++++++++++ legacy/resources/shaders/phong.vert | 2 +- src/mesh.cpp | 34 +++++++++-------- src/mesh.h | 3 +- src/object.cpp | 58 +++++++++++++++++++++++++++++ src/util/util.cpp | 2 +- src/util/util.h | 2 +- 7 files changed, 114 insertions(+), 20 deletions(-) create mode 100644 include/object.h create mode 100644 src/object.cpp diff --git a/include/object.h b/include/object.h new file mode 100644 index 0000000..8ddc618 --- /dev/null +++ b/include/object.h @@ -0,0 +1,33 @@ +#ifndef SRC_OBJECT_H_ +#define SRC_OBJECT_H_ + +// General includes +#include + +// GL includes +#include + +#if _WIN32 +#include +#else +#include +#endif + +#include +#include +#include + +// Custom includes +#include + +void LoadOBJ(Logger& logger, + std::string file, + std::vector& vertices, + std::vector& normals, + std::vector& elements); + +void FlatShade(std::vector& vertices, + std::vector& normals, + std::vector& elements); + +#endif diff --git a/legacy/resources/shaders/phong.vert b/legacy/resources/shaders/phong.vert index ac66227..80c8a96 100644 --- a/legacy/resources/shaders/phong.vert +++ b/legacy/resources/shaders/phong.vert @@ -2,7 +2,7 @@ layout(location = 0) in vec3 position; layout(location = 1) in vec3 normal; -layout(location = 2) in vec3 texCoord; +// layout(location = 2) in vec3 texCoord; out vec3 Normal; out vec3 FragPos; diff --git a/src/mesh.cpp b/src/mesh.cpp index f0d1f6f..1cd270a 100644 --- a/src/mesh.cpp +++ b/src/mesh.cpp @@ -6,17 +6,19 @@ Mesh::Mesh() { Mesh::Mesh(std::string objPath) { Logger logger; - objl::Loader loader; - bool canLoad = loader.LoadFile(objPath); + //objl::Loader loader; + //bool canLoad = loader.LoadFile(objPath); - if (!canLoad) { - logger << LOGGER_ERROR << "Cannot load obj '" << objPath << "'" << LOGGER_ENDL; - return; - } + //if (!canLoad) { + // logger << LOGGER_ERROR << "Cannot load obj '" << objPath << "'" << LOGGER_ENDL; + // return; + //} + + LoadOBJ(logger, objPath, vertices, normals, indices); logger << LOGGER_INFO << "Loaded: " << objPath << LOGGER_ENDL; - loadFromObj(loader.LoadedMeshes[0]); + //loadFromObj(loader.LoadedMeshes[0]); } Mesh::Mesh(objl::Mesh objMesh) { @@ -28,10 +30,10 @@ void Mesh::loadFromObj(objl::Mesh objMesh) { UintToGLuint(objMesh.Indices, indices); name = objMesh.MeshName; - Logger logger; - for (int i = 0; i < 100; i++) { - logger << LOGGER_DEBUG << normals[i].x << " " << normals[i].y << " " << normals[i].z << LOGGER_ENDL; - } + //Logger logger; + //for (int i = 0; i < 100; i++) { + // logger << LOGGER_DEBUG << normals[i].x << " " << normals[i].y << " " << normals[i].z << LOGGER_ENDL; + //} } void Mesh::setup() { @@ -48,13 +50,13 @@ void Mesh::setup() { std::vector toGpu; toGpu.insert(toGpu.end(), vertices.begin(), vertices.end()); toGpu.insert(toGpu.end(), normals.begin(), normals.end()); - toGpu.insert(toGpu.end(), texCoords.begin(), texCoords.end()); + //toGpu.insert(toGpu.end(), texCoords.begin(), texCoords.end()); glBufferData(GL_ARRAY_BUFFER, toGpu.size() * sizeof(glm::vec3), &toGpu[0], GL_STATIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(GLuint), + glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(GLushort), &indices[0], GL_STATIC_DRAW); // Positions @@ -68,9 +70,9 @@ void Mesh::setup() { (const void*)(vertices.size() * sizeof(glm::vec3))); // TexCoords - glEnableVertexAttribArray(2); - glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 0, - (const void*)((vertices.size() + texCoords.size()) * sizeof(glm::vec3))); + //glEnableVertexAttribArray(2); + //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; diff --git a/src/mesh.h b/src/mesh.h index 4aa24b2..d1de707 100644 --- a/src/mesh.h +++ b/src/mesh.h @@ -11,6 +11,7 @@ #include #include +#include #include #include "shader.h" @@ -37,7 +38,7 @@ public: // This is a vec3 so it can all pop into //one buffer :) std::vector texCoords; - std::vector indices; + std::vector indices; private: GLuint vertexBuffer; GLuint indexBuffer; diff --git a/src/object.cpp b/src/object.cpp new file mode 100644 index 0000000..3058aa6 --- /dev/null +++ b/src/object.cpp @@ -0,0 +1,58 @@ +#include + +#include + +// https://en.wikibooks.org/wiki/OpenGL_Programming/Modern_OpenGL_Tutorial_Load_OBJ +void LoadOBJ(Logger& logger, std::string file, std::vector& vertices, std::vector& normals, std::vector& elements) { + std::ifstream in(file, std::ios::in); + if (!in) { + logger << LOGGER_ERROR << "Cannot open " << file << LOGGER_ENDL; + return; + } + + std::string line; + while (getline(in, line)) { + if (line.substr(0,2) == "v ") { + std::istringstream s(line.substr(2)); + glm::vec3 v; s >> v.x; s >> v.y; s >> v.z; + vertices.push_back(v); + } else if (line.substr(0,2) == "f ") { + std::istringstream s(line.substr(2)); + GLushort a,b,c; + s >> a; s >> b; s >> c; + a--; b--; c--; + elements.push_back(a); elements.push_back(b); elements.push_back(c); + } else if (line[0] == '#') { } + else {} + } + + 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; + } + + logger << LOGGER_INFO << "Loaded OBJ: " << file << LOGGER_ENDL; +} + +void FlatShade(std::vector& vertices, std::vector& normals, std::vector& elements) { + std::vector shared_vertices; + for (int i = 0; i < elements.size(); i++) { + vertices.push_back(shared_vertices[elements[i]]); + if ((i % 3) == 2) { + GLushort ia = elements[i-2]; + GLushort ib = elements[i-1]; + GLushort ic = elements[i]; + glm::vec3 normal = glm::normalize(glm::cross( + shared_vertices[ic] - shared_vertices[ia], + shared_vertices[ib] - shared_vertices[ia])); + for (int n = 0; n < 3; n++) + normals.push_back(normal); + } + } +} diff --git a/src/util/util.cpp b/src/util/util.cpp index b003f62..b4ef812 100644 --- a/src/util/util.cpp +++ b/src/util/util.cpp @@ -23,7 +23,7 @@ void OBJLtoGLM(std::vector& inVertArr, } void UintToGLuint(std::vector& inIndices, - std::vector& outIndices) { + 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 cf9f3cd..00c1aba 100644 --- a/src/util/util.h +++ b/src/util/util.h @@ -12,7 +12,7 @@ void OBJLtoGLM(std::vector& inVertArr, std::vector& outTexCoord); void UintToGLuint(std::vector& inIndices, - std::vector& outIndices); + std::vector& outIndices); void OBJLVec3toGLM(objl::Vector3& inVec, glm::vec3& outVec);