فهرست منبع

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

Daniele Bartolini 10 سال پیش
والد
کامیت
6873ce1e4d

+ 1 - 1
docs/doxygen/Doxyfile.doxygen

@@ -547,7 +547,7 @@ INLINE_INFO            = YES
 # name. If set to NO the members will appear in declaration order.
 # The default value is: YES.
 
-SORT_MEMBER_DOCS       = YES
+SORT_MEMBER_DOCS       = NO
 
 # If the SORT_BRIEF_DOCS tag is set to YES then doxygen will sort the brief
 # descriptions of file, namespace and class members alphabetically by member

+ 27 - 14
docs/lua_api.txt

@@ -7,7 +7,7 @@ Vector3
 -------
 
 	**Vector3** (x, y, z) : Vector3
-		Returns a new vector from individual components.
+		Returns a new vector from individual elements.
 
 	**x** (v) : float
 		Returns the x value of the vector.
@@ -36,8 +36,8 @@ Vector3
 	**set_z** (v, z)
 		Sets the value of the z value of the vector.
 
-	**values** (v) : float, float, float
-		Returns the x, y and z values of the vector.
+	**elements** (v) : float, float, float
+		Returns the x, y and z elements of the vector.
 
 	**add** (a, b) : Vector3
 		Adds the vector *a* to *b* and returns the result.
@@ -79,10 +79,10 @@ Vector3
 		Returns the angle between the vectors *a* and *b*.
 
 	**max** (a, b) : Vector3
-		Returns a vector that contains the largest value for each component from *a* and *b*.
+		Returns a vector that contains the largest value for each element from *a* and *b*.
 
 	**min** (a, b) : Vector3
-		Returns a vector that contains the smallest value for each component from *a* and *b*.
+		Returns a vector that contains the smallest value for each element from *a* and *b*.
 
 	**lerp** (a, b, t) : Vector3
 		Returns the linearly interpolated vector between *a* and *b* at time *t* in [0, 1].
@@ -111,13 +111,13 @@ Vector3Box
 ----------
 
 	**Vector3Box** () : Vector3Box
-		Returns a new Vector3Box.
+		Returns a new Vector3Box initialized with the zero vector.
 
 	**Vector3Box** (v) : Vector3Box
 		Returns a new Vector3Box from the Vector3 *v*.
 
 	**Vector3Box** (x, y, z) : Vector3Box
-		Returns a new Vector3Box from components.
+		Returns a new Vector3Box from individual elements.
 
 	**store** (v)
 		Stores the Vector3 *v* in the box.
@@ -164,8 +164,8 @@ Quaternion
 	**power** (q, exp) : Quaternion
 		Returns the quaternion *q* raised to the power of *exp*.
 
-	**elements** (q) : x, y, z, w
-		Returns the elements of the quaternion.
+	**elements** (q) : float, float, float, float
+		Returns the x, y, z and w elements of the quaternion.
 
 	**look** (dir, [up]) : Quaternion
 		Returns the quaternion describing the rotation needed to face towards *dir*.
@@ -180,17 +180,24 @@ Quaternion
 	**forward** (q) : Vector3
 		Returns the forward axis of the rotation described by *q*.
 
+	**lerp** (a, b, t) : Quaternion
+		Returns the linearly interpolated quaternion between *a* and *b* at time *t* in [0, 1].
+		It uses NLerp.
+
+	**to_string** (q) : string
+		Returns a string representing the quaternion *q*.
+
 QuaternionBox
 -------------
 
 	**QuaternionBox** () : QuaternionBox
-		Returns a new QuaternionBox.
+		Returns a new QuaternionBox initialized with the identity quaternion.
 
 	**QuaternionBox** (q) : QuaternionBox
 		Returns a new QuaternionBox from the Quaternion *q*.
 
 	**QuaternionBox** (x, y, z, w) : QuaternionBox
-		Returns a new QuaternionBox from elements.
+		Returns a new QuaternionBox from individual elements.
 
 	**store(q)** ()
 		Stores the Quaternion *q* in the box.
