Skip to content

Commit

Permalink
Fix call to Camera::Compute when scene matrix is ​​changed via Camera…
Browse files Browse the repository at this point in the history
…:;SetLook.
  • Loading branch information
Wolftein committed Nov 10, 2024
1 parent 693e151 commit 0079844
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 44 deletions.
25 changes: 23 additions & 2 deletions Source/Public/Aurora.Base/Trait.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,34 @@ inline namespace Core
return std::addressof(Value);
}

// -=(Undocumented)=-
template<typename Type>
constexpr Type GetMask(Type Count)
{
return static_cast<Type>(-(Count != 0)) & static_cast<Type>(-1) >> (sizeof(Type) * CHAR_BIT - Count);
}

// -=(Undocumented)=-
template<typename Type, Type Mask, Type Shift>
constexpr Type GetBit(Type Field)
{
return (Field & Mask) >> Shift;
}

// -=(Undocumented)=-
template<typename Type>
constexpr Bool HasBit(Type Field, Type Value)
{
return (Field & Value) == Value;
}

// -=(Undocumented)=-
template<typename Type>
constexpr Type SetBit(Type Field, Type Value)
{
return Field | Value;
}

// -=(Undocumented)=-
template<typename Type, Type Mask, Type Shift>
constexpr Type SetBit(Type Field, Type Value)
Expand All @@ -135,8 +156,8 @@ inline namespace Core

// -=(Undocumented)=-
template<typename Type>
constexpr Type GetMask(Type Count)
constexpr Type ClearBit(Type Field, Type Value)
{
return static_cast<Type>(-(Count != 0)) & static_cast<Type>(-1) >> sizeof(Type) * CHAR_BIT - Count;
return Field & ~Value;
}
}
15 changes: 11 additions & 4 deletions Source/Public/Aurora.Graphic/Camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,24 @@ namespace Graphic

Bool Camera::Compute()
{
const Bool Dirty = mDirty;
const Bool Dirty = (mDirty > 0);

if (Dirty)
{
mDirty = false;
// Calculate the inverse scene matrix from the transformation (if applied)
if (HasBit(mDirty, k_DirtyBitTransformation))
{
mScene = mTransformation.AsMatrix().Inverse();
}

// Calculate the inverse view matrix from the transformation and calculate world matrix
mWorld = mProjection * (mView = mTransformation.AsMatrix().Inverse());
// Calculate the world matrix
mWorld = mProjection * mScene;

// Calculate the inverse of the world matrix
mInverse = mWorld.Inverse();

// Reset dirty flags
mDirty = 0;
}
return Dirty;
}
Expand Down
88 changes: 50 additions & 38 deletions Source/Public/Aurora.Graphic/Camera.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ namespace Graphic
}

// -=(Undocumented)=-
Ref<const Matrix4f> GetView() const
Ref<const Matrix4f> GetScene() const
{
return mView;
return mScene;
}

// -=(Undocumented)=-
Expand All @@ -53,51 +53,51 @@ namespace Graphic
// -=(Undocumented)=-
Vector3f GetRight() const
{
return mView.GetRight();
return mScene.GetRight();
}

// -=(Undocumented)=-
Vector3f GetLeft() const
{
return mView.GetLeft();
return mScene.GetLeft();
}

// -=(Undocumented)=-
Vector3f GetUp() const
{
return mView.GetUp();
return mScene.GetUp();
}

// -=(Undocumented)=-
Vector3f GetDown() const
{
return mView.GetDown();
return mScene.GetDown();
}

// -=(Undocumented)=-
Vector3f GetForward() const
{
return mView.GetForward();
return mScene.GetForward();
}

// -=(Undocumented)=-
Vector3f GetBackward() const
{
return mView.GetBackward();
return mScene.GetBackward();
}

// -=(Undocumented)=-
void SetPerspective(Real32 Eyes, Real32 Aspect, Real32 ZNear, Real32 ZFar)
{
mProjection = Matrix4f::CreatePerspective(Eyes, Aspect, ZNear, ZFar);
mDirty = true;
mDirty = SetBit(mDirty, k_DirtyBitProjection);
}

