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

Add a renderer for drawing debug stuff

Daniele Bartolini 12 лет назад
Родитель
Сommit
d3001d2966
6 измененных файлов с 243 добавлено и 0 удалено
  1. 2 0
      src/CMakeLists.txt
  2. 1 0
      src/Crown.h
  3. 18 0
      src/Device.cpp
  4. 3 0
      src/Device.h
  5. 144 0
      src/renderers/DebugRenderer.cpp
  6. 75 0
      src/renderers/DebugRenderer.h

+ 2 - 0
src/CMakeLists.txt

@@ -209,6 +209,7 @@ set (INPUT_HEADERS
 )
 
 set (RENDERERS_SRC
+	renderers/DebugRenderer.cpp
 )
 
 set (RENDERERS_HEADERS
@@ -218,6 +219,7 @@ set (RENDERERS_HEADERS
 	renderers/OcclusionQuery.h
 	renderers/Material.h
 	renderers/Texture.h
+	renderers/DebugRenderer.h
 )
 
 set (OS_HEADERS

+ 1 - 0
src/Crown.h

@@ -128,6 +128,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "OcclusionQuery.h"
 #include "Material.h"
 #include "Texture.h"
+#include "DebugRenderer.h"
 
 //// Engine/Windowing
 //#include "Bind.h"

+ 18 - 0
src/Device.cpp

@@ -30,6 +30,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "Log.h"
 #include "OS.h"
 #include "Renderer.h"
+#include "DebugRenderer.h"
 #include "Types.h"
 #include "String.h"
 #include "Args.h"
@@ -62,6 +63,7 @@ Device::Device() :
 	m_filesystem(NULL),
 	m_input_manager(NULL),
 	m_renderer(NULL),
+	m_debug_renderer(NULL),
 
 	m_game(NULL),
 	m_game_library(NULL)
@@ -116,6 +118,8 @@ bool Device::init(int argc, char** argv)
 		#endif
 	}
 
+	m_debug_renderer = new DebugRenderer(*m_renderer);
+
 	Log::I("Crown Engine initialized.");
 
 	Log::I("Initializing Game...");
@@ -170,6 +174,12 @@ void Device::shutdown()
 		delete m_renderer;
 	}
 
+	Log::I("Releasing DebugRenderer...");
+	if (m_debug_renderer)
+	{
+		delete m_debug_renderer;
+	}
+
 	Log::I("Releasing Filesystem...");
 
 	if (m_filesystem)
@@ -204,6 +214,12 @@ Renderer* Device::renderer()
 	return m_renderer;
 }
 
+//-----------------------------------------------------------------------------
+DebugRenderer* Device::debug_renderer()
+{
+	return m_debug_renderer;
+}
+
 //-----------------------------------------------------------------------------
 void Device::start()
 {
@@ -243,6 +259,8 @@ void Device::frame()
 
 	m_game->update();
 
+	m_debug_renderer->draw_all();
+
 	m_renderer->end_frame();
 }
 

+ 3 - 0
src/Device.h

@@ -34,6 +34,7 @@ namespace crown
 
 class Filesystem;
 class Renderer;
+class DebugRenderer;
 class InputManager;
 class Game;
 
@@ -59,6 +60,7 @@ public:
 	Filesystem*				filesystem();
 	InputManager*			input_manager();
 	Renderer*				renderer();
+	DebugRenderer*			debug_renderer();
 
 private:
 
@@ -83,6 +85,7 @@ private:
 	Filesystem*				m_filesystem;
 	InputManager*			m_input_manager;
 	Renderer*				m_renderer;
+	DebugRenderer*			m_debug_renderer;
 
 	// The game currently running
 	Game*					m_game;

+ 144 - 0
src/renderers/DebugRenderer.cpp

