Quellcode durchsuchen

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

Daniele Bartolini vor 10 Jahren
Ursprung
Commit
4434c77322
4 geänderte Dateien mit 106 neuen und 54 gelöschten Zeilen
  1. 10 7
      docs/lua_api.txt
  2. 41 10
      src/lua/lua_debug_line.cpp
  3. 46 31
      src/renderers/debug_line.cpp
  4. 9 6
      src/renderers/debug_line.h

+ 10 - 7
docs/lua_api.txt

@@ -759,21 +759,24 @@ DebugLine
 	**add_axes** (debug_line, tm, length)
 		Adds lines for each axis with the given *length*.
 
-	**add_cone** (debug_line, from, to, radius, color)
+	**add_circle** (debug_line, center, radius, normal, color, [segments = 36])
+		Adds a circle at *center* with the given *radius* and *normal* vector.
+
+	**add_cone** (debug_line, from, to, radius, color, [segments = 36])
 		Adds a cone with the base centered at *from* and the tip at *to*.
 
-	**add_sphere** (debug_line, center, radius, color)
-		Adds a sphere at *center* with the given *radius* and *color*.
+	**add_sphere** (debug_line, center, radius, color, [segments = 36])
+		Adds a sphere at *center* with the given *radius*.
 
 	**add_obb** (debug_line, tm, half_extents, color)
 		Adds an orientd bounding box. *tm* describes the position and orientation of
 		the box. *half_extents* describes the size of the box along the axis.
 
-	**clear** (debug_line)
-		Clears all the lines.
+	**reset** (debug_line)
+		Resets all the lines.
 
-	**commit** (debug_line)
-		Sends the lines to renderer for drawing.
+	**submit** (debug_line)
+		Submits the lines to renderer for drawing.
 
 Input
 =====

+ 41 - 10
src/lua/lua_debug_line.cpp

@@ -15,7 +15,10 @@ namespace crown
 static int debug_line_add_line(lua_State* L)
 {
 	LuaStack stack(L);
-	stack.get_debug_line(1)->add_line(stack.get_vector3(2), stack.get_vector3(3), stack.get_color4(4));
+	stack.get_debug_line(1)->add_line(stack.get_vector3(2)
+		, stack.get_vector3(3)
+		, stack.get_color4(4)
+		);
 	return 0;
 }
 
@@ -27,38 +30,65 @@ static int debug_line_add_axes(lua_State* L)
 	return 0;
 }
 
+static int debug_line_add_circle(lua_State* L)
+{
+	LuaStack stack(L);
+	const uint32_t segments = stack.num_args() >= 6 ? stack.get_int(6) : 36;
+	stack.get_debug_line(1)->add_circle(stack.get_vector3(2)
+		, stack.get_float(3)
+		, stack.get_vector3(4)
+		, stack.get_color4(5)
+		, segments
+		);
+	return 0;
+}
+
 static int debug_line_add_cone(lua_State* L)
 {
 	LuaStack stack(L);
-	stack.get_debug_line(1)->add_cone(stack.get_vector3(2), stack.get_vector3(3), stack.get_float(4), stack.get_color4(5));
+	const uint32_t segments = stack.num_args() >= 6 ? stack.get_int(6) : 36;
+	stack.get_debug_line(1)->add_cone(stack.get_vector3(2)
+		, stack.get_vector3(3)
+		, stack.get_float(4)
+		, stack.get_color4(5)
+		, segments
+		);
 	return 0;
 }
 
 static int debug_line_add_sphere(lua_State* L)
 {
 	LuaStack stack(L);
-	stack.get_debug_line(1)->add_sphere(stack.get_vector3(2), stack.get_float(3), stack.get_color4(4));
+	const uint32_t segments = stack.num_args() >= 5 ? stack.get_int(5) : 36;
+	stack.get_debug_line(1)->add_sphere(stack.get_vector3(2)
+		, stack.get_float(3)
+		, stack.get_color4(4)
+		, segments
+		);
 	return 0;
 }
 
 static int debug_line_add_obb(lua_State* L)
 {
 	LuaStack stack(L);
-	stack.get_debug_line(1)->add_obb(stack.get_matrix4x4(2), stack.get_vector3(3), stack.get_color4(4));
+	stack.get_debug_line(1)->add_obb(stack.get_matrix4x4(2)
+		, stack.get_vector3(3)
+		, stack.get_color4(4)
+		);
 	return 0;
 }
 
