Fixes confusing types (Rotation vs Vector) by splitting them

This commit is contained in:
2024-02-16 21:14:13 -05:00
parent 9baef6bd15
commit 30b5c81e68
12 changed files with 134 additions and 39 deletions

View File

@@ -58,7 +58,7 @@ namespace fgl::engine
};
template < int N >
inline std::tuple< float, float > extract( const Vector rotation, const RotationOrder order )
inline std::tuple< float, float > extract( const Rotation rotation, const RotationOrder order )
{
switch ( order )
{
@@ -73,7 +73,7 @@ namespace fgl::engine
return { glm::cos( rotation.y ), glm::sin( rotation.y ) };
}
break;
case XYZ:
case XYZ: // DEFAULT
switch ( N )
{
case 1:
@@ -81,7 +81,7 @@ namespace fgl::engine
case 2:
return { glm::cos( rotation.y ), glm::sin( rotation.y ) };
case 3:
return { glm::cos( rotation.z ), glm::sin( rotation.z ) };
return { glm::cos( -rotation.z ), glm::sin( -rotation.z ) };
}
break;
case YXZ:
@@ -134,7 +134,7 @@ namespace fgl::engine
std::unreachable();
}
glm::mat4 taitBryanMatrix( const Vector rotation, const RotationOrder order = DEFAULT )
glm::mat4 taitBryanMatrix( const Rotation rotation, const RotationOrder order = DEFAULT )
{
glm::mat4 mat { 1.0f };
@@ -239,7 +239,7 @@ namespace fgl::engine
}
}
void Camera::setViewYXZ( glm::vec3 position, const Vector rotation, const ViewMode mode )
void Camera::setViewYXZ( glm::vec3 position, const Rotation rotation, const ViewMode mode )
{
ZoneScoped;

View File

@@ -44,7 +44,7 @@ namespace fgl::engine
inline static TransformComponent frustum_alt_transform { WorldCoordinate( constants::WORLD_CENTER ),
glm::vec3( 1.0f ),
Vector( 0.0f, 0.0f, 0.0f ) };
{ 0.0f, 0.0f, 0.0f } };
inline static bool update_frustums { true };
inline static bool update_using_alt { false };
@@ -52,7 +52,7 @@ namespace fgl::engine
Camera()
{
setPerspectiveProjection( 90.0f, 16.0f / 9.0f, constants::NEAR_PLANE, constants::FAR_PLANE );
setViewYXZ( constants::CENTER, Vector( 0.0f, 0.0f, 0.0f ) );
setViewYXZ( constants::CENTER, Rotation( 0.0f, 0.0f, 0.0f ) );
}
WorldCoordinate getFrustumPosition() const;
@@ -109,7 +109,7 @@ namespace fgl::engine
TaitBryan
};
void setViewYXZ( glm::vec3 pos, const Vector rotation, const ViewMode mode = TaitBryan );
void setViewYXZ( glm::vec3 pos, const Rotation rotation, const ViewMode mode = TaitBryan );
};
} // namespace fgl::engine

View File

@@ -202,7 +202,7 @@ namespace fgl::engine
ImGui::Text( "%.3f ms", 1000.0f / ImGui::GetIO().Framerate );
ImGui::Text( "Average rolling frametime: %.3f ms", rolling_ms_average.average() );
auto inputVec3 = []( const std::string label, glm::vec3& vec )
auto inputVec3 = []( const std::string label, glm::vec3 vec )
{
ImGui::PushID( label.c_str() );
ImGui::PushItemWidth( 80 );
@@ -216,6 +216,9 @@ namespace fgl::engine
ImGui::PopID();
};
auto inputRVec3 = [ &inputVec3 ]( const std::string label, const Rotation rot )
{ inputVec3( label, glm::vec3( rot.x, rot.y, rot.z ) ); };
if ( ImGui::CollapsingHeader( "Camera" ) )
{
ImGui::PushItemWidth( 80 );
@@ -245,7 +248,7 @@ namespace fgl::engine
{
ImGui::PushID( "FrustumMatrix" );
inputVec3( "Translation", Camera::frustum_alt_transform.translation );
inputVec3( "Rotation", Camera::frustum_alt_transform.rotation );
inputRVec3( "Rotation", Camera::frustum_alt_transform.rotation );
ImGui::PopID();
}
}