@@ -0,0 +1,144 @@
+/*
+Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+#include "DebugRenderer.h"
+#include "Renderer.h"
+#include "MathUtils.h"
+
+namespace crown
+{
+
+//-----------------------------------------------------------------------------
+DebugRenderer::DebugRenderer(Renderer& renderer) :
+	m_renderer(renderer),
+	m_lines_count(0)
+{
+}
+
+//-----------------------------------------------------------------------------
+DebugRenderer::~DebugRenderer()
+{
+}
+
+//-----------------------------------------------------------------------------
+void DebugRenderer::add_line(const Vec3& start, const Vec3& end, const Color4& color, bool depth_write)
+{
+	if (m_lines_count >= MAX_DEBUG_LINES)
+	{
+		return;
+	}
+
+	m_lines[m_lines_count * 2 + 0] = start;
+	m_lines[m_lines_count * 2 + 1] = end;
+
+	m_colors[m_lines_count * 2 + 0] = color;
+	m_colors[m_lines_count * 2 + 1] = color;
+
+	m_depth_writes[m_lines_count * 2 + 0] = depth_write;
+	m_depth_writes[m_lines_count * 2 + 1] = depth_write;
+
+	m_lines_count++;
+}
+
+//-----------------------------------------------------------------------------
+void DebugRenderer::add_sphere(const Vec3& center, const float radius, const Color4& color, bool depth_write)
+{
+	const uint32_t deg_step = 15;
+
+	// XZ plane
+	for (uint32_t deg = 0; deg < 360; deg += deg_step)
+	{
+		float rad0 = math::deg_to_rad(deg);
+		float rad1 = math::deg_to_rad(deg + deg_step);
+
+		Vec3 start(math::cos(rad0) * radius, 0, -math::sin(rad0) * radius);
+		Vec3 end  (math::cos(rad1) * radius, 0, -math::sin(rad1) * radius);
+
+		add_line(center + start, center + end, color, depth_write);
+	}
+
+	// XY plane
+	for (uint32_t deg = 0; deg < 360; deg += deg_step)
+	{
+		float rad0 = math::deg_to_rad(deg);
+		float rad1 = math::deg_to_rad(deg + deg_step);
+
+		Vec3 start(math::cos(rad0) * radius, math::sin(rad0) * radius, 0);
+		Vec3 end  (math::cos(rad1) * radius, math::sin(rad1) * radius, 0);
+
+		add_line(center + start, center + end, color, depth_write);
+	}
+
+	// YZ plane
+	for (uint32_t deg = 0; deg < 360; deg += deg_step)
+	{
+		float rad0 = math::deg_to_rad(deg);
+		float rad1 = math::deg_to_rad(deg + deg_step);
+
+		Vec3 start(0, math::sin(rad0) * radius, -math::cos(rad0) * radius);
+		Vec3 end  (0, math::sin(rad1) * radius, -math::cos(rad1) * radius);
+
+		add_line(center + start, center + end, color, depth_write);
+	}
+}
+
+//-----------------------------------------------------------------------------
+void DebugRenderer::add_box(const Vec3& min, const Vec3& max, const Color4& color, bool depth_write)
+{
+	// Back lines
+	add_line(min                      , Vec3(max.x, min.y, min.z), color, depth_write);
+	add_line(Vec3(max.x, min.y, min.z), Vec3(max.x, max.y, min.z), color, depth_write);
+	add_line(Vec3(max.x, max.y, min.z), Vec3(min.x, max.y, min.z), color, depth_write);
+	add_line(Vec3(min.x, max.y, min.z), min                      , color, depth_write);
+
+	// Front lines
+	add_line(Vec3(min.x, min.y, max.z), Vec3(max.x, min.y, max.z), color, depth_write);
+	add_line(Vec3(max.x, min.y, max.z), Vec3(max.x, max.y, max.z), color, depth_write);
+	add_line(Vec3(max.x, max.y, max.z), Vec3(min.x, max.y, max.z), color, depth_write);
+	add_line(Vec3(min.x, max.y, max.z), Vec3(min.x, min.y, max.z), color, depth_write);
+
+	// Connect back and front vertices
+	add_line(min                      , Vec3(min.x, min.y, max.z), color, depth_write);
+	add_line(Vec3(max.x, min.y, min.z), Vec3(max.x, min.y, max.z), color, depth_write);
+	add_line(Vec3(max.x, max.y, min.z), Vec3(max.x, max.y, max.z), color, depth_write);
+	add_line(Vec3(min.x, max.y, min.z), Vec3(min.x, max.y, max.z), color, depth_write);
+}
+
+//-----------------------------------------------------------------------------
+void DebugRenderer::draw_all()
+{
+	if (m_lines_count > 0)
+	{
+		m_renderer.set_lighting(false);
+		m_renderer.set_texturing(0, false);
+
+		m_renderer.draw_lines(m_lines[0].to_float_ptr(), m_colors[0].to_float_ptr(), m_lines_count * 2);
+	}
+
+	m_lines_count = 0;
+}
+
+} // namespace crown
+

+ 75 - 0
src/renderers/DebugRenderer.h

@@ -0,0 +1,75 @@
+/*
+Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+#pragma once
+
+#include "Types.h"
+#include "Vec3.h"
+#include "Color4.h"
+
+namespace crown
+{
+
+const uint32_t MAX_DEBUG_LINES = 4096;
+
+class Renderer;
+
+/// Util class to render various types of primiteves
+/// for debugging purposes only.
+/// All the coordinates are in world-space.
+class DebugRenderer
+{
+public:
+
+				DebugRenderer(Renderer& renderer);
+				~DebugRenderer();
+
+	void		add_line(const Vec3& start, const Vec3& end, const Color4& color, bool depth_write);
+
+	/// Total cost: 72 lines
+	void		add_sphere(const Vec3& center, const float radius, const Color4& color, bool depth_write);
+
+	/// Total cost: 12 lines
+	void		add_box(const Vec3& min, const Vec3& max, const Color4& color, bool depth_write);
+
+private:
+
+	void		draw_all();
+
+private:
+
+	Renderer&	m_renderer;
+
+	uint32_t	m_lines_count;
+
+	Vec3		m_lines[MAX_DEBUG_LINES * 2];
+	Color4		m_colors[MAX_DEBUG_LINES * 2];
+	bool		m_depth_writes[MAX_DEBUG_LINES * 2];
+
+	friend class	Device;
+};
+
+} // namespace crown
+