-static int debug_line_clear(lua_State* L)
+static int debug_line_reset(lua_State* L)
 {
 	LuaStack stack(L);
-	stack.get_debug_line(1)->clear();
+	stack.get_debug_line(1)->reset();
 	return 0;
 }
 
-static int debug_line_commit(lua_State* L)
+static int debug_line_submit(lua_State* L)
 {
 	LuaStack stack(L);
-	stack.get_debug_line(1)->commit();
+	stack.get_debug_line(1)->submit();
 	return 0;
 }
 
@@ -73,11 +103,12 @@ void load_debug_line(LuaEnvironment& env)
 {
 	env.load_module_function("DebugLine", "add_line",   debug_line_add_line);
 	env.load_module_function("DebugLine", "add_axes",   debug_line_add_axes);
+	env.load_module_function("DebugLine", "add_circle", debug_line_add_circle);
 	env.load_module_function("DebugLine", "add_cone",   debug_line_add_cone);
 	env.load_module_function("DebugLine", "add_sphere", debug_line_add_sphere);
 	env.load_module_function("DebugLine", "add_obb",    debug_line_add_obb);
-	env.load_module_function("DebugLine", "clear",      debug_line_clear);
-	env.load_module_function("DebugLine", "commit",     debug_line_commit);
+	env.load_module_function("DebugLine", "reset",      debug_line_reset);
+	env.load_module_function("DebugLine", "submit",     debug_line_submit);
 	env.load_module_function("DebugLine", "__index",    "DebugLine");
 	env.load_module_function("DebugLine", "__tostring", debug_line_tostring);
 }

+ 46 - 31
src/renderers/debug_line.cpp

@@ -162,17 +162,51 @@ void DebugLine::add_axes(const Matrix4x4& m, float length)
 	add_line(pos, pos + z(m)*length, COLOR4_BLUE);
 }
 
