diff --git a/include/core/ecs/component.hpp b/include/core/ecs/component.hpp index 7a49dc6..fba768d 100644 --- a/include/core/ecs/component.hpp +++ b/include/core/ecs/component.hpp @@ -46,23 +46,17 @@ namespace Engine { class Mesh : public Component { public: Type type = Type::MESH; - // float *vertices; - // uint32_t *indices; - // float vertices[] = { - // 0.5f, 0.5f, 0.0f, // Top Right - // 0.5f, -0.5f, 0.0f, // Bottom Right - // -0.5f, -0.5f, 0.0f, // Bottom Left - // -0.5f, 0.5f, 0.0f // Top Left - // }; - // uint32_t indices[] = { - // 0, 1, 3, // First Triangle - // 1, 2, 3 // Second Triangle - // }; + std::vector vertices; + std::vector indices; std::unique_ptr VAO; std::unique_ptr VBO; std::unique_ptr EBO; - Mesh(); + /* + * Mesh initialization places the vertex data on the GPU and + * places it there. Temporary solution for creating a mesh. + */ + void Initialize(); }; } } diff --git a/include/core/geometry/primitives.hpp b/include/core/geometry/primitives.hpp index bb9da81..0d6a27d 100644 --- a/include/core/geometry/primitives.hpp +++ b/include/core/geometry/primitives.hpp @@ -22,21 +22,53 @@ namespace Engine { Plane() : Primitive() { name = std::string("Plane"); - // mesh->vertices = { - // 0.5f, 0.5f, 0.0f, - // 0.5f, -0.5f, 0.0f, - // -0.5f, -0.5f, 0.0f, - // -0.5f, 0.5f, 0.0f - // }; - // mesh->indices = { - // 0, 1, 3, - // 1, 2, 3 - // }; + mesh->vertices = { + 0.5f, 0.5f, 0.0f, + 0.5f, -0.5f, 0.0f, + -0.5f, -0.5f, 0.0f, + -0.5f, 0.5f, 0.0f + }; + mesh->indices = { + 0, 1, 3, + 1, 2, 3 + }; + mesh->Initialize(); } }; - class Cube : public Primitive {}; + class Cube : public Primitive { + public: + Cube() : Primitive() { + name = std::string("Cube"); + + mesh->vertices = { + -0.5f, -0.5f, 0.0f, + 0.5f, -0.5f, 0.0f, + -0.5f, 0.5f, 0.0f, + 0.5f, 0.5f, 0.0f, + -0.5f, -0.5f, -0.0f, + 0.5f, -0.5f, -0.0f, + -0.5f, 0.5f, -0.0f, + 0.5f, 0.5f, -0.0f + }; + mesh->indices = { + 2, 6, 7, + 2, 3, 7, + 0, 4, 5, + 0, 1, 5, + 0, 2, 6, + 0, 4, 6, + 1, 3, 7, + 1, 5, 7, + 0, 2, 3, + 0, 1, 3, + 4, 6, 7, + 4, 5, 7 + }; + mesh->Initialize(); + } + }; } } diff --git a/include/core/render/api.hpp b/include/core/render/api.hpp index 48a8b04..6d028c4 100644 --- a/include/core/render/api.hpp +++ b/include/core/render/api.hpp @@ -27,7 +27,6 @@ namespace Engine { public: std::unique_ptr logger = std::make_unique("render"); - std::unique_ptr shader; static std::shared_ptr& GetInstance(); diff --git a/src/core/ecs/component.cpp b/src/core/ecs/component.cpp index 1b5b4f0..8770b6d 100644 --- a/src/core/ecs/component.cpp +++ b/src/core/ecs/component.cpp @@ -5,40 +5,14 @@ using namespace Engine::Ecs::Component; -Mesh::Mesh() { +void Mesh::Initialize() { VAO = Engine::Render::VertexArray::GetInstance(); VBO = Engine::Render::VertexBuffer::GetInstance(); EBO = Engine::Render::IndexBuffer::GetInstance(); - GLfloat vertices[] = { - 0.5f, 0.5f, 0.0f, // Top Right - 0.5f, -0.5f, 0.0f, // Bottom Right - -0.5f, -0.5f, 0.0f, // Bottom Left - -0.5f, 0.5f, 0.0f // Top Left - }; - uint32_t indices[] = { - 0, 1, 3, // First Triangle - 1, 2, 3 // Second Triangle - }; - - // glBindVertexArray(VAO->object); - - // glBindBuffer(GL_ARRAY_BUFFER, VBO->object); - // glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); - - // glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO->object); - // glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW); - - // glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0); - // glEnableVertexAttribArray(0); - - // glBindBuffer(GL_ARRAY_BUFFER, 0); - - // glBindVertexArray(0); - VAO->bind(); - VBO->bind(vertices, sizeof(vertices)); - EBO->bind(indices, sizeof(indices)); + VBO->bind(vertices.data(), vertices.size() * sizeof(float)); + EBO->bind(indices.data(), indices.size() * sizeof(uint32_t)); VAO->layout(3, 3 * sizeof(float), 0); diff --git a/src/core/render/api.cpp b/src/core/render/api.cpp index 85e4199..07cdbb6 100644 --- a/src/core/render/api.cpp +++ b/src/core/render/api.cpp @@ -51,11 +51,9 @@ void Render::RenderScene(Engine::Scene::Scene *scene) { // TODO: If object hasn't own shader component shader->Use(); - // object->mesh->VAO->bind(); - glBindVertexArray(object->mesh->VAO->object); - glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); - // object->mesh->VAO->unbind(); - glBindVertexArray(0); + object->mesh->VAO->bind(); + glDrawElements(GL_TRIANGLES, object->mesh->indices.size(), GL_UNSIGNED_INT, 0); + object->mesh->VAO->unbind(); } } } diff --git a/src/main.cpp b/src/main.cpp index ba684d6..d231caa 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -614,8 +614,8 @@ class UserApplication : public Engine::EngineApplication { // this->scene->root->children.push_back(mp5); // this->scene->root->children.push_back(tank); - std::shared_ptr plane = std::make_shared(); - scene->root->entities.push_back(plane); + std::shared_ptr cube = std::make_shared(); + scene->root->entities.push_back(cube); GLFWwindow *window = static_cast(this->window)->object; glfwSetMouseButtonCallback(window, mouse_button_callback);