Frustum culling cleanup

This commit is contained in:
2024-02-22 22:07:28 -05:00
parent 48362761aa
commit 5a476e0fe2
6 changed files with 86 additions and 37 deletions

View File

@@ -476,7 +476,7 @@ namespace fgl::engine
{
auto sponza = GameObject::createGameObject();
sponza.model = model;
sponza.transform.translation = { 0.0f + ( y * 30 ), 1.0f, 0.0f + ( x * 20 ) };
sponza.transform.translation = { 0.0f + ( y * 30 ), 0.0f + ( x * 20 ), 0.0f };
sponza.transform.scale = { 0.007f, 0.007f, 0.007f };
sponza.transform.rotation = { 0.0f, 0.0f, 0.0f };

View File

@@ -27,8 +27,6 @@ namespace fgl::engine
void KeyboardMovementController::moveInPlaneXZ( GLFWwindow* window, float dt, fgl::engine::GameObject& target )
{
ImGui::Begin( "CameraMovement" );
Rotation rotate { 0.0f };
if ( glfwGetKey( window, key_mappings.look_right ) == GLFW_PRESS ) rotate.yaw() += 1.f;
@@ -108,13 +106,6 @@ namespace fgl::engine
if ( glm::dot( move_dir, move_dir ) > std::numeric_limits< float >::epsilon() )
target.transform.translation += ( move_speed * dt ) * glm::normalize( move_dir );
ImGui::Text( "Transform" );
ImGui::InputFloat( "X", &target.transform.translation.x, -10.0, 10.0 );
ImGui::InputFloat( "Y", &target.transform.translation.y, -10.0, 10.0 );
ImGui::InputFloat( "Z", &target.transform.translation.z, -10.0, 10.0 );
ImGui::End();
}
} // namespace fgl::engine

View File

@@ -10,6 +10,8 @@
#include "engine/primitives/Vector.hpp"
#include "tracy_colors.hpp"
#ifndef NDEBUG
namespace fgl::engine::debug
{
const ImVec2 windowSize()
@@ -253,3 +255,5 @@ namespace fgl::engine::debug
} // namespace screen
} // namespace fgl::engine::debug
#endif

View File

@@ -26,6 +26,7 @@ namespace fgl::engine
} // namespace fgl::engine
#ifndef NDEBUG
namespace fgl::engine::debug
{
@@ -100,4 +101,64 @@ namespace fgl::engine::debug
} // namespace screen
} // namespace fgl::engine::debug
} // namespace fgl::engine::debug
#else
namespace fgl::engine::debug
{
namespace world
{
//Dummy functions
inline void drawBoundingBox( const BoundingBox< CoordinateSpace::World >&, Camera&, const glm::vec3 )
{}
inline void drawBoundingBox( const BoundingBox< CoordinateSpace::World >&, Camera& )
{}
inline void drawLine(
const Coordinate< CoordinateSpace::World >,
const Coordinate< CoordinateSpace::World >,
const Camera&,
const glm::vec3 )
{}
inline void drawPointLabel( const Coordinate< CoordinateSpace::World >, const std::string, const Camera& )
{}
inline void drawLine( const Line< CoordinateSpace::World >, const Camera&, const glm::vec3 )
{}
inline void drawPointText( const Coordinate< CoordinateSpace::World >, const Camera&, const glm::vec3 )
{}
inline void
drawBoolAlpha( const Coordinate< CoordinateSpace::World >, const Camera&, const bool, const glm::vec2 )
{}
inline void
drawPoint( const Coordinate< CoordinateSpace::World >, const Camera&, const std::string, const glm::vec3 )
{}
inline void drawVector(
const Coordinate< CoordinateSpace::World >, Vector, const Camera&, const std::string, const glm::vec3 )
{}
inline void drawFrustum( const Frustum< CoordinateSpace::World >&, const Camera&, const WorldCoordinate )
{}
inline void drawFrustum( const Camera& )
{}
inline void drawPlane(
const Plane< CoordinateSpace::World >&,
const WorldCoordinate,
const Camera&,
const std::string,
const glm::vec3 )
{}
} // namespace world
} // namespace fgl::engine::debug
#endif

View File

@@ -7,6 +7,7 @@
#include <glm/mat4x4.hpp>
#include <tuple>
#include <utility>
#include "engine/primitives/Rotation.hpp"

View File

@@ -99,10 +99,10 @@ namespace fgl::engine
obj.model->getBoundingBox( Matrix< MatrixType::ModelToWorld >( obj.transform.mat4() ) )
};
debug::world::drawBoundingBox( model_bounding_box, info.camera );
if ( !model_bounding_box.isInFrustum( info.camera_frustum ) ) continue;
debug::world::drawBoundingBox( model_bounding_box, info.camera );
for ( const auto& primitive : obj.model->m_primitives )
{
ZoneScopedN( "Queue Primitive" );
@@ -130,29 +130,6 @@ namespace fgl::engine
//Draw command for this mesh already exists. Simply add a count to it
auto [ existing_cmd, model_matrix ] = *itter;
//Sort each model matrix by distance from camera. Render closest first
const auto camera_pos { info.camera.getPosition() };
{
ZoneScopedN( "Sort model matricies by distance" );
std::sort(
model_matrix.begin(),
model_matrix.end(),
[ camera_pos ]( const ModelMatrixInfo& first, const ModelMatrixInfo& second ) -> bool
{
const auto& first_pos_v4 { first.model_matrix[ 3 ] };
const auto& second_pos_v4 { second.model_matrix[ 3 ] };
const glm::vec3 first_pos { first_pos_v4.x, first_pos_v4.y, first_pos_v4.z };
const glm::vec3 second_pos { second_pos_v4.x, second_pos_v4.y, second_pos_v4.z };
const auto first_distance { glm::distance( first_pos, camera_pos ) };
const auto second_distance { glm::distance( second_pos, camera_pos ) };
return first_distance < second_distance;
} );
}
draw_pairs.erase( itter );
existing_cmd.instanceCount++;
model_matrix.emplace_back( matrix_info );
@@ -176,12 +153,27 @@ namespace fgl::engine
std::vector< vk::DrawIndexedIndirectCommand > draw_commands;
std::vector< ModelMatrixInfo > model_matrices;
const auto camera_pos { info.camera.getPosition() };
auto sortFunc = [ camera_pos ]( const ModelMatrixInfo first, const ModelMatrixInfo second ) -> bool
{
const glm::vec3 first_pos { first.model_matrix[ 3 ] };
const float first_distance { glm::distance( first_pos, camera_pos ) };
const glm::vec3 second_pos { second.model_matrix[ 3 ] };
const float second_distance { glm::distance( second_pos, camera_pos ) };
return first_distance < second_distance;
};
TracyCZoneN( filter_zone_TRACY, "Reorganize draw commands", true );
for ( auto& itter : draw_pairs )
{
auto cmd { itter.first };
cmd.firstInstance = model_matrices.size();
auto& matricies { itter.second };
auto matricies { std::move( itter.second ) };
std::sort( matricies.begin(), matricies.end(), sortFunc );
draw_commands.emplace_back( cmd );
model_matrices.insert( model_matrices.end(), matricies.begin(), matricies.end() );