Răsfoiți Sursa

Merge branch 'master' of github.com:taylor001/crown

Daniele Bartolini 10 ani în urmă
părinte
comite
aff4319d00

+ 1 - 0
README.md

@@ -4,6 +4,7 @@ Lightweight and flexible cross-platform game engine.
 I'm an independent developer and your contributions are invaluable to me. If you like the work I do, please consider supporting Crown development by means of a small contribution. I'm also available for hire to work on or with Crown or somewhat related technologies/projects.
 I'm an independent developer and your contributions are invaluable to me. If you like the work I do, please consider supporting Crown development by means of a small contribution. I'm also available for hire to work on or with Crown or somewhat related technologies/projects.
 
 
 [![PayPal](https://www.paypalobjects.com/en_US/i/btn/btn_donate_LG.gif)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=6FQMPUQQ8KQKW)
 [![PayPal](https://www.paypalobjects.com/en_US/i/btn/btn_donate_LG.gif)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=6FQMPUQQ8KQKW)
+[![Flattr](http://api.flattr.com/button/flattr-badge-large.png)](https://flattr.com/thing/4945485/Crown)
 
 
 ##What is it?
 ##What is it?
 
 

+ 1 - 1
src/core/math/intersection.cpp

@@ -39,7 +39,7 @@ float ray_sphere_intersection(const Vector3& from, const Vector3& dir, const Sph
 		return -1.0f;
 		return -1.0f;
 	}
 	}
 
 
-	return b - sqrt(det);
+	return b - sqrtf(det);
 }
 }
 
 
 // http://www.opengl-tutorial.org/miscellaneous/clicking-on-objects/picking-with-custom-ray-obb-function/
 // http://www.opengl-tutorial.org/miscellaneous/clicking-on-objects/picking-with-custom-ray-obb-function/

+ 25 - 0
src/core/math/math_types.h

@@ -91,4 +91,29 @@ struct Sphere
 	float r;
 	float r;
 };
 };
 
 
+const Vector2 VECTOR2_ZERO = { 0.0f, 0.0f };
+
+const Vector3 VECTOR3_ZERO     = {  0.0f,  0.0f,  0.0f };
+const Vector3 VECTOR3_XAXIS    = {  1.0f,  0.0f,  0.0f };
+const Vector3 VECTOR3_YAXIS    = {  0.0f,  1.0f,  0.0f };
+const Vector3 VECTOR3_ZAXIS    = {  0.0f,  0.0f,  1.0f };
+const Vector3 VECTOR3_FORWARD  = {  0.0f,  0.0f,  1.0f };
+const Vector3 VECTOR3_BACKWARD = {  0.0f,  0.0f, -1.0f };
+const Vector3 VECTOR3_LEFT     = { -1.0f,  0.0f,  0.0f };
+const Vector3 VECTOR3_RIGHT    = {  1.0f,  0.0f,  0.0f };
+const Vector3 VECTOR3_UP       = {  0.0f,  1.0f,  0.0f };
+const Vector3 VECTOR3_DOWN     = {  0.0f, -1.0f,  0.0f };
+
+const Vector4 VECTOR4_ZERO  = { 0.0f, 0.0f, 0.0f, 0.0f };
+const Vector4 VECTOR4_XAXIS = { 1.0f, 0.0f, 0.0f, 0.0f };
+const Vector4 VECTOR4_YAXIS = { 0.0f, 1.0f, 0.0f, 0.0f };
+const Vector4 VECTOR4_ZAXIS = { 0.0f, 0.0f, 1.0f, 0.0f };
+const Vector4 VECTOR4_WAXIS = { 0.0f, 0.0f, 0.0f, 1.0f };
+
+const Quaternion QUATERNION_IDENTITY = { 0.0f, 0.0f, 0.0f, 1.0f };
+
+const Matrix3x3 MATRIX3X3_IDENTITY = { VECTOR3_XAXIS, VECTOR3_YAXIS, VECTOR3_ZAXIS };
+
+const Matrix4x4 MATRIX4X4_IDENTITY = { VECTOR4_XAXIS, VECTOR4_YAXIS, VECTOR4_ZAXIS, VECTOR4_WAXIS };
+
 } // namespace crown
 } // namespace crown

