324 lines
11 KiB
C++
324 lines
11 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 "../../ecs/components/HydraShadowCasterComponent.h"
|
|
#include "../../buffer/HydraMaterialBufferManager.h"
|
|
#include "../../buffer/HydraTextureBufferManager.h"
|
|
#include "../../buffer/HydraMaterialBuffer.h"
|
|
#include "../../mesh/HydraMeshManager.h"
|
|
#include "../../mesh/HydraMesh.h"
|
|
|
|
#include <FastNoiseLite.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::dvec3 origin, glm::vec3 orientation)
|
|
{
|
|
m_divisions = divisions;
|
|
m_planet_diameter = planet_diameter;
|
|
m_origin = origin;
|
|
m_quad_dimension = quad_dimension;
|
|
m_orientation = orientation;
|
|
|
|
GetEngine()->ActorManager()->SetPositionAndOrientation(GetID(), origin, orientation, true);
|
|
_CreateMesh();
|
|
}
|
|
|
|
void HydraPlanetQuadActor::CheckResolution(glm::dvec3 player_position)
|
|
{
|
|
bool should_split = false;
|
|
double distance_to_quad = glm::length(player_position - m_centre);
|
|
//might need to bias this so it splits sooner and merges later
|
|
if(distance_to_quad < m_quad_dimension)
|
|
{
|
|
should_split = true;
|
|
}
|
|
//Not split but should be
|
|
if((should_split) && (!m_divided))
|
|
{
|
|
//Split this quad
|
|
_Split();
|
|
//Check its children to see if they should be split
|
|
//std::vector<HydraID> children = GetEngine()->ActorManager()->GetChildren(GetID());
|
|
for(HydraID child_id : m_child_quads)
|
|
{
|
|
HydraPlanetQuadActor* child = static_cast<HydraPlanetQuadActor*>(GetEngine()->ActorManager()->GetActor(child_id));
|
|
child->CheckResolution(player_position);
|
|
}
|
|
HydraRenderableComponent& renderable = GetEngine()->ECS()->GetComponent<HydraRenderableComponent>(GetID());
|
|
renderable.active = false;
|
|
}
|
|
else if((should_split) && (m_divided))
|
|
{
|
|
for(HydraID child_id : m_child_quads)
|
|
{
|
|
HydraPlanetQuadActor* child = static_cast<HydraPlanetQuadActor*>(GetEngine()->ActorManager()->GetActor(child_id));
|
|
child->CheckResolution(player_position);
|
|
}
|
|
}
|
|
else if ((!should_split) && (m_divided)) //Is split but should not be
|
|
{
|
|
//Merge this quad and all its children
|
|
}
|
|
}
|
|
|
|
|
|
|
|
glm::vec3 HydraPlanetQuadActor::_ProjectSphere(glm::dvec4 vert_in, glm::dmat4 trans, glm::dmat4 inv_trans, double diameter, glm::vec3 origin)
|
|
{
|
|
|
|
glm::dvec4 vert = trans * vert_in;
|
|
vert = glm::normalize(vert);
|
|
vert = vert * (m_planet_diameter / 2.0);
|
|
FastNoiseLite fnl;
|
|
fnl.SetNoiseType(FastNoiseLite::NoiseType::NoiseType_OpenSimplex2);
|
|
fnl.SetFractalType(FastNoiseLite::FractalType::FractalType_Ridged);
|
|
fnl.SetSeed(341411);
|
|
fnl.SetFrequency(0.00009);
|
|
fnl.SetFractalOctaves(8);
|
|
|
|
float noise = fnl.GetNoise(vert.x, vert.y, vert.z);
|
|
|
|
vert = glm::normalize(vert) * ((m_planet_diameter / 2.0) + (noise * 500.0f));
|
|
vert = inv_trans * glm::dvec4(vert.x, vert.y, vert.z, 1.0);
|
|
return glm::vec3(vert);
|
|
}
|
|
|
|
glm::dvec3 HydraPlanetQuadActor::_ProjectCentre(glm::dvec4 vert_in, glm::dmat4 trans, double diameter, glm::vec3 origin)
|
|
{
|
|
glm::dvec4 vert = trans * vert_in;
|
|
vert = glm::normalize(vert);
|
|
vert = vert * (m_planet_diameter / 2.0);
|
|
|
|
return glm::dvec3(vert);
|
|
}
|
|
|
|
void HydraPlanetQuadActor::_Split()
|
|
{
|
|
m_divided = true;
|
|
float quad_size = m_quad_dimension / 2.0f;
|
|
float radius = m_planet_diameter / 2.0f;
|
|
|
|
//2 | 3
|
|
//-----
|
|
//0 | 1
|
|
|
|
std::vector<glm::vec3> origins;
|
|
|
|
origins.push_back(m_origin);
|
|
origins.push_back(m_origin + glm::vec3(quad_size, 0, 0));
|
|
origins.push_back(m_origin + glm::vec3(0, quad_size, 0));
|
|
origins.push_back(m_origin + glm::vec3(quad_size, quad_size, 0));
|
|
// glm::vec3 origin0 = m_origin;
|
|
// glm::vec3 origin1 = m_origin + glm::vec3(quad_size, 0, 0);
|
|
// glm::vec3 origin2 = m_origin + glm::vec3(0, quad_size, 0);
|
|
// glm::vec3 origin3 = m_origin;
|
|
|
|
for(uint32_t i = 0; i < 4; i ++)
|
|
{
|
|
HydraID quad_id = GetEngine()->ActorManager()->CreateActor<HydraPlanetQuadActor>();
|
|
HydraPlanetQuadActor* quad = static_cast<HydraPlanetQuadActor*>(GetEngine()->ActorManager()->GetActor(quad_id));
|
|
quad->InitialiseQuad(m_planet_diameter, quad_size, m_divisions, origins[i], m_orientation);
|
|
GetEngine()->ActorManager()->InitialiseActor(quad_id);
|
|
//GetEngine()->ActorManager()->SetRelationship(GetID(), quad_id);
|
|
m_child_quads.push_back(quad_id);
|
|
}
|
|
|
|
}
|
|
|
|
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());
|
|
HydraPositionComponent pos_comp = GetEngine()->ECS()->GetComponent<HydraPositionComponent>(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 = 0;//-m_quad_dimension / 2.0f;
|
|
float min_y = 0;//-m_quad_dimension / 2.0f;
|
|
float min_z = 0.0f;
|
|
|
|
float max_x = m_quad_dimension;
|
|
float max_y = m_quad_dimension;
|
|
float max_z = 0.0f;
|
|
|
|
float min_u = 0;
|
|
float min_v = 1 * (m_divisions-1);
|
|
|
|
float max_u = 1 * (m_divisions-1);
|
|
float max_v = 0;
|
|
|
|
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);
|
|
|
|
// 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;
|
|
//glm::mat4 trans = GetEngine()->ActorManager()->GetActorPose(actor_id);
|
|
|
|
glm::dmat4 trans_matrix = glm::translate(pos_comp.position);
|
|
glm::dmat4 rot_matrix = glm::toMat4(glm::quat(pos_comp.orientation));
|
|
glm::dmat4 trans = trans_matrix * rot_matrix;
|
|
glm::dmat4 inv_trans = glm::inverse(trans);
|
|
|
|
glm::dvec4 centre = glm::dvec4(max_x - min_x, max_y - min_y, max_z - min_z, 1.0);
|
|
|
|
m_centre = _ProjectCentre(centre, trans, m_planet_diameter, glm::vec3(max_x, max_y, max_z) - glm::vec3(min_x, min_y, min_z));
|
|
|
|
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
|
|
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;
|
|
}
|
|
|
|
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};
|
|
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++)
|
|
{
|
|
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};
|
|
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;
|
|
HydraShadowCasterComponent shad_caster;
|
|
HydraECS *ecs = GetEngine()->ECS();
|
|
render_comp.renderable_types.set((uint32_t)HYDRA_RENDERABLE_TYPE::HYDRA_COLOUR, true);
|
|
render_comp.renderable_types.set((uint32_t)HYDRA_RENDERABLE_TYPE::HYDRA_SHADOW_CASTER, true);
|
|
ecs->AddComponent<HydraRenderableComponent>(GetID(), render_comp);
|
|
ecs->AddComponent<HydraShadowCasterComponent>(GetID(), shad_caster);
|
|
}
|
|
|