View File

@@ -29,10 +29,10 @@ namespace fgl::engine
{
ImGui::Begin( "CameraMovement" );
Vector rotate { 0.0f };
Rotation rotate { 0.0f };
if ( glfwGetKey( window, key_mappings.look_right ) == GLFW_PRESS ) rotate.yaw -= 1.f;
if ( glfwGetKey( window, key_mappings.look_left ) == GLFW_PRESS ) rotate.yaw += 1.f;
if ( glfwGetKey( window, key_mappings.look_right ) == GLFW_PRESS ) rotate.yaw += 1.f;
if ( glfwGetKey( window, key_mappings.look_left ) == GLFW_PRESS ) rotate.yaw -= 1.f;
if ( glfwGetKey( window, key_mappings.look_up ) == GLFW_PRESS ) rotate.pitch += 1.f;
if ( glfwGetKey( window, key_mappings.look_down ) == GLFW_PRESS ) rotate.pitch -= 1.f;
@@ -95,8 +95,8 @@ namespace fgl::engine
}
const glm::vec3 forward_dir { target.transform.rotation.forward() };
const glm::vec3 right_dir { target.transform.rotation.right() };
const glm::vec3 up_dir { constants::WORLD_UP };
const glm::vec3 right_dir { target.transform.rotation.right( up_dir ) };
glm::vec3 move_dir { 0.0f };
if ( glfwGetKey( window, key_mappings.move_forward ) == GLFW_PRESS ) move_dir += forward_dir;

View File

