Daniele Bartolini 8 år sedan
förälder
incheckning
59b0dae899

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

@@ -48,7 +48,7 @@ f32 ray_sphere_intersection(const Vector3& from, const Vector3& dir, const Spher
 	if (det < 0.0f || b < s.r)
 		return -1.0f;
 
-	return b - sqrtf(det);
+	return b - fsqrt(det);
 }
 
 // http://www.opengl-tutorial.org/miscellaneous/clicking-on-objects/picking-with-custom-ray-obb-function/

+ 148 - 0
src/core/math/math.cpp

@@ -0,0 +1,148 @@
+/*
+ * Copyright (c) 2012-2017 Daniele Bartolini and individual contributors.
+ * License: https://github.com/dbartolini/crown/blob/master/LICENSE
+ */
+
+#include "core/math/math.h"
+#include "core/types.h"
+#include <math.h>
+
+namespace crown
+{
+/// @addtogroup Math
+/// @{
+
+/// Returns whether @a a and @a b are equal according to @a epsilon.
+bool fequal(f32 a, f32 b, f32 epsilon)
+{
+	return b <= (a + epsilon)
+		&& b >= (a - epsilon)
+		;
+}
+
+/// Returns the minimum of @a a and @a b.
+f32 fmin(f32 a, f32 b)
+{
+	return a < b ? a : b;
+}
+
+/// Returns the maximum of @a a and @a b.
+f32 fmax(f32 a, f32 b)
+{
+	return a < b ? b : a;
+}
+
+/// Clamps @a val to @a min and @a max.
+f32 fclamp(f32 min, f32 max, f32 val)
+{
+	return fmin(fmax(min, val), max);
+}
+
+/// Returns the fractional part of @a a.
+f32 ffract(f32 a)
+{
+	return a - floorf(a);
+}
+
+/// Returns the absolute value of @a a.
+f32 fabs(f32 a)
+{
+	return ::fabsf(a);
+}
+
+f32 fsin(f32 a)
+{
+	return sinf(a);
+}
+
+f32 fcos(f32 a)
+{
+	return cosf(a);
+}
+
+/// Returns the arc cosine of @a a.
+f32 facos(f32 a)
+{
+	return ::acosf(a);
+}
+
+f32 ftan(f32 a)
+{
+	return tanf(a);
+}
+
+f32 fsqrt(f32 a)
+{
+	return fsqrt(a);
+}
+
+/// Returns @a deg in radians.
+f32 frad(f32 deg)
+{
+	return deg * PI / 180.0f;
+}
+
+/// Returns @a rad in degrees.
+f32 fdeg(f32 rad)
+{
+	return rad * 180.0f / PI;
+}
+
+/// Returns the linear interpolated value between @a p0 and @a p1 at time @a t
+f32 lerp(const f32 p0, const f32 p1, f32 t)
+{
+	return (1.0f - t) * p0 + t * p1;
+}
+
+/// Returns the cosine interpolated value between @a p0 and @a p1 at time @a t
+f32 cosine(const f32 p0, const f32 p1, f32 t)
+{
+	const f32 f = t * PI;
+	const f32 g = (1.0f - fcos(f)) * 0.5f;
+
+	return p0 + g * (p1 - p0);
+}
+
+/// Returns the cubic interpolated value between @a p0 and @a p1 at time @a t
+f32 cubic(const f32 p0, const f32 p1, f32 t)
+{
+	const f32 tt  = t * t;
+	const f32 ttt = tt * t;
+
+	return p0 * (2.0f * ttt - 3.0f * tt + 1.0f) + p1 * (3.0f * tt  - 2.0f * ttt);
+}
+
+/// Bezier interpolation
+f32 bezier(const f32 p0, const f32 p1, const f32 p2, const f32 p3, f32 t)
+{
+	const f32 u   = 1.0f - t;
+	const f32 tt  = t * t ;
+	const f32 uu  = u * u;
+	const f32 uuu = uu * u;
+	const f32 ttt = tt * t;
+
+	const f32 tmp = (uuu * p0)
+		+ (3.0f * uu * t * p1)
+		+ (3.0f * u * tt * p2)
+		+ (ttt * p3);
+
+	return tmp;
+}
+
+/// Catmull-Rom interpolation
+f32 catmull_rom(const f32 p0, const f32 p1, const f32 p2, const f32 p3, f32 t)
+{
+	const f32 tt  = t * t;
+	const f32 ttt = tt * t;
+
+	const f32 tmp = (2.0f * p1)
+		+ (-p0 + p2) * t
+		+ ((2.0f * p0) - (5.0f * p1) + (4.0f * p2) - p3) * tt
+		+ (-p0 + (3.0f * p1) + (-3.0f * p2) + p3) * ttt;
+
+	return tmp * 0.5f;
+}
+
+/// @}
+
+} // namespace crown

