Forráskód Böngészése

Make everything work with uniform scale

Panagiotis Christopoulos Charitos 9 éve
szülő
commit
7d50937aea

+ 1 - 1
include/anki/math/Axisang.h

@@ -214,7 +214,7 @@ private:
 };
 
 /// F32 Axisang
-typedef TAxisang<F32> Axisang;
+using Axisang = TAxisang<F32>;
 /// @}
 
 } // end namespace anki

+ 1 - 1
include/anki/math/Euler.h

@@ -183,7 +183,7 @@ private:
 };
 
 /// F32 Euler angles
-typedef TEuler<F32> Euler;
+using Euler = TEuler<F32>;
 /// @}
 
 } // end namespace anki

+ 1 - 1
include/anki/math/Mat3.h

@@ -195,7 +195,7 @@ TMat3<T> operator/(T f, const TMat3<T>& m3)
 }
 
 /// F32 3x3 matrix
-typedef TMat3<F32> Mat3;
+using Mat3 = TMat3<F32>;
 static_assert(sizeof(Mat3) == sizeof(F32) * 3 * 3, "Incorrect size");
 /// @}
 

+ 1 - 1
include/anki/math/Mat3x4.h

@@ -262,7 +262,7 @@ TMat3x4<F32> TMat3x4<F32>::combineTransformations(const TMat3x4<F32>& b) const;
 #endif
 
 /// F32 4x4 matrix
-typedef TMat3x4<F32> Mat3x4;
+using Mat3x4 = TMat3x4<F32>;
 static_assert(sizeof(Mat3x4) == sizeof(F32) * 3 * 4, "Incorrect size");
 /// @}
 

+ 1 - 1
include/anki/math/Mat4.h

@@ -476,7 +476,7 @@ void TMat4<F32>::Base::transpose();
 #endif
 
 /// F32 4x4 matrix
-typedef TMat4<F32> Mat4;
+using Mat4 = TMat4<F32>;
 static_assert(sizeof(Mat4) == sizeof(F32) * 4 * 4, "Incorrect size");
 /// @}
 

+ 1 - 1
include/anki/math/Quat.h

@@ -286,7 +286,7 @@ public:
 };
 
 /// F32 quaternion
-typedef TQuat<F32> Quat;
+using Quat = TQuat<F32>;
 /// @}
 
 } // end namespace anki

+ 3 - 3
include/anki/math/Transform.h

@@ -160,7 +160,7 @@ public:
 		o.m_rotation = m_rotation;
 		o.m_rotation.transposeRotationPart();
 		o.m_scale = 1.0 / m_scale;
-		o.m_origin = -((o.m_rotation * o.m_scale) * m_origin).xyz0();
+		o.m_origin = -(o.m_rotation * (o.m_scale * m_origin)).xyz0();
 		return o;
 	}
 
@@ -168,7 +168,7 @@ public:
 	{
 		m_rotation.transposeRotationPart();
 		m_scale = 1.0 / m_scale;
-		m_origin = -((m_rotation * m_scale) * m_origin);
+		m_origin = -(m_rotation * (m_scale * m_origin));
 	}
 
 	/// Transform a TVec3
@@ -209,7 +209,7 @@ private:
 };
 
 /// F32 transformation
-typedef TTransform<F32> Transform;
+using Transform = TTransform<F32>;
 /// @}
 
 } // end namespace anki

+ 4 - 4
include/anki/math/Vec2.h

@@ -91,17 +91,17 @@ TVec2<T> operator/(const T f, const TVec2<T>& b)
 }
 
 /// F32 2D vector
-typedef TVec2<F32> Vec2;
+using Vec2 = TVec2<F32>;
 static_assert(sizeof(Vec2) == sizeof(F32) * 2, "Incorrect size");
 
 /// Half float 2D vector
-typedef TVec2<F16> HVec2;
+using HVec2 = TVec2<F16>;
 
 /// 32bit signed integer 2D vector
-typedef TVec2<I32> IVec2;
+using IVec2 = TVec2<I32>;
 
 /// 32bit unsigned integer 2D vector
-typedef TVec2<U32> UVec2;
+using UVec2 = TVec2<U32>;
 /// @}
 
 } // end namespace anki

+ 4 - 4
include/anki/math/Vec3.h

@@ -124,17 +124,17 @@ TVec3<T> operator/(const T f, const TVec3<T>& v)
 }
 
 /// F32 3D vector
-typedef TVec3<F32> Vec3;
+using Vec3 = TVec3<F32>;
 static_assert(sizeof(Vec3) == sizeof(F32) * 3, "Incorrect size");
 
 /// Half float 3D vector
-typedef TVec3<F16> HVec3;
+using HVec3 = TVec3<F16>;
 
 /// 32bit signed integer 3D vector
-typedef TVec3<I32> IVec3;
+using IVec3 = TVec3<I32>;
 
 /// 32bit unsigned integer 3D vector
-typedef TVec3<U32> UVec3;
+using UVec3 = TVec3<U32>;
 /// @}
 
 } // end namespace anki

+ 7 - 7
include/anki/math/Vec4.h

@@ -17,7 +17,7 @@ namespace anki
 template<typename T>
 struct TVec4Simd
 {
-	typedef Array<T, 4> Type;
+	using Type = Array<T, 4>;
 };
 
 #if ANKI_SIMD == ANKI_SIMD_SSE
@@ -25,14 +25,14 @@ struct TVec4Simd
 template<>
 struct TVec4Simd<F32>
 {
-	typedef __m128 Type;
+	using Type = __m128;
 };
 #elif ANKI_SIMD == ANKI_SIMD_NEON
 // Specialize for F32
 template<>
 struct TVec4Simd<F32>
 {
-	typedef float32x4_t Type;
+	using Type = float32x4_t;
 };
 #endif
 
