Cleanup some GameObject access methods

This commit is contained in:
2024-06-24 08:20:11 -04:00
parent d326af84d5
commit 779253b5e4
11 changed files with 85 additions and 66 deletions

View File

@@ -109,7 +109,7 @@ namespace fgl::engine
auto viewer { GameObject::createGameObject() };
viewer.m_transform.translation = WorldCoordinate( constants::WORLD_CENTER + glm::vec3( 0.0f, 0.0f, 2.5f ) );
viewer.getTransform().translation = WorldCoordinate( constants::WORLD_CENTER + glm::vec3( 0.0f, 0.0f, 2.5f ) );
KeyboardMovementController camera_controller {};
@@ -169,7 +169,7 @@ namespace fgl::engine
delta_time,
command_buffer,
gui_command_buffer,
{ camera, viewer.m_transform },
{ camera, viewer.getTransform() },
global_descriptor_sets[ frame_index ],
m_game_objects_root,
m_renderer.getCurrentTracyCTX(),
@@ -281,20 +281,16 @@ namespace fgl::engine
{
ZoneScopedN( "Load phyiscs test" );
std::vector< std::shared_ptr< Model > > assets { Model::createModelsFromScene(
Device::getInstance(),
"assets/PhysicsTest.glb",
m_entity_renderer.getVertexBuffer(),
m_entity_renderer.getIndexBuffer() ) };
"assets/PhysicsTest.glb", m_entity_renderer.getVertexBuffer(), m_entity_renderer.getIndexBuffer() ) };
for ( auto& model : assets )
{
GameObject object { GameObject::createGameObject() };
object.m_model = std::move( model );
object.m_transform.translation = WorldCoordinate( 0.0f );
object.object_flags |= IS_VISIBLE | IS_ENTITY;
object.getModel() = std::move( model );
object.getTransform().translation = WorldCoordinate( 0.0f );
object.addFlag( IS_VISIBLE | IS_ENTITY );
assert( object.m_model );
object.m_model->stage( command_buffer );
object.getModel()->stage( command_buffer );
m_game_objects_root.addGameObject( std::move( object ) );
}

View File

@@ -19,7 +19,7 @@ namespace fgl::engine
void GameObject::drawImGui()
{
ImGui::InputText( "Name", &this->name );
ImGui::InputText( "Name", &( this->getName() ) );
// Transform - Position
WorldCoordinate& translation { this->m_transform.translation };
@@ -32,9 +32,7 @@ namespace fgl::engine
gui::dragFloat3( "Scale", scale );
for ( const GameObjectComponentBase* component : components )
{
}
{}
}
OrientedBoundingBox< CoordinateSpace::World > GameObject::getBoundingBox() const

View File

@@ -9,6 +9,7 @@
#include <vector>
#include "engine/primitives/TransformComponent.hpp"
#include "model/Model.hpp"
namespace fgl::engine
{
@@ -88,6 +89,8 @@ namespace fgl::engine
static constexpr GameObjectID INVALID_ID { std::numeric_limits< GameObjectID >::max() };
private:
GameObjectID m_id { INVALID_ID };
GameObjectFlagType object_flags { GameObjectFlagMask::MASK_DEFAULT };
TransformComponent m_transform {};
@@ -95,16 +98,20 @@ namespace fgl::engine
std::vector< GameObjectComponentBase* > components {};
std::shared_ptr< Model > m_model { nullptr };
std::string name { "Unnamed Game Object" };
std::string name {};
private:
GameObject( GameObjectID obj_id ) : m_id( obj_id ) {}
GameObject() = delete;
FGL_DELETE_DEFAULT_CTOR( GameObject );
FGL_DELETE_COPY( GameObject );
public:
GameObject& operator=( GameObject&& other ) = default;
GameObject( GameObject&& other ) = default;
template < typename T >
requires is_component< T >
bool hasComponent()
@@ -117,22 +124,48 @@ namespace fgl::engine
return false;
}
GameObject( const GameObject& other ) = delete;
GameObject& operator=( const GameObject& other ) = delete;
//Flags
GameObjectFlagType flags() const { return object_flags; }
GameObject( GameObject&& other ) = default;
GameObject& operator=( GameObject&& ) = default;
void addFlag( GameObjectFlagType flag ) { object_flags |= flag; }
inline const WorldCoordinate& getPosition() const { return m_transform.translation; }
void removeFlag( GameObjectFlagType flag ) { object_flags &= ( ~flag ); }
inline const Rotation& getRotation() const { return m_transform.rotation; }
//Model
bool hasModel() const { return m_model != nullptr; }
const std::shared_ptr< Model >& getModel() const { return m_model; }
std::shared_ptr< Model >& getModel() { return m_model; }
//Transform
TransformComponent& getTransform() { return m_transform; }
const TransformComponent& getTransform() const { return m_transform; }
const WorldCoordinate& getPosition() const { return m_transform.translation; }
const Rotation& getRotation() const { return m_transform.rotation; }
//Bounding Box
OrientedBoundingBox< CoordinateSpace::World > getBoundingBox() const;
//Misc
static GameObject createGameObject();
inline GameObjectID getId() const { return m_id; }
//! Returns the name of the game object. If no name is set then the name of the model is used.
inline std::string& getName()
{
if ( name.empty() && m_model )
{
name = m_model->getName();
}
return name;
}
void drawImGui();
};

