i think i broke it further
This commit is contained in:
33
include/object.h
Normal file
33
include/object.h
Normal file
@@ -0,0 +1,33 @@
|
||||
#ifndef SRC_OBJECT_H_
|
||||
#define SRC_OBJECT_H_
|
||||
|
||||
// General includes
|
||||
#include <vector>
|
||||
|
||||
// GL includes
|
||||
#include <glad/glad.hpp>
|
||||
|
||||
#if _WIN32
|
||||
#include <SDL.h>
|
||||
#else
|
||||
#include <SDL2/SDL.h>
|
||||
#endif
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
|
||||
// Custom includes
|
||||
#include <logger.h>
|
||||
|
||||
void LoadOBJ(Logger& logger,
|
||||
std::string file,
|
||||
std::vector<glm::vec3>& vertices,
|
||||
std::vector<glm::vec3>& normals,
|
||||
std::vector<GLushort>& elements);
|
||||
|
||||
void FlatShade(std::vector<glm::vec3>& vertices,
|
||||
std::vector<glm::vec3>& normals,
|
||||
std::vector<GLushort>& elements);
|
||||
|
||||
#endif
|
||||
@@ -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;
|
||||
|
||||
34
src/mesh.cpp
34
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<glm::vec3> 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;
|
||||
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
|
||||
#include <OBJLoader.h>
|
||||
#include <object.h>
|
||||
|
||||
#include <logger.h>
|
||||
#include "shader.h"
|
||||
@@ -37,7 +38,7 @@ public:
|
||||
// This is a vec3 so it can all pop into
|
||||
//one buffer :)
|
||||
std::vector<glm::vec3> texCoords;
|
||||
std::vector<GLuint> indices;
|
||||
std::vector<GLushort> indices;
|
||||
private:
|
||||
GLuint vertexBuffer;
|
||||
GLuint indexBuffer;
|
||||
|
||||
58
src/object.cpp
Normal file
58
src/object.cpp
Normal file
@@ -0,0 +1,58 @@
|
||||
#include <object.h>
|
||||
|
||||
#include <fstream>
|
||||
|
||||
// https://en.wikibooks.org/wiki/OpenGL_Programming/Modern_OpenGL_Tutorial_Load_OBJ
|
||||
void LoadOBJ(Logger& logger, std::string file, std::vector<glm::vec3>& vertices, std::vector<glm::vec3>& normals, std::vector<GLushort>& 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<glm::vec3>& vertices, std::vector<glm::vec3>& normals, std::vector<GLushort>& elements) {
|
||||
std::vector<glm::vec3> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -23,7 +23,7 @@ void OBJLtoGLM(std::vector<objl::Vertex>& inVertArr,
|
||||
}
|
||||
|
||||
void UintToGLuint(std::vector<unsigned int>& inIndices,
|
||||
std::vector<GLuint>& outIndices) {
|
||||
std::vector<GLushort>& outIndices) {
|
||||
|
||||
for (int i = 0; i < inIndices.size(); i++) {
|
||||
outIndices.push_back(inIndices[i]);
|
||||
|
||||
@@ -12,7 +12,7 @@ void OBJLtoGLM(std::vector<objl::Vertex>& inVertArr,
|
||||
std::vector<glm::vec3>& outTexCoord);
|
||||
|
||||
void UintToGLuint(std::vector<unsigned int>& inIndices,
|
||||
std::vector<GLuint>& outIndices);
|
||||
std::vector<GLushort>& outIndices);
|
||||
|
||||
void OBJLVec3toGLM(objl::Vector3& inVec, glm::vec3& outVec);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user