From ed08a78fb62ea67911f62345198956234af7d826 Mon Sep 17 00:00:00 2001 From: SeanOMik Date: Wed, 8 Dec 2021 23:31:46 -0500 Subject: [PATCH] Add some comments to ObjModel. --- include/simpleengine/objects/3d/obj_model.h | 39 +++++++++++---------- src/objects/3d/obj_model.cpp | 23 ++++++++---- 2 files changed, 36 insertions(+), 26 deletions(-) diff --git a/include/simpleengine/objects/3d/obj_model.h b/include/simpleengine/objects/3d/obj_model.h index 1e935dd..5dd4664 100644 --- a/include/simpleengine/objects/3d/obj_model.h +++ b/include/simpleengine/objects/3d/obj_model.h @@ -19,9 +19,27 @@ namespace simpleengine::objects_3d { class ObjModel : public simpleengine::gfx::TexturedModel { private: + /** + * @brief Split a string multiple times (if possible) with a character delimiter. + * + * @param str The string to split. + * @param delim The character to split by. + * @return std::vector The tokens that were split out of str. + */ std::vector split_string(std::string str, const char delim); - static void process_vertex(const std::vector& vertex_data, std::vector& indicies, const std::vector& in_textures, - const std::vector& in_normals, std::vector& out_textures, std::vector& out_normals); + + /** + * @brief Process a vertex from tokens read from the .obj file. + * + * @param vertex_data The vertex string tokens to process the vertex from. + * @param in_textures The texture coords that are unsorted, just read from the .obj file. + * @param in_normals The normals that are unsorted, just read from the .obj file. + * @param out_indicies (out) The vector to insert the indicies that were extracted from `vertex_data` into. + * @param out_textures (out) The vector to insert the texture coords that were extracted from `vertex_data` into. + * @param out_normals (out) The vector to insert the normals that were extracted from `vertex_data` into. + */ + static void process_vertex(const std::vector& vertex_data, const std::vector& in_textures, + const std::vector& in_normals, std::vector& out_indicies, std::vector& out_textures, std::vector& out_normals); private: /** * @brief This is replaced with `lit_vertices`!!!! @@ -35,22 +53,5 @@ namespace simpleengine::objects_3d { ObjModel(GLFWwindow *window, gfx::Shader shader, gfx::Texture texture, std::ifstream file_stream); virtual void update(const float& delta_time) override; - - /* virtual void render(GLFWwindow* target) override { - shader.use(); - - shader.set_uniform_matrix_4f("transform_matrix", transform_matrix, false); - - // When binding to the texture, also tell the shader if the texture is set or not. - if (texture.has_value()) { - shader.set_uniform_int("texture_is_set", true, false); - texture.value().bind(); - } else { - shader.set_uniform_int("texture_is_set", false, false); - } - - vao.bind(); - glDrawElements(GL_TRIANGLES, indicies.size(), GL_UNSIGNED_INT, 0); - } */ }; } \ No newline at end of file diff --git a/src/objects/3d/obj_model.cpp b/src/objects/3d/obj_model.cpp index 39ea425..70f5ad0 100644 --- a/src/objects/3d/obj_model.cpp +++ b/src/objects/3d/obj_model.cpp @@ -17,11 +17,12 @@ namespace simpleengine::objects_3d { return tokens; } - void ObjModel::process_vertex(const std::vector& vertex_data, std::vector& indicies, const std::vector& in_textures, - const std::vector& in_normals, std::vector& out_textures, std::vector& out_normals) { + void ObjModel::process_vertex(const std::vector& vertex_data, const std::vector& in_textures, + const std::vector& in_normals, std::vector& out_indicies, std::vector& out_textures, std::vector& out_normals) { + // Get the index the current vertex and put it in indicies int currentVertexIndex = stoi(vertex_data[0]) - 1; - indicies.push_back(currentVertexIndex); + out_indicies.push_back(currentVertexIndex); // Read texture coords glm::vec2 current_tex = in_textures.at(stoi(vertex_data[1]) - 1); @@ -46,13 +47,17 @@ namespace simpleengine::objects_3d { throw std::runtime_error("Failed to open ObjModel model file"); } + // The vertices, texture coords, and normals that were read from the obj file + // these are not in a particular order. std::vector obj_vertices; std::vector obj_textures; std::vector obj_normals; + // The texture coords and normals that have been sorted. std::vector textures; std::vector normals; + // Read the vertices, texture coords, and normals. Break when run into indices std::string line; while (std::getline(file_stream, line)) { std::vector line_tokens = split_string(line, ' '); @@ -72,6 +77,7 @@ namespace simpleengine::objects_3d { } } + // Process the indicies. This will sort everything for storing inside of the Vertex list. do { if (!line.starts_with("f")) { continue; @@ -81,21 +87,24 @@ namespace simpleengine::objects_3d { std::vector vertex2 = split_string(line_tokens[2], '/'); std::vector vertex3 = split_string(line_tokens[3], '/'); - process_vertex(vertex1, indicies, obj_textures, obj_normals, textures, normals); - process_vertex(vertex2, indicies, obj_textures, obj_normals, textures, normals); - process_vertex(vertex3, indicies, obj_textures, obj_normals, textures, normals); + process_vertex(vertex1, obj_textures, obj_normals, indicies, textures, normals); + process_vertex(vertex2, obj_textures, obj_normals, indicies, textures, normals); + process_vertex(vertex3, obj_textures, obj_normals, indicies, textures, normals); } while (std::getline(file_stream, line)); file_stream.close(); + // Insert everything into lit_vertices. for (int i = 0; i < obj_vertices.size(); i++) { lit_vertices.emplace_back(simpleengine::Vectorf(obj_vertices.at(i)), glm::vec3(1.f), textures.at(i), normals.at(i)); } + // Create VAO and EBO and assign buffers vao.bind(); vbo.buffer(lit_vertices.data(), 0, sizeof(LitVertex) * lit_vertices.size()); ebo.buffer(indicies.data(), 0, indicies.size() * sizeof(GLuint)); + // Enable VAO attributes vao.enable_attrib(vbo, 0, 3, GL_FLOAT, sizeof(LitVertex), offsetof(LitVertex, position)); //vao.enable_attrib(vbo, 1, 3, GL_FLOAT, sizeof(LitVertex), offsetof(LitVertex, color)); vao.enable_attrib(vbo, 1, 2, GL_FLOAT, sizeof(LitVertex), offsetof(LitVertex, tex_coord)); @@ -106,6 +115,6 @@ namespace simpleengine::objects_3d { } void ObjModel::update(const float& delta_time) { - this->rotate_y(0.5f); + this->rotate_y(0.5f); // Slowly rotate (for debugging) } } \ No newline at end of file