View File

@@ -77,7 +77,7 @@ namespace fgl::engine
if ( cursor_enabled )
{
const auto& original_rotation { target.m_transform.rotation };
const auto& original_rotation { target.getTransform().rotation };
Rotation yaw_rotation {};
Rotation pitch_rotation {};
@@ -89,7 +89,7 @@ namespace fgl::engine
|| yaw_change < std::numeric_limits< float >::epsilon() )
yaw_rotation.yaw() += ( dt * yaw_change );
target.m_transform.rotation = yaw_rotation * original_rotation * pitch_rotation;
target.getTransform().rotation = yaw_rotation * original_rotation * pitch_rotation;
}
else // No cursor
{
@@ -97,19 +97,19 @@ namespace fgl::engine
const float xpos { pos.x };
const float ypos { pos.y };
Rotation target_rotation { target.m_transform.rotation };
Rotation target_rotation { target.getTransform().rotation };
target_rotation.yaw() += ( xpos * 0.006f ) * look_speed;
target_rotation.pitch() -= ( ypos * 0.006f ) * look_speed;
target.m_transform.rotation = target_rotation;
target.getTransform().rotation = target_rotation;
setCursorPos( window, { 0, 0 } );
}
const Vector forward_dir { target.m_transform.rotation.forward() };
const Vector up_dir { target.m_transform.rotation.up() };
const Vector right_dir { target.m_transform.rotation.right() };
const Vector forward_dir { target.getTransform().rotation.forward() };
const Vector up_dir { target.getTransform().rotation.up() };
const Vector right_dir { target.getTransform().rotation.right() };
Vector move_dir { 0.0f };
if ( glfwGetKey( window, key_mappings.move_forward ) == GLFW_PRESS ) move_dir += forward_dir;
@@ -122,7 +122,7 @@ namespace fgl::engine
const NormalVector n_move_dir { move_dir };
if ( glm::dot( move_dir.vec(), move_dir.vec() ) > std::numeric_limits< float >::epsilon() )
target.m_transform.translation += n_move_dir * ( move_speed * dt );
target.getTransform().translation += n_move_dir * ( move_speed * dt );
}
} // namespace fgl::engine

View File

@@ -249,9 +249,9 @@ namespace fgl::engine::gui
{
for ( GameObject& entity : *leaf )
{
ImGui::PushID( entity.m_id );
ImGui::PushID( entity.getId() );
if ( ImGui::Selectable( entity.name.c_str() ) )
if ( ImGui::Selectable( entity.getName().c_str() ) )
{
selected_object = &entity;
}

View File

@@ -68,16 +68,12 @@ namespace fgl::engine
return draw_commands;
}
Model::Model(
Device& device, ModelBuilder& builder, const OrientedBoundingBox< CoordinateSpace::Model > bounding_box ) :
Model( device, std::move( builder.m_primitives ), bounding_box )
Model::Model( ModelBuilder& builder, const OrientedBoundingBox< CoordinateSpace::Model > bounding_box ) :
Model( std::move( builder.m_primitives ), bounding_box )
{}
Model::Model(
Device& device,
std::vector< Primitive >&& primitives,
const OrientedBoundingBox< CoordinateSpace::Model > bounding_box ) :
m_device( device ),
std::vector< Primitive >&& primitives, const OrientedBoundingBox< CoordinateSpace::Model > bounding_box ) :
m_draw_parameters( buildParameters( primitives ) ),
m_bounding_box( bounding_box )
{
@@ -86,7 +82,7 @@ namespace fgl::engine
}
std::shared_ptr< Model > Model::
createModel( Device& device, const std::filesystem::path& path, Buffer& vertex_buffer, Buffer& index_buffer )
createModel( const std::filesystem::path& path, Buffer& vertex_buffer, Buffer& index_buffer )
{
ZoneScoped;
std::cout << "Creating model: " << path << std::endl;
@@ -96,14 +92,14 @@ namespace fgl::engine
//Calculate bounding box
OrientedBoundingBox bounding_box { buildBoundingBox( builder.m_primitives ) };
auto model_ptr { std::make_shared< Model >( device, builder, bounding_box ) };
auto model_ptr { std::make_shared< Model >( builder, bounding_box ) };
std::cout << "Finished making model: " << path << std::endl;
return model_ptr;
}
std::vector< std::shared_ptr< Model > > Model::createModelsFromScene(
Device& device, const std::filesystem::path& path, Buffer& vertex_buffer, Buffer& index_buffer )
std::vector< std::shared_ptr< Model > > Model::
createModelsFromScene( const std::filesystem::path& path, Buffer& vertex_buffer, Buffer& index_buffer )
{
ZoneScoped;
std::cout << "Loading scene: " << path << std::endl;
@@ -116,7 +112,6 @@ namespace fgl::engine
}
std::shared_ptr< Model > Model::createModelFromVerts(
Device& device,
std::vector< Vertex > verts,
std::vector< std::uint32_t > indicies,
Buffer& vertex_buffer,
@@ -128,7 +123,7 @@ namespace fgl::engine
OrientedBoundingBox bounding_box { buildBoundingBox( builder.m_primitives ) };
auto model_ptr { std::make_shared< Model >( device, builder, bounding_box ) };
auto model_ptr { std::make_shared< Model >( builder, bounding_box ) };
return model_ptr;
}

