less broken
This commit is contained in:
@@ -20,6 +20,21 @@
|
||||
"cmakeCommandArgs": "",
|
||||
"buildCommandArgs": "-v",
|
||||
"ctestCommandArgs": ""
|
||||
},
|
||||
{
|
||||
"name": "x64-Build",
|
||||
"generator": "Ninja",
|
||||
"configurationType": "Release",
|
||||
"inheritEnvironments": [
|
||||
"msvc_x64_x64"
|
||||
],
|
||||
"buildRoot": "E:\\Projects\\SMH-Engine\\build",
|
||||
// "buildRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}",
|
||||
"installRoot": "E:\\Projects\\SMH-Engine\\build",
|
||||
// "installRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}",
|
||||
"cmakeCommandArgs": "",
|
||||
"buildCommandArgs": "-v",
|
||||
"ctestCommandArgs": ""
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
#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
|
||||
28
src/mesh.cpp
28
src/mesh.cpp
@@ -6,19 +6,17 @@ 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;
|
||||
//}
|
||||
|
||||
LoadOBJ(logger, objPath, vertices, normals, indices);
|
||||
if (!canLoad) {
|
||||
logger << LOGGER_ERROR << "Cannot load obj '" << objPath << "'" << LOGGER_ENDL;
|
||||
return;
|
||||
}
|
||||
|
||||
logger << LOGGER_INFO << "Loaded: " << objPath << LOGGER_ENDL;
|
||||
|
||||
//loadFromObj(loader.LoadedMeshes[0]);
|
||||
loadFromObj(loader.LoadedMeshes[0]);
|
||||
}
|
||||
|
||||
Mesh::Mesh(objl::Mesh objMesh) {
|
||||
@@ -27,7 +25,7 @@ Mesh::Mesh(objl::Mesh objMesh) {
|
||||
|
||||
void Mesh::loadFromObj(objl::Mesh objMesh) {
|
||||
OBJLtoGLM(objMesh.Vertices, vertices, normals, texCoords);
|
||||
UintToGLuint(objMesh.Indices, indices);
|
||||
indices = objMesh.Indices;
|
||||
name = objMesh.MeshName;
|
||||
|
||||
//Logger logger;
|
||||
@@ -50,13 +48,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(GLushort),
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(GLuint),
|
||||
&indices[0], GL_STATIC_DRAW);
|
||||
|
||||
// Positions
|
||||
@@ -70,9 +68,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
src/mesh.h
11
src/mesh.h
@@ -11,12 +11,19 @@
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
|
||||
#include <OBJLoader.h>
|
||||
#include <object.h>
|
||||
// #include <objectLoader.h>
|
||||
|
||||
#include <logger.h>
|
||||
#include "shader.h"
|
||||
#include "./util/util.h"
|
||||
|
||||
// Not in use yet
|
||||
struct Vertex {
|
||||
glm::vec3 vertice;
|
||||
glm::vec3 normal;
|
||||
glm::vec2 texCoord;
|
||||
};
|
||||
|
||||
class Mesh {
|
||||
public:
|
||||
Mesh();
|
||||
@@ -38,7 +45,7 @@ public:
|
||||
// This is a vec3 so it can all pop into
|
||||
//one buffer :)
|
||||
std::vector<glm::vec3> texCoords;
|
||||
std::vector<GLushort> indices;
|
||||
std::vector<unsigned int> indices;
|
||||
private:
|
||||
GLuint vertexBuffer;
|
||||
GLuint indexBuffer;
|
||||
|
||||
@@ -1,58 +0,0 @@
|
||||
#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<GLushort>& outIndices) {
|
||||
std::vector<GLuint>& 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<GLushort>& outIndices);
|
||||
std::vector<GLuint>& outIndices);
|
||||
|
||||
void OBJLVec3toGLM(objl::Vector3& inVec, glm::vec3& outVec);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user