@@ -205,7 +212,7 @@ Matrix4x4
 ---------
 
 	**Matrix4x4** (xx, xy, xz, xw, yx, yy, yz, yw, zx, zy, zz, zw, tx, ty, tz, tw) : Matrix4x4
-		Returns a new matrix from individual components.
+		Returns a new matrix from individual elements.
 
 	**from_quaternion** (q) : Matrix4x4
 		Returns a new matrix from *q*.
@@ -282,6 +289,9 @@ Matrix4x4
 Matrix4x4Box
 ------------
 
+	**Matrix4x4Box** () : Matrix4x4Box
+		Returns a new Matrix4x4Box initialized with the identity matrix.
+
 	**Matrix4x4Box** (m) : Matrix4x4Box
 		Returns a new Matrix4x4Box from the Matrix4x4 *m*.
 
@@ -295,7 +305,10 @@ Color4
 ------
 
 	**Color4** (r, g, b, a) : Color4
-		Returns a new Color4 from individual components.
+		Returns a new Color4 from individual elements.
+
+	**to_string** (c) : string
+		Returns a string representing the color *c*.
 
 Math
 ----
@@ -993,7 +1006,7 @@ Pad Button Names
 Pad Axis Names
 ~~~~~~~~~~~~~~
 
-	* ``left``, ``right``: Returns the direction (x, y) of the left or right thumbstick [-1; +1]. The z component represents the left or right trigger [0; +1].
+	* ``left``, ``right``: Returns the direction (x, y) of the left or right thumbstick [-1; +1]. The z element represents the left or right trigger [0; +1].
 
 Window
 ======

+ 2 - 2
src/core/json/sjson.cpp

@@ -310,7 +310,7 @@ namespace sjson
 		CE_FATAL("Bad array");
 	}
 
-	void parse_root_object(const char* json, Map<DynamicString, const char*>& object)
+	static void parse_root_object(const char* json, Map<DynamicString, const char*>& object)
 	{
 		CE_ASSERT_NOT_NULL(json);
 
@@ -387,7 +387,7 @@ namespace sjson
 			parse_root_object(json, object);
 	}
 