-void DebugLine::add_cone(const Vector3& from, const Vector3& to, float radius, const Color4& color)
+void DebugLine::add_circle(const Vector3& center, float radius, const Vector3& normal, const Color4& color, uint32_t segments)
+{
+	const Vector3 dir = normal;
+	const Vector3 arr[] =
+	{
+		{ dir.z, dir.z, -dir.x -dir.y },
+		{ -dir.y -dir.z, dir.x, dir.x }
+	};
+	const int idx = ((dir.z != 0.0f) && (-dir.x != dir.y));
+	Vector3 right = arr[idx];
+	normalize(right);
+
+	const float incr = 360.0f / (float)(segments >= 3 ? segments : 3);
+	float deg0 = 0.0f;
+	for (uint32_t ss = 0; ss < segments; ++ss, deg0 += incr)
+	{
+		const float rad0 = to_rad(deg0);
+		const float rad1 = to_rad(deg0 + incr);
+
+		const Vector3 from0 = right*cos(-rad0) + cross(dir, right)*sin(-rad0) + dir*dot(dir, right)*(1.0f-cos(-rad0));
+		const Vector3 from1 = right*cos(-rad1) + cross(dir, right)*sin(-rad1) + dir*dot(dir, right)*(1.0f-cos(-rad1));
+
+		add_line(center + radius*from0, center + radius*from1, color);
+	}
+}
+
+void DebugLine::add_cone(const Vector3& from, const Vector3& to, float radius, const Color4& color, uint32_t segments)
 {
 	Vector3 dir = to - from;
 	normalize(dir);
-	const Vector3 right = cross(dir, VECTOR3_YAXIS);
-	const uint32_t deg_step = 15;
+	const Vector3 arr[] =
+	{
+		{ dir.z, dir.z, -dir.x -dir.y },
+		{ -dir.y -dir.z, dir.x, dir.x }
+	};
+	const int idx = ((dir.z != 0.0f) && (-dir.x != dir.y));
+	Vector3 right = arr[idx];
+	normalize(right);
 
-	for (uint32_t deg = 0; deg < 360; deg += deg_step)
+	const float incr = 360.0f / (float)(segments >= 3 ? segments : 3);
+	float deg0 = 0.0f;
+	for (uint32_t ss = 0; ss < segments; ++ss, deg0 += incr)
 	{
-		const float rad0 = to_rad(float(deg));
-		const float rad1 = to_rad(float(deg + deg_step));
+		const float rad0 = to_rad(deg0);
+		const float rad1 = to_rad(deg0 + incr);
 
 		const Vector3 from0 = right*cos(-rad0) + cross(dir, right)*sin(-rad0) + dir*dot(dir, right)*(1.0f-cos(-rad0));
 		const Vector3 from1 = right*cos(-rad1) + cross(dir, right)*sin(-rad1) + dir*dot(dir, right)*(1.0f-cos(-rad1));
@@ -182,30 +216,11 @@ void DebugLine::add_cone(const Vector3& from, const Vector3& to, float radius, c
 	}
 }
 
-void DebugLine::add_sphere(const Vector3& center, const float radius, const Color4& color)
+void DebugLine::add_sphere(const Vector3& center, const float radius, const Color4& color, uint32_t segments)
 {
-	const uint32_t deg_step = 15;
-
-	for (uint32_t deg = 0; deg < 360; deg += deg_step)
-	{
-		const float rad0 = to_rad(float(deg));
-		const float rad1 = to_rad(float(deg + deg_step));
-
-		// XZ plane
-		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);
-
-		// XY plane
-		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);
-
-		// YZ plane
-		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_circle(center, radius, VECTOR3_XAXIS, color, segments);
+	add_circle(center, radius, VECTOR3_YAXIS, color, segments);
+	add_circle(center, radius, VECTOR3_ZAXIS, color, segments);
 }
 
 void DebugLine::add_obb(const Matrix4x4& tm, const Vector3& half_extents, const Color4& color)
@@ -232,12 +247,12 @@ void DebugLine::add_obb(const Matrix4x4& tm, const Vector3& half_extents, const
 	add_line(o - x + y - z, o - x + y + z, color);
 }
 
-void DebugLine::clear()
+void DebugLine::reset()
 {
 	_num = 0;
 }
 
-void DebugLine::commit()
+void DebugLine::submit()
 {
 	if (!_num)
 		return;

+ 9 - 6
src/renderers/debug_line.h

@@ -32,21 +32,24 @@ struct DebugLine
 	/// Adds lines for each axis with the given @a length.
 	void add_axes(const Matrix4x4& m, float length = 1.0f);
 
+	/// Adds a circle at @a center with the given @a radius and @a normal vector.
+	void add_circle(const Vector3& center, float radius, const Vector3& normal, const Color4& color, uint32_t segments = 36);
+
 	/// Adds a cone with the base centered at @a from and the tip at @a to.
-	void add_cone(const Vector3& from, const Vector3& to, float radius, const Color4& color);
+	void add_cone(const Vector3& from, const Vector3& to, float radius, const Color4& color, uint32_t segments = 36);
 
 	/// Adds a sphere at @a center with the given @a radius and @a color.
-	void add_sphere(const Vector3& center, const float radius, const Color4& color);
+	void add_sphere(const Vector3& center, const float radius, const Color4& color, uint32_t segments = 36);
 
 	/// Adds an orientd bounding box. @a tm describes the position and orientation of
 	/// the box. @a half_extents describes the size of the box along the axis.
 	void add_obb(const Matrix4x4& tm, const Vector3& half_extents, const Color4& color);
 
-	/// Clears all the lines.
-	void clear();
+	/// Resets all the lines.
+	void reset();
 
-	/// Sends the lines to renderer for drawing.
-	void commit();
+	/// Submits the lines to renderer for drawing.
+	void submit();
 
 public: