Loading...
Searching...
No Matches
Quad.cpp
Go to the documentation of this file.
1#include "hellfire/graphics/geometry/Quad.h"
2
3#include <vector>
4
5#include "hellfire/ecs/RenderableComponent.h"
6#include "hellfire/ecs/Entity.h"
7#include "hellfire/ecs/TransformComponent.h"
8#include "hellfire/ecs/components/MeshComponent.h"
9#include "hellfire/scene/Scene.h"
10
11namespace hellfire {
12 const std::vector<float> Quad::vertices_ = {
13 1.0, 1.0, 0.0, // top-right
14 -1.0, 1.0, 0.0, // top-left
15 1.0, -1.0, 0.0, // bottom-left
16 -1.0, -1.0, 0.0 // bottom-right
17 };
18
19 // Elements
20 const std::vector<unsigned int> Quad::indices_ = {
21 0, 1, 3, // First triangle
22 0, 3, 2, // Second triangle
23 };
24
25 // uvs
26 const std::vector<float> Quad::uvs_ = {
27 1.0, 1.0, // top-right
28 0.0, 1.0, // top-left
29 1.0, 0.0, // bottom-right
30 0.0, 0.0 // bottom-left
31 };
32
33 EntityID Quad::create(Scene* scene, const std::string& name, const Config& config) {
34 EntityID id = scene->create_entity(name);
35 Entity* entity = scene->get_entity(id);
36
37 // Add mesh component
38 auto* mesh_comp = entity->add_component<MeshComponent>();
39 std::vector<Vertex> vertices;
40 std::vector<unsigned int> indices;
41 get_quad_data(vertices, indices, config.color);
42 mesh_comp->set_mesh(std::make_shared<Mesh>(vertices, indices));
43 mesh_comp->set_source(MeshSource::INTERNAL);
44
45 // Add renderable component
46 auto* renderable = entity->add_component<RenderableComponent>();
47 if (config.material) {
48 renderable->set_material(config.material);
49 } else {
50 auto material = MaterialBuilder::create("Quad Material");
51 renderable->set_material(material);
52 }
53
54 // Apply transform
55 entity->transform()->set_position(config.position);
56 entity->transform()->set_rotation(config.rotation);
57 entity->transform()->set_scale(config.scale);
58
59 return id;
60 }
61
62 std::shared_ptr<Mesh> Quad::create_mesh() {
63 std::vector<Vertex> vertices;
64 std::vector<unsigned int> indices;
65 get_quad_data(vertices, indices);
66 return std::make_shared<Mesh>(vertices, indices);
67 }
68
69 void Quad::get_quad_data(std::vector<Vertex>& vertices, std::vector<unsigned int>& indices,
70 const glm::vec3& color) {
71 vertices.clear();
72 indices.clear();
73
74 vertices.reserve(4);
75 indices.assign(indices_.begin(), indices_.end());
76
77 // Extract the four corner positions
78 glm::vec3 p0(vertices_[0], vertices_[1], vertices_[2]);
79 glm::vec3 p1(vertices_[3], vertices_[4], vertices_[5]);
80 glm::vec3 p2(vertices_[6], vertices_[7], vertices_[8]);
81
82 // Calculate two edge vectors
83 glm::vec3 edge1 = p1 - p0;
84 glm::vec3 edge2 = p2 - p0;
85
86 glm::vec3 normal = glm::normalize(glm::cross(edge1, edge2));
87 // Create vertex data
88 for (size_t i = 0; i < 4; ++i) {
89 Vertex v;
90 v.position = glm::vec3(vertices_[i * 3], vertices_[i * 3 + 1], vertices_[i * 3 + 2]);
91 v.color = color;
92 v.normal = normal;
93 v.texCoords = glm::vec2(uvs_[i * 2], uvs_[i * 2 + 1]);
94 vertices.push_back(v);
95 }
96 }
97}
TransformComponent * transform()
Definition Entity.cpp:42
static const std::vector< float > uvs_
Definition Quad.h:36
static EntityID create(Scene *scene, const std::string &name, const Config &config=Config{})
Definition Quad.cpp:33
static const std::vector< unsigned int > indices_
Definition Quad.h:37
static std::shared_ptr< Mesh > create_mesh()
Definition Quad.cpp:62
static const std::vector< float > vertices_
Definition Quad.h:35
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