diff --git a/examples/dev_testing/src/main.cpp b/examples/dev_testing/src/main.cpp index d63cd65..818d002 100644 --- a/examples/dev_testing/src/main.cpp +++ b/examples/dev_testing/src/main.cpp @@ -1,6 +1,6 @@ #include "simpleengine/camera.h" +#include "simpleengine/gfx/model.h" #include "simpleengine/gfx/texture.h" -#include "simpleengine/objects/2d/shapes/square.h" #include "simpleengine/vector.h" #include #include @@ -8,7 +8,6 @@ #include #include #include -#include #include #include @@ -48,18 +47,38 @@ int main(int argc, char *argv[]) { simpleengine::gfx::Texture stall_texture("resources/stallTexture.png"); - auto stall = std::make_shared(simpleengine::gfx::Shader(base_shader_program), "resources/stall.obj"); + 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); - /* std::vector vertices = { - { simpleengine::Vectorf(-0.5f, -0.5f, -1.f), glm::vec3(1.f, 0.f, 0.f), glm::vec2(0.f, 0.f) }, // bottom left + /* std::vector square_vertices = { + { simpleengine::Vectorf(0.5f, 0.5f, -1.f), glm::vec3(1.f, 0.f, 0.f), glm::vec2(0.f, 0.f) }, // top right { simpleengine::Vectorf(0.5f, -0.5f, -1.f), glm::vec3(0.f, 1.f, 0.f), glm::vec2(1.f, 0.f) }, // bottom right - { simpleengine::Vectorf(0.f, 0.5f, -1.f), glm::vec3(0.f, 0.f, 1.f), glm::vec2(0.5f, 1.0f) }, // top + { simpleengine::Vectorf(-0.5f, -0.5f, -1.f), glm::vec3(0.f, 0.f, 1.f), glm::vec2(0.5f, 1.0f) }, // bottom left + { simpleengine::Vectorf(-0.5f, 0.5f, -1.f), glm::vec3(.5f, 0.5f, 0.f), glm::vec2(0.5f, 1.0f) }, // top left }; - auto tri = std::make_shared(base_shader_program, vertices); - //tri->set_texture(wall_texture); + std::vector indicies = { + 0, 1, 3, + 1, 2, 3 + }; + + auto square = std::make_shared(game.get_window(), base_shader_program, square_vertices, indicies); + square->translate(1.25f, 0.f, -1.f); + square->scale(.75f); + game.add_event(square); + + std::vector tri_vertices = { + { simpleengine::Vectorf(-0.5f, -0.5f, -1.f), glm::vec3(1.f, 0.f, 0.f), glm::vec2(0.f, 0.f) }, // top right + { simpleengine::Vectorf(0.5f, -0.5f, -1.f), glm::vec3(0.f, 1.f, 0.f), glm::vec2(1.f, 0.f) }, // bottom right + { simpleengine::Vectorf(0.f, 0.5f, -1.f), glm::vec3(0.f, 0.f, 1.f), glm::vec2(0.5f, 1.0f) }, // bottom left + }; + + auto tri = std::make_shared(game.get_window(), base_shader_program, tri_vertices); + tri->translate(-1.25f, 0.f, -1.f); + tri->scale(.75f); game.add_event(tri); */ /* std::vector vertices = { @@ -109,8 +128,7 @@ int main(int argc, char *argv[]) { 23,21,22 }; - auto square = std::make_shared(base_shader_program, vertices, indicies); - //square->set_texture(crate_texture); + auto square = std::make_shared(game.get_window(), base_shader_program, vertices, indicies); game.add_event(square); */ auto camera = std::make_shared(game.get_window(), base_shader_program); diff --git a/include/simpleengine/gfx/model.h b/include/simpleengine/gfx/model.h new file mode 100644 index 0000000..5935405 --- /dev/null +++ b/include/simpleengine/gfx/model.h @@ -0,0 +1,33 @@ +#pragma once + +#include "shader.h" +#include "vao.h" +#include "vbo.h" +#include "../vertex.h" +#include "../renderable.h" +#include "../transformable.h" + +#include + +namespace simpleengine::gfx { + class Model : public simpleengine::Renderable, public simpleengine::Transformable { + public: + std::vector vertices; + std::vector indicies; + gfx::VBO ebo; + gfx::VBO vbo; + gfx::VAO vao; + + gfx::Shader shader; + + Model(GLFWwindow* window, gfx::Shader shader, std::vector vertices, std::vector indicies = std::vector()); + Model(GLFWwindow* window, std::shared_ptr shader_program, std::vector vertices, + std::vector indicies = std::vector()); + protected: + void setup_vertexes(); + public: + + virtual void update(const float& delta_time) override; + virtual void render(GLFWwindow* target) override; + }; +} \ No newline at end of file diff --git a/include/simpleengine/gfx/textured_model.h b/include/simpleengine/gfx/textured_model.h new file mode 100644 index 0000000..b369c8d --- /dev/null +++ b/include/simpleengine/gfx/textured_model.h @@ -0,0 +1,30 @@ +#pragma once + +/* #include "shader.h" +#include "vao.h" +#include "vbo.h" +#include "../vertex.h" +#include "../renderable.h" +#include "../transformable.h" */ + +#include "shader.h" +#include "model.h" +#include "texture.h" + +#include + +#include + +namespace simpleengine::gfx { + class TexturedModel : public simpleengine::gfx::Model { + public: + gfx::Texture texture; + + TexturedModel(GLFWwindow* window, gfx::Shader shader, gfx::Texture texture, std::vector vertices, std::vector indicies); + TexturedModel(GLFWwindow* window, std::shared_ptr shader_program, gfx::Texture texture, std::vector vertices, + std::vector indicies = std::vector()); + + virtual void update(const float& delta_time) override; + virtual void render(GLFWwindow* target) override; + }; +} \ No newline at end of file diff --git a/include/simpleengine/gfx/vbo.h b/include/simpleengine/gfx/vbo.h index 73fab84..4ab1839 100644 --- a/include/simpleengine/gfx/vbo.h +++ b/include/simpleengine/gfx/vbo.h @@ -15,6 +15,8 @@ namespace simpleengine::gfx { GLint type; bool dynamic; + VBO() = default; + VBO(GLint type, bool dynamic) : type(type), dynamic(dynamic) { glGenBuffers(1, &handle); } diff --git a/include/simpleengine/objects/2d/shapes/square.h b/include/simpleengine/objects/2d/shapes/square.h deleted file mode 100644 index 5bf208a..0000000 --- a/include/simpleengine/objects/2d/shapes/square.h +++ /dev/null @@ -1,52 +0,0 @@ -#pragma once - -#ifdef __linux__ -#include -#include -#elif -#include -#include -#endif - -#include - -#include - -#include "../../../renderable.h" -#include "../../../vertex.h" -#include "../../../transformable.h" -#include "../../../optional.h" - -#include "../../../gfx/vbo.h" -#include "../../../gfx/vao.h" -#include "../../../gfx/texture.h" -#include "../../../gfx/shader.h" - -#include -#include - -namespace simpleengine::objects_2d::shapes { - class Square : public simpleengine::Renderable, public simpleengine::Transformable { - private: - gfx::Shader shader; - nonstd::optional texture; - public: - std::vector vertices; - std::vector indicies; - gfx::VBO ebo; - gfx::VBO vbo; - gfx::VAO vao; - - Square(gfx::Shader shader, std::vector vertices, std::vector indicies); - - Square(std::shared_ptr shader_program, std::vector vertices, std::vector indicies); - - virtual ~Square() = default; - - void set_texture(gfx::Texture texture); - - virtual void update(const float& delta_time) override; - - virtual void render(GLFWwindow* target) override; - }; -} \ No newline at end of file diff --git a/include/simpleengine/objects/2d/shapes/triangle.h b/include/simpleengine/objects/2d/shapes/triangle.h deleted file mode 100644 index 423a7ab..0000000 --- a/include/simpleengine/objects/2d/shapes/triangle.h +++ /dev/null @@ -1,47 +0,0 @@ -#pragma once - -#ifdef __linux__ -#include -#include -#elif -#include -#include -#endif - -#include - -#include - -#include "../../../renderable.h" -#include "../../../vertex.h" -#include "../../../gfx/vbo.h" -#include "../../../gfx/vao.h" -#include "../../../gfx/shader.h" -#include "../../../gfx/texture.h" -#include "../../../optional.h" -#include "../../../transformable.h" - -#include - -namespace simpleengine::objects_2d::shapes { - class Triangle : public simpleengine::Renderable, public simpleengine::Transformable { - private: - gfx::Shader shader; // This only stores the shader program - nonstd::optional texture; - public: - std::vector vertices; - gfx::VBO vbo; - gfx::VAO vao; - - Triangle(gfx::Shader shader, std::vector vertices); - - Triangle(std::shared_ptr shader_program, std::vector vertices); - - virtual ~Triangle() = default; - - void set_texture(gfx::Texture texture); - - virtual void update(const float& delta_time) override; - virtual void render(GLFWwindow* target) override; - }; -} \ No newline at end of file diff --git a/include/simpleengine/objects/3d/obj_model.h b/include/simpleengine/objects/3d/obj_model.h index a41db47..6c7016b 100644 --- a/include/simpleengine/objects/3d/obj_model.h +++ b/include/simpleengine/objects/3d/obj_model.h @@ -14,9 +14,10 @@ #include "../../gfx/vbo.h" #include "../../gfx/shader.h" #include "../../gfx/texture.h" +#include "../../gfx/textured_model.h" namespace simpleengine::objects_3d { - class ObjModel : public simpleengine::Renderable, public simpleengine::Transformable { + class ObjModel : public simpleengine::gfx::TexturedModel { private: std::vector split_string(std::string str, const char delim) { std::istringstream ss(str); @@ -50,31 +51,31 @@ namespace simpleengine::objects_3d { out_normals.at(currentVertexIndex) = current_norm; } - nonstd::optional texture; + //nonstd::optional texture; public: - std::vector model_vertices; + /* std::vector model_vertices; std::vector indicies; gfx::VBO ebo; gfx::VBO vbo; gfx::VAO vao; - gfx::Shader shader; + gfx::Shader shader; */ - ObjModel(gfx::Shader shader, std::string filename) : ObjModel(shader, std::ifstream(filename, std::ios::in | std::ios::binary)) { + 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(gfx::Shader shader, std::ifstream file_stream) : - shader(shader), ebo(gfx::VBO(GL_ELEMENT_ARRAY_BUFFER, false)), vbo(gfx::VBO(GL_ARRAY_BUFFER, false)), - texture(nonstd::nullopt) { + 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 vertices; - std::vector read_textures; - std::vector read_normals; + std::vector obj_vertices; + std::vector obj_textures; + std::vector obj_normals; std::vector textures; std::vector normals; @@ -85,13 +86,13 @@ namespace simpleengine::objects_3d { if (line_tokens.front() == "v") { //glm::vec3 vertex(stof(line_tokens[1]), stof(line_tokens[2]), stof(line_tokens[3])); - vertices.emplace_back(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") { - read_textures.emplace_back(stof(line_tokens[1]), stof(line_tokens[2])); + obj_textures.emplace_back(stof(line_tokens[1]), stof(line_tokens[2])); } else if (line_tokens.front() == "vn") { - read_normals.emplace_back(stof(line_tokens[1]), stof(line_tokens[2]), stof(line_tokens[3])); + obj_normals.emplace_back(stof(line_tokens[1]), stof(line_tokens[2]), stof(line_tokens[3])); } else if (line_tokens.front() == "f") { - auto size = vertices.size(); + auto size = obj_vertices.size(); textures.resize(size); normals.resize(size); @@ -110,19 +111,19 @@ namespace simpleengine::objects_3d { std::vector vertex2 = split_string(line_tokens[2], '/'); std::vector vertex3 = split_string(line_tokens[3], '/'); - process_vertex(vertex1, indicies, read_textures, read_normals, textures, normals); - process_vertex(vertex2, indicies, read_textures, read_normals, textures, normals); - process_vertex(vertex3, indicies, read_textures, read_normals, textures, normals); + 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 < vertices.size(); i++) { - model_vertices.emplace_back(simpleengine::Vectorf(vertices.at(i)), glm::vec3(1.f), textures.at(i)); + 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(model_vertices.data(), 0, sizeof(Vertex) * model_vertices.size()); + 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)); @@ -136,8 +137,6 @@ namespace simpleengine::objects_3d { glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); - - this->translate(glm::vec3(0.f, -1.f, -15.f)); } void set_texture(gfx::Texture texture) { @@ -148,7 +147,7 @@ namespace simpleengine::objects_3d { this->rotate_y(0.5f); } - virtual void render(GLFWwindow* target) override { + /* virtual void render(GLFWwindow* target) override { shader.use(); shader.set_uniform_matrix_4f("transform_matrix", transform_matrix, false); @@ -163,6 +162,6 @@ namespace simpleengine::objects_3d { vao.bind(); glDrawElements(GL_TRIANGLES, indicies.size(), GL_UNSIGNED_INT, 0); - } + } */ }; } \ No newline at end of file diff --git a/include/simpleengine/transformable.h b/include/simpleengine/transformable.h index 0655ea8..ec467d7 100644 --- a/include/simpleengine/transformable.h +++ b/include/simpleengine/transformable.h @@ -52,10 +52,6 @@ namespace simpleengine { transform_matrix = glm::translate(transform_matrix, glm::vec3(x, y, z)); } - virtual void translate(const float& x, const float& y, const float& z) { - transform_matrix = glm::translate(transform_matrix, glm::vec3(x, y, z)); - } - virtual void translate(const glm::vec3& vec) { transform_matrix = glm::translate(transform_matrix, vec); } diff --git a/src/gfx/model.cpp b/src/gfx/model.cpp new file mode 100644 index 0000000..9786723 --- /dev/null +++ b/src/gfx/model.cpp @@ -0,0 +1,54 @@ +#include "gfx/model.h" + +namespace simpleengine::gfx { + Model::Model(GLFWwindow* window, gfx::Shader shader, std::vector vertices, std::vector indicies) : + simpleengine::Renderable(window), shader(shader), vertices(vertices), indicies(indicies), vbo(gfx::VBO(GL_ARRAY_BUFFER, false)), + ebo(gfx::VBO(GL_ELEMENT_ARRAY_BUFFER, false)) { + + setup_vertexes(); + } + + Model::Model(GLFWwindow* window, std::shared_ptr shader_program, std::vector vertices, std::vector indicies) : + Model(window, gfx::Shader(shader_program), vertices, indicies) { + + } + + void Model::setup_vertexes() { + vao.bind(); + vbo.buffer(vertices.data(), 0, sizeof(Vertex) * vertices.size()); + if (!indicies.empty()) { + 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 Model::update(const float& delta_time) { + //this->rotate_y(1.f); + } + + void Model::render(GLFWwindow* target) { + 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. + shader.set_uniform_int("texture_is_set", (GLint) false, false); + + vao.bind(); + if (indicies.empty()) { + glDrawArrays(GL_TRIANGLES, 0, vertices.size()); + } else { + glDrawElements(GL_TRIANGLES, indicies.size(), GL_UNSIGNED_INT, 0); + } + } +} \ No newline at end of file diff --git a/src/gfx/textured_model.cpp b/src/gfx/textured_model.cpp new file mode 100644 index 0000000..3036ec7 --- /dev/null +++ b/src/gfx/textured_model.cpp @@ -0,0 +1,34 @@ +#include "gfx/textured_model.h" + +namespace simpleengine::gfx { + TexturedModel::TexturedModel(GLFWwindow* window, gfx::Shader shader, gfx::Texture texture, std::vector vertices, + std::vector indicies) : simpleengine::gfx::Model(window, shader, vertices, indicies), texture(texture) { + + } + + TexturedModel::TexturedModel(GLFWwindow* window, std::shared_ptr shader_program, gfx::Texture texture, + std::vector vertices, std::vector indicies) : TexturedModel(window, gfx::Shader(shader_program), + texture, vertices, indicies) { + + } + + void TexturedModel::update(const float& delta_time) { + + } + + void TexturedModel::render(GLFWwindow* target) { + shader.use(); + shader.set_uniform_matrix_4f("transform_matrix", transform_matrix, false); + + // When binding to the texture, tell the shader if the texture is set or not. + shader.set_uniform_int("texture_is_set", (GLint) true, false); + texture.bind(); + + vao.bind(); + if (indicies.empty()) { + glDrawArrays(GL_TRIANGLES, 0, vertices.size()); + } else { + glDrawElements(GL_TRIANGLES, indicies.size(), GL_UNSIGNED_INT, 0); + } + } +} \ No newline at end of file diff --git a/src/objects/2d/square.cpp b/src/objects/2d/square.cpp deleted file mode 100644 index 9677325..0000000 --- a/src/objects/2d/square.cpp +++ /dev/null @@ -1,55 +0,0 @@ -#include "objects/2d/shapes/square.h" - -namespace simpleengine::objects_2d::shapes { - Square::Square(gfx::Shader shader, std::vector vertices, std::vector indicies) : - simpleengine::Renderable(nullptr), shader(shader), vertices(vertices), indicies(indicies), - ebo(gfx::VBO(GL_ELEMENT_ARRAY_BUFFER, false)), vbo(gfx::VBO(GL_ARRAY_BUFFER, false)), - texture(nonstd::nullopt) { - - 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); - } - - Square::Square(std::shared_ptr shader_program, std::vector vertices, std::vector indicies) : - Square(gfx::Shader(shader_program), vertices, indicies) { - - } - - void Square::set_texture(gfx::Texture texture) { - this->texture = texture; - } - - void Square::update(const float& delta_time) { - this->rotate_y(1.f); - } - - void Square::render(GLFWwindow* target) { - 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/2d/triangle.cpp b/src/objects/2d/triangle.cpp deleted file mode 100644 index 549c61c..0000000 --- a/src/objects/2d/triangle.cpp +++ /dev/null @@ -1,56 +0,0 @@ -#include "objects/2d/shapes/triangle.h" - -namespace simpleengine::objects_2d::shapes { - Triangle::Triangle(gfx::Shader shader, std::vector vertices) : simpleengine::Renderable(nullptr), - simpleengine::Transformable(glm::mat4(1.f)), shader(shader), vertices(vertices), - vbo(gfx::VBO(GL_ARRAY_BUFFER, false)), texture(nonstd::nullopt) { - vao.bind(); - vbo.buffer(vertices.data(), 0, sizeof(Vertex) * vertices.size()); - - vao.enable_attrib(vbo, 0, 3, GL_FLOAT, sizeof(Vertex), offsetof(Vertex, position)); - vao.enable_attrib(vbo, 1, 3, GL_FLOAT, sizeof(Vertex), offsetof(Vertex, color)); - vao.enable_attrib(vbo, 2, 2, GL_FLOAT, sizeof(Vertex), offsetof(Vertex, tex_coord)); - - glBindBuffer(GL_ARRAY_BUFFER, 0); - glBindVertexArray(0); - - // rotate_z(90.f); - // scale(.5f); - } - - Triangle::Triangle(std::shared_ptr shader_program, std::vector vertices) : - Triangle(gfx::Shader(shader_program), vertices) { - - } - - void Triangle::set_texture(gfx::Texture texture) { - this->texture = texture; - - // Tell the shader that there is a texture set. - shader.set_uniform_int("texture_is_set", true); - } - - void Triangle::update(const float& delta_time) { - /* for (Vertex& vertex : vertices) { - vertex.position.translate_x(0.01f); - vertex.position.translate_y(0.01f); - } */ - } - - void Triangle::render(GLFWwindow* target) { - 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(); - glDrawArrays(GL_TRIANGLES, 0, vertices.size()); - } -} \ No newline at end of file