Просмотр исходного кода

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

Daniele Bartolini 10 лет назад
Родитель
Сommit
3f5da40c5b
3 измененных файлов с 59 добавлено и 8 удалено
  1. 17 0
      src/lua/lua_debug_line.cpp
  2. 36 8
      src/renderers/debug_line.cpp
  3. 6 0
      src/renderers/debug_line.h

+ 17 - 0
src/lua/lua_debug_line.cpp

@@ -19,6 +19,21 @@ static int debug_line_add_line(lua_State* L)
 	return 0;
 }
 
+static int debug_line_add_axes(lua_State* L)
+{
+	LuaStack stack(L);
+	const float length = stack.num_args() == 3 ? stack.get_float(3) : 1.0f;
+	stack.get_debug_line(1)->add_axes(stack.get_matrix4x4(2), length);
+	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));
+	return 0;
+}
+
 static int debug_line_add_sphere(lua_State* L)
 {
 	LuaStack stack(L);
@@ -57,6 +72,8 @@ static int debug_line_tostring(lua_State* L)
 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_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);

+ 36 - 8
src/renderers/debug_line.cpp

@@ -154,28 +154,56 @@ void DebugLine::add_line(const Vector3& start, const Vector3& end, const Color4&
 	_num++;
 }
 
+void DebugLine::add_axes(const Matrix4x4& m, float length)
+{
+	const Vector3 pos = translation(m);
+	add_line(pos, pos + x(m)*length, COLOR4_RED);
+	add_line(pos, pos + y(m)*length, COLOR4_GREEN);
+	add_line(pos, pos + z(m)*length, COLOR4_BLUE);
+}
+
+void DebugLine::add_cone(const Vector3& from, const Vector3& to, float radius, const Color4& color)
+{
+	Vector3 dir = to - from;
+	normalize(dir);
+	const Vector3 right = cross(dir, VECTOR3_YAXIS);
+	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));
+
+		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(from + radius*from0, to, color);
+		add_line(from + radius*from0, from + radius*from1, color);
+	}
+}
+
 void DebugLine::add_sphere(const Vector3& center, const float radius, const Color4& color)
 {
 	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);
+		const float rad0 = to_rad(float(deg));
+		const float rad1 = to_rad(float(deg + deg_step));
 
 		// XZ plane
-		const Vector3 start0 = vector3(cos(rad0) * radius, 0, -sin(rad0) * radius);
-		const Vector3 end0   = vector3(cos(rad1) * radius, 0, -sin(rad1) * radius);
+		const Vector3 start0 = vector3(cos(rad0)*radius, 0, -sin(rad0)*radius);
+		const Vector3 end0   = vector3(cos(rad1)*radius, 0, -sin(rad1)*radius);
 		add_line(center + start0, center + end0, color);
 
 		// XY plane
-		const Vector3 start1 = vector3(cos(rad0) * radius, sin(rad0) * radius, 0);
-		const Vector3 end1   = vector3(cos(rad1) * radius, sin(rad1) * radius, 0);
+		const Vector3 start1 = vector3(cos(rad0)*radius, sin(rad0)*radius, 0);
+		const Vector3 end1   = vector3(cos(rad1)*radius, sin(rad1)*radius, 0);
 		add_line(center + start1, center + end1, color);
 
 		// YZ plane
-		const Vector3 start2 = vector3(0, sin(rad0) * radius, -cos(rad0) * radius);
-		const Vector3 end2   = vector3(0, sin(rad1) * radius, -cos(rad1) * radius);
+		const Vector3 start2 = vector3(0, sin(rad0)*radius, -cos(rad0)*radius);
+		const Vector3 end2   = vector3(0, sin(rad1)*radius, -cos(rad1)*radius);
 		add_line(center + start2, center + end2, color);
 	}
 }

+ 6 - 0
src/renderers/debug_line.h

@@ -29,6 +29,12 @@ struct DebugLine
 	/// Adds a line from @a start to @a end with the given @a color.
 	void add_line(const Vector3& start, const Vector3& end, const Color4& color);
 
+	/// Adds lines for each axis with the given @a length.
+	void add_axes(const Matrix4x4& m, float length = 1.0f);
+
+	/// 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);
+
 	/// 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);