More work on frustum math
This commit is contained in:
@@ -141,7 +141,7 @@ namespace fgl::engine
|
||||
throw std::runtime_error( "Unimplemented view mode" );
|
||||
}
|
||||
|
||||
//frustum = base_frustum * view_matrix;
|
||||
frustum = frustumTranslationMatrix() * base_frustum;
|
||||
|
||||
return;
|
||||
}
|
||||
@@ -173,4 +173,18 @@ namespace fgl::engine
|
||||
return { near_plane, far_plane, top_plane, bottom_plane, right_plane, left_plane };
|
||||
}
|
||||
|
||||
const Matrix< MatrixType::ModelToWorld > Camera::frustumTranslationMatrix() const
|
||||
{
|
||||
glm::mat4 translation { 1.0f };
|
||||
|
||||
translation[ 3 ] = glm::vec4( getPosition(), 1.0f );
|
||||
|
||||
//Apply rotation
|
||||
translation[ 0 ] = glm::vec4( getRight(), 0.0f );
|
||||
translation[ 1 ] = glm::vec4( getUp(), 0.0f );
|
||||
translation[ 2 ] = glm::vec4( getForward(), 0.0f );
|
||||
|
||||
return Matrix< MatrixType::ModelToWorld >( translation );
|
||||
}
|
||||
|
||||
} // namespace fgl::engine
|
||||
@@ -13,8 +13,6 @@
|
||||
|
||||
namespace fgl::engine
|
||||
{
|
||||
constexpr static auto WORLD_UP { glm::vec3 { 0.0f, 1.0f, 0.0f } };
|
||||
|
||||
class Camera;
|
||||
|
||||
Frustum< CoordinateSpace::Model >
|
||||
@@ -34,6 +32,8 @@ namespace fgl::engine
|
||||
friend Frustum< CoordinateSpace::Model > createFrustum(
|
||||
const Camera& camera, const float aspect, const float fovy, const float near, const float far );
|
||||
|
||||
const Matrix< MatrixType::ModelToWorld > frustumTranslationMatrix() const;
|
||||
|
||||
public:
|
||||
|
||||
const Frustum< CoordinateSpace::Model >& getBaseFrustum() const { return base_frustum; }
|
||||
@@ -53,28 +53,31 @@ namespace fgl::engine
|
||||
void setOrthographicProjection( float left, float right, float top, float bottom, float near, float far );
|
||||
void setPerspectiveProjection( float fovy, float aspect, float near, float far );
|
||||
|
||||
const glm::vec3 getPosition() const { return glm::vec3( -view_matrix[ 3 ] ); }
|
||||
const Coordinate< CoordinateSpace::World > getPosition() const { return WorldCoordinate( -view_matrix[ 3 ] ); }
|
||||
|
||||
const glm::vec3 getUp() const
|
||||
const Vector getUp() const
|
||||
{
|
||||
return glm::normalize( glm::vec3( view_matrix[ 0 ][ 1 ], view_matrix[ 1 ][ 1 ], view_matrix[ 2 ][ 1 ] ) );
|
||||
return Vector(
|
||||
glm::normalize( glm::vec3( view_matrix[ 0 ][ 1 ], view_matrix[ 1 ][ 1 ], view_matrix[ 2 ][ 1 ] ) ) );
|
||||
}
|
||||
|
||||
const glm::vec3 getRight() const
|
||||
const Vector getRight() const
|
||||
{
|
||||
return glm::normalize( glm::vec3( view_matrix[ 0 ][ 0 ], view_matrix[ 1 ][ 0 ], view_matrix[ 2 ][ 0 ] ) );
|
||||
return Vector(
|
||||
glm::normalize( glm::vec3( view_matrix[ 0 ][ 0 ], view_matrix[ 1 ][ 0 ], view_matrix[ 2 ][ 0 ] ) ) );
|
||||
}
|
||||
|
||||
const glm::vec3 getForward() const
|
||||
const Vector getForward() const
|
||||
{
|
||||
return glm::normalize( glm::vec3( view_matrix[ 0 ][ 2 ], view_matrix[ 1 ][ 2 ], view_matrix[ 2 ][ 2 ] ) );
|
||||
return Vector(
|
||||
glm::normalize( glm::vec3( view_matrix[ 0 ][ 2 ], view_matrix[ 1 ][ 2 ], view_matrix[ 2 ][ 2 ] ) ) );
|
||||
}
|
||||
|
||||
const glm::vec3 getLeft() const { return -getRight(); }
|
||||
const Vector getLeft() const { return -getRight(); }
|
||||
|
||||
const glm::vec3 getBackward() const { return -getForward(); }
|
||||
const Vector getBackward() const { return -getForward(); }
|
||||
|
||||
const glm::vec3 getDown() const { return -getUp(); }
|
||||
const Vector getDown() const { return -getUp(); }
|
||||
|
||||
void setViewDirection( glm::vec3 pos, glm::vec3 direction, glm::vec3 up = constants::WORLD_UP );
|
||||
void setViewTarget( glm::vec3 pos, glm::vec3 target, glm::vec3 up = constants::WORLD_UP );
|
||||
|
||||
@@ -237,7 +237,7 @@ namespace fgl::engine
|
||||
return { Coordinate< CType >( midpoint ), scale };
|
||||
}
|
||||
|
||||
//Instantiate the template
|
||||
//Synthesize the template
|
||||
template class BoundingBox< CoordinateSpace::Model >;
|
||||
template class BoundingBox< CoordinateSpace::World >;
|
||||
|
||||
|
||||
@@ -1,10 +0,0 @@
|
||||
//
|
||||
// Created by kj16609 on 2/12/24.
|
||||
//
|
||||
|
||||
#pragma once
|
||||
|
||||
namespace fgl::engine
|
||||
{
|
||||
|
||||
} // namespace fgl::engine
|
||||
@@ -55,35 +55,28 @@ namespace fgl::engine
|
||||
assert( near_plane.direction() != far_plane.direction() );
|
||||
}
|
||||
|
||||
template < MatrixType MType >
|
||||
Frustum< EvolvedType< MType >() > operator*( Matrix< MType > matrix ) const
|
||||
{
|
||||
Frustum< EvolvedType< MType >() > result = *this;
|
||||
result.near = near * matrix;
|
||||
result.far = far * matrix;
|
||||
result.top = top * matrix;
|
||||
result.bottom = bottom * matrix;
|
||||
result.right = right * matrix;
|
||||
result.left = left * matrix;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool pointInside( const WorldCoordinate& coord ) const
|
||||
{
|
||||
// clang-format off
|
||||
return
|
||||
near.isForward( coord ) && far.isForward( coord )
|
||||
&& top.isForward( coord ) && bottom.isForward( coord )
|
||||
&& right.isForward( coord ) && left.isForward( coord );
|
||||
// clang-format on
|
||||
static_assert(
|
||||
CType == CoordinateSpace::World, "pointInside can only be called on World coordinate Frustums" );
|
||||
|
||||
return near.isForward( coord ) && far.isForward( coord ) && top.isForward( coord )
|
||||
&& bottom.isForward( coord ) && right.isForward( coord ) && left.isForward( coord );
|
||||
}
|
||||
};
|
||||
|
||||
template < CoordinateSpace CType, MatrixType MType >
|
||||
Frustum< EvolvedType< MType >() > operator*( const Matrix< MType >& matrix, const Frustum< CType >& frustum )
|
||||
{
|
||||
return frustum * matrix;
|
||||
Frustum< EvolvedType< MType >() > result {};
|
||||
result.near = matrix * frustum.near;
|
||||
result.far = matrix * frustum.far;
|
||||
result.top = matrix * frustum.top;
|
||||
result.bottom = matrix * frustum.bottom;
|
||||
result.right = matrix * frustum.right;
|
||||
result.left = matrix * frustum.left;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
} // namespace fgl::engine
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
#include <glm/vec4.hpp>
|
||||
|
||||
#include "Coordinate.hpp"
|
||||
#include "Matrix.hpp"
|
||||
#include "Vector.hpp"
|
||||
#include "engine/constants.hpp"
|
||||
|
||||
@@ -32,8 +33,11 @@ namespace fgl::engine
|
||||
Plane( glm::normalize( normal ), glm::dot( glm::normalize( normal ), point ) )
|
||||
{}
|
||||
|
||||
Plane( const Vector vector, const float distance ) : m_distance( distance ), m_direction( vector ) {}
|
||||
|
||||
Plane() = default;
|
||||
|
||||
/*
|
||||
Plane operator*( glm::mat4 matrix ) const
|
||||
{
|
||||
assert( valid() );
|
||||
@@ -41,12 +45,12 @@ namespace fgl::engine
|
||||
Plane result = *this;
|
||||
const glm::vec3 new_direction { matrix * glm::vec4( m_direction, 1.0f ) };
|
||||
|
||||
const auto new_distance { glm::dot( new_direction, m_direction ) + m_distance };
|
||||
const float new_distance { glm::dot( new_direction, m_direction ) + m_distance };
|
||||
result.m_direction = glm::normalize( new_direction );
|
||||
result.m_distance = new_distance;
|
||||
|
||||
return result;
|
||||
}
|
||||
}*/
|
||||
|
||||
//! Returns the closest point on the plane to the 0,0,0 origin
|
||||
Coordinate< CType > getPosition() const
|
||||
@@ -77,4 +81,21 @@ namespace fgl::engine
|
||||
float distance() const { return m_distance; }
|
||||
};
|
||||
|
||||
template < CoordinateSpace CType, MatrixType MType >
|
||||
Plane< EvolvedType< MType >() > operator*( const Matrix< MType >& matrix, const Plane< CType >& plane )
|
||||
{
|
||||
constexpr auto NewCT { EvolvedType< MType >() };
|
||||
constexpr auto OldCT { CType };
|
||||
|
||||
const Coordinate< OldCT > old_center { plane.getPosition() };
|
||||
|
||||
//Translate old_center using matrix
|
||||
const Coordinate< NewCT > new_center { matrix * old_center };
|
||||
|
||||
//Calculate distance between new_center and 0,0,0
|
||||
const float new_distance { glm::dot( plane.direction(), new_center ) };
|
||||
|
||||
return { glm::normalize( new_center ), new_distance };
|
||||
}
|
||||
|
||||
} // namespace fgl::engine
|
||||
|
||||
@@ -23,6 +23,11 @@ namespace fgl::engine
|
||||
Vector operator*( const float scalar ) const { return Vector( static_cast< glm::vec3 >( *this ) * scalar ); }
|
||||
};
|
||||
|
||||
inline Vector operator-( const Vector vec )
|
||||
{
|
||||
return Vector( -static_cast< glm::vec3 >( vec ) );
|
||||
}
|
||||
|
||||
} // namespace fgl::engine
|
||||
|
||||
namespace glm
|
||||
|
||||
Reference in New Issue
Block a user