Browse Source

Add non-uniform scalling to Mat

Panagiotis Christopoulos Charitos 1 year ago
parent
commit
ed771f4933

+ 20 - 11
AnKi/Math/Mat.h

@@ -132,15 +132,20 @@ public:
 		m(3, 3) = m33;
 	}
 
-	constexpr TMat(const TVec<T, 3>& translation, const TMat<T, 3, 3>& rotation, const T scale = T(1)) requires(kSize == 16)
+	constexpr TMat(const TVec<T, 3>& translation, const TMat<T, 3, 3>& rotation, const TVec<T, 3>& scale = TVec<T, 3>(T(1))) requires(kSize == 16)
 	{
-		if(isZero<T>(scale - T(1)))
+		if(scale == TVec<T, 3>(T(1)))
 		{
 			setRotationPart(rotation);
 		}
 		else
 		{
-			setRotationPart(rotation * scale);
+			const auto a = rotation.getColumn(0) * scale.x();
+			const auto b = rotation.getColumn(1) * scale.y();
+			const auto c = rotation.getColumn(2) * scale.z();
+			TMat<T, 3, 3> rot;
+			rot.setColumns(a, b, c);
+			setRotationPart(rot);
 		}
 
 		setTranslationPart(translation);
@@ -151,7 +156,7 @@ public:
 	}
 
 	explicit constexpr TMat(const TTransform<T>& t) requires(kSize == 16)
-		: TMat(t.getOrigin().xyz(), t.getRotation().getRotationPart(), t.getScale())
+		: TMat(t.getOrigin().xyz(), t.getRotation().getRotationPart(), TVec<T, 3>(t.getScale()))
 	{
 	}
 
@@ -191,37 +196,41 @@ public:
 		m_rows[2] = m4.getRow(2);
 	}
 
-	explicit constexpr TMat(const TVec<T, 3>& translation, const TMat<T, 3, 3>& rotation, const T scale = T(1)) requires(kSize == 12)
+	explicit constexpr TMat(const TVec<T, 3>& translation, const TMat<T, 3, 3>& rotation,
+							const TVec<T, 3>& scale = TVec<T, 3>(T(1))) requires(kSize == 12)
 	{
-		if(isZero<T>(scale - T(1)))
+		if(scale == TVec<T, 3>(T(1)))
 		{
 			setRotationPart(rotation);
 		}
 		else
 		{
-			setRotationPart(rotation * scale);
+			const auto a = rotation.getColumn(0) * scale.x();
+			const auto b = rotation.getColumn(1) * scale.y();
+			const auto c = rotation.getColumn(2) * scale.z();
+			setColumns(a, b, c);
 		}
 
 		setTranslationPart(translation);
 	}
 
-	explicit constexpr TMat(const TVec<T, 3>& translation, const TQuat<T>& q, const T scale = T(1)) requires(kSize == 12)
+	explicit constexpr TMat(const TVec<T, 3>& translation, const TQuat<T>& q, const TVec<T, 3>& scale = TVec<T, 3>(T(1))) requires(kSize == 12)
 		: TMat(translation, TMat<T, 3, 3>(q), scale)
 	{
 	}
 
-	explicit constexpr TMat(const TVec<T, 3>& translation, const TEuler<T>& b, const T scale = T(1)) requires(kSize == 12)
+	explicit constexpr TMat(const TVec<T, 3>& translation, const TEuler<T>& b, const TVec<T, 3>& scale = TVec<T, 3>(T(1))) requires(kSize == 12)
 		: TMat(translation, TMat<T, 3, 3>(b), scale)
 	{
 	}
 
-	explicit constexpr TMat(const TVec<T, 3>& translation, const TAxisang<T>& b, const T scale = T(1)) requires(kSize == 12)
+	explicit constexpr TMat(const TVec<T, 3>& translation, const TAxisang<T>& b, const TVec<T, 3>& scale = TVec<T, 3>(T(1))) requires(kSize == 12)
 		: TMat(translation, TMat<T, 3, 3>(b), scale)
 	{
 	}
 
 	explicit constexpr TMat(const TTransform<T>& t) requires(kSize == 12)
-		: TMat(t.getOrigin().xyz(), t.getRotation().getRotationPart(), t.getScale())
+		: TMat(t.getOrigin().xyz(), t.getRotation().getRotationPart(), TVec<T, 3>(t.getScale()))
 	{
 	}
 	/// @}

+ 20 - 46
AnKi/Math/Transform.h

@@ -22,7 +22,7 @@ public:
 	constexpr TTransform()
 		: m_origin(T(0))
 		, m_rotation(TMat<T, 3, 4>::getIdentity())
-		, m_scale(1.0)
+		, m_scale(T(1))
 	{
 	}
 
