2026-08-17 21:44:00 +01:00
|
|
|
#include "HydraPlanetQuadActor.h"
|
|
|
|
|
#include "../../engine/HydraEngine.h"
|
|
|
|
|
#include "../../actor/HydraActorManager.h"
|
|
|
|
|
#include "../../task/HydraTask.h"
|
|
|
|
|
#include "../../ecs/HydraECS.h"
|
|
|
|
|
#include "../../ecs/components/HydraPositionComponent.h"
|
|
|
|
|
#include "../../ecs/components/HydraMeshComponent.h"
|
|
|
|
|
#include "../../ecs/components/HydraRenderableComponent.h"
|
|
|
|
|
#include "../../ecs/components/HydraMaterialComponent.h"
|
|
|
|
|
#include "../../buffer/HydraMaterialBufferManager.h"
|
|
|
|
|
#include "../../buffer/HydraTextureBufferManager.h"
|
|
|
|
|
#include "../../buffer/HydraMaterialBuffer.h"
|
|
|
|
|
#include "../../mesh/HydraMeshManager.h"
|
|
|
|
|
#include "../../mesh/HydraMesh.h"
|
|
|
|
|
|
|
|
|
|
void HydraPlanetQuadActor::InitialiseComponents()
|
|
|
|
|
{
|
|
|
|
|
// HydraECS* ecs = GetEngine()->ECS();
|
|
|
|
|
// HydraID entity_id = ecs->CreateEntity();
|
|
|
|
|
_CreatePosition();
|
|
|
|
|
// _CreatePhysics();
|
|
|
|
|
|
|
|
|
|
_CreateMaterial();
|
2026-09-05 21:56:45 +01:00
|
|
|
|
2026-08-17 21:44:00 +01:00
|
|
|
_CreateRenderableComponent();
|
2026-09-05 21:56:45 +01:00
|
|
|
}
|
2026-08-17 21:44:00 +01:00
|
|
|
|
|
|
|
|
void HydraPlanetQuadActor::DeferredInitialiseComponents()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-05 21:56:45 +01:00
|
|
|
void HydraPlanetQuadActor::InitialiseQuad(float planet_diameter, float quad_dimension, uint32_t divisions, glm::dvec3 origin, glm::vec3 orientation)
|
2026-09-04 15:26:37 +01:00
|
|
|
{
|
|
|
|
|
m_divisions = divisions;
|
|
|
|
|
m_planet_diameter = planet_diameter;
|
|
|
|
|
m_origin = origin;
|
|
|
|
|
m_quad_dimension = quad_dimension;
|
2026-09-05 21:56:45 +01:00
|
|
|
GetEngine()->ActorManager()->SetPositionAndOrientation(GetID(), origin, orientation, true);
|
2026-09-04 15:26:37 +01:00
|
|
|
_CreateMesh();
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-05 21:56:45 +01:00
|
|
|
glm::vec3 HydraPlanetQuadActor::_ProjectSphere(glm::dvec4 vert_in, glm::mat4 trans, glm::mat4 inv_trans, double diameter, glm::vec3 origin)
|
|
|
|
|
{
|
|
|
|
|
glm::dvec4 vert = trans * vert_in;
|
|
|
|
|
vert = glm::normalize(vert);
|
|
|
|
|
vert.x *= (m_planet_diameter / 2.0);
|
|
|
|
|
vert.y *= (m_planet_diameter / 2.0);
|
|
|
|
|
vert.z *= (m_planet_diameter / 2.0);
|
|
|
|
|
vert = inv_trans * glm::dvec4(vert.x, vert.y, vert.z, 1.0);
|
|
|
|
|
return glm::vec3(vert);
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-17 21:44:00 +01:00
|
|
|
void HydraPlanetQuadActor::_CreatePosition()
|
|
|
|
|
{
|
|
|
|
|
HydraECS *ecs = GetEngine()->ECS();
|
|
|
|
|
HydraPositionComponent position_component;
|
2026-09-05 21:56:45 +01:00
|
|
|
|
2026-08-17 21:44:00 +01:00
|
|
|
ecs->AddComponent<HydraPositionComponent>(GetID(), position_component);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HydraPlanetQuadActor::_CreateMesh()
|
|
|
|
|
{
|
|
|
|
|
HydraMeshManager *mesh_manager = GetEngine()->MeshManager();
|
|
|
|
|
HydraID mesh_id = mesh_manager->CreateMesh();
|
|
|
|
|
HydraMesh *mesh = mesh_manager->GetMesh(mesh_id);
|
|
|
|
|
|
|
|
|
|
HydraMaterialComponent mat_comp = GetEngine()->ECS()->GetComponent<HydraMaterialComponent>(GetID());
|
2026-09-05 21:56:45 +01:00
|
|
|
HydraPositionComponent pos_comp = GetEngine()->ECS()->GetComponent<HydraPositionComponent>(GetID());
|
2026-09-04 22:07:16 +01:00
|
|
|
|
2026-09-04 15:26:37 +01:00
|
|
|
uint32_t vert_counts = m_divisions * m_divisions;
|
2026-09-05 21:56:45 +01:00
|
|
|
uint32_t index_counts = ((m_divisions - 1) * (m_divisions - 1)) * 6;
|
2026-09-04 15:26:37 +01:00
|
|
|
mesh->vertexes.resize(vert_counts);
|
|
|
|
|
mesh->indexes.resize(index_counts);
|
2026-09-05 21:56:45 +01:00
|
|
|
|
2026-09-04 15:26:37 +01:00
|
|
|
float min_x = -m_quad_dimension / 2.0f;
|
2026-09-04 22:07:16 +01:00
|
|
|
float min_y = -m_quad_dimension / 2.0f;
|
2026-09-04 15:26:37 +01:00
|
|
|
float min_z = 0.0f;
|
2026-08-17 21:44:00 +01:00
|
|
|
|
2026-09-04 22:07:16 +01:00
|
|
|
float max_x = m_quad_dimension / 2.0f;
|
2026-09-04 15:26:37 +01:00
|
|
|
float max_y = m_quad_dimension / 2.0f;
|
|
|
|
|
float max_z = 0.0f;
|
2026-08-17 21:44:00 +01:00
|
|
|
|
2026-09-05 21:56:45 +01:00
|
|
|
float min_u = 0;
|
|
|
|
|
float min_v = 1 * (m_divisions-1);
|
|
|
|
|
|
|
|
|
|
float max_u = 1 * (m_divisions-1);
|
2026-08-17 21:44:00 +01:00
|
|
|
float max_v = 0;
|
|
|
|
|
|
2026-09-05 21:56:45 +01:00
|
|
|
float xy_inc = m_quad_dimension / (m_divisions - 1);
|
|
|
|
|
float u_inc = max_u / (m_divisions - 1);
|
|
|
|
|
float v_inc = -min_v / (m_divisions - 1);
|
2026-08-17 21:44:00 +01:00
|
|
|
|
2026-09-04 15:26:37 +01:00
|
|
|
// 1---2
|
|
|
|
|
// | / |
|
|
|
|
|
// 0---3
|
2026-08-17 21:44:00 +01:00
|
|
|
HydraID actor_id = GetID();
|
2026-09-05 21:56:45 +01:00
|
|
|
|
2026-09-04 15:26:37 +01:00
|
|
|
float y = min_y;
|
|
|
|
|
float x = min_x;
|
|
|
|
|
float u = min_u;
|
|
|
|
|
float v = min_v;
|
2026-08-17 21:44:00 +01:00
|
|
|
|
2026-09-04 22:07:16 +01:00
|
|
|
uint32_t vert_idx = 0;
|
2026-09-04 15:26:37 +01:00
|
|
|
uint32_t index_idx = 0;
|
2026-09-05 21:56:45 +01:00
|
|
|
//glm::mat4 trans = GetEngine()->ActorManager()->GetActorPose(actor_id);
|
2026-09-04 22:07:16 +01:00
|
|
|
|
2026-09-05 21:56:45 +01:00
|
|
|
glm::mat4 trans_matrix = glm::translate(pos_comp.position);
|
|
|
|
|
glm::mat4 rot_matrix = glm::toMat4(glm::quat(pos_comp.orientation));
|
|
|
|
|
glm::mat4 trans = trans_matrix * rot_matrix;
|
|
|
|
|
glm::mat4 inv_trans = glm::inverse(trans);
|
|
|
|
|
for (uint32_t y_count = 0; y_count < m_divisions - 1; y_count++)
|
2026-09-04 15:26:37 +01:00
|
|
|
{
|
2026-09-05 21:56:45 +01:00
|
|
|
for (uint32_t x_count = 0; x_count < m_divisions - 1; x_count++)
|
2026-09-04 15:26:37 +01:00
|
|
|
{
|
2026-09-05 21:56:45 +01:00
|
|
|
// Create Vertex
|
2026-08-17 21:44:00 +01:00
|
|
|
|
2026-09-05 21:56:45 +01:00
|
|
|
glm::dvec4 vert = glm::dvec4(x, y, min_z, 1.0);
|
|
|
|
|
glm::vec3 final_vert = _ProjectSphere(vert, trans, inv_trans, m_planet_diameter, m_origin);
|
|
|
|
|
mesh->vertexes[vert_idx] = {final_vert.x, final_vert.y, final_vert.z, u, v, actor_id, mat_comp.material_id, 0};
|
|
|
|
|
|
|
|
|
|
uint32_t ind0 = vert_idx;
|
|
|
|
|
uint32_t ind1 = vert_idx + m_divisions;
|
|
|
|
|
uint32_t ind2 = vert_idx + m_divisions + 1;
|
|
|
|
|
uint32_t ind3 = vert_idx + 1;
|
|
|
|
|
|
|
|
|
|
mesh->indexes[index_idx + 0] = ind0;
|
|
|
|
|
mesh->indexes[index_idx + 1] = ind2;
|
|
|
|
|
mesh->indexes[index_idx + 2] = ind1;
|
|
|
|
|
mesh->indexes[index_idx + 3] = ind0;
|
|
|
|
|
mesh->indexes[index_idx + 4] = ind3;
|
|
|
|
|
mesh->indexes[index_idx + 5] = ind2;
|
|
|
|
|
|
|
|
|
|
x += xy_inc;
|
|
|
|
|
u += u_inc;
|
|
|
|
|
vert_idx++;
|
|
|
|
|
index_idx += 6;
|
2026-09-04 15:26:37 +01:00
|
|
|
}
|
2026-09-05 21:56:45 +01:00
|
|
|
|
|
|
|
|
glm::dvec4 vert = glm::dvec4(x, y, min_z, 1.0);
|
|
|
|
|
glm::vec3 final_vert = _ProjectSphere(vert, trans,inv_trans, m_planet_diameter, m_origin);
|
|
|
|
|
mesh->vertexes[vert_idx] = {final_vert.x, final_vert.y, final_vert.z, u, v, actor_id, mat_comp.material_id, 0};
|
|
|
|
|
// Add the end vert but no indexes
|
|
|
|
|
// mesh->vertexes[vert_idx] = {x, y, min_z, u, v, actor_id, mat_comp.material_id, 0};
|
2026-09-04 22:07:16 +01:00
|
|
|
vert_idx++;
|
2026-09-04 15:26:37 +01:00
|
|
|
y += xy_inc;
|
2026-09-04 22:07:16 +01:00
|
|
|
v += v_inc;
|
2026-09-04 15:26:37 +01:00
|
|
|
x = min_x;
|
|
|
|
|
u = min_u;
|
|
|
|
|
}
|
2026-09-05 21:56:45 +01:00
|
|
|
// the last row
|
|
|
|
|
for (uint32_t x_count = 0; x_count < m_divisions; x_count++)
|
2026-09-04 15:26:37 +01:00
|
|
|
{
|
2026-09-05 21:56:45 +01:00
|
|
|
glm::dvec4 vert = glm::dvec4(x, y, min_z, 1.0);
|
|
|
|
|
glm::vec3 final_vert = _ProjectSphere(vert, trans, inv_trans, m_planet_diameter, m_origin);
|
|
|
|
|
mesh->vertexes[vert_idx] = {final_vert.x, final_vert.y, final_vert.z, u, v, actor_id, mat_comp.material_id, 0};
|
2026-09-04 15:26:37 +01:00
|
|
|
x += xy_inc;
|
2026-09-04 22:07:16 +01:00
|
|
|
u += u_inc;
|
2026-09-04 15:26:37 +01:00
|
|
|
vert_idx++;
|
|
|
|
|
}
|
2026-08-17 21:44:00 +01:00
|
|
|
|
|
|
|
|
mesh_manager->CalculateNormals(mesh->mesh_id);
|
|
|
|
|
|
|
|
|
|
HydraECS *ecs = GetEngine()->ECS();
|
|
|
|
|
HydraMeshComponent mesh_comp;
|
|
|
|
|
|
|
|
|
|
mesh_comp.mesh_id = mesh_id;
|
|
|
|
|
mesh_comp.mesh_state = HYDRA_STATE::Waiting;
|
|
|
|
|
|
|
|
|
|
ecs->AddComponent<HydraMeshComponent>(GetID(), mesh_comp);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HydraPlanetQuadActor::_CreateMaterial()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
HydraEngine *engine = GetEngine();
|
|
|
|
|
std::string filename = std::string(texture_base) + std::string("/DesignArea/DesignArea_Albedo_white.png");
|
|
|
|
|
|
|
|
|
|
HydraID tex_id = engine->TextureBufferManager()->LoadTexture(filename);
|
|
|
|
|
|
|
|
|
|
HydraMaterialBuffer material = {};
|
|
|
|
|
|
|
|
|
|
material.diffuse_texture_id = tex_id;
|
|
|
|
|
material.diffuse_colour = glm::vec4(1, 1, 1, 1);
|
2026-08-28 16:36:25 +01:00
|
|
|
material.emmissive_metalic_rough = glm::vec4(0, 0.5, 0.3, 1);
|
2026-08-17 21:44:00 +01:00
|
|
|
HydraID mat_id = engine->MaterialBufferManager()->CreateMaterial(material);
|
|
|
|
|
|
|
|
|
|
HydraMaterialComponent mat_comp = {};
|
|
|
|
|
mat_comp.material_id = mat_id;
|
|
|
|
|
engine->ECS()->AddComponent<HydraMaterialComponent>(GetID(), mat_comp);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HydraPlanetQuadActor::_CreatePhysics()
|
|
|
|
|
{
|
|
|
|
|
// HydraECSPhysicsComponent physics_component;
|
|
|
|
|
// physics_component.physics_state = Hydra_State::Waiting;
|
|
|
|
|
// physics_component.physics_definition.actor_id = GetID();
|
|
|
|
|
// physics_component.physics_definition.physics_type = HydraPhyicsType::Static;
|
|
|
|
|
// physics_component.physics_definition.physics_shape = HydraPhysicsShape::HydraBoxShape;
|
|
|
|
|
// physics_component.physics_definition.mass = 100000.0f;
|
|
|
|
|
// physics_component.physics_definition.extent_x = 1000.0f;
|
|
|
|
|
// physics_component.physics_definition.extent_y = 0.5f;
|
|
|
|
|
// physics_component.physics_definition.extent_z = 1000.0f;
|
|
|
|
|
// physics_component.physics_definition.dynamic_friction = 0.5f;
|
|
|
|
|
// physics_component.physics_definition.restitution = 0.5f;
|
|
|
|
|
|
|
|
|
|
// HydraECS* ecs = GetEngine()->ECS();
|
|
|
|
|
|
|
|
|
|
// ecs->AddComponent<HydraECSPhysicsComponent>(GetID(), physics_component);
|
|
|
|
|
}
|
|
|
|
|
void HydraPlanetQuadActor::_CreateRenderableComponent()
|
|
|
|
|
{
|
|
|
|
|
HydraRenderableComponent render_comp;
|
|
|
|
|
HydraECS *ecs = GetEngine()->ECS();
|
|
|
|
|
render_comp.renderable_types.set((uint32_t)HYDRA_RENDERABLE_TYPE::HYDRA_COLOUR, true);
|
|
|
|
|
ecs->AddComponent<HydraRenderableComponent>(GetID(), render_comp);
|
|
|
|
|
}
|