Loading...
Searching...
No Matches
Cube.cpp
Go to the documentation of this file.
1#include "hellfire/graphics/geometry/Cube.h"
2#include <iostream>
3#include <glm/glm.hpp>
4
5#include "hellfire/ecs/RenderableComponent.h"
6#include "hellfire/ecs/TransformComponent.h"
7#include "hellfire/ecs/components/MeshComponent.h"
8#include "hellfire/graphics/Mesh.h"
9#include "hellfire/scene/Scene.h"
10
11namespace hellfire {
12// Static geometry data
13 const std::vector<float> Cube::vertices_ = {
14 // Front face (Z+)
15 -1.0f, -1.0f, 1.0f, // 0: bottom-left
16 1.0f, -1.0f, 1.0f, // 1: bottom-right
17 1.0f, 1.0f, 1.0f, // 2: top-right
18 -1.0f, 1.0f, 1.0f, // 3: top-left
19
20 // Back face (Z-)
21 -1.0f, -1.0f, -1.0f, // 4: bottom-left
22 1.0f, -1.0f, -1.0f, // 5: bottom-right
23 1.0f, 1.0f, -1.0f, // 6: top-right
24 -1.0f, 1.0f, -1.0f, // 7: top-left
25
26 // Left face (X-)
27 -1.0f, -1.0f, -1.0f, // 8: bottom-back
28 -1.0f, -1.0f, 1.0f, // 9: bottom-front
29 -1.0f, 1.0f, 1.0f, // 10: top-front
30 -1.0f, 1.0f, -1.0f, // 11: top-back
31
32 // Right face (X+)
33 1.0f, -1.0f, -1.0f, // 12: bottom-back
34 1.0f, -1.0f, 1.0f, // 13: bottom-front
35 1.0f, 1.0f, 1.0f, // 14: top-front
36 1.0f, 1.0f, -1.0f, // 15: top-back
37
38 // Bottom face (Y-)
39 -1.0f, -1.0f, -1.0f, // 16: back-left
40 1.0f, -1.0f, -1.0f, // 17: back-right
41 1.0f, -1.0f, 1.0f, // 18: front-right
42 -1.0f, -1.0f, 1.0f, // 19: front-left
43
44 // Top face (Y+)
45 -1.0f, 1.0f, -1.0f, // 20: back-left
46 1.0f, 1.0f, -1.0f, // 21: back-right
47 1.0f, 1.0f, 1.0f, // 22: front-right
48 -1.0f, 1.0f, 1.0f // 23: front-left
49 };
50
51 const std::vector<float> Cube::uvs_ = {
52 // Front face
53 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f,
54 // Back face
55 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,
56 // Left face
57 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f,
58 // Right face
59 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,
60 // Bottom face
61 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f,
62 // Top face
63 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f
64 };
65
66 const std::vector<unsigned int> Cube::indices_ = {
67 // Front face
68 0, 1, 2, 0, 2, 3,
69 // Back face
70 5, 4, 7, 5, 7, 6,
71 // Left face
72 8, 9, 10, 8, 10, 11,
73 // Right face
74 13, 12, 15, 13, 15, 14,
75 // Bottom face
76 16, 17, 18, 16, 18, 19,
77 // Top face
78 23, 22, 21, 23, 21, 20
79 };
80
81 EntityID Cube::create(Scene* scene, const std::string& name, const Config& config) {
82 EntityID id = scene->create_entity(name);
83 Entity* entity = scene->get_entity(id);
84
85 // Add mesh component
86 auto* mesh_comp = entity->add_component<MeshComponent>();
87 std::vector<Vertex> vertices;
88 std::vector<unsigned int> indices;
89 get_cube_data(vertices, indices, config.color);
90 mesh_comp->set_mesh(std::make_shared<Mesh>(vertices, indices));
91 mesh_comp->set_source(MeshSource::INTERNAL);
92
93 // Add renderable component
94 auto* renderable = entity->add_component<RenderableComponent>();
95 if (config.material) {
96 renderable->set_material(config.material);
97 } else {
98 auto material = MaterialBuilder::create("Cube Material");
99 renderable->set_material(material);
100 }
101
102 // Apply transform
103 entity->transform()->set_position(config.position);
104 entity->transform()->set_rotation(config.rotation);
105 entity->transform()->set_scale(config.scale);
106
107 return id;
108 }
109
110 std::shared_ptr<Mesh> Cube::create_mesh() {
111 std::vector<Vertex> vertices;
112 std::vector<unsigned int> indices;
113 get_cube_data(vertices, indices);
114 return std::make_shared<Mesh>(vertices, indices);
115 }
116
117 void Cube::get_cube_data(std::vector<Vertex>& vertices, std::vector<unsigned int>& indices,
118 const glm::vec3& color) {
119 vertices.clear();
120 indices.clear();
121
122 vertices.reserve(24); // 6 faces * 4 vertices each
123 indices.assign(indices_.begin(), indices_.end());
124
125 // Create vertex data
126 for (size_t i = 0; i < 24; ++i) {
127 Vertex v;
128 v.position = glm::vec3(vertices_[i * 3], vertices_[i * 3 + 1], vertices_[i * 3 + 2]);
129 v.color = color;
130
131 // Determine face and assign normal
132 int face = i / 4;
133 switch (face) {
134 case 0: v.normal = glm::vec3(0.0f, 0.0f, 1.0f); break; // Front face (+Z)
135 case 1: v.normal = glm::vec3(0.0f, 0.0f, -1.0f); break; // Back face (-Z)
136 case 2: v.normal = glm::vec3(-1.0f, 0.0f, 0.0f); break; // Left face (-X)
137 case 3: v.normal = glm::vec3(1.0f, 0.0f, 0.0f); break; // Right face (+X)
138 case 4: v.normal = glm::vec3(0.0f, -1.0f, 0.0f); break; // Bottom face (-Y)
139 case 5: v.normal = glm::vec3(0.0f, 1.0f, 0.0f); break; // Top face (+Y)
140 default: v.normal = glm::vec3(0.0f, 1.0f, 0.0f); break;
141 }
142
143 v.texCoords = glm::vec2(uvs_[i * 2], uvs_[i * 2 + 1]);
144 vertices.push_back(v);
145 }
146 }
147}
static const std::vector< unsigned int > indices_
Definition Cube.h:35
static EntityID create(Scene *scene, const std::string &name, const Config &config)
Definition Cube.cpp:81
static const std::vector< float > vertices_
Definition Cube.h:33
static const std::vector< float > uvs_
Definition Cube.h:34
static std::shared_ptr< Mesh > create_mesh()
Definition Cube.cpp:110
TransformComponent * transform()
Definition Entity.cpp:42
Manages a collection of entities and their hierarchical relationships.
Definition Scene.h:24
Entity * get_entity(EntityID id)
Retrieves an entity by its ID.
Definition Scene.cpp:81
EntityID create_entity(const std::string &name="GameObject")
Creates a new entity in the scene.
Definition Scene.cpp:29
void set_position(float x, float y, float z)
void set_rotation(float x, float y, float z)
Definition Vertex.h:5