4#include <hellfire/graphics/geometry/Sphere.h>
6#include "hellfire/ecs/RenderableComponent.h"
7#include "hellfire/ecs/TransformComponent.h"
8#include "hellfire/ecs/components/MeshComponent.h"
9#include "hellfire/scene/Scene.h"
17 auto *mesh_comp = entity->add_component<MeshComponent>();
18 std::vector<Vertex> vertices;
19 std::vector<
unsigned int> indices;
22 get_sphere_data(vertices, indices, 1.0f, config
.rings, config
.sectors);
25 for (
auto &vertex: vertices) {
26 vertex.color = config.color;
29 mesh_comp->set_mesh(std::make_shared<
Mesh>(vertices, indices));
33 auto *renderable = entity->add_component<RenderableComponent>();
34 if (config.material) {
35 renderable->set_material(config.material);
38 renderable->set_material(material);
49 std::vector<Vertex> vertices;
50 std::vector<
unsigned int> indices;
51 get_sphere_data(vertices, indices, 1.0f, rings, sectors);
52 return std::make_shared<
Mesh>(vertices, indices);
55 void Sphere::get_sphere_data(std::vector<Vertex> &vertices, std::vector<
unsigned int> &indices,
56 float radius,
int rings,
int sectors) {
61 for (
int ring = 0; ring <= rings; ++ring) {
62 constexpr float PI = 3.14159265359f;
63 const float phi = PI *
static_cast<
float>(ring) /
static_cast<
float>(rings);
65 const float ring_radius = sin(phi);
67 for (
int sector = 0; sector <= sectors; ++sector) {
68 const float theta = 2.0f * PI *
static_cast<
float>(sector) /
static_cast<
float>(sectors);
69 float x = ring_radius * cos(theta);
70 float z = ring_radius * sin(theta);
73 vertex.normal = glm::normalize(glm::vec3(x, y, z));
74 vertex.position = vertex.normal * radius;
75 vertex.color = glm::vec3(1.0f);
78 vertex.texCoords = glm::vec2(
79 static_cast<
float>(sector) /
static_cast<
float>(sectors),
80 static_cast<
float>(ring) /
static_cast<
float>(rings)
83 vertices.push_back(vertex);
87 for (
int ring = 0; ring < rings; ++ring) {
88 for (
int sector = 0; sector < sectors; ++sector) {
89 const int current = ring * (sectors + 1) + sector;
90 const int next = current + sectors + 1;
93 indices.push_back(current);
94 indices.push_back(current + 1);
95 indices.push_back(next);
97 indices.push_back(current + 1);
98 indices.push_back(next + 1);
99 indices.push_back(next);
104 void Sphere::subdivide_triangle(std::vector<glm::vec3> &vertices,
const glm::vec3 &v1,
const glm::vec3 &v2,
const glm::vec3 &v3,
107 vertices.push_back(v1);
108 vertices.push_back(v2);
109 vertices.push_back(v3);
114 const glm::vec3 v12 = glm::normalize((v1 + v2) * 0.5f);
115 const glm::vec3 v23 = glm::normalize((v2 + v3) * 0.5f);
116 const glm::vec3 v31 = glm::normalize((v3 + v1) * 0.5f);
119 subdivide_triangle(vertices, v1, v12, v31, depth - 1);
120 subdivide_triangle(vertices, v2, v23, v12, depth - 1);
121 subdivide_triangle(vertices, v3, v31, v23, depth - 1);
122 subdivide_triangle(vertices, v12, v23, v31, depth - 1);
TransformComponent * transform()
Manages a collection of entities and their hierarchical relationships.
Entity * get_entity(EntityID id)
Retrieves an entity by its ID.
EntityID create_entity(const std::string &name="GameObject")
Creates a new entity in the scene.
static std::shared_ptr< Mesh > create_mesh(int rings=32, int sectors=32)
static EntityID create(Scene *scene, const std::string &name, const Config &config)