View File

@@ -49,18 +49,17 @@ namespace fgl::engine
std::vector< vk::DrawIndexedIndirectCommand > getDrawCommand( const std::uint32_t index ) const;
//TODO: Switch to using shared_ptr instead of unique_ptr
static std::shared_ptr< Model > createModel(
Device& device, const std::filesystem::path& path, Buffer& vertex_buffer, Buffer& index_buffer );
static std::shared_ptr< Model >
createModel( const std::filesystem::path& path, Buffer& vertex_buffer, Buffer& index_buffer );
static std::shared_ptr< Model > createModelFromVerts(
Device& device,
std::vector< Vertex > verts,
std::vector< std::uint32_t > indicies,
Buffer& vertex_buffer,
Buffer& index_buffer );
static std::vector< std::shared_ptr< Model > > createModelsFromScene(
Device& device, const std::filesystem::path& path, Buffer& vertex_buffer, Buffer& index_buffer );
static std::vector< std::shared_ptr< Model > >
createModelsFromScene( const std::filesystem::path& path, Buffer& vertex_buffer, Buffer& index_buffer );
void stage( vk::raii::CommandBuffer& cmd_buffer );
@@ -68,13 +67,10 @@ namespace fgl::engine
void setName( std::string str ) { m_name = str; }
Model(
Device& device, ModelBuilder& builder, const OrientedBoundingBox< CoordinateSpace::Model > bounding_box );
Model( ModelBuilder& builder, const OrientedBoundingBox< CoordinateSpace::Model > bounding_box );
Model(
Device& device,
std::vector< Primitive >&& primitives,
const OrientedBoundingBox< CoordinateSpace::Model > bounding_box );
std::vector< Primitive >&& primitives, const OrientedBoundingBox< CoordinateSpace::Model > bounding_box );
~Model() = default;

View File

@@ -329,7 +329,7 @@ namespace fgl::engine
const auto bounding_box { createModelBoundingBox( finished_primitives ) };
return std::make_shared< Model >( Device::getInstance(), std::move( finished_primitives ), bounding_box );
return std::make_shared< Model >( std::move( finished_primitives ), bounding_box );
}
void SceneBuilder::handleNode( const int node_idx, const tinygltf::Model& root )

View File

@@ -28,8 +28,9 @@ namespace fgl::engine
std::vector< std::uint32_t > indicies { 0, 1, 2, 3 };
std::shared_ptr< Model > model { Model::createModelFromVerts(
Device::getInstance(), std::move( verts ), std::move( indicies ), vertex_buffer, index_buffer ) };
std::shared_ptr< Model > model {
Model::createModelFromVerts( std::move( verts ), std::move( indicies ), vertex_buffer, index_buffer )
};
return model;
}

View File

@@ -31,16 +31,16 @@ namespace fgl::engine
{
ZoneScopedN( "Process object" );
if ( !( ( obj.object_flags & flags ) == flags ) ) continue;
if ( !( ( obj.flags() & flags ) == flags ) ) continue;
assert( obj.m_model );
assert( obj.hasModel() );
// debug::world::drawBoundingBox( obj.getBoundingBox() );
for ( const Primitive& primitive : obj.m_model->m_primitives )
for ( const Primitive& primitive : obj.getModel()->m_primitives )
{
//assert( primitive.m_texture );
const ModelMatrixInfo matrix_info { .model_matrix = obj.m_transform.mat4(),
const ModelMatrixInfo matrix_info { .model_matrix = obj.getTransform().mat4(),
.texture_idx = primitive.getTextureID() };
// If the textureless flag is on and we have a texture then skip the primitive.c

View File

@@ -175,7 +175,7 @@ namespace fgl::engine
for ( GameObject& obj : game_objects )
{
const auto& obj_coordinate { obj.m_transform.translation };
const auto& obj_coordinate { obj.getTransform().translation };
const bool is_right { obj_coordinate.x > center.x };
const bool is_forward { obj_coordinate.y > center.y };
const bool is_up { obj_coordinate.z > center.z };
@@ -251,7 +251,7 @@ namespace fgl::engine
if ( std::find_if(
game_objects.begin(),
game_objects.end(),
[ id ]( const GameObject& obj ) { return obj.m_id == id; } )
[ id ]( const GameObject& obj ) { return obj.getId() == id; } )
!= game_objects.end() )
{
return this;
@@ -293,7 +293,7 @@ namespace fgl::engine
bool OctTreeNode::canContain( const GameObject& obj )
{
return m_bounds.contains( obj.m_transform.translation );
return m_bounds.contains( obj.getTransform().translation );
}
GameObject OctTreeNode::extract( const GameObject::GameObjectID id )