Allows for constexpr NormalVector

This commit is contained in:
2024-08-09 19:33:21 -04:00
parent e56946be3b
commit ac6d7b0915
7 changed files with 37 additions and 31 deletions

View File

@@ -335,9 +335,9 @@ namespace fgl::engine
createFrustum( const float aspect, const float fov_y, const float near, const float far )
{
const Plane< CoordinateSpace::Model > near_plane { ModelCoordinate( constants::WORLD_FORWARD * near ),
NormalVector::bypass( constants::WORLD_FORWARD ) };
NormalVector( constants::WORLD_FORWARD ) };
const Plane< CoordinateSpace::Model > far_plane { ModelCoordinate( constants::WORLD_FORWARD * far ),
NormalVector::bypass( constants::WORLD_BACKWARD ) };
NormalVector( constants::WORLD_BACKWARD ) };
const float half_height { far * glm::tan( fov_y / 2.0f ) };
const float half_width { half_height * aspect };

View File

@@ -19,17 +19,15 @@ namespace fgl::engine
struct Frustum
{
Plane< CType > near { Coordinate< CType >( constants::WORLD_CENTER ),
NormalVector::bypass( constants::WORLD_FORWARD ) };
Plane< CType > far { Coordinate< CType >( constants::WORLD_CENTER ),
NormalVector::bypass( constants::WORLD_FORWARD ) };
Plane< CType > top { Coordinate< CType >( constants::WORLD_CENTER ),
NormalVector::bypass( constants::WORLD_FORWARD ) };
NormalVector( constants::WORLD_FORWARD ) };
Plane< CType > far { Coordinate< CType >( constants::WORLD_CENTER ), NormalVector( constants::WORLD_FORWARD ) };
Plane< CType > top { Coordinate< CType >( constants::WORLD_CENTER ), NormalVector( constants::WORLD_FORWARD ) };
Plane< CType > bottom { Coordinate< CType >( constants::WORLD_CENTER ),
NormalVector::bypass( constants::WORLD_FORWARD ) };
NormalVector( constants::WORLD_FORWARD ) };
Plane< CType > right { Coordinate< CType >( constants::WORLD_CENTER ),
NormalVector::bypass( constants::WORLD_FORWARD ) };
NormalVector( constants::WORLD_FORWARD ) };
Plane< CType > left { Coordinate< CType >( constants::WORLD_CENTER ),
NormalVector::bypass( constants::WORLD_FORWARD ) };
NormalVector( constants::WORLD_FORWARD ) };
Coordinate< CType > m_position {};
@@ -39,6 +37,7 @@ namespace fgl::engine
Frustum() = default;
//TODO: Change this to be, near far, top bottom, left right
Frustum(
const Plane< CType > near_plane,
const Plane< CType > far_plane,

View File

@@ -67,11 +67,11 @@ namespace fgl::engine
std::array< Coordinate< CType >, POINT_COUNT > points() const;
std::array< LineSegment< CType >, LINE_COUNT > lines() const;
constexpr NormalVector right() const { return NormalVector::bypass( constants::WORLD_RIGHT ); }
constexpr NormalVector right() const { return NormalVector( constants::WORLD_RIGHT ); }
constexpr NormalVector up() const { return NormalVector::bypass( constants::WORLD_UP ); }
constexpr NormalVector up() const { return NormalVector( constants::WORLD_UP ); }
constexpr NormalVector forward() const { return NormalVector::bypass( constants::WORLD_FORWARD ); }
constexpr NormalVector forward() const { return NormalVector( constants::WORLD_FORWARD ); }
};
} // namespace fgl::engine

View File

@@ -28,11 +28,11 @@ namespace fgl::engine
float span() const { return this->scale().x; }
constexpr NormalVector right() const { return NormalVector::bypass( constants::WORLD_RIGHT ); }
constexpr NormalVector right() const { return NormalVector( constants::WORLD_RIGHT ); }
constexpr NormalVector up() const { return NormalVector::bypass( constants::WORLD_UP ); }
constexpr NormalVector up() const { return NormalVector( constants::WORLD_UP ); }
constexpr NormalVector forward() const { return NormalVector::bypass( constants::WORLD_FORWARD ); }
constexpr NormalVector forward() const { return NormalVector( constants::WORLD_FORWARD ); }
};
} // namespace fgl::engine

View File

@@ -12,7 +12,7 @@ namespace fgl::engine
template < CoordinateSpace CType >
PointPlane< CType >::PointPlane() :
coordinate( constants::WORLD_CENTER ),
vector( NormalVector::bypass( constants::WORLD_FORWARD ) )
vector( NormalVector( constants::WORLD_FORWARD ) )
{}
template < CoordinateSpace CType >

View File

@@ -9,8 +9,6 @@
namespace fgl::engine
{
NormalVector::NormalVector( const glm::vec3 vector ) : glm::vec3( glm::normalize( vector ) )
{}
NormalVector::NormalVector( const fgl::engine::Vector vector ) : NormalVector( vector.vec() )
{}

View File

@@ -20,21 +20,33 @@ namespace fgl::engine
class Vector;
//! A vector that must be a distance of 1
class NormalVector : private glm::vec3
inline constexpr float length( glm::vec3 vec )
{
constexpr explicit NormalVector( const glm::vec3 point, [[maybe_unused]] const bool ) : glm::vec3( point ) {}
return std::sqrt( std::pow( vec.x, 2 ) + std::pow( vec.y, 2 ) + std::pow( vec.z, 2 ) );
}
public:
inline constexpr glm::vec3 normalize( glm::vec3 vec )
{
if constexpr ( std::is_constant_evaluated() )
{
const auto len { length( vec ) };
return glm::vec3( vec.x / len, vec.y / len, vec.z / len );
}
else
return glm::normalize( vec );
}
//TODO: Make my own normalize function to bypass the fact glm::normalize can't be constexpr
NormalVector() : glm::vec3( glm::normalize( glm::vec3( 1.0f ) ) ) {}
NormalVector( const NormalVector& other ) = default;
//! A vector that must be a distance of 1
struct NormalVector : private glm::vec3
{
constexpr NormalVector() : glm::vec3( normalize( glm::vec3( 1.0f ) ) ) {}
explicit NormalVector( const Vector vec );
explicit NormalVector( const glm::vec3 vec );
constexpr explicit NormalVector( const glm::vec3 vec ) : glm::vec3( normalize( vec ) ) {}
NormalVector( const NormalVector& other ) = default;
NormalVector& operator=( const NormalVector& other ) = default;
const glm::vec3& vec() const { return static_cast< const glm::vec3& >( *this ); }
@@ -43,9 +55,6 @@ namespace fgl::engine
Vector operator*( const float scalar ) const;
NormalVector operator-() const { return NormalVector( -static_cast< glm::vec3 >( *this ) ); }
//Used to bypass the glm::normalize constructor
constexpr static NormalVector bypass( const glm::vec3 point ) { return NormalVector( point, true ); }
};
} // namespace fgl::engine