-	void parse_root_object(const char* json, JsonObject& object)
+	static void parse_root_object(const char* json, JsonObject& object)
 	{
 		CE_ASSERT_NOT_NULL(json);
 

+ 3 - 3
src/core/math/color4.h

@@ -13,7 +13,7 @@ namespace crown
 /// @addtogroup Math
 /// @{
 
-/// Returns a new color from individual components.
+/// Returns a new color from individual elements.
 inline Color4 color4(float r, float g, float b, float a)
 {
 	Color4 c;
@@ -24,7 +24,7 @@ inline Color4 color4(float r, float g, float b, float a)
 	return c;
 }
 
-/// Returns a new color from individual components. Alpha is set to 255.
+/// Returns a new color from individual elements. Alpha is set to 255.
 inline Color4 from_rgb(uint8_t r, uint8_t g, uint8_t b)
 {
 	Color4 c;
@@ -35,7 +35,7 @@ inline Color4 from_rgb(uint8_t r, uint8_t g, uint8_t b)
 	return c;
 }
 
-/// Returns a new color from individual components.
+/// Returns a new color from individual elements.
 inline Color4 from_rgba(uint8_t r, uint8_t g, uint8_t b, uint8_t a)
 {
 	Color4 c;

+ 11 - 8
src/core/math/intersection.cpp

@@ -44,7 +44,7 @@ float ray_sphere_intersection(const Vector3& from, const Vector3& dir, const Sph
 	const float b   = dot(v, dir);
 	const float rr  = s.r * s.r;
 	const float bb  = b * b;
-	const float det = rr - dot(v, v) + b;
+	const float det = rr - dot(v, v) + bb;
 
 	if (det < 0.0f || b < s.r)
 		return -1.0f;
@@ -149,20 +149,23 @@ float ray_obb_intersection(const Vector3& from, const Vector3& dir, const Matrix
 	return tmin;
 }
 
-bool plane_3_intersection(const Plane& p1, const Plane& p2, const Plane& p3, Vector3& ip)
+bool plane_3_intersection(const Plane& a, const Plane& b, const Plane& c, Vector3& ip)
 {
-	const Vector3 n1 = p1.n;
-	const Vector3 n2 = p2.n;
-	const Vector3 n3 = p3.n;
-	const float den  = -dot(cross(n1, n2), n3);
+	const Vector3 na = a.n;
+	const Vector3 nb = b.n;
+	const Vector3 nc = c.n;
+	const float den  = -dot(cross(na, nb), nc);
 
 	if (fequal(den, 0.0f))
 		return false;
 
 	const float inv_den = 1.0f / den;
 
-	Vector3 res = p1.d * cross(n2, n3) + p2.d * cross(n3, n1) + p3.d * cross(n1, n2);
-	ip = res * inv_den;
+	const Vector3 nbnc = a.d * cross(nb, nc);
+	const Vector3 ncna = b.d * cross(nc, na);
+	const Vector3 nanb = c.d * cross(na, nb);
+
+	ip = (nbnc + ncna + nanb) * inv_den;
 
 	return true;
 }

+ 6 - 1
src/core/math/intersection.h

@@ -28,8 +28,13 @@ float ray_sphere_intersection(const Vector3& from, const Vector3& dir, const Sph
 /// bounding box (tm, half_extents) or -1.0 if no intersection.
 float ray_obb_intersection(const Vector3& from, const Vector3& dir, const Matrix4x4& tm, const Vector3& half_extents);
 
-bool plane_3_intersection(const Plane& p1, const Plane& p2, const Plane& p3, Vector3& ip);
+/// Returns whether the planes @a a, @a b and @a c intersects and if so fills @a ip with the intersection point.
+bool plane_3_intersection(const Plane& a, const Plane& b, const Plane& c, Vector3& ip);
+
+/// Returns whether the frustum @a f and the sphere @a s intersects.
 bool frustum_sphere_intersection(const Frustum& f, const Sphere& s);
+
+/// Returns whether the frustum @a f and the AABB @a b intersects.
 bool frustum_box_intersection(const Frustum& f, const AABB& b);
 
 /// @}

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

@@ -15,7 +15,7 @@ namespace crown
 /// @addtogroup Math
 /// @{
 
-/// Returns a new matrix from individual components.
+/// Returns a new matrix from individual elements.
 inline Matrix4x4 matrix4x4(float xx, float xy, float xz, float xw
 	, float yx, float yy, float yz, float yw
 	, float zx, float zy, float zz, float zw
@@ -45,7 +45,7 @@ inline Matrix4x4 matrix4x4(float xx, float xy, float xz, float xw
 	return m;
 }
 
-/// Returns a new matrix from individual components.
+/// Returns a new matrix from individual elements.
 inline Matrix4x4 matrix4x4(const float a[16])
 {
 	Matrix4x4 m;

+ 26 - 1
src/core/math/quaternion.h

@@ -14,7 +14,7 @@ namespace crown
 /// @addtogroup Math
 /// @{
 
-/// Returns a new quaternion from individual components.
+/// Returns a new quaternion from individual elements.
 inline Quaternion quaternion(float x, float y, float z, float w)
 {
 	Quaternion q;
@@ -180,5 +180,30 @@ inline Vector3 forward(const Quaternion& q)
 	return m.z;
 }
 
+/// Returns the linearly interpolated quaternion between *a* and *b* at time *t* in [0, 1]. It uses NLerp.
+inline Quaternion lerp(const Quaternion& a, const Quaternion& b, float t)
+{
+	const float t1 = 1.0f - t;
+
+	Quaternion r;
+
+	if (dot(a, b) < 0.0f)
+	{
+		r.x = t1*a.x + t*-b.x;
+		r.y = t1*a.y + t*-b.y;
+		r.z = t1*a.z + t*-b.z;
+		r.w = t1*a.w + t*-b.w;
+	}
+	else
+	{
+		r.x = t1*a.x + t*b.x;
+		r.y = t1*a.y + t*b.y;
+		r.z = t1*a.z + t*b.z;
+		r.w = t1*a.w + t*b.w;
+	}
+
+	return normalize(r);
+}
+
 // @}
 } // namespace crown

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

@@ -13,7 +13,7 @@ namespace crown
 /// @addtogroup Math
 /// @{
 
-/// Returns a new vector from individual components.
+/// Returns a new vector from individual elements.
 inline Vector2 vector2(float x, float y)
 {
 	Vector2 v;
@@ -140,7 +140,7 @@ inline float angle(const Vector2& a, const Vector2& b)
 	return acos(dot(a, b) / (length(a) * length(b)));
 }
 
-/// Returns a vector that contains the largest value for each component from @a a and @a b.
+/// Returns a vector that contains the largest value for each element from @a a and @a b.
 inline Vector2 max(const Vector2& a, const Vector2& b)
 {
 	Vector2 v;
@@ -149,7 +149,7 @@ inline Vector2 max(const Vector2& a, const Vector2& b)
 	return v;
 }
 
-/// Returns a vector that contains the smallest value for each component from @a a and @a b.
+/// Returns a vector that contains the smallest value for each element from @a a and @a b.
 inline Vector2 min(const Vector2& a, const Vector2& b)
 {
 	Vector2 v;

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

@@ -13,7 +13,7 @@ namespace crown
 /// @addtogroup Math
 /// @{
 
-/// Returns a new vector from individual components.
+/// Returns a new vector from individual elements.
 inline Vector3 vector3(float x, float y, float z)
 {
 	Vector3 v;
@@ -157,7 +157,7 @@ inline float angle(const Vector3& a, const Vector3& b)
 	return acos(dot(a, b) / (length(a) * length(b)));
 }
 
-/// Returns a vector that contains the largest value for each component from @a a and @a b.
+/// Returns a vector that contains the largest value for each element from @a a and @a b.
 inline Vector3 max(const Vector3& a, const Vector3& b)
 {
 	Vector3 v;
@@ -167,7 +167,7 @@ inline Vector3 max(const Vector3& a, const Vector3& b)
 	return v;
 }
 
-/// Returns a vector that contains the smallest value for each component from @a a and @a b.
+/// Returns a vector that contains the smallest value for each element from @a a and @a b.
 inline Vector3 min(const Vector3& a, const Vector3& b)
 {
 	Vector3 v;

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

@@ -13,7 +13,7 @@ namespace crown
 /// @addtogroup Math
 /// @{
 
-/// Returns a new vector from individual components.
+/// Returns a new vector from individual elements.
 inline Vector4 vector4(float x, float y, float z, float w)
 {
 	Vector4 v;
@@ -158,7 +158,7 @@ inline float angle(const Vector4& a, const Vector4& b)
 	return acos(dot(a, b) / (length(a) * length(b)));
 }
 
-/// Returns a vector that contains the largest value for each component from @a a and @a b.
+/// Returns a vector that contains the largest value for each element from @a a and @a b.
 inline Vector4 max(const Vector4& a, const Vector4& b)
 {
 	Vector4 v;
@@ -169,7 +169,7 @@ inline Vector4 max(const Vector4& a, const Vector4& b)
 	return v;
 }
 
-/// Returns a vector that contains the smallest value for each component from @a a and @a b.
+/// Returns a vector that contains the smallest value for each element from @a a and @a b.
 inline Vector4 min(const Vector4& a, const Vector4& b)
 {
 	Vector4 v;

+ 4 - 4
src/device.cpp

@@ -38,7 +38,7 @@
 namespace crown
 {
 
-Device::Device(DeviceOptions& opts)
+Device::Device(const DeviceOptions& opts)
 	: _allocator(default_allocator(), MAX_SUBSYSTEMS_HEAP)
 	, _width(0)
 	, _height(0)
@@ -263,9 +263,9 @@ ResourcePackage* Device::create_resource_package(StringId64 id)
 	return CE_NEW(default_allocator(), ResourcePackage)(id, *_resource_manager);
 }
 
-void Device::destroy_resource_package(ResourcePackage& package)
+void Device::destroy_resource_package(ResourcePackage& rp)
 {
-	CE_DELETE(default_allocator(), &package);
+	CE_DELETE(default_allocator(), &rp);
 }
 
 void Device::reload(StringId64 type, StringId64 name)
@@ -443,7 +443,7 @@ void Device::read_config()
 char _buffer[sizeof(Device)];
 Device* _device = NULL;
 
-void init(DeviceOptions& opts)
+void init(const DeviceOptions& opts)
 {
 	CE_ASSERT(_device == NULL, "Crown already initialized");
 	_device = new (_buffer) Device(opts);

+ 9 - 9
src/device.h

@@ -35,8 +35,9 @@ namespace crown
 /// @ingroup Device
 struct Device
 {
-	Device(DeviceOptions& opts);
+	Device(const DeviceOptions& opts);
 
+	/// Initializes the engine.
 	void init();
 
 	/// Shutdowns the engine freeing all the allocated resources.
@@ -51,8 +52,7 @@ struct Device
 	/// Returns a string identifying the engine version.
 	const char* version() const { return CROWN_VERSION_MAJOR "." CROWN_VERSION_MINOR "." CROWN_VERSION_MICRO; }
 
-	/// Returns wheter the engine is running (i.e. it is advancing
-	/// the simulation).
+	/// Returns wheter the engine is running.
 	bool is_running() const;
 
 	/// Return the number of frames rendered.
@@ -91,11 +91,11 @@ struct Device
 	/// Returns the resource package @a id.
 	ResourcePackage* create_resource_package(StringId64 id);
 
-	/// Destroy a previously created resource @a package.
+	/// Destroys the resource package @a rp.
 	/// @note
-	/// To unload the resources loaded by the package, you have to call
-	/// ResourcePackage::unload() first.
-	void destroy_resource_package(ResourcePackage& package);
+	/// Resources are not automatically unloaded.
+	/// You have to call ResourcePackage::unload() before destroying a package.
+	void destroy_resource_package(ResourcePackage& rp);
 
 	/// Reloads the resource @a type @a name.
 	void reload(StringId64 type, StringId64 name);
@@ -136,7 +136,7 @@ private:
 	float _last_delta_time;
 	double _time_since_start;
 
-	DeviceOptions& _device_options;
+	const DeviceOptions& _device_options;
 	Filesystem* _bundle_filesystem;
 	StringId64 _boot_package_id;
 	StringId64 _boot_script_id;
@@ -235,7 +235,7 @@ private:
 };
 
 bool next_event(OsEvent& ev);
-void init(DeviceOptions& opts);
+void init(const DeviceOptions& opts);
 void update();
 void shutdown();
 Device* device();

+ 36 - 8
src/lua/lua_math.cpp

@@ -126,7 +126,7 @@ static int vector3_set_z(lua_State* L)
 	return 0;
 }
 
-static int vector3_values(lua_State* L)
+static int vector3_elements(lua_State* L)
 {
 	LuaStack stack(L);
 	Vector3& a = stack.get_vector3(1);
@@ -301,7 +301,7 @@ static int vector3_to_string(lua_State* L)
 {
 	LuaStack stack(L);
 	const Vector3 v = stack.get_vector3(1);
-	char buf[32];
+	char buf[256];
 	snprintf(buf, sizeof(buf), "%.4f %.4f %.4f", v.x, v.y, v.z);
 	stack.push_string(buf);
 	return 1;
@@ -326,7 +326,7 @@ static int vector3box_new(lua_State* L)
 	LuaStack stack(L);
 
 	if (stack.num_args() == 0)
-		stack.push_vector3box(Vector3());
+		stack.push_vector3box(VECTOR3_ZERO);
 	else if (stack.num_args() == 1)
 		stack.push_vector3box(stack.get_vector3(1));
 	else
@@ -569,7 +569,7 @@ static int matrix4x4_to_string(lua_State* L)
 {
 	LuaStack stack(L);
 	Matrix4x4& a = stack.get_matrix4x4(1);
-	char buf[256];
+	char buf[1024];
 	snprintf(buf, sizeof(buf),
 		"%.4f, %.4f, %.4f, %.4f\n"
 		"%.4f, %.4f, %.4f, %.4f\n"
@@ -587,7 +587,12 @@ static int matrix4x4_to_string(lua_State* L)
 static int matrix4x4box_new(lua_State* L)
 {
 	LuaStack stack(L);
-	stack.push_matrix4x4box(stack.get_matrix4x4(1));
+
+	if (stack.num_args() == 0)
+		stack.push_matrix4x4(MATRIX4X4_IDENTITY);
+	else
+		stack.push_matrix4x4box(stack.get_matrix4x4(1));
+
 	return 1;
 }
 
@@ -744,11 +749,30 @@ static int quaternion_forward(lua_State* L)
 	return 1;
 }
 
+static int quaternion_lerp(lua_State* L)
+{
+	LuaStack stack(L);
+	stack.push_quaternion(lerp(stack.get_quaternion(1), stack.get_quaternion(2), stack.get_float(3)));
+	return 1;
+}
+
+static int quaternion_to_string(lua_State* L)
+{
+	LuaStack stack(L);
+	const Quaternion q = stack.get_quaternion(1);
+	char buf[256];
+	snprintf(buf, sizeof(buf), "%.4f %.4f %.4f %.4f", q.x, q.y, q.z, q.w);
+	stack.push_string(buf);
+	return 1;
+}
+
 static int quaternionbox_new(lua_State* L)
 {
 	LuaStack stack(L);
 
-	if (stack.num_args() == 1)
+	if (stack.num_args() == 0)
+		stack.push_quaternionbox(QUATERNION_IDENTITY);
+	else if (stack.num_args() == 1)
 		stack.push_quaternionbox(stack.get_quaternion(1));
 	else
 		stack.push_quaternionbox(quaternion(stack.get_float(1)
@@ -900,7 +924,7 @@ void load_math(LuaEnvironment& env)
 	env.load_module_function("Vector3", "set_x",            vector3_set_x);
 	env.load_module_function("Vector3", "set_y",            vector3_set_y);
 	env.load_module_function("Vector3", "set_z",            vector3_set_z);
-	env.load_module_function("Vector3", "values",           vector3_values);
+	env.load_module_function("Vector3", "elements",         vector3_elements);
 	env.load_module_function("Vector3", "add",              vector3_add);
 	env.load_module_function("Vector3", "subtract",         vector3_subtract);
 	env.load_module_function("Vector3", "multiply",         vector3_multiply);
@@ -991,6 +1015,8 @@ void load_math(LuaEnvironment& env)
 	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_function("Quaternion", "lerp",               quaternion_lerp);
+	env.load_module_function("Quaternion", "to_string",          quaternion_to_string);
 
 	env.load_module_constructor("Quaternion", quaternion_ctor);
 
@@ -1002,7 +1028,9 @@ void load_math(LuaEnvironment& env)
 
 	env.load_module_constructor("QuaternionBox", quaternionbox_ctor);
 
-	env.load_module_function("Color4", "new", color4_new);
+	env.load_module_function("Color4", "new",       color4_new);
+	env.load_module_function("Color4", "to_string", quaternion_to_string);
+
 	env.load_module_constructor("Color4",     color4_ctor);
 
 	env.load_module_function("Lightuserdata_mt", "__add",      lightuserdata_add);