+ 1 - 61
src/core/math/math_utils.h

@@ -71,66 +71,6 @@ inline bool is_pow_2(uint32_t x)
 	return !(x & (x - 1)) && x;
 	return !(x & (x - 1)) && x;
 }
 }
 
 
-inline float ceil(float x)
-{
-	return ceilf(x);
-}
-
-inline float floor(float x)
-{
-	return floorf(x);
-}
-
-inline float sqrt(float x)
-{
-	return sqrtf(x);
-}
-
-inline float inv_sqrt(float x)
-{
-	return 1.0f / sqrt(x);
-}
-
-inline float sin(float x)
-{
-	return sinf(x);
-}
-
-inline float cos(float x)
-{
-	return cosf(x);
-}
-
-inline float asin(float x)
-{
-	return asinf(x);
-}
-
-inline float acos(float x)
-{
-	return acosf(x);
-}
-
-inline float tan(float x)
-{
-	return tanf(x);
-}
-
-inline float atan2(float y, float x)
-{
-	return atan2f(y, x);
-}
-
-inline float abs(float x)
-{
-	return fabs(x);
-}
-
-inline float fmod(float n, float d)
-{
-	return ::fmod(n, d);
-}
-
 /// Returns the linear interpolated value between @a p0 and @a p1 at time @a t
 /// Returns the linear interpolated value between @a p0 and @a p1 at time @a t
 template <typename T>
 template <typename T>
 inline T linear(const T& p0, const T& p1, float t)
 inline T linear(const T& p0, const T& p1, float t)
@@ -143,7 +83,7 @@ template <typename T>
 inline T cosine(const T& p0, const T& p1, float t)
 inline T cosine(const T& p0, const T& p1, float t)
 {
 {
 	const float f = t * PI;
 	const float f = t * PI;
-	const float g = (1.0f - cos(f)) * 0.5f;
+	const float g = (1.0f - cosf(f)) * 0.5f;
 
 
 	return p0 + (g * (p1 - p0));
 	return p0 + (g * (p1 - p0));
 }
 }

+ 0 - 80
src/core/math/matrix3x3.h

@@ -7,15 +7,12 @@
 
 
 #include "math_types.h"
 #include "math_types.h"
 #include "vector3.h"
 #include "vector3.h"
