Skip to content

Commit

Permalink
Added cube primitive and Initialize method for mesh component
Browse files Browse the repository at this point in the history
  • Loading branch information
dorosch committed Sep 8, 2022
1 parent 3417b26 commit d6db67c
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 61 deletions.
20 changes: 7 additions & 13 deletions include/core/ecs/component.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<float> vertices;
std::vector<uint32_t> indices;
std::unique_ptr<Render::VertexArray> VAO;
std::unique_ptr<Render::VertexBuffer> VBO;
std::unique_ptr<Render::IndexBuffer> EBO;

Mesh();
/*
* Mesh initialization places the vertex data on the GPU and
* places it there. Temporary solution for creating a mesh.
*/
void Initialize();
};
}
}
Expand Down
54 changes: 43 additions & 11 deletions include/core/geometry/primitives.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
};
}
}

Expand Down
1 change: 0 additions & 1 deletion include/core/render/api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ namespace Engine {

public:
std::unique_ptr<Logger> logger = std::make_unique<Logger>("render");

std::unique_ptr<ShaderProgram> shader;

static std::shared_ptr<Render>& GetInstance();
Expand Down
32 changes: 3 additions & 29 deletions src/core/ecs/component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
8 changes: 3 additions & 5 deletions src/core/render/api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}
4 changes: 2 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Engine::Geometry::Plane> plane = std::make_shared<Engine::Geometry::Plane>();
scene->root->entities.push_back(plane);
std::shared_ptr<Engine::Geometry::Cube> cube = std::make_shared<Engine::Geometry::Cube>();
scene->root->entities.push_back(cube);

GLFWwindow *window = static_cast<Engine::Window::GLFWWindowProvider*>(this->window)->object;
glfwSetMouseButtonCallback(window, mouse_button_callback);
Expand Down

0 comments on commit d6db67c

Please sign in to comment.