// -=(Undocumented)=-
void SetOrthographic(Real32 Left, Real32 Right, Real32 Bottom, Real32 Top, Real32 ZNear, Real32 ZFar)
{
mProjection = Matrix4f::CreateOrthographic(Left, Right, Bottom, Top, ZNear, ZFar);
mDirty = true;
mDirty = SetBit(mDirty, k_DirtyBitProjection);
}

// -=(Undocumented)=-
Expand All @@ -106,32 +106,39 @@ namespace Graphic
SetOrthographic(0, Width, Height, 0, ZNear, ZFar);
}

// -=(Undocumented)=-
void SetLook(Ref<const Vector3f> Eye, Ref<const Vector3f> Focus, Ref<const Vector3f> Up)
{
mScene = Matrix4f::CreateLook(Eye, Focus, Up);
mDirty = SetBit(0u, k_DirtyBitProjection);
}

// -=(Undocumented)=-
void SetPosition(Ref<const Vector3f> Position)
{
mTransformation.SetPosition(Position);
mDirty = true;
mDirty = SetBit(mDirty, k_DirtyBitTransformation);
}

// -=(Undocumented)=-
void SetPosition(Ref<const Vector2f> Position)
{
mTransformation.SetPosition(Position);
mDirty = true;
mDirty = SetBit(mDirty, k_DirtyBitTransformation);
}

// -=(Undocumented)=-
void SetPosition(Real32 X, Real32 Y, Real32 Z)
{
mTransformation.SetPosition(Vector3f(X, Y, Z));
mDirty = true;
mDirty = SetBit(mDirty, k_DirtyBitTransformation);
}

// -=(Undocumented)=-
void SetPosition(Real32 X, Real32 Y)
{
mTransformation.SetPosition(Vector2f(X, Y));
mDirty = true;
mDirty = SetBit(mDirty, k_DirtyBitTransformation);
}

// -=(Undocumented)=-
Expand All @@ -144,28 +151,28 @@ namespace Graphic
void SetScale(Ref<const Vector3f> Scale)
{
mTransformation.SetScale(Scale);
mDirty = true;
mDirty = SetBit(mDirty, k_DirtyBitTransformation);
}

// -=(Undocumented)=-
void SetScale(Ref<const Vector2f> Scale)
{
mTransformation.SetScale(Scale);
mDirty = true;
mDirty = SetBit(mDirty, k_DirtyBitTransformation);
}

// -=(Undocumented)=-
void SetScale(Real32 X, Real32 Y, Real32 Z)
{
mTransformation.SetScale(Vector3f(X, Y, Z));
mDirty = true;
mDirty = SetBit(mDirty, k_DirtyBitTransformation);
}

// -=(Undocumented)=-
void SetScale(Real32 X, Real32 Y)
{
mTransformation.SetScale(Vector2f(X, Y));
mDirty = true;
mDirty = SetBit(mDirty, k_DirtyBitTransformation);
}

// -=(Undocumented)=-
Expand All @@ -178,42 +185,42 @@ namespace Graphic
void SetRotation(Real32 Angle, Ref<const Vector3f> Axis)
{
mTransformation.SetRotation(Quaternionf::FromAngles(Angle, Axis));
mDirty = true;
mDirty = SetBit(mDirty, k_DirtyBitTransformation);
}

// -=(Undocumented)=-
void SetRotation(Real32 Angle, Ref<const Vector2f> Axis)
{
mTransformation.SetRotation(Quaternionf::FromAngles(Angle, Vector3f(Axis.GetX(), Axis.GetY(), 0)));
mDirty = true;
mDirty = SetBit(mDirty, k_DirtyBitTransformation);
}

// -=(Undocumented)=-
void SetRotation(Ref<const Vector3f> Angles)
{
mTransformation.SetRotation(Quaternionf::FromEulerAngles(Angles));
mDirty = true;
mDirty = SetBit(mDirty, k_DirtyBitTransformation);
}

// -=(Undocumented)=-
void SetRotation(Real32 Pitch, Real32 Yaw, Real32 Roll)
{
mTransformation.SetRotation(Quaternionf::FromEulerAngles(Pitch, Yaw, Roll));
mDirty = true;
mDirty = SetBit(mDirty, k_DirtyBitTransformation);
}

// -=(Undocumented)=-
void SetRotation(Ref<const Vector3f> Direction, Ref<const Vector3f> Up)
{
mTransformation.SetRotation(Quaternionf::FromDirection(Direction, Up));
mDirty = true;
mDirty = SetBit(mDirty, k_DirtyBitTransformation);
}

