Proper obj loading kinda
This commit is contained in:
@@ -83,6 +83,7 @@ Display::Display(std::string name, Logger& logger, int w, int h,
|
||||
// Load OpenGL
|
||||
gladLoadGLLoader(SDL_GL_GetProcAddress);
|
||||
glEnable(GL_MULTISAMPLE);
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
logger << LOGGER_INFO << "Loaded OpenGL" << LOGGER_ENDL;
|
||||
logger << LOGGER_ENDL;
|
||||
isClosed = false;
|
||||
@@ -175,6 +176,7 @@ Display::Display(std::string name, Logger& logger, int w, int h,
|
||||
// Load OpenGL
|
||||
gladLoadGLLoader(SDL_GL_GetProcAddress);
|
||||
glEnable(GL_MULTISAMPLE);
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
logger << LOGGER_INFO << "Loaded OpenGL" << LOGGER_ENDL;
|
||||
logger << LOGGER_ENDL;
|
||||
isClosed = false;
|
||||
|
||||
15
src/main.cpp
15
src/main.cpp
@@ -29,14 +29,25 @@ int main (int argc, char** argv) {
|
||||
Shader shader;
|
||||
shader.load("./resources/shaders/phong").attatch().link().use();
|
||||
|
||||
Mesh mesh{ "./resources/dragon.obj" };
|
||||
Mesh mesh{ "./resources/test1.obj" };
|
||||
mesh.setup();
|
||||
|
||||
SDL_Event e;
|
||||
while (!display.isClosed) {
|
||||
while (SDL_PollEvent(&e))
|
||||
while (SDL_PollEvent(&e)) {
|
||||
if (e.type == SDL_QUIT || e.key.keysym.sym == SDLK_ESCAPE)
|
||||
display.isClosed = true;
|
||||
}
|
||||
|
||||
const Uint8* state = SDL_GetKeyboardState(NULL);
|
||||
|
||||
if (state[SDL_SCANCODE_Q]) {
|
||||
mesh.rotation += -1.5f;
|
||||
}
|
||||
|
||||
if (state[SDL_SCANCODE_E]) {
|
||||
mesh.rotation += 1.5f;
|
||||
}
|
||||
|
||||
mesh.bind();
|
||||
mesh.render(shader);
|
||||
|
||||
@@ -92,7 +92,7 @@ void Mesh::render(Shader& shader) {
|
||||
// Model matrice
|
||||
glm::mat4 model = glm::mat4(1.0f);
|
||||
model = glm::translate(model, { -17.0f, -17.0f, -17.0f });
|
||||
model = glm::rotate(model, glm::radians(-160.0f), glm::vec3(0.0f, 1.0f, 0.0f));
|
||||
model = glm::rotate(model, glm::radians(-160.0f + this->rotation), glm::vec3(0.0f, 1.0f, 0.0f));
|
||||
// Gets uniform for model matrice, to be used later
|
||||
GLint uniTrans = glGetUniformLocation(shader.getProgram(), "model");
|
||||
glUniformMatrix4fv(uniTrans, 1, GL_FALSE, glm::value_ptr(model));
|
||||
|
||||
@@ -56,6 +56,8 @@ public:
|
||||
//one buffer :)
|
||||
std::vector<glm::vec3> texCoords;
|
||||
std::vector<unsigned int> indices;
|
||||
|
||||
float rotation = 0.0f;
|
||||
private:
|
||||
GLuint vertexBuffer;
|
||||
GLuint indexBuffer;
|
||||
|
||||
@@ -17,23 +17,18 @@ void OBJLtoGLM(ObjLMesh& mesh,
|
||||
|
||||
for (const auto &shape : mesh.shapes) {
|
||||
for (const auto& index : shape.mesh.indices) {
|
||||
|
||||
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]
|
||||
};
|
||||
outVert.push_back(vertex);
|
||||
|
||||
if (uniqueVertices.count(vertex) == 0) {
|
||||
uniqueVertices[vertex] = static_cast<uint32_t>(outVert.size());
|
||||
outVert.push_back(vertex);
|
||||
}
|
||||
|
||||
// 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]
|
||||
// });
|
||||
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],
|
||||
@@ -41,11 +36,15 @@ void OBJLtoGLM(ObjLMesh& mesh,
|
||||
// 0.0f
|
||||
// });
|
||||
|
||||
outIndices.push_back(index.vertex_index);
|
||||
// if (uniqueVertices.count(vertex) == 0) {
|
||||
// uniqueVertices[vertex] = static_cast<uint32_t>(outVert.size());
|
||||
// }
|
||||
|
||||
outIndices.push_back(outIndices.size());
|
||||
}
|
||||
}
|
||||
|
||||
ComputeNormals(outNorm, outVert, outIndices);
|
||||
// ComputeNormals(outNorm, outVert, outIndices);
|
||||
}
|
||||
|
||||
void ComputeNormals(std::vector<glm::vec3>& normals,
|
||||
|
||||
Reference in New Issue
Block a user