@@ -220,17 +220,17 @@ TVec4<F32> TVec4<F32>::Base::getAbs() const;
 #endif
 
 /// F32 4D vector
-typedef TVec4<F32> Vec4;
+using Vec4 = TVec4<F32>;
 static_assert(sizeof(Vec4) == sizeof(F32) * 4, "Incorrect size");
 
 /// Half float 4D vector
-typedef TVec4<F16> HVec4;
+using HVec4 = TVec4<F16>;
 
 /// 32bit signed integer 4D vector
-typedef TVec4<I32> IVec4;
+using IVec4 = TVec4<I32>;
 
 /// 32bit unsigned integer 4D vector
-typedef TVec4<U32> UVec4;
+using UVec4 = TVec4<U32>;
 /// @}
 
 } // end namespace anki

+ 4 - 0
include/anki/scene/StaticCollisionNode.h

@@ -23,6 +23,10 @@ public:
 
 	~StaticCollisionNode();
 
+	/// Initialize the node.
+	/// @param[in] name The name of the node.
+	/// @param[in] resourceFname The file to load. It points to a .ankicl file.
+	/// @param[in] transform The transformation. That cannot change.
 	ANKI_USE_RESULT Error init(const CString& name,
 		const CString& resourceFname,
 		const Transform& transform);

+ 9 - 9
tools/scene/Common.cpp

@@ -25,24 +25,24 @@ void log(const char* file, int line, unsigned type, const char* fmt, ...)
 	{
 	case 1:
 		fprintf(stdout,
-			TERMINAL_COL_INFO "(%s:%4d) Info: %s\n" TERMINAL_COL_RESET,
+			TERMINAL_COL_INFO "[I] %s (%s:%d)\n" TERMINAL_COL_RESET,
+			buffer,
 			file,
-			line,
-			buffer);
+			line);
 		break;
 	case 2:
 		fprintf(stderr,
-			TERMINAL_COL_ERROR "(%s:%4d) Error: %s\n" TERMINAL_COL_RESET,
+			TERMINAL_COL_ERROR "[E] %s (%s:%d)\n" TERMINAL_COL_RESET,
+			buffer,
 			file,
-			line,
-			buffer);
+			line);
 		break;
 	case 3:
 		fprintf(stderr,
-			TERMINAL_COL_WARNING "(%s:%4d) Warning: %s\n" TERMINAL_COL_RESET,
+			TERMINAL_COL_WARNING "[W] %s (%s:%d)\n" TERMINAL_COL_RESET,
+			buffer,
 			file,
-			line,
-			buffer);
+			line);
 		break;
 	};
 }

+ 28 - 5
tools/scene/Exporter.cpp

@@ -150,6 +150,25 @@ static void removeScale(aiMatrix4x4& m)
 	m.c3 /= scale;
 }
 
+//==============================================================================
+static float getUniformScale(const aiMatrix4x4& m)
+{
+	const float SCALE_THRESHOLD = 0.01; // 1 cm
+
+	aiVector3D xAxis(m.a1, m.b1, m.c1);
+	aiVector3D yAxis(m.a2, m.b2, m.c2);
+	aiVector3D zAxis(m.a3, m.b3, m.c3);
+
+	float scale = xAxis.Length();
+	if(std::abs(scale - yAxis.Length()) > SCALE_THRESHOLD
+		|| std::abs(scale - zAxis.Length()) > SCALE_THRESHOLD)
+	{
+		ERROR("No uniform scale in the matrix");
+	}
+
+	return scale;
+}
+
 //==============================================================================
 // Exporter                                                                    =
 //==============================================================================
@@ -208,8 +227,9 @@ aiMatrix3x3 Exporter::toAnkiMatrix(const aiMatrix3x3& in) const
 }
 
 //==============================================================================
-void Exporter::writeTransform(const aiMatrix4x4& mat)
+void Exporter::writeTransform(const aiMatrix4x4& inmat)
 {
+	aiMatrix4x4 mat = inmat;
 	std::ofstream& file = m_sceneFile;
 
 	float pos[3];
@@ -222,6 +242,9 @@ void Exporter::writeTransform(const aiMatrix4x4& mat)
 	file << "trf:setOrigin(Vec4.new(" << pos[0] << ", " << pos[1] << ", "
 		 << pos[2] << ", 0))\n";
 
+	float scale = getUniformScale(mat);
+	removeScale(mat);
+
 	file << "rot = Mat3x4.new()\n";
 	file << "rot:setAll(";
 	for(unsigned j = 0; j < 3; j++)
@@ -246,7 +269,7 @@ void Exporter::writeTransform(const aiMatrix4x4& mat)
 	file << ")\n";
 
 	file << "trf:setRotation(rot)\n";
-	file << "trf:setScale(1.0)\n";
+	file << "trf:setScale(" << scale << ")\n";
 }
 
 //==============================================================================
@@ -1099,9 +1122,9 @@ void Exporter::exportAll()
 				exportCollisionMesh(i);
 
 				std::string fname = m_rpath + node.m_collisionMesh + ".ankicl";
-				file << "node = scene:newStaticCollisionNode(\""
-					 << nodeName << "_cl" << "\", \"" << fname
-					 << "\", trf)\n";
+				file << "node = scene:newStaticCollisionNode(\"" << nodeName
+					 << "_cl"
+					 << "\", \"" << fname << "\", trf)\n";
 			}
 			else
 			{