소스 검색

Add DebugLine::add_circle

Daniele Bartolini 10 년 전
부모
커밋
bf3ae8ba38
3개의 변경된 파일84개의 추가작업 그리고 35개의 파일을 삭제
  1. 35 4
      src/lua/lua_debug_line.cpp
  2. 44 29
      src/renderers/debug_line.cpp
  3. 5 2
      src/renderers/debug_line.h

+ 35 - 4
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,24 +30,51 @@ 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;
 }
 
@@ -73,6 +103,7 @@ 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);

+ 44 - 29
src/renderers/debug_line.cpp

@@ -156,17 +156,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));
@@ -176,30 +210,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)

+ 5 - 2
src/renderers/debug_line.h

@@ -31,11 +31,14 @@ 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 extents describes the size of the box along the axis.