// -=(Undocumented)=-
void SetRotation(Ref<const Vector3f> Eye, Ref<const Vector3f> Focus, Ref<const Vector3f> Up)
{
mTransformation.SetRotation(Quaternionf::FromDirection(Focus - Eye, Up));
mDirty = true;
mDirty = SetBit(mDirty, k_DirtyBitTransformation);
}

// -=(Undocumented)=-
Expand All @@ -226,84 +233,84 @@ namespace Graphic
void Translate(Ref<const Vector3f> Translation)
{
mTransformation.Translate(Translation);
mDirty = true;
mDirty = SetBit(mDirty, k_DirtyBitTransformation);
}

// -=(Undocumented)=-
void Translate(Ref<const Vector2f> Translation)
{
mTransformation.Translate(Translation);
mDirty = true;
mDirty = SetBit(mDirty, k_DirtyBitTransformation);
}

// -=(Undocumented)=-
void Translate(Real32 X, Real32 Y, Real32 Z)
{
mTransformation.Translate(Vector3f(X, Y, Z));
mDirty = true;
mDirty = SetBit(mDirty, k_DirtyBitTransformation);
}

// -=(Undocumented)=-
void Translate(Real32 X, Real32 Y)
{
mTransformation.Translate(Vector2f(X, Y));
mDirty = true;
mDirty = SetBit(mDirty, k_DirtyBitTransformation);
}

// -=(Undocumented)=-
void Scale(Ref<const Vector3f> Scale)
{
mTransformation.Scale(Scale);
mDirty = true;
mDirty = SetBit(mDirty, k_DirtyBitTransformation);
}

// -=(Undocumented)=-
void Scale(Ref<const Vector2f> Scale)
{
mTransformation.Scale(Scale);
mDirty = true;
mDirty = SetBit(mDirty, k_DirtyBitTransformation);
}

// -=(Undocumented)=-
void Scale(Real32 X, Real32 Y, Real32 Z)
{
mTransformation.Scale(Vector3f(X, Y, Z));
mDirty = true;
mDirty = SetBit(mDirty, k_DirtyBitTransformation);
}

// -=(Undocumented)=-
void Scale(Real32 X, Real32 Y)
{
mTransformation.Scale(Vector2f(X, Y));
mDirty = true;
mDirty = SetBit(mDirty, k_DirtyBitTransformation);
}

// -=(Undocumented)=-
void Rotate(Real32 Angle, Ref<const Vector3f> Axis)
{
mTransformation.Rotate(Angle, Axis);
mDirty = true;
mDirty = SetBit(mDirty, k_DirtyBitTransformation);
}

// -=(Undocumented)=-
void Rotate(Real32 Angle, Ref<const Vector2f> Axis)
{
mTransformation.Rotate(Angle, Vector3f(Axis.GetX(), Axis.GetY(), 0));
mDirty = true;
mDirty = SetBit(mDirty, k_DirtyBitTransformation);
}

// -=(Undocumented)=-
void Rotate(Ref<const Vector3f> Angles)
{
mTransformation.Rotate(Angles);
mDirty = true;
mDirty = SetBit(mDirty, k_DirtyBitTransformation);
}

// -=(Undocumented)=-
void Rotate(Ref<const Vector2f> Angles)
{
mTransformation.Rotate(Angles);
mDirty = true;
mDirty = SetBit(mDirty, k_DirtyBitTransformation);
}

// -=(Undocumented)=-
Expand All @@ -318,14 +325,19 @@ namespace Graphic
// -=(Undocumented)=-
Vector2f GetScreenCoordinates(Ref<const Vector2f> Position, Ref<const Rectf> Viewport) const;

private:

static constexpr UInt32 k_DirtyBitTransformation = 1 << 0;
static constexpr UInt32 k_DirtyBitProjection = 1 << 1;

private:

// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

Bool mDirty;
UInt32 mDirty;
Matrix4f mProjection;
Matrix4f mView;
Matrix4f mScene;
Matrix4f mWorld;
Matrix4f mInverse;
Transformf mTransformation;
Expand Down

0 comments on commit 0079844

Please sign in to comment.