diff --git a/examples/dev_testing/src/main.cpp b/examples/dev_testing/src/main.cpp index 0ad740c..27415fa 100644 --- a/examples/dev_testing/src/main.cpp +++ b/examples/dev_testing/src/main.cpp @@ -49,7 +49,6 @@ int main(int argc, char *argv[]) { auto stall = std::make_shared(game.get_window(), simpleengine::gfx::Shader(base_shader_program), stall_texture, "resources/stall.obj"); - stall->set_texture(stall_texture); stall->translate(0.f, -4.f, -18.f); game.add_event(stall); diff --git a/include/simpleengine/gfx/textured_model.h b/include/simpleengine/gfx/textured_model.h index b369c8d..935e568 100644 --- a/include/simpleengine/gfx/textured_model.h +++ b/include/simpleengine/gfx/textured_model.h @@ -20,7 +20,8 @@ namespace simpleengine::gfx { public: gfx::Texture texture; - TexturedModel(GLFWwindow* window, gfx::Shader shader, gfx::Texture texture, std::vector vertices, std::vector indicies); + TexturedModel(GLFWwindow* window, gfx::Shader shader, gfx::Texture texture, std::vector vertices, + std::vector indicies = std::vector()); TexturedModel(GLFWwindow* window, std::shared_ptr shader_program, gfx::Texture texture, std::vector vertices, std::vector indicies = std::vector()); diff --git a/include/simpleengine/objects/3d/obj_model.h b/include/simpleengine/objects/3d/obj_model.h index 6c7016b..84f4164 100644 --- a/include/simpleengine/objects/3d/obj_model.h +++ b/include/simpleengine/objects/3d/obj_model.h @@ -19,133 +19,14 @@ namespace simpleengine::objects_3d { class ObjModel : public simpleengine::gfx::TexturedModel { private: - std::vector split_string(std::string str, const char delim) { - std::istringstream ss(str); - - std::vector tokens; - size_t pos = 0; - std::string token; - while ((pos = str.find(delim)) != std::string::npos) { - token = str.substr(0, pos); - tokens.push_back(token); - str.erase(0, pos + 1); - } - tokens.push_back(str); - - return tokens; - } - + 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) { - - int currentVertexIndex = stoi(vertex_data[0]) - 1; - indicies.push_back(currentVertexIndex); - - // Read texture coords - glm::vec2 current_tex = in_textures.at(stoi(vertex_data[1]) - 1); - current_tex.y = 1 - current_tex.y; - out_textures.at(currentVertexIndex) = current_tex; - - // Read normals - glm::vec3 current_norm = in_normals.at(stoi(vertex_data[2]) - 1); - out_normals.at(currentVertexIndex) = current_norm; - } - - //nonstd::optional texture; + const std::vector& in_normals, std::vector& out_textures, std::vector& out_normals); public: - /* std::vector model_vertices; - std::vector indicies; - gfx::VBO ebo; - gfx::VBO vbo; - gfx::VAO vao; - gfx::Shader shader; */ + ObjModel(GLFWwindow *window, gfx::Shader shader, gfx::Texture texture, std::string filename); + ObjModel(GLFWwindow *window, gfx::Shader shader, gfx::Texture texture, std::ifstream file_stream); - ObjModel(GLFWwindow *window, gfx::Shader shader, gfx::Texture texture, std::string filename) : - ObjModel(window, shader, texture, std::ifstream(filename, std::ios::in | std::ios::binary)) { - - } - - ObjModel(GLFWwindow *window, gfx::Shader shader, gfx::Texture texture, std::ifstream file_stream) : - simpleengine::gfx::TexturedModel(window, shader, texture, std::vector()) { - - if (!file_stream.is_open()) { - std::cerr << "File stream that was given to ObjModel::ObjModel is not open!" << std::endl; - throw std::runtime_error("Failed to open ObjModel model file"); - } - - std::vector obj_vertices; - std::vector obj_textures; - std::vector obj_normals; - - std::vector textures; - std::vector normals; - - std::string line; - while (std::getline(file_stream, line)) { - std::vector line_tokens = split_string(line, ' '); - - if (line_tokens.front() == "v") { - //glm::vec3 vertex(stof(line_tokens[1]), stof(line_tokens[2]), stof(line_tokens[3])); - obj_vertices.emplace_back(stof(line_tokens[1]), stof(line_tokens[2]), stof(line_tokens[3])); - } else if (line_tokens.front() == "vt") { - obj_textures.emplace_back(stof(line_tokens[1]), stof(line_tokens[2])); - } else if (line_tokens.front() == "vn") { - obj_normals.emplace_back(stof(line_tokens[1]), stof(line_tokens[2]), stof(line_tokens[3])); - } else if (line_tokens.front() == "f") { - auto size = obj_vertices.size(); - textures.resize(size); - normals.resize(size); - - std::cout << "Textures should be size of " << size << " but is a size of " << textures.size() << std::endl; - std::cout << "Normals should be size of " << size << " but is a size of " << normals.size() << std::endl; - break; - } - } - - do { - if (!line.starts_with("f")) { - continue; - } - std::vector line_tokens = split_string(line, ' '); - std::vector vertex1 = split_string(line_tokens[1], '/'); - 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); - } while (std::getline(file_stream, line)); - - file_stream.close(); - - for (int i = 0; i < obj_vertices.size(); i++) { - vertices.emplace_back(simpleengine::Vectorf(obj_vertices.at(i)), glm::vec3(1.f), textures.at(i)); - } - - vao.bind(); - vbo.buffer(vertices.data(), 0, sizeof(Vertex) * vertices.size()); - ebo.buffer(indicies.data(), 0, indicies.size() * sizeof(GLuint)); - - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, position)); - glEnableVertexAttribArray(0); - - glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, color)); - glEnableVertexAttribArray(1); - - glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, tex_coord)); - glEnableVertexAttribArray(2); - - glBindBuffer(GL_ARRAY_BUFFER, 0); - glBindVertexArray(0); - } - - void set_texture(gfx::Texture texture) { - this->texture = texture; - } - - virtual void update(const float& delta_time) override { - this->rotate_y(0.5f); - } + virtual void update(const float& delta_time) override; /* virtual void render(GLFWwindow* target) override { shader.use(); diff --git a/src/objects/3d/obj_model.cpp b/src/objects/3d/obj_model.cpp new file mode 100644 index 0000000..0d7946f --- /dev/null +++ b/src/objects/3d/obj_model.cpp @@ -0,0 +1,118 @@ +#include "objects/3d/obj_model.h"" + +namespace simpleengine::objects_3d { + std::vector ObjModel::split_string(std::string str, const char delim) { + std::istringstream ss(str); + + std::vector tokens; + size_t pos = 0; + std::string token; + while ((pos = str.find(delim)) != std::string::npos) { + token = str.substr(0, pos); + tokens.push_back(token); + str.erase(0, pos + 1); + } + tokens.push_back(str); + + 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) { + + int currentVertexIndex = stoi(vertex_data[0]) - 1; + indicies.push_back(currentVertexIndex); + + // Read texture coords + glm::vec2 current_tex = in_textures.at(stoi(vertex_data[1]) - 1); + current_tex.y = 1 - current_tex.y; + out_textures.at(currentVertexIndex) = current_tex; + + // Read normals + glm::vec3 current_norm = in_normals.at(stoi(vertex_data[2]) - 1); + out_normals.at(currentVertexIndex) = current_norm; + } + + ObjModel::ObjModel(GLFWwindow *window, gfx::Shader shader, gfx::Texture texture, std::string filename) : + ObjModel(window, shader, texture, std::ifstream(filename, std::ios::in | std::ios::binary)) { + + } + + ObjModel::ObjModel(GLFWwindow *window, gfx::Shader shader, gfx::Texture texture, std::ifstream file_stream) : + simpleengine::gfx::TexturedModel(window, shader, texture, std::vector()) { + + if (!file_stream.is_open()) { + std::cerr << "File stream that was given to ObjModel::ObjModel is not open!" << std::endl; + throw std::runtime_error("Failed to open ObjModel model file"); + } + + std::vector obj_vertices; + std::vector obj_textures; + std::vector obj_normals; + + std::vector textures; + std::vector normals; + + std::string line; + while (std::getline(file_stream, line)) { + std::vector line_tokens = split_string(line, ' '); + + if (line_tokens.front() == "v") { + //glm::vec3 vertex(stof(line_tokens[1]), stof(line_tokens[2]), stof(line_tokens[3])); + obj_vertices.emplace_back(stof(line_tokens[1]), stof(line_tokens[2]), stof(line_tokens[3])); + } else if (line_tokens.front() == "vt") { + obj_textures.emplace_back(stof(line_tokens[1]), stof(line_tokens[2])); + } else if (line_tokens.front() == "vn") { + obj_normals.emplace_back(stof(line_tokens[1]), stof(line_tokens[2]), stof(line_tokens[3])); + } else if (line_tokens.front() == "f") { + auto size = obj_vertices.size(); + textures.resize(size); + normals.resize(size); + + std::cout << "Textures should be size of " << size << " but is a size of " << textures.size() << std::endl; + std::cout << "Normals should be size of " << size << " but is a size of " << normals.size() << std::endl; + break; + } + } + + do { + if (!line.starts_with("f")) { + continue; + } + std::vector line_tokens = split_string(line, ' '); + std::vector vertex1 = split_string(line_tokens[1], '/'); + 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); + } while (std::getline(file_stream, line)); + + file_stream.close(); + + for (int i = 0; i < obj_vertices.size(); i++) { + vertices.emplace_back(simpleengine::Vectorf(obj_vertices.at(i)), glm::vec3(1.f), textures.at(i)); + } + + vao.bind(); + vbo.buffer(vertices.data(), 0, sizeof(Vertex) * vertices.size()); + ebo.buffer(indicies.data(), 0, indicies.size() * sizeof(GLuint)); + + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, position)); + glEnableVertexAttribArray(0); + + glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, color)); + glEnableVertexAttribArray(1); + + glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, tex_coord)); + glEnableVertexAttribArray(2); + + glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindVertexArray(0); + } + + void ObjModel::update(const float& delta_time) { + this->rotate_y(0.5f); + } +} \ No newline at end of file