+ 26 - 84
src/core/math/math.h

@@ -6,7 +6,6 @@
 #pragma once
 
 #include "core/types.h"
-#include <math.h>
 
 namespace crown
 {
@@ -19,115 +18,58 @@ static const f32 PI_HALF       = 1.57079632679489661923f;
 static const f32 FLOAT_EPSILON = 1.0e-7f;
 
 /// Returns whether @a a and @a b are equal according to @a epsilon.
-inline bool fequal(f32 a, f32 b, f32 epsilon = FLOAT_EPSILON)
-{
-	return b <= (a + epsilon)
-		&& b >= (a - epsilon)
-		;
-}
+bool fequal(f32 a, f32 b, f32 epsilon = FLOAT_EPSILON);
 
 /// Returns the minimum of @a a and @a b.
-inline f32 fmin(f32 a, f32 b)
-{
-	return a < b ? a : b;
-}
+f32 fmin(f32 a, f32 b);
 
 /// Returns the maximum of @a a and @a b.
-inline f32 fmax(f32 a, f32 b)
-{
-	return a < b ? b : a;
-}
+f32 fmax(f32 a, f32 b);
 
 /// Clamps @a val to @a min and @a max.
-inline f32 fclamp(f32 min, f32 max, f32 val)
-{
-	return fmin(fmax(min, val), max);
-}
+f32 fclamp(f32 min, f32 max, f32 val);
 
 /// Returns the fractional part of @a a.
-inline f32 ffract(f32 a)
-{
-	return a - floorf(a);
-}
+f32 ffract(f32 a);
 
 /// Returns the absolute value of @a a.
-inline f32 fabs(f32 a)
-{
-	return ::fabsf(a);
-}
+f32 fabs(f32 a);
+
+/// Returns the sine of @a a.
+f32 fsin(f32 a);
+
+/// Returns the cosine of @a a.
+f32 fcos(f32 a);
 
 /// Returns the arc cosine of @a a.
-inline f32 facos(f32 a)
-{
-	return ::acosf(a);
-}
+f32 facos(f32 a);
+
+/// Returns the tangent of @a a.
+f32 ftan(f32 a);
+
+/// Returns the nonnegative square root of @a a.
+f32 fsqrt(f32 a);
 
 /// Returns @a deg in radians.
-inline f32 frad(f32 deg)
-{
-	return deg * PI / 180.0f;
-}
+f32 frad(f32 deg);
 
 /// Returns @a rad in degrees.
-inline f32 fdeg(f32 rad)
-{
-	return rad * 180.0f / PI;
-}
+f32 fdeg(f32 rad);
 
 /// Returns the linear interpolated value between @a p0 and @a p1 at time @a t
-inline f32 lerp(const f32 p0, const f32 p1, f32 t)
-{
-	return (1.0f - t) * p0 + t * p1;
-}
+f32 lerp(const f32 p0, const f32 p1, f32 t);
 
 /// Returns the cosine interpolated value between @a p0 and @a p1 at time @a t
-inline f32 cosine(const f32 p0, const f32 p1, f32 t)
-{
-	const f32 f = t * PI;
-	const f32 g = (1.0f - cosf(f)) * 0.5f;
-
-	return p0 + g * (p1 - p0);
-}
+f32 cosine(const f32 p0, const f32 p1, f32 t);
 
 /// Returns the cubic interpolated value between @a p0 and @a p1 at time @a t
-inline f32 cubic(const f32 p0, const f32 p1, f32 t)
-{
-	const f32 tt  = t * t;
-	const f32 ttt = tt * t;
-
-	return p0 * (2.0f * ttt - 3.0f * tt + 1.0f) + p1 * (3.0f * tt  - 2.0f * ttt);
-}
+f32 cubic(const f32 p0, const f32 p1, f32 t);
 
 /// Bezier interpolation
-inline f32 bezier(const f32 p0, const f32 p1, const f32 p2, const f32 p3, f32 t)
-{
-	const f32 u   = 1.0f - t;
-	const f32 tt  = t * t ;
-	const f32 uu  = u * u;
-	const f32 uuu = uu * u;
-	const f32 ttt = tt * t;
-
-	const f32 tmp = (uuu * p0)
-		+ (3.0f * uu * t * p1)
-		+ (3.0f * u * tt * p2)
-		+ (ttt * p3);
-
-	return tmp;
-}
+f32 bezier(const f32 p0, const f32 p1, const f32 p2, const f32 p3, f32 t);
 
 /// Catmull-Rom interpolation
-inline f32 catmull_rom(const f32 p0, const f32 p1, const f32 p2, const f32 p3, f32 t)
-{
-	const f32 tt  = t * t;
-	const f32 ttt = tt * t;
-
-	const f32 tmp = (2.0f * p1)
-		+ (-p0 + p2) * t
-		+ ((2.0f * p0) - (5.0f * p1) + (4.0f * p2) - p3) * tt
-		+ (-p0 + (3.0f * p1) + (-3.0f * p2) + p3) * ttt;
-
-	return tmp * 0.5f;
-}
+f32 catmull_rom(const f32 p0, const f32 p1, const f32 p2, const f32 p3, f32 t);
 
 /// @}
 

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

@@ -294,7 +294,7 @@ inline Matrix4x4 operator*(Matrix4x4 a, const Matrix4x4& b)
 /// Sets the matrix @a m to perspective.
 inline void perspective(Matrix4x4& m, f32 fovy, f32 aspect, f32 nnear, f32 ffar)
 {
-	const f32 height = 1.0f / tanf(fovy * 0.5f);
+	const f32 height = 1.0f / ftan(fovy * 0.5f);
 	const f32 width = height * 1.0f / aspect;
 	const f32 aa = ffar / (ffar - nnear);
 	const f32 bb = -nnear * aa;

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

@@ -37,7 +37,7 @@ Quaternion quaternion(const Matrix3x3& m)
 		index = 3;
 	}
 
-	const f32 biggest = sqrtf(max + 1.0f) * 0.5f;
+	const f32 biggest = fsqrt(max + 1.0f) * 0.5f;
 	const f32 mult = 0.25f / biggest;
 
 	Quaternion tmp;

+ 5 - 5
src/core/math/quaternion.h

@@ -29,8 +29,8 @@ inline Quaternion quaternion(f32 x, f32 y, f32 z, f32 w)
 inline Quaternion quaternion(const Vector3& axis, f32 angle)
 {
 	const f32 ha = angle * 0.5f;
-	const f32 sa = sinf(ha);
-	const f32 ca = cosf(ha);
+	const f32 sa = fsin(ha);
+	const f32 ca = fcos(ha);
 	Quaternion q;
 	q.x = axis.x * sa;
 	q.y = axis.y * sa;
@@ -94,7 +94,7 @@ inline f32 dot(const Quaternion& a, const Quaternion& b)
 /// Returns the length of @a q.
 inline f32 length(const Quaternion& q)
 {
-	return sqrtf(dot(q, q));
+	return fsqrt(dot(q, q));
 }
 
 /// Normalizes the quaternion @a q and returns the result.
@@ -135,10 +135,10 @@ inline Quaternion power(const Quaternion& q, f32 exp)
 	{
 		const f32 alpha = facos(q.w); // alpha = theta/2
 		const f32 new_alpha = alpha * exp;
-		const f32 mult = sinf(new_alpha) / sinf(alpha);
+		const f32 mult = fsin(new_alpha) / fsin(alpha);
 
 		Quaternion r;
-		r.w = cosf(new_alpha);
+		r.w = fcos(new_alpha);
 		r.x = q.x * mult;
 		r.y = q.y * mult;
 		r.z = q.z * mult;

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

@@ -21,7 +21,7 @@ namespace sphere
 			rr = fmax(rr, length_squared(pi - s.c));
 		}
 
-		s.r = sqrtf(rr);
+		s.r = fsqrt(rr);
 	}
 
 	void add_spheres(Sphere& s, u32 num, const Sphere* spheres)
@@ -34,7 +34,7 @@ namespace sphere
 			if (dist < (si.r + s.r) * (si.r + s.r))
 			{
 				if (si.r*si.r > s.r*s.r)
-					s.r = sqrtf(dist + si.r*si.r);
+					s.r = fsqrt(dist + si.r*si.r);
 			}
 		}
 	}

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

