From dfdbed24c45a880962121e22e0d91c35de69e9d6 Mon Sep 17 00:00:00 2001 From: parax0 Date: Fri, 8 Apr 2016 19:16:26 -0600 Subject: [PATCH] Change CTransform4f to inherit from CMatrix4f --- src/Core/Render/CDrawUtil.cpp | 10 +- src/Core/Render/CRenderBucket.cpp | 2 +- src/Core/Render/CRenderer.cpp | 4 +- src/Core/Resource/CFont.cpp | 3 +- src/Core/Resource/CSkeleton.cpp | 2 +- src/Core/Scene/CSceneNode.cpp | 2 +- src/Core/Scene/CScriptNode.cpp | 2 +- src/Editor/CBasicViewport.cpp | 2 +- src/Editor/CGizmo.cpp | 6 +- src/Editor/Widgets/WTextureGLWidget.cpp | 2 +- src/Math/CMatrix4f.h | 1 + src/Math/CTransform4f.cpp | 192 ++++++++++++------------ src/Math/CTransform4f.h | 26 ++-- 13 files changed, 124 insertions(+), 130 deletions(-) diff --git a/src/Core/Render/CDrawUtil.cpp b/src/Core/Render/CDrawUtil.cpp index 6de0ff51..912733e5 100644 --- a/src/Core/Render/CDrawUtil.cpp +++ b/src/Core/Render/CDrawUtil.cpp @@ -142,7 +142,7 @@ void CDrawUtil::DrawCube(const CColor& Color) void CDrawUtil::DrawCube(const CVector3f& Position, const CColor& Color) { - CGraphics::sMVPBlock.ModelMatrix = CTransform4f::TranslationMatrix(Position).ToMatrix4f(); + CGraphics::sMVPBlock.ModelMatrix = CTransform4f::TranslationMatrix(Position); CGraphics::UpdateMVPBlock(); UseColorShader(Color); DrawCube(); @@ -172,7 +172,7 @@ void CDrawUtil::DrawWireCube(const CAABox& kAABox, const CColor& kColor) CTransform4f Transform; Transform.Scale(kAABox.Size()); Transform.Translate(kAABox.Center()); - CGraphics::sMVPBlock.ModelMatrix = Transform.ToMatrix4f(); + CGraphics::sMVPBlock.ModelMatrix = Transform; CGraphics::UpdateMVPBlock(); UseColorShader(kColor); @@ -204,7 +204,7 @@ void CDrawUtil::DrawWireSphere(const CVector3f& Position, float Radius, const CC CTransform4f Transform; Transform.Scale(Radius); Transform.Translate(Position); - CGraphics::sMVPBlock.ModelMatrix = Transform.ToMatrix4f(); + CGraphics::sMVPBlock.ModelMatrix = Transform; CGraphics::UpdateMVPBlock(); // Set other render params @@ -223,7 +223,7 @@ void CDrawUtil::DrawBillboard(CTexture* pTexture, const CVector3f& Position, con Init(); // Create translation-only model matrix - CGraphics::sMVPBlock.ModelMatrix = CTransform4f::TranslationMatrix(Position).ToMatrix4f(); + CGraphics::sMVPBlock.ModelMatrix = CTransform4f::TranslationMatrix(Position); CGraphics::UpdateMVPBlock(); // Set uniforms @@ -252,7 +252,7 @@ void CDrawUtil::DrawLightBillboard(ELightType Type, const CColor& LightColor, co Init(); // Create translation-only model matrix - CGraphics::sMVPBlock.ModelMatrix = CTransform4f::TranslationMatrix(Position).ToMatrix4f(); + CGraphics::sMVPBlock.ModelMatrix = CTransform4f::TranslationMatrix(Position); CGraphics::UpdateMVPBlock(); // Set uniforms diff --git a/src/Core/Render/CRenderBucket.cpp b/src/Core/Render/CRenderBucket.cpp index 4e9ed458..f5c4cd70 100644 --- a/src/Core/Render/CRenderBucket.cpp +++ b/src/Core/Render/CRenderBucket.cpp @@ -46,7 +46,7 @@ void CRenderBucket::Sort(CCamera* pCamera) float Intensity = 1.f - (Dot / 50.f); CColor CubeColor(Intensity, Intensity, Intensity, 1.f); - CGraphics::sMVPBlock.ModelMatrix = CTransform4f::TranslationMatrix(Point).ToMatrix4f(); + CGraphics::sMVPBlock.ModelMatrix = CTransform4f::TranslationMatrix(Point); CGraphics::UpdateMVPBlock(); CDrawUtil::DrawCube(CubeColor); } diff --git a/src/Core/Render/CRenderer.cpp b/src/Core/Render/CRenderer.cpp index 81d8c9e0..2cefb8f4 100644 --- a/src/Core/Render/CRenderer.cpp +++ b/src/Core/Render/CRenderer.cpp @@ -190,7 +190,7 @@ void CRenderer::RenderBloom() { CDrawUtil::UseTextureShader(skTintColors[iPass]); CVector3f Translate(skHOffset[iPass] * BloomHScale, 0.f, 0.f); - CGraphics::sMVPBlock.ModelMatrix = CTransform4f::TranslationMatrix(Translate).ToMatrix4f(); + CGraphics::sMVPBlock.ModelMatrix = CTransform4f::TranslationMatrix(Translate); CGraphics::UpdateMVPBlock(); glBlendFunc(GL_ONE, GL_ONE); CDrawUtil::DrawSquare(); @@ -210,7 +210,7 @@ void CRenderer::RenderBloom() { CDrawUtil::UseTextureShader(skTintColors[iPass]); CVector3f Translate(0.f, skVOffset[iPass] * BloomVScale, 0.f); - CGraphics::sMVPBlock.ModelMatrix = CTransform4f::TranslationMatrix(Translate).ToMatrix4f(); + CGraphics::sMVPBlock.ModelMatrix = CTransform4f::TranslationMatrix(Translate); CGraphics::UpdateMVPBlock(); glBlendFunc(GL_ONE, GL_ONE); CDrawUtil::DrawSquare(); diff --git a/src/Core/Resource/CFont.cpp b/src/Core/Resource/CFont.cpp index d73457c4..f1d6e951 100644 --- a/src/Core/Resource/CFont.cpp +++ b/src/Core/Resource/CFont.cpp @@ -101,7 +101,6 @@ CVector2f CFont::RenderString(const TString& rkString, CRenderer* /*pRenderer*/, GlyphTransform.Scale(CVector3f((float) pGlyph->Width / 2, (float) pGlyph->Height, 1.f)); GlyphTransform.Scale(Scale); GlyphTransform.Translate(CVector3f(XTrans, YTrans, 0.f)); - CMatrix4f Glyph4f = GlyphTransform.ToMatrix4f(); // Get glyph layer u8 GlyphLayer = pGlyph->RGBAChannel; @@ -109,7 +108,7 @@ CVector2f CFont::RenderString(const TString& rkString, CRenderer* /*pRenderer*/, else if (mTextureFormat == 8) GlyphLayer = 3; // Load shader uniforms, buffer texture - glUniformMatrix4fv(ModelMtxLoc, 1, GL_FALSE, (GLfloat*) &Glyph4f); + glUniformMatrix4fv(ModelMtxLoc, 1, GL_FALSE, (GLfloat*) &GlyphTransform); smGlyphVertices.BufferAttrib(eTex0, &pGlyph->TexCoords); // Draw fill diff --git a/src/Core/Resource/CSkeleton.cpp b/src/Core/Resource/CSkeleton.cpp index 88ce11eb..6fe11f5e 100644 --- a/src/Core/Resource/CSkeleton.cpp +++ b/src/Core/Resource/CSkeleton.cpp @@ -84,7 +84,7 @@ void CSkeleton::Draw(FRenderOptions /*Options*/) CTransform4f Transform; Transform.Scale(0.025f); Transform.Translate(pBone->AbsolutePosition()); - CGraphics::sMVPBlock.ModelMatrix = Transform.ToMatrix4f(); + CGraphics::sMVPBlock.ModelMatrix = Transform; CGraphics::UpdateMVPBlock(); CDrawUtil::DrawSphere(CColor::skWhite); diff --git a/src/Core/Scene/CSceneNode.cpp b/src/Core/Scene/CSceneNode.cpp index 197944c7..3ec0f0ee 100644 --- a/src/Core/Scene/CSceneNode.cpp +++ b/src/Core/Scene/CSceneNode.cpp @@ -124,7 +124,7 @@ void CSceneNode::SetInheritance(bool InheritPos, bool InheritRot, bool InheritSc void CSceneNode::LoadModelMatrix() { - CGraphics::sMVPBlock.ModelMatrix = Transform().ToMatrix4f(); + CGraphics::sMVPBlock.ModelMatrix = Transform(); CGraphics::UpdateMVPBlock(); } diff --git a/src/Core/Scene/CScriptNode.cpp b/src/Core/Scene/CScriptNode.cpp index a626009b..49cffb93 100644 --- a/src/Core/Scene/CScriptNode.cpp +++ b/src/Core/Scene/CScriptNode.cpp @@ -242,7 +242,7 @@ void CScriptNode::DrawSelection() CTransform4f Transform; Transform.Rotate(AbsoluteRotation()); Transform.Translate(AbsolutePosition()); - CGraphics::sMVPBlock.ModelMatrix = Transform.ToMatrix4f(); + CGraphics::sMVPBlock.ModelMatrix = Transform; CGraphics::UpdateMVPBlock(); CGraphics::sPixelBlock.TintColor = CColor::skWhite; diff --git a/src/Editor/CBasicViewport.cpp b/src/Editor/CBasicViewport.cpp index 955062f0..3ed1f0c2 100644 --- a/src/Editor/CBasicViewport.cpp +++ b/src/Editor/CBasicViewport.cpp @@ -300,7 +300,7 @@ void CBasicViewport::DrawAxes() glClear(GL_DEPTH_BUFFER_BIT); glDepthRange(0.f, 1.f); - CGraphics::sMVPBlock.ModelMatrix = CTransform4f::TranslationMatrix(mCamera.Direction() * 5).ToMatrix4f(); + CGraphics::sMVPBlock.ModelMatrix = CTransform4f::TranslationMatrix(mCamera.Direction() * 5); CGraphics::sMVPBlock.ViewMatrix = mViewInfo.RotationOnlyViewMatrix; CGraphics::sMVPBlock.ProjectionMatrix = Math::OrthographicMatrix(-1.f, 1.f, -1.f, 1.f, 0.1f, 100.f); CGraphics::UpdateMVPBlock(); diff --git a/src/Editor/CGizmo.cpp b/src/Editor/CGizmo.cpp index d5331d03..ac5bde75 100644 --- a/src/Editor/CGizmo.cpp +++ b/src/Editor/CGizmo.cpp @@ -74,11 +74,11 @@ void CGizmo::Draw(FRenderOptions /*Options*/, int ComponentIndex, const SViewInf // Set model matrix if (pPart[ComponentIndex].IsBillboard) - CGraphics::sMVPBlock.ModelMatrix = mBillboardTransform.ToMatrix4f(); + CGraphics::sMVPBlock.ModelMatrix = mBillboardTransform; else if ((mMode == eScale) && ((mSelectedAxes & pPart[ComponentIndex].ModelAxes) != 0)) - CGraphics::sMVPBlock.ModelMatrix = mScaledTransform.ToMatrix4f(); + CGraphics::sMVPBlock.ModelMatrix = mScaledTransform; else - CGraphics::sMVPBlock.ModelMatrix = mTransform.ToMatrix4f(); + CGraphics::sMVPBlock.ModelMatrix = mTransform; CGraphics::UpdateMVPBlock(); diff --git a/src/Editor/Widgets/WTextureGLWidget.cpp b/src/Editor/Widgets/WTextureGLWidget.cpp index 47344855..73c7c8cd 100644 --- a/src/Editor/Widgets/WTextureGLWidget.cpp +++ b/src/Editor/Widgets/WTextureGLWidget.cpp @@ -57,7 +57,7 @@ void WTextureGLWidget::paintGL() // Draw texture CDrawUtil::UseTextureShader(); mpTexture->Bind(0); - CGraphics::sMVPBlock.ModelMatrix = mTexTransform.ToMatrix4f(); + CGraphics::sMVPBlock.ModelMatrix = mTexTransform; CGraphics::UpdateMVPBlock(); CDrawUtil::DrawSquare(); diff --git a/src/Math/CMatrix4f.h b/src/Math/CMatrix4f.h index 43a39865..0c08bf74 100644 --- a/src/Math/CMatrix4f.h +++ b/src/Math/CMatrix4f.h @@ -9,6 +9,7 @@ class CTransform4f; class CMatrix4f { +protected: union { float m[4][4]; diff --git a/src/Math/CTransform4f.cpp b/src/Math/CTransform4f.cpp index f76770fe..4d7e2b16 100644 --- a/src/Math/CTransform4f.cpp +++ b/src/Math/CTransform4f.cpp @@ -10,10 +10,21 @@ CTransform4f::CTransform4f() *this = skIdentity; } +CTransform4f::CTransform4f(const CMatrix4f& rkMtx) +{ + for (int iRow = 0; iRow < 3; iRow++) + for (int iCol = 0; iCol < 4; iCol++) + m[iRow][iCol] = rkMtx[iRow][iCol]; + + SetupRow4(); +} + CTransform4f::CTransform4f(IInputStream& rInput) { for (int iVal = 0; iVal < 12; iVal++) _m[iVal] = rInput.ReadFloat(); + + SetupRow4(); } CTransform4f::CTransform4f(float Diagonal) @@ -22,6 +33,7 @@ CTransform4f::CTransform4f(float Diagonal) m[0][0] = Diagonal; m[1][1] = Diagonal; m[2][2] = Diagonal; + m[3][3] = 1.f; } CTransform4f::CTransform4f(float m00, float m01, float m02, float m03, @@ -40,18 +52,23 @@ CTransform4f::CTransform4f(float m00, float m01, float m02, float m03, m[2][1] = m21; m[2][2] = m22; m[2][3] = m23; + SetupRow4(); } CTransform4f::CTransform4f(CVector3f Position, CQuaternion Rotation, CVector3f Scale) { *this = skIdentity; - Translate(Position); - Rotate(Rotation); this->Scale(Scale); + Rotate(Rotation); + Translate(Position); } -CTransform4f::CTransform4f(CVector3f, CVector3f, CVector3f) +CTransform4f::CTransform4f(CVector3f Position, CVector3f Rotation, CVector3f Scale) { + *this = skIdentity; + this->Scale(Scale); + Rotate(Rotation); + Translate(Position); } void CTransform4f::Write(IOutputStream& rOut) @@ -100,6 +117,11 @@ void CTransform4f::Scale(float XScale, float YScale, float ZScale) Scale(CVector3f(XScale, YScale, ZScale)); } +void CTransform4f::SetIdentity() +{ + *this = skIdentity; +} + void CTransform4f::ZeroTranslation() { m[0][3] = 0.f; @@ -125,32 +147,22 @@ CTransform4f CTransform4f::MultiplyIgnoreTranslation(const CTransform4f& rkMtx) return Out; } -CTransform4f CTransform4f::Inverse() const -{ - // This uses CMatrix4f because I suck at math - // todo - rewrite this without using CMatrix4f - CMatrix4f Mat4 = ToMatrix4f().Inverse(); - CTransform4f Out; - memcpy(&Out[0][0], &Mat4[0][0], sizeof(CTransform4f)); - return Out; -} - CTransform4f CTransform4f::QuickInverse() const { - CTransform4f out; - out[0][0] = m[0][0]; - out[0][1] = m[1][0]; - out[0][2] = m[2][0]; - out[0][3] = -((m[0][0] * m[0][3]) + (m[1][0] * m[1][3]) + (m[2][0] * m[2][3])); - out[1][0] = m[0][1]; - out[1][1] = m[1][1]; - out[1][2] = m[2][1]; - out[1][3] = -((m[0][1] * m[0][3]) + (m[1][1] * m[1][3]) + (m[2][1] * m[2][3])); - out[2][0] = m[0][2]; - out[2][1] = m[1][2]; - out[2][2] = m[2][2]; - out[2][3] = -((m[0][2] * m[0][3]) + (m[1][2] * m[1][3]) + (m[2][2] * m[2][3])); - return out; + CTransform4f Out; + Out[0][0] = m[0][0]; + Out[0][1] = m[1][0]; + Out[0][2] = m[2][0]; + Out[0][3] = -((m[0][0] * m[0][3]) + (m[1][0] * m[1][3]) + (m[2][0] * m[2][3])); + Out[1][0] = m[0][1]; + Out[1][1] = m[1][1]; + Out[1][2] = m[2][1]; + Out[1][3] = -((m[0][1] * m[0][3]) + (m[1][1] * m[1][3]) + (m[2][1] * m[2][3])); + Out[2][0] = m[0][2]; + Out[2][1] = m[1][2]; + Out[2][2] = m[2][2]; + Out[2][3] = -((m[0][2] * m[0][3]) + (m[1][2] * m[1][3]) + (m[2][2] * m[2][3])); + return Out; } CTransform4f CTransform4f::NoTranslation() const @@ -169,7 +181,7 @@ CTransform4f CTransform4f::TranslationOnly() const CTransform4f CTransform4f::RotationOnly() const { - return CTransform4f::FromMatrix4f(Inverse().ToMatrix4f().Transpose()); + return Inverse().Transpose(); } // ************ OPERATORS ************ @@ -185,39 +197,39 @@ const float* CTransform4f::operator[](long Index) const CVector3f CTransform4f::operator*(const CVector3f& rkVec) const { - CVector3f out; - out.X = (m[0][0] * rkVec.X) + (m[0][1] * rkVec.Y) + (m[0][2] * rkVec.Z) + (m[0][3]); - out.Y = (m[1][0] * rkVec.X) + (m[1][1] * rkVec.Y) + (m[1][2] * rkVec.Z) + (m[1][3]); - out.Z = (m[2][0] * rkVec.X) + (m[2][1] * rkVec.Y) + (m[2][2] * rkVec.Z) + (m[2][3]); - return out; + CVector3f Out; + Out.X = (m[0][0] * rkVec.X) + (m[0][1] * rkVec.Y) + (m[0][2] * rkVec.Z) + (m[0][3]); + Out.Y = (m[1][0] * rkVec.X) + (m[1][1] * rkVec.Y) + (m[1][2] * rkVec.Z) + (m[1][3]); + Out.Z = (m[2][0] * rkVec.X) + (m[2][1] * rkVec.Y) + (m[2][2] * rkVec.Z) + (m[2][3]); + return Out; } CVector4f CTransform4f::operator*(const CVector4f& rkVec) const { - CVector4f out; - out.X = (m[0][0] * rkVec.X) + (m[0][1] * rkVec.Y) + (m[0][2] * rkVec.Z) + (m[0][3] * rkVec.W); - out.Y = (m[1][0] * rkVec.X) + (m[1][1] * rkVec.Y) + (m[1][2] * rkVec.Z) + (m[1][3] * rkVec.W); - out.Z = (m[2][0] * rkVec.X) + (m[2][1] * rkVec.Y) + (m[2][2] * rkVec.Z) + (m[2][3] * rkVec.W); - out.W = rkVec.W; - return out; + CVector4f Out; + Out.X = (m[0][0] * rkVec.X) + (m[0][1] * rkVec.Y) + (m[0][2] * rkVec.Z) + (m[0][3] * rkVec.W); + Out.Y = (m[1][0] * rkVec.X) + (m[1][1] * rkVec.Y) + (m[1][2] * rkVec.Z) + (m[1][3] * rkVec.W); + Out.Z = (m[2][0] * rkVec.X) + (m[2][1] * rkVec.Y) + (m[2][2] * rkVec.Z) + (m[2][3] * rkVec.W); + Out.W = rkVec.W; + return Out; } CTransform4f CTransform4f::operator*(const CTransform4f& rkMtx) const { - CTransform4f out; - out[0][0] = (m[0][0] * rkMtx[0][0]) + (m[0][1] * rkMtx[1][0]) + (m[0][2] * rkMtx[2][0]); - out[0][1] = (m[0][0] * rkMtx[0][1]) + (m[0][1] * rkMtx[1][1]) + (m[0][2] * rkMtx[2][1]); - out[0][2] = (m[0][0] * rkMtx[0][2]) + (m[0][1] * rkMtx[1][2]) + (m[0][2] * rkMtx[2][2]); - out[0][3] = (m[0][0] * rkMtx[0][3]) + (m[0][1] * rkMtx[1][3]) + (m[0][2] * rkMtx[2][3]) + m[0][3]; - out[1][0] = (m[1][0] * rkMtx[0][0]) + (m[1][1] * rkMtx[1][0]) + (m[1][2] * rkMtx[2][0]); - out[1][1] = (m[1][0] * rkMtx[0][1]) + (m[1][1] * rkMtx[1][1]) + (m[1][2] * rkMtx[2][1]); - out[1][2] = (m[1][0] * rkMtx[0][2]) + (m[1][1] * rkMtx[1][2]) + (m[1][2] * rkMtx[2][2]); - out[1][3] = (m[1][0] * rkMtx[0][3]) + (m[1][1] * rkMtx[1][3]) + (m[1][2] * rkMtx[2][3]) + m[1][3]; - out[2][0] = (m[2][0] * rkMtx[0][0]) + (m[2][1] * rkMtx[1][0]) + (m[2][2] * rkMtx[2][0]); - out[2][1] = (m[2][0] * rkMtx[0][1]) + (m[2][1] * rkMtx[1][1]) + (m[2][2] * rkMtx[2][1]); - out[2][2] = (m[2][0] * rkMtx[0][2]) + (m[2][1] * rkMtx[1][2]) + (m[2][2] * rkMtx[2][2]); - out[2][3] = (m[2][0] * rkMtx[0][3]) + (m[2][1] * rkMtx[1][3]) + (m[2][2] * rkMtx[2][3]) + m[2][3]; - return out; + CTransform4f Out; + Out[0][0] = (m[0][0] * rkMtx[0][0]) + (m[0][1] * rkMtx[1][0]) + (m[0][2] * rkMtx[2][0]); + Out[0][1] = (m[0][0] * rkMtx[0][1]) + (m[0][1] * rkMtx[1][1]) + (m[0][2] * rkMtx[2][1]); + Out[0][2] = (m[0][0] * rkMtx[0][2]) + (m[0][1] * rkMtx[1][2]) + (m[0][2] * rkMtx[2][2]); + Out[0][3] = (m[0][0] * rkMtx[0][3]) + (m[0][1] * rkMtx[1][3]) + (m[0][2] * rkMtx[2][3]) + m[0][3]; + Out[1][0] = (m[1][0] * rkMtx[0][0]) + (m[1][1] * rkMtx[1][0]) + (m[1][2] * rkMtx[2][0]); + Out[1][1] = (m[1][0] * rkMtx[0][1]) + (m[1][1] * rkMtx[1][1]) + (m[1][2] * rkMtx[2][1]); + Out[1][2] = (m[1][0] * rkMtx[0][2]) + (m[1][1] * rkMtx[1][2]) + (m[1][2] * rkMtx[2][2]); + Out[1][3] = (m[1][0] * rkMtx[0][3]) + (m[1][1] * rkMtx[1][3]) + (m[1][2] * rkMtx[2][3]) + m[1][3]; + Out[2][0] = (m[2][0] * rkMtx[0][0]) + (m[2][1] * rkMtx[1][0]) + (m[2][2] * rkMtx[2][0]); + Out[2][1] = (m[2][0] * rkMtx[0][1]) + (m[2][1] * rkMtx[1][1]) + (m[2][2] * rkMtx[2][1]); + Out[2][2] = (m[2][0] * rkMtx[0][2]) + (m[2][1] * rkMtx[1][2]) + (m[2][2] * rkMtx[2][2]); + Out[2][3] = (m[2][0] * rkMtx[0][3]) + (m[2][1] * rkMtx[1][3]) + (m[2][2] * rkMtx[2][3]) + m[2][3]; + return Out; } void CTransform4f::operator*=(const CTransform4f& rkMtx) @@ -246,63 +258,45 @@ bool CTransform4f::operator!=(const CTransform4f& rkMtx) const return (!(*this == rkMtx)); } -// ************ CONVERSION ************ -CMatrix4f CTransform4f::ToMatrix4f() const -{ - return CMatrix4f(m[0][0], m[0][1], m[0][2], m[0][3], - m[1][0], m[1][1], m[1][2], m[1][3], - m[2][0], m[2][1], m[2][2], m[2][3], - 0.f, 0.f, 0.f, 1.f); -} - // ************ STATIC ************ CTransform4f CTransform4f::TranslationMatrix(CVector3f Translation) { - CTransform4f out = skIdentity; - out[0][3] = Translation.X; - out[1][3] = Translation.Y; - out[2][3] = Translation.Z; - return out; + CTransform4f Out = skIdentity; + Out[0][3] = Translation.X; + Out[1][3] = Translation.Y; + Out[2][3] = Translation.Z; + return Out; } CTransform4f CTransform4f::RotationMatrix(CQuaternion Rotation) { - CTransform4f out = skIdentity; - float x = Rotation.X; - float y = Rotation.Y; - float z = Rotation.Z; - float w = Rotation.W; - float x2 = x * x; - float y2 = y * y; - float z2 = z * z; + CTransform4f Out = skIdentity; + float X = Rotation.X; + float Y = Rotation.Y; + float Z = Rotation.Z; + float W = Rotation.W; + float X2 = X * X; + float Y2 = Y * Y; + float Z2 = Z * Z; - out[0][0] = 1.0f - (2 * y2) - (2 * z2); - out[0][1] = (2 * x * y) - (2 * z * w); - out[0][2] = (2 * x * z) + (2 * y * w); - out[1][0] = (2 * x * y) + (2 * z * w); - out[1][1] = 1.0f - (2 * x2) - (2 * z2); - out[1][2] = (2 * y * z) - (2 * x * w); - out[2][0] = (2 * x * z) - (2 * y * w); - out[2][1] = (2 * y * z) + (2 * x * w); - out[2][2] = 1.0f - (2 * x2) - (2 * y2); - return out; + Out[0][0] = 1.0f - (2 * Y2) - (2 * Z2); + Out[0][1] = (2 * X * Y) - (2 * Z * W); + Out[0][2] = (2 * X * Z) + (2 * Y * W); + Out[1][0] = (2 * X * Y) + (2 * Z * W); + Out[1][1] = 1.0f - (2 * X2) - (2 * Z2); + Out[1][2] = (2 * Y * Z) - (2 * X * W); + Out[2][0] = (2 * X * Z) - (2 * Y * W); + Out[2][1] = (2 * Y * Z) + (2 * X * W); + Out[2][2] = 1.0f - (2 * X2) - (2 * Y2); + return Out; } - CTransform4f CTransform4f::ScaleMatrix(CVector3f Scale) - { - CTransform4f out = skIdentity; - out[0][0] = Scale.X; - out[1][1] = Scale.Y; - out[2][2] = Scale.Z; - return out; - } - -CTransform4f CTransform4f::FromMatrix4f(const CMatrix4f& rkMtx) +CTransform4f CTransform4f::ScaleMatrix(CVector3f Scale) { - CTransform4f Out; - for (int iRow = 0; iRow < 3; iRow++) - for (int iCol = 0; iCol < 4; iCol++) - Out[iRow][iCol] = rkMtx[iRow][iCol]; + CTransform4f Out = skIdentity; + Out[0][0] = Scale.X; + Out[1][1] = Scale.Y; + Out[2][2] = Scale.Z; return Out; } diff --git a/src/Math/CTransform4f.h b/src/Math/CTransform4f.h index 1e54218f..2f5d3a0a 100644 --- a/src/Math/CTransform4f.h +++ b/src/Math/CTransform4f.h @@ -8,18 +8,12 @@ class CVector3f; class CVector4f; class CQuaternion; -class CMatrix4f; -class CTransform4f +class CTransform4f : public CMatrix4f { - union - { - float m[3][4]; - float _m[12]; - }; - public: CTransform4f(); + CTransform4f(const CMatrix4f& rkMtx); CTransform4f(IInputStream& rInput); CTransform4f(float Diagonal); CTransform4f(float m00, float m01, float m02, float m03, @@ -37,22 +31,18 @@ public: void Rotate(float XRot, float YRot, float ZRot); void Scale(CVector3f Scale); void Scale(float XScale, float YScale, float ZScale); + void SetIdentity(); void ZeroTranslation(); CTransform4f MultiplyIgnoreTranslation(const CTransform4f& rkMtx) const; - CTransform4f Inverse() const; CTransform4f QuickInverse() const; CTransform4f NoTranslation() const; CTransform4f TranslationOnly() const; CTransform4f RotationOnly() const; - // Conversion - CMatrix4f ToMatrix4f() const; - // Static static CTransform4f TranslationMatrix(CVector3f Translation); static CTransform4f RotationMatrix(CQuaternion Rotation); static CTransform4f ScaleMatrix(CVector3f Scale); - static CTransform4f FromMatrix4f(const CMatrix4f& rkMtx); static CTransform4f FromGlmMat4(const glm::mat4& rkMtx); // Operators @@ -68,6 +58,16 @@ public: // Constant static const CTransform4f skIdentity; static const CTransform4f skZero; + + // Protected Utility +protected: + inline void SetupRow4() + { + m[3][0] = 0.f; + m[3][1] = 0.f; + m[3][2] = 0.f; + m[3][3] = 1.f; + } }; #endif // CTRANSFORM4F_H