@@ -31,7 +31,7 @@ public:
 		, m_rotation(b.m_rotation)
 		, m_scale(b.m_scale)
 	{
-		checkW();
+		check();
 	}
 
 	explicit TTransform(const TMat<T, 4, 4>& m4)
@@ -47,7 +47,7 @@ public:
 		m_rotation.setColumns(s0 / scales.x(), s1 / scales.x(), s2 / scales.x(), TVec<T, 3>(T(0)));
 		m_origin = m4.getTranslationPart().xyz0();
 		m_scale = scales.x();
-		checkW();
+		check();
 	}
 
 	TTransform(const TVec<T, 4>& origin, const TMat<T, 3, 4>& rotation, const T scale)
@@ -55,31 +55,7 @@ public:
 		, m_rotation(rotation)
 		, m_scale(scale)
 	{
-		checkW();
-	}
-
-	explicit TTransform(const TVec<T, 4>& origin)
-		: m_origin(origin)
-		, m_rotation(TMat<T, 3, 4>::getIdentity())
-		, m_scale(T(1))
-	{
-		checkW();
-	}
-
-	explicit TTransform(const TMat<T, 3, 4>& rotation)
-		: m_origin(TVec<T, 4>(T(0)))
-		, m_rotation(rotation)
-		, m_scale(T(1))
-	{
-		checkW();
-	}
-
-	TTransform(const T scale)
-		: m_origin(TVec<T, 4>(T(0)))
-		, m_rotation(TMat<T, 3, 4>::getIdentity())
-		, m_scale(scale)
-	{
-		checkW();
+		check();
 	}
 	/// @}
 
@@ -90,23 +66,18 @@ public:
 		return m_origin;
 	}
 
-	[[nodiscard]] TVec<T, 4>& getOrigin()
-	{
-		return m_origin;
-	}
-
 	void setOrigin(const TVec<T, 4>& o)
 	{
 		m_origin = o;
-		checkW();
+		check();
 	}
 
-	[[nodiscard]] const TMat<T, 3, 4>& getRotation() const
+	void setOrigin(const TVec<T, 3>& o)
 	{
-		return m_rotation;
+		m_origin = o.xyz0();
 	}
 
-	[[nodiscard]] TMat<T, 3, 4>& getRotation()
+	[[nodiscard]] const TMat<T, 3, 4>& getRotation() const
 	{
 		return m_rotation;
 	}
@@ -116,12 +87,13 @@ public:
 		m_rotation = r;
 	}
 
-	[[nodiscard]] T getScale() const
+	void setRotation(const TMat<T, 3, 3>& r)
 	{
-		return m_scale;
+		m_rotation.setRotationPart(r);
+		m_rotation.setTranslationPart(TVec<T, 3>(T(0)));
 	}
 
-	[[nodiscard]] T& getScale()
+	[[nodiscard]] T getScale() const
 	{
 		return m_scale;
 	}
@@ -129,6 +101,7 @@ public:
 	void setScale(const T s)
 	{
 		m_scale = s;
+		check();
 	}
 	/// @}
 
@@ -139,7 +112,7 @@ public:
 		m_origin = b.m_origin;
 		m_rotation = b.m_rotation;
 		m_scale = b.m_scale;
-		checkW();
+		check();
 		return *this;
 	}
 