@@ -104,7 +104,7 @@ inline f32 length_squared(const Vector2& a)
 /// Returns the length of @a a.
 inline f32 length(const Vector2& a)
 {
-	return sqrtf(length_squared(a));
+	return fsqrt(length_squared(a));
 }
 
 /// Normalizes @a a and returns the result.

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

@@ -119,7 +119,7 @@ inline f32 length_squared(const Vector3& a)
 /// Returns the length of @a a.
 inline f32 length(const Vector3& a)
 {
-	return sqrtf(length_squared(a));
+	return fsqrt(length_squared(a));
 }
 
 /// Normalizes @a a and returns the result.

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

@@ -118,7 +118,7 @@ inline f32 length_squared(const Vector4& a)
 /// Returns the length of @a a.
 inline f32 length(const Vector4& a)
 {
-	return sqrtf(length_squared(a));
+	return fsqrt(length_squared(a));
 }
 
 /// Normalizes @a a and returns the result.

+ 6 - 3
src/resource/expression_language.cpp

@@ -6,6 +6,8 @@
 #include <limits.h>
 #include <stdlib.h>
 
+namespace crown
+{
 namespace skinny { namespace expression_language {
 
 	/// Byte code constants.
@@ -40,7 +42,7 @@ namespace skinny { namespace expression_language {
 
 	inline float length(float a, float b)
 	{
-		return sqrtf((b - a) * (b - a));
+		return fsqrt((b - a) * (b - a));
 	}
 
 	inline float match(float a, float b)
@@ -67,8 +69,8 @@ namespace skinny { namespace expression_language {
 			case OP_MUL: b=POP(); a=POP(); PUSH(a*b); break;
 			case OP_DIV: b=POP(); a=POP(); PUSH(a/b); break;
 			case OP_UNARY_MINUS: PUSH(-POP()); break;
-			case OP_SIN: PUSH(sinf(POP())); break;
-			case OP_COS: PUSH(cosf(POP())); break;
+			case OP_SIN: PUSH(fsin(POP())); break;
+			case OP_COS: PUSH(fcos(POP())); break;
 			case OP_ABS: a = POP(); PUSH(fabs(a)); break;
 			case OP_MATCH: b=POP(); a=POP(); PUSH(match(a, b)); break;
 			case OP_MATCH2D: d=POP(); c=POP(); b=POP(); a=POP(); PUSH(match2d(a,b,c,d)); break;
@@ -484,3 +486,4 @@ namespace skinny { namespace expression_language {
 	}
 
 }} // skinny::expression_langauge
+} // namespace crown

+ 3 - 0
src/resource/expression_language.h

@@ -28,6 +28,8 @@
 /// If you compile offline you can exclude this code in the runtime version.
 #define CAN_COMPILE
 
+namespace crown
+{
 namespace skinny { namespace expression_language {
 	#ifdef CAN_COMPILE
 		/// Compiles the @a source and stores the result in the @a byte_code.
@@ -69,3 +71,4 @@ namespace skinny { namespace expression_language {
 	/// They should match the list of variable names supplied to the compile function.
 	bool run(const unsigned *byte_code, const float *variables, Stack &stack);
 } } // skinny::expression_language
+} // namespace crown

+ 3 - 3
src/world/debug_line.cpp

@@ -69,7 +69,7 @@ void DebugLine::add_arc(const Vector3& center, f32 radius, const Vector3& plane_
 	for (u32 i = 0; i <= segments; ++i)
 	{
 		const f32 t = step * i - PI_HALF;
-		const Vector3 to = center + x*cosf(t) + y*sinf(t);
+		const Vector3 to = center + x*fcos(t) + y*fsin(t);
 		add_line(from, to, color);
 		from = to;
 	}
@@ -94,7 +94,7 @@ void DebugLine::add_circle(const Vector3& center, f32 radius, const Vector3& nor
 	for (u32 i = 0; i <= segments; ++i)
 	{
 		const f32 t = step * i - PI_HALF;
-		const Vector3 to = center + x*cosf(t) + y*sinf(t);
+		const Vector3 to = center + x*fcos(t) + y*fsin(t);
 		add_line(from, to, color);
 		from = to;
 	}
@@ -121,7 +121,7 @@ void DebugLine::add_cone(const Vector3& base_center, const Vector3& tip, f32 rad
 	for (u32 i = 0; i <= segments; ++i)
 	{
 		const f32 t = step * i - PI_HALF;
-		const Vector3 to = base_center + x*cosf(t) + y*sinf(t);
+		const Vector3 to = base_center + x*fcos(t) + y*fsin(t);
 		add_line(from, to, color);
 		add_line(from, tip, color);
 		from = to;

+ 1 - 1
src/world/render_world.cpp

@@ -996,7 +996,7 @@ void RenderWorld::LightManager::debug_draw(u32 start_index, u32 num, DebugLine&
 			{
 				const f32 angle  = _data.spot_angle[i];
 				const f32 range  = _data.range[i];
-				const f32 radius = tan(angle)*range;
+				const f32 radius = ftan(angle)*range;
 				dl.add_cone(pos + range*dir, pos, radius, COLOR4_YELLOW);
 			}
 			break;