@@ -22,7 +22,7 @@ namespace fgl::engine::debug
ZoneScoped;
const ImVec2 window_size { windowSize() };
return Coordinate< CoordinateSpace::Screen >( glm::project(
return Coordinate< CoordinateSpace::Screen >( glm::projectZO(
static_cast< glm::vec3 >( world_point ),
glm::mat4( 1.0f ),
camera.getProjectionViewMatrix(),

View File

@@ -15,7 +15,40 @@ namespace fgl::engine
{
WorldCoordinate translation { constants::DEFAULT_VEC3 };
glm::vec3 scale { 1.0f, 1.0f, 1.0f };
Vector rotation { 0.0f, 0.0f, 0.0f };
struct RotationVector : public glm::vec3
{
float& pitch { x };
float& roll { y };
float& yaw { z };
RotationVector& operator=( const RotationVector& other )
{
*static_cast< glm::vec3* >( this ) = other;
return *this;
}
RotationVector( const float value ) : glm::vec3( value ) {}
RotationVector( const float pitch_r, const float roll_r, const float yaw_r ) :
glm::vec3( pitch_r, roll_r, yaw_r )
{}
RotationVector& operator+=( const glm::vec3 vec )
{
glm::vec3::operator+=( vec );
return *this;
}
glm::vec3 forward() const
{
//TODO: Figure out how to do this with Z axis bullshit
return glm::vec3 { glm::sin( yaw ), glm::cos( yaw ), 0.0f };
}
glm::vec3 right( const glm::vec3 up ) const { return glm::cross( up, forward() ); }
} rotation { 0.0f, 0.0f, 0.0f };
//TODO: Figure this out and replace TransformComponent with a template of CType instead
glm::mat4 mat4() const;
@@ -25,4 +58,6 @@ namespace fgl::engine
glm::mat3 normalMatrix() const;
};
using Rotation = TransformComponent::RotationVector;
} // namespace fgl::engine

View File

@@ -49,13 +49,15 @@ namespace fgl::engine
glm::vec3 Vector::forward() const
{
//TODO: Figure out Z shit
return { std::sin( yaw ), std::cos( yaw ), 0.0f };
return static_cast< glm::vec3 >( *this );
}
glm::vec3 Vector::right() const
glm::vec3 Vector::right( const Vector up ) const
{
const auto forward_dir { forward() };
return { -forward_dir.y, forward_dir.x, 0.0f };
const Vector forward_dir { forward() };
const Vector down { up };
return glm::cross( down, forward_dir );
}
} // namespace fgl::engine

View File

@@ -16,13 +16,20 @@ namespace fgl::engine
{
public:
float& roll { y };
float& pitch { x };
float& yaw { z };
constexpr explicit Vector( const float value ) : glm::vec3( value ) {}
constexpr explicit Vector( const glm::vec3 vec ) : glm::vec3( vec ) {}
constexpr explicit Vector( const glm::vec3 vec ) : glm::vec3( vec )
{
assert(
( x <= 1.0f || std::numeric_limits< float >::max() )
&& "Value too high for Vector. Forgot to normalize?" );
assert(
( y <= 1.0f || std::numeric_limits< float >::max() )
&& "Value too high for Vector. Forgot to normalize?" );
assert(
( z <= 1.0f || std::numeric_limits< float >::max() )
&& "Value too high for Vector. Forgot to normalize?" );
}
constexpr explicit Vector( const float x, const float y, const float z ) : glm::vec3( x, y, z ) {}
@@ -30,7 +37,7 @@ namespace fgl::engine
Vector operator*( const float scalar ) const { return Vector( static_cast< glm::vec3 >( *this ) * scalar ); }
glm::vec3 right() const;
glm::vec3 right( const Vector up = Vector( constants::WORLD_UP ) ) const;
glm::vec3 forward() const;
//Copy

View File

@@ -24,12 +24,41 @@ TEST_CASE( "Camera", "[camera]" )
camera.setOrthographicProjection( 1.0f, 1.0f, 1.0f, 1.0f, constants::NEAR_PLANE, constants::FAR_PLANE );
}
const auto camera_up { camera.getUp() };
REQUIRE( camera_up == constants::WORLD_UP );
SECTION( "Default orientation" )
{
const auto camera_up { camera.getUp() };
REQUIRE( camera_up == constants::WORLD_UP );
const auto camera_forward { camera.getForward() };
REQUIRE( camera_forward == constants::WORLD_FORWARD );
const auto camera_forward { camera.getForward() };
REQUIRE( camera_forward == constants::WORLD_FORWARD );
const auto camera_right { camera.getRight() };
REQUIRE( camera_right == constants::WORLD_RIGHT );
const auto camera_right { camera.getRight() };
REQUIRE( camera_right == constants::WORLD_RIGHT );
}
SECTION( "Rotations" )
{
Rotation rotation_vec { 0.0f };
SECTION( "Yaw+ (Right)" )
{
rotation_vec.yaw = glm::radians( 90.0f );
camera.setViewYXZ( constants::WORLD_CENTER, rotation_vec );
const auto camera_forward { camera.getForward() };
REQUIRE( camera_forward.x == constants::WORLD_LEFT.x );
REQUIRE( camera_forward.y <= std::numeric_limits< float >::epsilon() );
REQUIRE( camera_forward.z <= std::numeric_limits< float >::epsilon() );
}
SECTION( "Yaw- (Left)" )
{
rotation_vec.yaw = glm::radians( -90.0f );
camera.setViewYXZ( constants::WORLD_CENTER, rotation_vec );
const auto camera_forward { camera.getForward() };
REQUIRE( camera_forward.x == constants::WORLD_RIGHT.x );
REQUIRE( camera_forward.y <= std::numeric_limits< float >::epsilon() );
REQUIRE( camera_forward.z <= std::numeric_limits< float >::epsilon() );
}
}
}

View File

@@ -32,7 +32,7 @@ TEST_CASE( "Frustum", "[frustum][rotation][translation]" )
{
SECTION( "Backwards" )
{
camera.setViewYXZ( constants::WORLD_CENTER - constants::WORLD_FORWARD, Vector( 0.0f, 0.0f, 0.0f ) );
camera.setViewYXZ( constants::WORLD_CENTER - constants::WORLD_FORWARD, Rotation( 0.0f, 0.0f, 0.0f ) );
//Translate backwards by 1 world unit
const auto translated_backwards { camera.getFrustumBounds() };
@@ -58,7 +58,7 @@ TEST_CASE( "Frustum", "[frustum][rotation][translation]" )
SECTION( "Forward" )
{
camera.setViewYXZ( constants::WORLD_CENTER + constants::WORLD_FORWARD, Vector( 0.0f, 0.0f, 0.0f ) );
camera.setViewYXZ( constants::WORLD_CENTER + constants::WORLD_FORWARD, Rotation( 0.0f, 0.0f, 0.0f ) );
//Translate forward by 1 world unit
const auto translated_forward { camera.getFrustumBounds() };
@@ -74,7 +74,7 @@ TEST_CASE( "Frustum", "[frustum][rotation][translation]" )
SECTION( "Up" )
{
camera.setViewYXZ( constants::WORLD_CENTER + constants::WORLD_UP, Vector( 0.0f, 0.0f, 0.0f ) );
camera.setViewYXZ( constants::WORLD_CENTER + constants::WORLD_UP, Rotation( 0.0f, 0.0f, 0.0f ) );
//Translate up by 1 world unit
const auto translated_up { camera.getFrustumBounds() };
@@ -170,7 +170,7 @@ TEST_CASE( "Frustum", "[frustum][rotation][translation]" )
//Testing rotation of the camera
SECTION( "Pitch" )
{
Vector rotation { 0.0f, 0.0f, 0.0f };
Rotation rotation { 0.0f, 0.0f, 0.0f };
rotation.pitch -= glm::radians( 90.0f );
camera.setViewYXZ( constants::CENTER, rotation );
@@ -201,7 +201,7 @@ TEST_CASE( "Frustum", "[frustum][rotation][translation]" )
SECTION( "Yaw" )
{
Vector rotation { 0.0f, 0.0f, 0.0f };
Rotation rotation { 0.0f, 0.0f, 0.0f };
rotation.yaw += glm::radians( 90.0f );
camera.setViewYXZ( constants::CENTER, rotation );
@@ -233,7 +233,7 @@ TEST_CASE( "Frustum", "[frustum][rotation][translation]" )
SECTION( "Roll" )
{
Vector rotation { 0.0f, 0.0f, 0.0f };
Rotation rotation { 0.0f, 0.0f, 0.0f };
rotation.roll -= glm::radians( 90.0f );
camera.setViewYXZ( constants::CENTER, rotation );

View File

@@ -15,7 +15,7 @@ TEST_CASE( "Transform", "[transform][rotation][translation]" )
component.translation = constants::WORLD_CENTER;
component.scale = glm::vec3( 1.0f );
component.rotation = Vector( 0.0f );
component.rotation = Rotation( 0.0f );
REQUIRE( component.mat4() == glm::mat4( 1.0f ) );

19
tests/src/VectorTests.cpp Normal file
View File

@@ -0,0 +1,19 @@
//
// Created by kj16609 on 2/16/24.
//
#include <catch2/catch_all.hpp>
#include "engine/primitives/Vector.hpp"
using namespace fgl::engine;
TEST_CASE( "Vector", "[vector][transforms]" )
{
SECTION( "Right after rotation" )
{
Vector rotation_vec { constants::WORLD_FORWARD };
//Rotate by 90 degrees on yaw
}
}