Skip to content

Commit

Permalink
Added support of multiple meshes per object
Browse files Browse the repository at this point in the history
  • Loading branch information
dorosch committed Sep 18, 2022
1 parent d59331f commit c5b8144
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 15 deletions.
5 changes: 3 additions & 2 deletions include/core/ecs/entity.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <memory>
#include <string>
#include <vector>

#include "core/ecs/component.hpp"

Expand All @@ -14,7 +15,7 @@ namespace Engine {
std::string name;
std::unique_ptr<Component::Transform> transform = std::make_unique<Component::Transform>();
std::unique_ptr<Component::Material> material;
std::unique_ptr<Component::Mesh> mesh;
std::vector<std::unique_ptr<Component::Mesh>> meshes;
std::unique_ptr<Component::Light> light;
std::unique_ptr<Component::Camera> camera;

Expand All @@ -28,7 +29,7 @@ namespace Engine {
case Component::Type::MATERIAL:
return material != nullptr;
case Component::Type::MESH:
return mesh != nullptr;
return meshes.size() > 0;
case Component::Type::LIGHT:
return light != nullptr;
case Component::Type::CAMERA:
Expand Down
12 changes: 7 additions & 5 deletions include/core/geometry/primitives.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,16 @@ namespace Engine {
namespace Geometry {
class Primitive : public Object {
public:
Primitive() : Object() {
mesh = std::make_unique<Ecs::Component::Mesh>();
}
Primitive() : Object() {};
};


class Plane : public Primitive {
public:
Plane() : Primitive() {
name = std::string("Plane");
std::unique_ptr<Ecs::Component::Mesh> mesh = std::make_unique<Ecs::Component::Mesh>();

name = std::string("Plane");
mesh->vertices = {
-0.5f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f,
0.5f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f,
Expand All @@ -31,15 +30,17 @@ namespace Engine {
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f
};
mesh->Initialize();
meshes.push_back(std::move(mesh));
}
};


class Cube : public Primitive {
public:
Cube() : Primitive() {
name = std::string("Cube");
std::unique_ptr<Ecs::Component::Mesh> mesh = std::make_unique<Ecs::Component::Mesh>();

name = std::string("Cube");
mesh->vertices = {
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
Expand Down Expand Up @@ -84,6 +85,7 @@ namespace Engine {
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f
};
mesh->Initialize();
meshes.push_back(std::move(mesh));
}
};
}
Expand Down
18 changes: 11 additions & 7 deletions src/core/render/api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,12 @@ void Render::RenderScene(const Engine::Scene::Scene *scene) {
shader->UniformFloat("material.shininess", object->material->shininess);
}

object->mesh->VAO->bind();
// TODO: Add calculation of the number of lines to the mesh method
glDrawArrays(GL_TRIANGLES, 0, object->mesh->vertices.size() / 6);
object->mesh->VAO->unbind();
for (auto const &mesh : object->meshes) {
mesh->VAO->bind();
// TODO: Add calculation of the number of lines to the mesh method
glDrawArrays(GL_TRIANGLES, 0, mesh->vertices.size() / 6);
mesh->VAO->unbind();
}
}
}

Expand All @@ -154,9 +156,11 @@ void Render::RenderScene(const Engine::Scene::Scene *scene) {
lightingShader->UniformMatrix("projection", projection);

// If the editor is enabled, then draw where the light source is
light->mesh->VAO->bind();
glDrawArrays(GL_TRIANGLES, 0, light->mesh->vertices.size() / 6);
light->mesh->VAO->unbind();
for (auto const &mesh : light->meshes) {
mesh->VAO->bind();
glDrawArrays(GL_TRIANGLES, 0, mesh->vertices.size() / 6);
mesh->VAO->unbind();
}
}

scene->environment->skybox->update(projection * view);
Expand Down
2 changes: 1 addition & 1 deletion src/editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ namespace Engine {

if (selectedEntity->HasComponent(Ecs::Component::Type::MESH)) {
if (ImGui::CollapsingHeader("Mesh", ImGuiTreeNodeFlags_None)) {
ImGui::Text("vertices: %ld", selectedEntity->mesh->vertices.size());
ImGui::Text("meshes: %ld", selectedEntity->meshes.size());
}
}

Expand Down

0 comments on commit c5b8144

Please sign in to comment.