-#include "quaternion.h"
 
 
 namespace crown
 namespace crown
 {
 {
 /// @addtogroup Math
 /// @addtogroup Math
 /// @{
 /// @{
 
 
-const Matrix3x3 MATRIX3X3_IDENTITY = { VECTOR3_XAXIS, VECTOR3_YAXIS, VECTOR3_ZAXIS };
-
 inline Matrix3x3 matrix3x3(const Vector3& x, const Vector3& y, const Vector3& z)
 inline Matrix3x3 matrix3x3(const Vector3& x, const Vector3& y, const Vector3& z)
 {
 {
 	Matrix3x3 m;
 	Matrix3x3 m;
@@ -235,83 +232,6 @@ inline void set_identity(Matrix3x3& m)
 	m.z.z = 1.0f;
 	m.z.z = 1.0f;
 }
 }
 
 
-/// Returns the rotation portion of the matrix @a m as a Quaternion.
-inline Quaternion rotation(const Matrix3x3& m)
-{
-	const float ww = m.x.x + m.y.y + m.z.z;
-	const float xx = m.x.x - m.y.y - m.z.z;
-	const float yy = m.y.y - m.x.x - m.z.z;
-	const float zz = m.z.z - m.x.x - m.y.y;
-	float max = ww;
-	uint32_t index = 0;
-
-	if (xx > max)
-	{
-		max = xx;
-		index = 1;
-	}
-
-	if (yy > max)
-	{
-		max = yy;
-		index = 2;
-	}
-
-	if (zz > max)
-	{
-		max = zz;
-		index = 3;
-	}
-
-	const float biggest = sqrt(max + 1.0f) * 0.5f;
-	const float mult = 0.25f / biggest;
-
-	Quaternion tmp;
-	switch (index)
-	{
-		case 0:
-		{
-			tmp.w = biggest;
-			tmp.x = (m.y.z - m.z.y) * mult;
-			tmp.y = (m.z.x - m.x.z) * mult;
-			tmp.z = (m.x.y - m.y.x) * mult;
-			break;
-		}
-		case 1:
-		{
-			tmp.x = biggest;
-			tmp.w = (m.y.z - m.z.y) * mult;
-			tmp.y = (m.x.y + m.y.x) * mult;
-			tmp.z = (m.z.x + m.x.z) * mult;
-			break;
-		}
-		case 2:
-		{
-			tmp.y = biggest;
-			tmp.w = (m.z.x - m.x.z) * mult;
-			tmp.x = (m.x.y + m.y.x) * mult;
-			tmp.z = (m.y.z + m.z.y) * mult;
-			break;
-		}
-		case 3:
-		{
-			tmp.z = biggest;
-			tmp.w = (m.x.y - m.y.x) * mult;
-			tmp.x = (m.z.x + m.x.z) * mult;
-			tmp.y = (m.y.z + m.z.y) * mult;
-			break;
-		}
-		default:
-		{
-			CE_FATAL("You should not be here");
-			break;
-		}
-	}
-
-	normalize(tmp);
-	return tmp;
-}
-
 /// Returns the scale of the matrix @a m.
 /// Returns the scale of the matrix @a m.
 inline Vector3 scale(const Matrix3x3& m)
 inline Vector3 scale(const Matrix3x3& m)
 {
 {

+ 4 - 4
src/core/math/matrix4x4.h

@@ -15,8 +15,6 @@ namespace crown
 /// @addtogroup Math
 /// @addtogroup Math
 /// @{
 /// @{
 
 
-const Matrix4x4 MATRIX4X4_IDENTITY = { VECTOR4_XAXIS, VECTOR4_YAXIS, VECTOR4_ZAXIS, VECTOR4_WAXIS };
-
 inline Matrix4x4 matrix4x4(float r1c1, float r2c1, float r3c1, float r4c1,
 inline Matrix4x4 matrix4x4(float r1c1, float r2c1, float r3c1, float r4c1,
 	float r1c2, float r2c2, float r3c2, float r4c2,
 	float r1c2, float r2c2, float r3c2, float r4c2,
 	float r1c3, float r2c3, float r3c3, float r4c3,
 	float r1c3, float r2c3, float r3c3, float r4c3,
@@ -276,7 +274,7 @@ inline Matrix4x4 operator*(Matrix4x4 a, const Matrix4x4& b)
 /// Sets the matrix @a m to perspective.
 /// Sets the matrix @a m to perspective.
 inline void set_perspective(Matrix4x4& m, float fovy, float aspect, float near, float far)
 inline void set_perspective(Matrix4x4& m, float fovy, float aspect, float near, float far)
 {
 {
-	const float height = 1.0f / tan(fovy * ((float) PI / 180.0f) * 0.5f);
+	const float height = 1.0f / tanf(fovy * ((float) PI / 180.0f) * 0.5f);
 	const float width = height * 1.0f / aspect;
 	const float width = height * 1.0f / aspect;
 	const float aa = far / (far - near);
 	const float aa = far / (far - near);
 	const float bb = -near * aa;
 	const float bb = -near * aa;
@@ -582,7 +580,7 @@ inline Matrix3x3 to_matrix3x3(const Matrix4x4& m)
 /// Returns the rotation portion of the matrix @a m as a Quaternion.
 /// Returns the rotation portion of the matrix @a m as a Quaternion.
 inline Quaternion rotation(const Matrix4x4& m)
 inline Quaternion rotation(const Matrix4x4& m)
 {
 {
-	return rotation(to_matrix3x3(m));
+	return quaternion(to_matrix3x3(m));
 }
 }
 
 
 /// Sets the rotation portion of the matrix @a m.
 /// Sets the rotation portion of the matrix @a m.
@@ -591,9 +589,11 @@ inline void set_rotation(Matrix4x4& m, const Matrix3x3& rot)
 	m.x.x = rot.x.x;
 	m.x.x = rot.x.x;
 	m.x.y = rot.x.y;
 	m.x.y = rot.x.y;
 	m.x.z = rot.x.z;
 	m.x.z = rot.x.z;
+
 	m.y.x = rot.y.x;
 	m.y.x = rot.y.x;
 	m.y.y = rot.y.y;
 	m.y.y = rot.y.y;
 	m.y.z = rot.y.z;
 	m.y.z = rot.y.z;
+
 	m.z.x = rot.z.x;
 	m.z.x = rot.z.x;
 	m.z.y = rot.z.y;
 	m.z.y = rot.z.y;
 	m.z.z = rot.z.z;
 	m.z.z = rot.z.z;

+ 88 - 0
src/core/math/quaternion.cpp

@@ -0,0 +1,88 @@
+/*
+ * Copyright (c) 2012-2015 Daniele Bartolini and individual contributors.
+ * License: https://github.com/taylor001/crown/blob/master/LICENSE
+ */
+
+#include "quaternion.h"
+
+namespace crown
+{
+
+/// Returns the rotation portion of the matrix @a m as a Quaternion.
+Quaternion quaternion(const Matrix3x3& m)
+{
+	const float ww = m.x.x + m.y.y + m.z.z;
+	const float xx = m.x.x - m.y.y - m.z.z;
+	const float yy = m.y.y - m.x.x - m.z.z;
+	const float zz = m.z.z - m.x.x - m.y.y;
+	float max = ww;
+	uint32_t index = 0;
+
+	if (xx > max)
+	{
+		max = xx;
+		index = 1;
+	}
+
+	if (yy > max)
+	{
+		max = yy;
+		index = 2;
+	}
+
+	if (zz > max)
+	{
+		max = zz;
+		index = 3;
+	}
+
+	const float biggest = sqrtf(max + 1.0f) * 0.5f;
+	const float mult = 0.25f / biggest;
+
+	Quaternion tmp;
+	switch (index)
+	{
+		case 0:
+		{
+			tmp.w = biggest;
+			tmp.x = (m.y.z - m.z.y) * mult;
+			tmp.y = (m.z.x - m.x.z) * mult;
+			tmp.z = (m.x.y - m.y.x) * mult;
+			break;
+		}
+		case 1:
+		{
+			tmp.x = biggest;
+			tmp.w = (m.y.z - m.z.y) * mult;
+			tmp.y = (m.x.y + m.y.x) * mult;
+			tmp.z = (m.z.x + m.x.z) * mult;
+			break;
+		}
+		case 2:
+		{
+			tmp.y = biggest;
+			tmp.w = (m.z.x - m.x.z) * mult;
+			tmp.x = (m.x.y + m.y.x) * mult;
+			tmp.z = (m.y.z + m.z.y) * mult;
+			break;
+		}
+		case 3:
+		{
+			tmp.z = biggest;
+			tmp.w = (m.x.y - m.y.x) * mult;
+			tmp.x = (m.z.x + m.x.z) * mult;
+			tmp.y = (m.y.z + m.z.y) * mult;
+			break;
+		}
+		default:
+		{
+			CE_FATAL("You should not be here");
+			break;
+		}
+	}
+
+	normalize(tmp);
+	return tmp;
+}
+
+} // namespace crown

+ 45 - 13
src/core/math/quaternion.h

@@ -6,16 +6,15 @@
 #pragma once
 #pragma once
 
 
 #include "types.h"
 #include "types.h"
-#include "vector3.h"
 #include "math_types.h"
 #include "math_types.h"
+#include "math_utils.h"
+#include "matrix3x3.h"
 
 
 namespace crown
 namespace crown
 {
 {
 /// @addtogroup Math
 /// @addtogroup Math
 /// @{
 /// @{
 
 
-const Quaternion QUATERNION_IDENTITY = { 0.0f, 0.0f, 0.0f, 1.0f };
-
 inline Quaternion quaternion(float x, float y, float z, float w)
 inline Quaternion quaternion(float x, float y, float z, float w)
 {
 {
 	Quaternion q;
 	Quaternion q;
@@ -29,13 +28,15 @@ inline Quaternion quaternion(float x, float y, float z, float w)
 inline Quaternion quaternion(const Vector3& axis, float angle)
 inline Quaternion quaternion(const Vector3& axis, float angle)
 {
 {
 	Quaternion q;
 	Quaternion q;
-	q.x = axis.x * sin(angle * 0.5f);
-	q.y = axis.y * sin(angle * 0.5f);
-	q.z = axis.z * sin(angle * 0.5f);
-	q.w = cos(angle * 0.5f);
+	q.x = axis.x * sinf(angle * 0.5f);
+	q.y = axis.y * sinf(angle * 0.5f);
+	q.z = axis.z * sinf(angle * 0.5f);
+	q.w = cosf(angle * 0.5f);
 	return q;
 	return q;
 }
 }
 
 
+Quaternion quaternion(const Matrix3x3& m);
+
 inline Quaternion& operator*=(Quaternion& a, const Quaternion& b)
 inline Quaternion& operator*=(Quaternion& a, const Quaternion& b)
 {
 {
 	const float t_w = a.w*b.w - a.x*b.x - a.y*b.y - a.z*b.z;
 	const float t_w = a.w*b.w - a.x*b.x - a.y*b.y - a.z*b.z;
@@ -87,7 +88,7 @@ inline float dot(const Quaternion& a, const Quaternion& b)
 /// Returns the length of @a q.
 /// Returns the length of @a q.
 inline float length(const Quaternion& q)
 inline float length(const Quaternion& q)
 {
 {
-	return sqrt(q.w * q.w + q.x * q.x + q.y * q.y + q.z * q.z);
+	return sqrtf(q.w * q.w + q.x * q.x + q.y * q.y + q.z * q.z);
 }
 }
 
 
 /// Normalizes the quaternion @a q and returns the result.
 /// Normalizes the quaternion @a q and returns the result.
@@ -121,13 +122,14 @@ inline Quaternion inverse(const Quaternion& q)
 /// Returns the quaternion @a q raised to the power of @a exp.
 /// Returns the quaternion @a q raised to the power of @a exp.
 inline Quaternion power(const Quaternion& q, float exp)
 inline Quaternion power(const Quaternion& q, float exp)
 {
 {
-	if (abs(q.w) < 0.9999)
+	if (fabs(q.w) < 0.9999)
 	{
 	{
-		float alpha = acos(q.w); // alpha = theta/2
-		float new_alpha = alpha * exp;
-		float mult = sin(new_alpha) / sin(alpha);
+		const float alpha = acos(q.w); // alpha = theta/2
+		const float new_alpha = alpha * exp;
+		const float mult = sinf(new_alpha) / sinf(alpha);
+
 		Quaternion tmp;
 		Quaternion tmp;
-		tmp.w = cos(new_alpha);
+		tmp.w = cosf(new_alpha);
 		tmp.x = q.x * mult;
 		tmp.x = q.x * mult;
 		tmp.y = q.y * mult;
 		tmp.y = q.y * mult;
 		tmp.z = q.z * mult;
 		tmp.z = q.z * mult;
@@ -137,5 +139,35 @@ inline Quaternion power(const Quaternion& q, float exp)
 	return q;
 	return q;
 }
 }
 
 
+inline Quaternion look(const Vector3& dir, const Vector3& up = VECTOR3_YAXIS)
+{
+	const Vector3 right = cross(dir, up);
+	const Vector3 nup = cross(right, dir);
+
+	Matrix3x3 m;
+	m.x = -right;
+	m.y = nup;
+	m.z = dir;
+	return quaternion(m);
+}
+
+inline Vector3 right(const Quaternion& q)
+{
+	const Matrix3x3 m = matrix3x3(q);
+	return m.x;
+}
+
+inline Vector3 up(const Quaternion& q)
+{
+	const Matrix3x3 m = matrix3x3(q);
+	return m.y;
+}
+
+inline Vector3 forward(const Quaternion& q)
+{
+	const Matrix3x3 m = matrix3x3(q);
+	return m.z;
+}
+
 // @}
 // @}
 } // namespace crown
 } // namespace crown

+ 2 - 2
src/core/math/sphere.h

@@ -51,7 +51,7 @@ namespace sphere
 
 
 			const float dist = squared_length(*p - s.c);
 			const float dist = squared_length(*p - s.c);
 			if (dist > s.r*s.r)
 			if (dist > s.r*s.r)
-				s.r = sqrt(dist);
+				s.r = sqrtf(dist);
 
 
 			points = (const void*)((const char*)points + stride);
 			points = (const void*)((const char*)points + stride);
 		}
 		}
@@ -71,7 +71,7 @@ namespace sphere
 			if (dist < (spheres[i].r + s.r) * (spheres[i].r + s.r))
 			if (dist < (spheres[i].r + s.r) * (spheres[i].r + s.r))
 			{
 			{
 				if (spheres[i].r * spheres[i].r > s.r * s.r)
 				if (spheres[i].r * spheres[i].r > s.r * s.r)
-					s.r = sqrt(dist + spheres[i].r * spheres[i].r);
+					s.r = sqrtf(dist + spheres[i].r * spheres[i].r);
 			}
 			}
 		}
 		}
 	}
 	}

+ 1 - 3
src/core/math/vector2.h

@@ -14,8 +14,6 @@ namespace crown
 /// @addtogroup Math
 /// @addtogroup Math
 /// @{
 /// @{
 
 
-const Vector2 VECTOR2_ZERO = { 0.0f, 0.0f };
-
 inline Vector2 vector2(float x, float y)
 inline Vector2 vector2(float x, float y)
 {
 {
 	Vector2 v;
 	Vector2 v;
@@ -113,7 +111,7 @@ inline float dot(const Vector2& a, const Vector2& b)
 /// Returns the lenght of @a a.
 /// Returns the lenght of @a a.
 inline float length(const Vector2& a)
 inline float length(const Vector2& a)
 {
 {
-	return sqrt(a.x * a.x + a.y * a.y);
+	return sqrtf(a.x * a.x + a.y * a.y);
 }
 }
 
 
 /// Returns the squared length of @a a.
 /// Returns the squared length of @a a.

+ 1 - 12
src/core/math/vector3.h

@@ -15,17 +15,6 @@ namespace crown
 /// @addtogroup Math
 /// @addtogroup Math
 /// @{
 /// @{
 
 
-const Vector3 VECTOR3_ZERO     = { 0.0f, 0.0f, 0.0f };
-const Vector3 VECTOR3_XAXIS    = { 1.0f, 0.0f, 0.0f };
-const Vector3 VECTOR3_YAXIS    = { 0.0f, 1.0f, 0.0f };
-const Vector3 VECTOR3_ZAXIS    = { 0.0f, 0.0f, 1.0f };
-const Vector3 VECTOR3_FORWARD  = { 0.0f, 0.0f, 1.0f };
-const Vector3 VECTOR3_BACKWARD = { 0.0f, 0.0f, -1.0f };
-const Vector3 VECTOR3_LEFT     = { -1.0f, 0.0f, 0.0f };
-const Vector3 VECTOR3_RIGHT    = { 1.0f, 0.0f, 0.0f };
-const Vector3 VECTOR3_UP       = { 0.0f, 1.0f, 0.0f };
-const Vector3 VECTOR3_DOWN     = { 0.0f, -1.0f, 0.0f };
-
 inline Vector3 vector3(float x, float y, float z)
 inline Vector3 vector3(float x, float y, float z)
 {
 {
 	Vector3 v;
 	Vector3 v;
@@ -135,7 +124,7 @@ inline Vector3 cross(const Vector3& a, const Vector3& b)
 /// Returns the lenght of @a a.
 /// Returns the lenght of @a a.
 inline float length(const Vector3& a)
 inline float length(const Vector3& a)
 {
 {
-	return sqrt(a.x * a.x + a.y * a.y + a.z * a.z);
+	return sqrtf(a.x * a.x + a.y * a.y + a.z * a.z);
 }
 }
 
 
 /// Returns the squared length of @a a.
 /// Returns the squared length of @a a.

+ 1 - 7
src/core/math/vector4.h

@@ -15,12 +15,6 @@ namespace crown
 /// @addtogroup Math
 /// @addtogroup Math
 /// @{
 /// @{
 
 
-const Vector4 VECTOR4_ZERO  = { 0.0f, 0.0f, 0.0f, 0.0f };
-const Vector4 VECTOR4_XAXIS = { 1.0f, 0.0f, 0.0f, 0.0f };
-const Vector4 VECTOR4_YAXIS = { 0.0f, 1.0f, 0.0f, 0.0f };
-const Vector4 VECTOR4_ZAXIS = { 0.0f, 0.0f, 1.0f, 0.0f };
-const Vector4 VECTOR4_WAXIS = { 0.0f, 0.0f, 0.0f, 1.0f };
-
 /// Returns the Vector3 portion of @a a. (i.e. truncates w)
 /// Returns the Vector3 portion of @a a. (i.e. truncates w)
 Vector3 to_vector3(const Vector4& a);
 Vector3 to_vector3(const Vector4& a);
 
 
@@ -146,7 +140,7 @@ inline float dot(const Vector4& a, const Vector4& b)
 /// Returns the lenght of @a a.
 /// Returns the lenght of @a a.
 inline float length(const Vector4& a)
 inline float length(const Vector4& a)
 {
 {
-	return sqrt(a.x * a.x + a.y * a.y + a.z * a.z + a.w * a.w);
+	return sqrtf(a.x * a.x + a.y * a.y + a.z * a.z + a.w * a.w);
 }
 }
 
 
 /// Returns the squared length of @a a.
 /// Returns the squared length of @a a.

+ 33 - 0
src/lua/lua_math.cpp

@@ -646,6 +646,35 @@ static int quaternion_elements(lua_State* L)
 	return 4;
 	return 4;
 }
 }
 
 
+static int quaternion_look(lua_State* L)
+{
+	LuaStack stack(L);
+	const Vector3 up = stack.num_args() == 2 ? stack.get_vector3(2) : VECTOR3_YAXIS;
+	stack.push_quaternion(look(stack.get_vector3(1), up));
+	return 1;
+}
+
+static int quaternion_right(lua_State* L)
+{
+	LuaStack stack(L);
+	stack.push_vector3(right(stack.get_quaternion(1)));
+	return 1;
+}
+
+static int quaternion_up(lua_State* L)
+{
+	LuaStack stack(L);
+	stack.push_vector3(up(stack.get_quaternion(1)));
+	return 1;
+}
+
+static int quaternion_forward(lua_State* L)
+{
+	LuaStack stack(L);
+	stack.push_vector3(forward(stack.get_quaternion(1)));
+	return 1;
+}
+
 static int quaternionbox_new(lua_State* L)
 static int quaternionbox_new(lua_State* L)
 {
 {
 	LuaStack stack(L);
 	LuaStack stack(L);
@@ -889,6 +918,10 @@ void load_math(LuaEnvironment& env)
 	env.load_module_function("Quaternion", "inverse",            quaternion_inverse);
 	env.load_module_function("Quaternion", "inverse",            quaternion_inverse);
 	env.load_module_function("Quaternion", "power",              quaternion_power);
 	env.load_module_function("Quaternion", "power",              quaternion_power);
 	env.load_module_function("Quaternion", "elements",           quaternion_elements);
 	env.load_module_function("Quaternion", "elements",           quaternion_elements);
+	env.load_module_function("Quaternion", "look",               quaternion_look);
+	env.load_module_function("Quaternion", "right",              quaternion_right);
+	env.load_module_function("Quaternion", "up",                 quaternion_up);
+	env.load_module_function("Quaternion", "forward",            quaternion_forward);
 
 
 	env.load_module_constructor("Quaternion", quaternion_ctor);
 	env.load_module_constructor("Quaternion", quaternion_ctor);
 
 

+ 6 - 6
src/renderers/debug_line.cpp

@@ -192,18 +192,18 @@ void DebugLine::add_sphere(const Vector3& center, const float radius, const Colo
 		const float rad1 = to_rad(float(deg + deg_step));
 		const float rad1 = to_rad(float(deg + deg_step));
 
 
 		// XZ plane
 		// XZ plane
-		const Vector3 start0 = vector3(cos(rad0)*radius, 0.0f, -sin(rad0)*radius);
-		const Vector3 end0   = vector3(cos(rad1)*radius, 0.0f, -sin(rad1)*radius);
+		const Vector3 start0 = vector3(cosf(rad0)*radius, 0.0f, -sinf(rad0)*radius);
+		const Vector3 end0   = vector3(cosf(rad1)*radius, 0.0f, -sinf(rad1)*radius);
 		add_line(center + start0, center + end0, color);
 		add_line(center + start0, center + end0, color);
 
 
 		// XY plane
 		// XY plane
-		const Vector3 start1 = vector3(cos(rad0)*radius, sin(rad0)*radius, 0.0f);
-		const Vector3 end1   = vector3(cos(rad1)*radius, sin(rad1)*radius, 0.0f);
+		const Vector3 start1 = vector3(cosf(rad0)*radius, sinf(rad0)*radius, 0.0f);
+		const Vector3 end1   = vector3(cosf(rad1)*radius, sinf(rad1)*radius, 0.0f);
 		add_line(center + start1, center + end1, color);
 		add_line(center + start1, center + end1, color);
 
 
 		// YZ plane
 		// YZ plane
-		const Vector3 start2 = vector3(0.0f, sin(rad0)*radius, -cos(rad0)*radius);
-		const Vector3 end2   = vector3(0.0f, sin(rad1)*radius, -cos(rad1)*radius);
+		const Vector3 start2 = vector3(0.0f, sinf(rad0)*radius, -cosf(rad0)*radius);
+		const Vector3 end2   = vector3(0.0f, sinf(rad1)*radius, -cosf(rad1)*radius);
 		add_line(center + start2, center + end2, color);
 		add_line(center + start2, center + end2, color);
 	}
 	}
 }
 }

+ 2 - 2
src/world/scene_graph.cpp

@@ -152,7 +152,7 @@ Vector3 SceneGraph::local_position(TransformInstance i) const
 
 
 Quaternion SceneGraph::local_rotation(TransformInstance i) const
 Quaternion SceneGraph::local_rotation(TransformInstance i) const
 {
 {
-	return rotation(_data.local[i.i].rotation);
+	return quaternion(_data.local[i.i].rotation);
 }
 }
 
 
 Vector3 SceneGraph::local_scale(TransformInstance i) const
 Vector3 SceneGraph::local_scale(TransformInstance i) const
@@ -162,7 +162,7 @@ Vector3 SceneGraph::local_scale(TransformInstance i) const
 
 
 Matrix4x4 SceneGraph::local_pose(TransformInstance i) const
 Matrix4x4 SceneGraph::local_pose(TransformInstance i) const
 {
 {
-	Matrix4x4 tr = matrix4x4(rotation(_data.local[i.i].rotation), _data.local[i.i].position);
+	Matrix4x4 tr = matrix4x4(quaternion(_data.local[i.i].rotation), _data.local[i.i].position);
 	set_scale(tr, _data.local[i.i].scale);
 	set_scale(tr, _data.local[i.i].scale);
 	return tr;
 	return tr;
 }
 }