461 lines
16 KiB
C++
461 lines
16 KiB
C++
#include "HydraPlanetQuadActor.h"
|
|
#include "../../engine/HydraEngine.h"
|
|
#include "../../actor/HydraActorManager.h"
|
|
#include "../../actor/HydraTestCubeActor.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::Destroy()
|
|
{
|
|
}
|
|
|
|
void HydraPlanetQuadActor::InitialiseQuad(HydraID parent_id, 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;
|
|
m_parent_id = parent_id;
|
|
// GetEngine()->ActorManager()->SetPositionAndOrientation(GetID(), origin, orientation, true);
|
|
_CreateMesh();
|
|
}
|
|
|
|
void HydraPlanetQuadActor::CheckResolution(glm::dvec3 player_position)
|
|
{
|
|
HydraMeshComponent mesh_comp = GetEngine()->ECS()->GetComponent<HydraMeshComponent>(GetID());
|
|
if (mesh_comp.mesh_state == HYDRA_STATE::Ready)
|
|
{
|
|
bool should_split = false;
|
|
bool should_merge = 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) && (m_quad_dimension > 64))
|
|
{
|
|
should_split = true;
|
|
}
|
|
if (distance_to_quad > m_quad_dimension * 1.1f)
|
|
{
|
|
should_merge = 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_merge) && (m_divided)) // Is split but should not be
|
|
{
|
|
for (HydraID child_id : m_child_quads)
|
|
{
|
|
HydraPlanetQuadActor *child = static_cast<HydraPlanetQuadActor *>(GetEngine()->ActorManager()->GetActor(child_id));
|
|
child->CheckResolution(player_position);
|
|
GetEngine()->ActorManager()->DeleteActor(child_id);
|
|
}
|
|
m_child_quads.clear();
|
|
m_divided = false;
|
|
HydraRenderableComponent &renderable = GetEngine()->ECS()->GetComponent<HydraRenderableComponent>(GetID());
|
|
renderable.active = true;
|
|
}
|
|
}
|
|
}
|
|
|
|
HydraPlanetQuadActor::~HydraPlanetQuadActor()
|
|
{
|
|
if (m_center_actor != INVALID_HYDRA_ID)
|
|
{
|
|
GetEngine()->ActorManager()->DeleteActor(m_center_actor);
|
|
}
|
|
}
|
|
|
|
void HydraPlanetQuadActor::_MergeQuads()
|
|
{
|
|
}
|
|
|
|
glm::vec3 HydraPlanetQuadActor::_ProjectSphere(glm::dvec4 vert_in, glm::dmat4 trans, glm::dmat4 inv_trans, double diameter, glm::vec3 origin)
|
|
{
|
|
|
|
return glm::dvec3(vert_in);
|
|
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(15);
|
|
fnl.SetFractalLacunarity(2.1);
|
|
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::vec3 vert_in, glm::dmat4 trans, double diameter)
|
|
{
|
|
|
|
return glm::dvec3(vert_in);
|
|
glm::dvec4 vert = trans * glm::dvec4(vert_in, 1);
|
|
|
|
vert = glm::normalize(vert);
|
|
vert = vert * (m_planet_diameter / 2.0);
|
|
|
|
// vert_in = glm::normalize(vert_in);
|
|
// vert_in = vert_in * (m_planet_diameter / 2.0f);
|
|
FastNoiseLite fnl;
|
|
fnl.SetNoiseType(FastNoiseLite::NoiseType::NoiseType_OpenSimplex2);
|
|
fnl.SetFractalType(FastNoiseLite::FractalType::FractalType_Ridged);
|
|
fnl.SetSeed(341411);
|
|
fnl.SetFrequency(0.00009);
|
|
fnl.SetFractalOctaves(15);
|
|
fnl.SetFractalLacunarity(2.1);
|
|
|
|
float noise = fnl.GetNoise(vert.x, vert.y, vert.z);
|
|
|
|
vert = glm::normalize(vert) * ((m_planet_diameter / 2.0) + (noise * 500.0f));
|
|
return glm::dvec3(vert);
|
|
}
|
|
|
|
void HydraPlanetQuadActor::_Split()
|
|
{
|
|
m_divided = true;
|
|
float child_quad_size = m_quad_dimension / 2.0f;
|
|
float child_half_quad_size = child_quad_size / 2.0f;
|
|
float radius = m_planet_diameter / 2.0f;
|
|
|
|
// 2 | 3
|
|
//-------
|
|
// 0 | 1
|
|
|
|
std::vector<glm::vec3> origins;
|
|
|
|
origins.push_back(glm::vec3(m_origin.x - child_half_quad_size, m_origin.y - child_half_quad_size, m_origin.z));
|
|
origins.push_back(glm::vec3(m_origin.x + child_half_quad_size, m_origin.y - child_half_quad_size, m_origin.z));
|
|
origins.push_back(glm::vec3(m_origin.x - child_half_quad_size, m_origin.y + child_half_quad_size, m_origin.z));
|
|
origins.push_back(glm::vec3(m_origin.x + child_half_quad_size, m_origin.y + child_half_quad_size, m_origin.z));
|
|
|
|
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));
|
|
GetEngine()->ActorManager()->InitialiseActor(quad_id);
|
|
quad->InitialiseQuad(GetID(), m_planet_diameter, child_quad_size, m_divisions, origins[i], m_orientation);
|
|
|
|
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 = 4;
|
|
uint32_t index_counts = 4;
|
|
|
|
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 = 0;
|
|
|
|
float max_u = 1;
|
|
float max_v = 1;
|
|
|
|
// 1---2
|
|
// | / |
|
|
// 0---3
|
|
|
|
HydraID actor_id = GetID();
|
|
|
|
glm::dvec4 quad_direction = glm::toMat4(glm::quat(m_orientation)) * glm::dvec4(0, 0, 1, 1);
|
|
glm::dvec4 transformed_origin = glm::toMat4(glm::quat(m_orientation)) * glm::dvec4(m_origin, 1);
|
|
glm::dvec4 quad_position = transformed_origin + (quad_direction * (m_planet_diameter / 2.0));
|
|
|
|
glm::dmat4 trans_matrix = glm::translate(glm::dvec3(quad_position));
|
|
glm::dmat4 rot_matrix = glm::toMat4(glm::quat(m_orientation));
|
|
glm::dmat4 trans = trans_matrix * rot_matrix;
|
|
glm::dmat4 inv_trans = glm::inverse(trans);
|
|
|
|
GetEngine()->ActorManager()->SetPositionAndOrientation(GetID(), quad_position, m_orientation, true);
|
|
|
|
m_centre = _ProjectCentre(glm::vec3(0, 0, 1), trans, m_planet_diameter);
|
|
|
|
// Create Vertex
|
|
glm::dvec4 vert = glm::dvec4(min_x, min_y, min_z, 1.0);
|
|
glm::vec3 final_vert = _ProjectSphere(vert, trans, inv_trans, m_planet_diameter, m_origin);
|
|
mesh->vertexes[0] = {final_vert.x, final_vert.y, final_vert.z, min_u, min_v, actor_id, mat_comp.material_id, 0};
|
|
|
|
vert = glm::dvec4(min_x, max_y, min_z, 1.0);
|
|
final_vert = _ProjectSphere(vert, trans, inv_trans, m_planet_diameter, m_origin);
|
|
mesh->vertexes[1] = {final_vert.x, final_vert.y, final_vert.z, min_u, max_v, actor_id, mat_comp.material_id, 0};
|
|
|
|
vert = glm::dvec4(max_x, max_y, min_z, 1.0);
|
|
final_vert = _ProjectSphere(vert, trans, inv_trans, m_planet_diameter, m_origin);
|
|
mesh->vertexes[2] = {final_vert.x, final_vert.y, final_vert.z, max_u, max_v, actor_id, mat_comp.material_id, 0};
|
|
|
|
vert = glm::dvec4(max_x, min_y, min_z, 1.0);
|
|
final_vert = _ProjectSphere(vert, trans, inv_trans, m_planet_diameter, m_origin);
|
|
mesh->vertexes[3] = {final_vert.x, final_vert.y, final_vert.z, max_u, min_v, actor_id, mat_comp.material_id, 0};
|
|
|
|
mesh->indexes[0] = 0;
|
|
mesh->indexes[1] = 3;
|
|
mesh->indexes[2] = 1;
|
|
mesh->indexes[3] = 2;
|
|
|
|
|
|
//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);
|
|
HydraRenderableComponent &render_comp = GetEngine()->ECS()->GetComponent<HydraRenderableComponent>(GetID());
|
|
render_comp.active = true;
|
|
}
|
|
// 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 = -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 * (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::dvec4 quad_direction = glm::toMat4(glm::quat(m_orientation)) * glm::dvec4(0,0,1,1);
|
|
// glm::dvec4 transformed_origin = glm::toMat4(glm::quat(m_orientation)) * glm::dvec4(m_origin, 1);
|
|
// glm::dvec4 quad_position = transformed_origin + (quad_direction * (m_planet_diameter /2.0));
|
|
|
|
// glm::dmat4 trans_matrix = glm::translate(glm::dvec3(quad_position));
|
|
// glm::dmat4 rot_matrix = glm::toMat4(glm::quat(m_orientation));
|
|
// glm::dmat4 trans = trans_matrix * rot_matrix;
|
|
// glm::dmat4 inv_trans = glm::inverse(trans);
|
|
|
|
// GetEngine()->ActorManager()->SetPositionAndOrientation(GetID(), quad_position, m_orientation, true);
|
|
|
|
// m_centre = _ProjectCentre(glm::vec3(0,0,1), trans, m_planet_diameter);
|
|
|
|
// // m_center_actor = GetEngine()->ActorManager()->CreateActor<HydraTestCubeActor>();
|
|
// // HydraTestCubeActor* center_actor = static_cast<HydraTestCubeActor*>(GetEngine()->ActorManager()->GetActor(m_center_actor));
|
|
// // center_actor->SetScale(1);
|
|
// // GetEngine()->ActorManager()->InitialiseActor(m_center_actor);
|
|
// // GetEngine()->ActorManager()->SetPosition(m_center_actor, m_centre);
|
|
|
|
// 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);
|
|
// HydraRenderableComponent& render_comp = GetEngine()->ECS()->GetComponent<HydraRenderableComponent>(GetID());
|
|
// render_comp.active = true;
|
|
// }
|
|
|
|
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_LANDSCAPE, 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);
|
|
render_comp.active = false;
|
|
}
|