190 lines
5.6 KiB
C++
190 lines
5.6 KiB
C++
#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();
|
|
|
|
_CreateRenderableComponent();
|
|
}
|
|
|
|
void HydraPlanetQuadActor::DeferredInitialiseComponents()
|
|
{
|
|
}
|
|
|
|
void HydraPlanetQuadActor::InitialiseQuad(float planet_diameter, float quad_dimension, uint32_t divisions, glm::vec3 origin)
|
|
{
|
|
m_divisions = divisions;
|
|
m_planet_diameter = planet_diameter;
|
|
m_origin = origin;
|
|
m_quad_dimension = quad_dimension;
|
|
_CreateMesh();
|
|
}
|
|
|
|
void HydraPlanetQuadActor::_CreatePosition()
|
|
{
|
|
HydraECS *ecs = GetEngine()->ECS();
|
|
HydraPositionComponent position_component;
|
|
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());
|
|
|
|
uint32_t vert_counts = m_divisions * m_divisions;
|
|
uint32_t index_counts = ((m_divisions - 1) * (m_divisions -1)) * 6;
|
|
mesh->vertexes.resize(vert_counts);
|
|
mesh->indexes.resize(index_counts);
|
|
|
|
float min_x = -m_quad_dimension / 2.0f;
|
|
float min_y = -m_quad_dimension / 2.0f;
|
|
float min_z = 0.0f;
|
|
|
|
float max_x = m_quad_dimension / 2.0f;
|
|
float max_y = m_quad_dimension / 2.0f;
|
|
float max_z = 0.0f;
|
|
|
|
float min_u = 0;
|
|
float min_v = 1;
|
|
|
|
float max_u = 1;
|
|
float max_v = 0;
|
|
|
|
float xy_inc = m_quad_dimension / m_divisions;
|
|
float u_inc = max_u / (m_divisions-1);
|
|
float v_inc = -min_v / (m_divisions-1);
|
|
|
|
// 1---2
|
|
// | / |
|
|
// 0---3
|
|
HydraID actor_id = GetID();
|
|
|
|
float y = min_y;
|
|
float x = min_x;
|
|
float u = min_u;
|
|
float v = min_v;
|
|
|
|
uint32_t vert_idx = 0;
|
|
uint32_t index_idx = 0;
|
|
|
|
for(uint32_t y_count = 0; y_count < m_divisions - 1; y_count ++)
|
|
{
|
|
for(uint32_t x_count = 0; x_count < m_divisions - 1; x_count ++)
|
|
{
|
|
//Create Vertex
|
|
mesh->vertexes[vert_idx] = {x, y, min_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;
|
|
}
|
|
|
|
//Add the end vert but no indexes
|
|
mesh->vertexes[vert_idx] = {x, y, min_z, u, v, actor_id, mat_comp.material_id, 0};
|
|
vert_idx++;
|
|
y += xy_inc;
|
|
v += v_inc;
|
|
x = min_x;
|
|
u = min_u;
|
|
|
|
}
|
|
//the last row
|
|
for(uint32_t x_count = 0; x_count < m_divisions; x_count ++)
|
|
{
|
|
mesh->vertexes[vert_idx] = {x, y, min_z, u, v, actor_id, mat_comp.material_id, 0};
|
|
x += xy_inc;
|
|
u += u_inc;
|
|
vert_idx++;
|
|
}
|
|
|
|
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);
|
|
material.emmissive_metalic_rough = glm::vec4(0, 0.5, 0.3, 1);
|
|
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);
|
|
}
|