@@ -169,7 +142,7 @@ public:
 	/// @copybrief combineTTransformations
 	[[nodiscard]] TTransform combineTransformations(const TTransform& b) const
 	{
-		checkW();
+		check();
 		const TTransform& a = *this;
 		TTransform out;
 
@@ -184,7 +157,7 @@ public:
 	/// Get the inverse transformation. Its faster that inverting a Mat4
 	[[nodiscard]] TTransform getInverse() const
 	{
-		checkW();
+		check();
 		TTransform o;
 		o.m_rotation = m_rotation;
 		o.m_rotation.transposeRotationPart();
@@ -203,14 +176,14 @@ public:
 	/// Transform a TVec3
 	[[nodiscard]] TVec<T, 3> transform(const TVec<T, 3>& b) const
 	{
-		checkW();
+		check();
 		return (m_rotation.getRotationPart() * (b * m_scale)) + m_origin.xyz();
 	}
 
 	/// Transform a TVec4. SIMD optimized
 	[[nodiscard]] TVec<T, 4> transform(const TVec<T, 4>& b) const
 	{
-		checkW();
+		check();
 		TVec<T, 4> out = TVec<T, 4>(m_rotation * (b * m_scale), T(0)) + m_origin;
 		return out;
 	}
@@ -252,9 +225,10 @@ private:
 	T m_scale; ///< The uniform scaling
 	/// @}
 
-	void checkW() const
+	void check() const
 	{
 		ANKI_ASSERT(m_origin.w() == T(0));
+		ANKI_ASSERT(m_scale > T(0));
 	}
 };
 

+ 1 - 1
AnKi/Scene/Components/SkinComponent.cpp

@@ -216,7 +216,7 @@ void SkinComponent::visitBones(const Bone& bone, const Mat3x4& parentTrf, const
 	if(bonesAnimated.get(bone.getIndex()))
 	{
 		const Trf& t = m_animationTrfs[bone.getIndex()];
-		outMat = parentTrf.combineTransformations(Mat3x4(t.m_translation.xyz(), Mat3(t.m_rotation), t.m_scale));
+		outMat = parentTrf.combineTransformations(Mat3x4(t.m_translation.xyz(), Mat3(t.m_rotation), Vec3(t.m_scale)));
 	}
 	else
 	{

+ 1 - 1
AnKi/Scene/Events/JitterMoveEvent.cpp

@@ -38,7 +38,7 @@ Error JitterMoveEvent::update([[maybe_unused]] Second prevUpdateTime, Second crn
 
 	F32 factor = F32(sin(getDelta(crntTime) * kPi));
 
-	trf.getOrigin() = linearInterpolate(m_originalPos, m_newPos, factor);
+	trf.setOrigin(linearInterpolate(m_originalPos, m_newPos, factor));
 
 	node->setLocalTransform(trf);
 

+ 19 - 7
AnKi/Scene/SceneNode.h

@@ -314,40 +314,52 @@ public:
 	/// @{
 	void rotateLocalX(F32 angleRad)
 	{
-		m_ltrf.getRotation().rotateXAxis(angleRad);
+		Mat3x4 r = m_ltrf.getRotation();
+		r.rotateXAxis(angleRad);
+		m_ltrf.setRotation(r);
 		m_localTransformDirty = true;
 	}
+
 	void rotateLocalY(F32 angleRad)
 	{
-		m_ltrf.getRotation().rotateYAxis(angleRad);
+		Mat3x4 r = m_ltrf.getRotation();
+		r.rotateYAxis(angleRad);
+		m_ltrf.setRotation(r);
 		m_localTransformDirty = true;
 	}
+
 	void rotateLocalZ(F32 angleRad)
 	{
-		m_ltrf.getRotation().rotateZAxis(angleRad);
+		Mat3x4 r = m_ltrf.getRotation();
+		r.rotateZAxis(angleRad);
+		m_ltrf.setRotation(r);
 		m_localTransformDirty = true;
 	}
+
 	void moveLocalX(F32 distance)
 	{
 		Vec3 x_axis = m_ltrf.getRotation().getColumn(0);
-		m_ltrf.getOrigin() += Vec4(x_axis, 0.0) * distance;
+		m_ltrf.setOrigin(m_ltrf.getOrigin() + Vec4(x_axis, 0.0f) * distance);
 		m_localTransformDirty = true;
 	}
+
 	void moveLocalY(F32 distance)
 	{
 		Vec3 y_axis = m_ltrf.getRotation().getColumn(1);
-		m_ltrf.getOrigin() += Vec4(y_axis, 0.0) * distance;
+		m_ltrf.setOrigin(m_ltrf.getOrigin() + Vec4(y_axis, 0.0) * distance);
 		m_localTransformDirty = true;
 	}
+
 	void moveLocalZ(F32 distance)
 	{
 		Vec3 z_axis = m_ltrf.getRotation().getColumn(2);
-		m_ltrf.getOrigin() += Vec4(z_axis, 0.0) * distance;
+		m_ltrf.setOrigin(m_ltrf.getOrigin() + Vec4(z_axis, 0.0) * distance);
 		m_localTransformDirty = true;
 	}
+
 	void scale(F32 s)
 	{
-		m_ltrf.getScale() *= s;
+		m_ltrf.setScale(m_ltrf.getScale() * s);
 		m_localTransformDirty = true;
 	}
 

+ 1 - 1
Samples/PhysicsPlayground/Main.cpp

@@ -155,7 +155,7 @@ Error MyApp::sampleExtraInit()
 			monkey->newComponent<ModelComponent>()->loadModelResource("Assets/Suzanne_dynamic_36043dae41fe12d5.ankimdl");
 
 			Transform trf(Vec4(-4.3f, 12.0f, -3.0f, 0.0f), Mat3x4::getIdentity(), 1.0f);
-			trf.getOrigin().y() -= F32(i) * 1.25f;
+			trf.setOrigin(trf.getOrigin() - Vec4(0.0f, F32(i) * 1.25f, 0.0f, 0.0f));
 			// trf.getOrigin().x() -= i * 0.25f;
 
 			// monkey->getFirstComponentOfType<MoveComponent>().setLocalTransform(trf);