Преглед на файлове

All drawers in one file

Panagiotis Christopoulos Charitos преди 13 години
родител
ревизия
6f7bb74f0a
променени са 5 файла, в които са добавени 279 реда и са изтрити 71 реда
  1. 0 14
      anki/renderer/Dbg.cpp
  2. 1 29
      anki/renderer/Dbg.h
  3. 181 0
      anki/renderer/Drawer.cpp
  4. 66 0
      anki/renderer/Drawer.h
  5. 31 28
      anki/renderer/RenderingPass.h

+ 0 - 14
anki/renderer/Dbg.cpp

@@ -1,22 +1,8 @@
 #include "anki/renderer/Dbg.h"
 #include "anki/renderer/Renderer.h"
 #include "anki/renderer/CollisionDbgDrawer.h"
-#include "anki/scene/RenderableNode.h"
-#include "anki/scene/Scene.h"
-#include "anki/scene/Camera.h"
-#include "anki/scene/Light.h"
-#include "anki/scene/ParticleEmitterNode.h"
 #include "anki/renderer/RendererInitializer.h"
 #include "anki/renderer/SceneDbgDrawer.h"
-#include "anki/scene/SkinNode.h"
-#include "anki/scene/SpotLight.h"
-#include "anki/scene/Octree.h"
-#include "anki/scene/ModelNode.h"
-#include "anki/resource/Model.h"
-#include <boost/foreach.hpp>
-
-
-extern anki::ModelNode* horse;
 
 
 namespace anki {

+ 1 - 29
anki/renderer/Dbg.h

@@ -21,35 +21,7 @@ namespace anki {
 class Dbg: public SwitchableRenderingPass
 {
 public:
-	enum DebugFlag
-	{
-		DF_NONE = 0,
-		DF_SPATIAL = 1,
-		DF_MOVABLE = 2,
-		DF_FRUSTUMABLE = 4
-	};
-
 	Dbg(Renderer& r_);
-
-	/// @name Flag manipulation
-	/// @{
-	void enableFlag(DebugFlag flag, bool enable = true)
-	{
-		flags = enable ? flags | flag : flags & ~flag;
-	}
-	void disableFlag(DebugFlag flag)
-	{
-		enableFlag(flag, false);
-	}
-	bool isFlagEnabled(DebugFlag flag) const
-	{
-		return flags & flag;
-	}
-	uint getFlagsBitmask() const
-	{
-		return flags;
-	}
-	/// @}
 	
 	void init(const RendererInitializer& initializer);
 	void run();
@@ -90,7 +62,7 @@ private:
 	/// This is a container of some precalculated spheres. Its a map that
 	/// from sphere complexity it returns a vector of lines (Vec3s in
 	/// pairs)
-	std::map<uint, std::vector<Vec3> > complexityToPreCalculatedSphere;
+	std::map<uint, std::vector<Vec3>> complexityToPreCalculatedSphere;
 };
 
 

+ 181 - 0
anki/renderer/Drawer.cpp

@@ -0,0 +1,181 @@
+#include "anki/renderer/Drawer.h"
+#include "anki/resource/ShaderProgram.h"
+
+
+namespace anki {
+
+
+//==============================================================================
+DebugDrawer::DebugDrawer()
+{
+	sProg.load("shaders/Dbg.glsl");
+
+	positionsVbo.create(GL_ARRAY_BUFFER, sizeof(positions), NULL,
+		GL_DYNAMIC_DRAW);
+	colorsVbo.create(GL_ARRAY_BUFFER, sizeof(colors), NULL, GL_DYNAMIC_DRAW);
+	vao.create();
+	const int positionAttribLoc = 0;
+	vao.attachArrayBufferVbo(positionsVbo, positionAttribLoc, 3, GL_FLOAT,
+		GL_FALSE, 0, NULL);
+	const int colorAttribLoc = 1;
+	vao.attachArrayBufferVbo(colorsVbo, colorAttribLoc, 3, GL_FLOAT, GL_FALSE,
+		0, NULL);
+
+	pointIndex = 0;
+	modelMat.setIdentity();
+	crntCol = Vec3(1.0, 0.0, 0.0);
+}
+
+
+//==============================================================================
+void DebugDrawer::drawLine(const Vec3& from, const Vec3& to, const Vec4& color)
+{
+	setColor(color);
+	begin();
+		pushBackVertex(from);
+		pushBackVertex(to);
+	end();
+}
+
+
+//==============================================================================
+void DebugDrawer::drawGrid()
+{
+	Vec4 col0(0.5, 0.5, 0.5, 1.0);
+	Vec4 col1(0.0, 0.0, 1.0, 1.0);
+	Vec4 col2(1.0, 0.0, 0.0, 1.0);
+
+	const float SPACE = 1.0; // space between lines
+	const int NUM = 57;  // lines number. must be odd
+
+	const float GRID_HALF_SIZE = ((NUM - 1) * SPACE / 2);
+
+	setColor(col0);
+
+	begin();
+
+	for(int x = - NUM / 2 * SPACE; x < NUM / 2 * SPACE; x += SPACE)
+	{
+		setColor(col0);
+
+		// if the middle line then change color
+		if(x == 0)
+		{
+			setColor(col1);
+		}
+
+		// line in z
+		pushBackVertex(Vec3(x, 0.0, -GRID_HALF_SIZE));
+		pushBackVertex(Vec3(x, 0.0, GRID_HALF_SIZE));
+
+		// if middle line change col so you can highlight the x-axis
+		if(x == 0)
+		{
+			setColor(col2);
+		}
+
+		// line in the x
+		pushBackVertex(Vec3(-GRID_HALF_SIZE, 0.0, x));
+		pushBackVertex(Vec3(GRID_HALF_SIZE, 0.0, x));
+	}
+
+	// render
+	end();
+}
+
+
+//==============================================================================
+void DebugDrawer::drawSphere(float radius, int complexity)
+{
+	std::vector<Vec3>* sphereLines;
+
+	// Pre-calculate the sphere points5
+	//
+	std::map<uint, std::vector<Vec3> >::iterator it =
+		complexityToPreCalculatedSphere.find(complexity);
+
+	if(it != complexityToPreCalculatedSphere.end()) // Found
+	{
+		sphereLines = &(it->second);
+	}
+	else // Not found
+	{
+		complexityToPreCalculatedSphere[complexity] = std::vector<Vec3>();
+		sphereLines = &complexityToPreCalculatedSphere[complexity];
+
+		float fi = Math::PI / complexity;
+
+		Vec3 prev(1.0, 0.0, 0.0);
+		for(float th = fi; th < Math::PI * 2.0 + fi; th += fi)
+		{
+			Vec3 p = Mat3(Euler(0.0, th, 0.0)) * Vec3(1.0, 0.0, 0.0);
+
+			for(float th2 = 0.0; th2 < Math::PI; th2 += fi)
+			{
+				Mat3 rot(Euler(th2, 0.0, 0.0));
+
+				Vec3 rotPrev = rot * prev;
+				Vec3 rotP = rot * p;
+
+				sphereLines->push_back(rotPrev);
+				sphereLines->push_back(rotP);
+
+				Mat3 rot2(Euler(0.0, 0.0, Math::PI / 2));
+
+				sphereLines->push_back(rot2 * rotPrev);
+				sphereLines->push_back(rot2 * rotP);
+			}
+
+			prev = p;
+		}
+	}
+
+	// Render
+	//
+	modelMat = modelMat * Mat4(Vec3(0.0), Mat3::getIdentity(), radius);
+
+	begin();
+	for(const Vec3& p : *sphereLines)
+	{
+		if(pointIndex >= MAX_POINTS_PER_DRAW)
+		{
+			end();
+			begin();
+		}
+
+		pushBackVertex(p);
+	}
+	end();
+}
+
+
+//==============================================================================
+void DebugDrawer::drawCube(float size)
+{
+	Vec3 maxPos = Vec3(0.5 * size);
+	Vec3 minPos = Vec3(-0.5 * size);
+
+	std::array<Vec3, 8> points = {{
+		Vec3(maxPos.x(), maxPos.y(), maxPos.z()),  // right top front
+		Vec3(minPos.x(), maxPos.y(), maxPos.z()),  // left top front
+		Vec3(minPos.x(), minPos.y(), maxPos.z()),  // left bottom front
+		Vec3(maxPos.x(), minPos.y(), maxPos.z()),  // right bottom front
+		Vec3(maxPos.x(), maxPos.y(), minPos.z()),  // right top back
+		Vec3(minPos.x(), maxPos.y(), minPos.z()),  // left top back
+		Vec3(minPos.x(), minPos.y(), minPos.z()),  // left bottom back
+		Vec3(maxPos.x(), minPos.y(), minPos.z())   // right bottom back
+	}};
+
+	std::array<uint, 24> indeces = {{0, 1, 1, 2, 2, 3, 3, 0, 4, 5, 5, 6, 6,
+		7, 7, 4, 0, 4, 1, 5, 2, 6, 3, 7}};
+
+	begin();
+		for(uint id : indeces)
+		{
+			pushBackVertex(points[id]);
+		}
+	end();
+}
+
+
+}  // namespace anki

+ 66 - 0
anki/renderer/Drawer.h

@@ -0,0 +1,66 @@
+#ifndef ANKI_RENDERER_DRAWER_H
+#define ANKI_RENDERER_DRAWER_H
+
+#include "anki/math/Math.h"
+#include "anki/gl/Vbo.h"
+#include "anki/gl/Vao.h"
+#include "anki/resource/Resource.h"
+#include <array>
+#include <map>
+
+
+namespace anki {
+
+
+/// Draws simple primitives
+class DebugDrawer
+{
+public:
+	DebugDrawer();
+
+	void drawGrid();
+	void drawSphere(float radius, int complexity = 4);
+	void drawCube(float size = 1.0);
+	void drawLine(const Vec3& from, const Vec3& to, const Vec4& color);
+
+	/// @name Render functions. Imitate the GL 1.1 immediate mode
+	/// @{
+	void begin(); ///< Initiates the draw
+	void end(); ///< Draws
+	void pushBackVertex(const Vec3& pos); ///< Something like glVertex
+	/// Something like glColor
+	void setColor(const Vec3& col)
+	{
+		crntCol = col;
+	}
+	/// Something like glColor
+	void setColor(const Vec4& col)
+	{
+		crntCol = Vec3(col);
+	}
+	void setModelMat(const Mat4& modelMat);
+	/// @}
+
+private:
+	ShaderProgramResourcePointer sProg;
+	static const uint MAX_POINTS_PER_DRAW = 256;
+	boost::array<Vec3, MAX_POINTS_PER_DRAW> positions;
+	boost::array<Vec3, MAX_POINTS_PER_DRAW> colors;
+	Mat4 modelMat;
+	uint pointIndex;
+	Vec3 crntCol;
+	Vbo positionsVbo;
+	Vbo colorsVbo;
+	Vao vao;
+
+	/// This is a container of some precalculated spheres. Its a map that
+	/// from sphere complexity it returns a vector of lines (Vec3s in
+	/// pairs)
+	std::map<uint, std::vector<Vec3>> complexityToPreCalculatedSphere;
+};
+
+
+}  // namespace anki
+
+
+#endif

+ 31 - 28
anki/renderer/RenderingPass.h

@@ -12,42 +12,45 @@ struct RendererInitializer;
 /// Rendering pass
 class RenderingPass
 {
-	public:
-		RenderingPass(Renderer& r_)
-		:	r(r_)
-		{}
+public:
+	RenderingPass(Renderer& r_)
+		: r(r_)
+	{}
 
-		/// All passes should have an init
-		virtual void init(const RendererInitializer& initializer) = 0;
+	virtual ~RenderingPass()
+	{}
 
-	protected:
-		Renderer& r; ///< Know your father
+	/// All passes should have an init
+	virtual void init(const RendererInitializer& initializer) = 0;
+
+protected:
+	Renderer& r; ///< Know your father
 };
 
 
 /// Rendering pass that can be enabled or disabled
 class SwitchableRenderingPass: public RenderingPass
 {
-	public:
-		SwitchableRenderingPass(Renderer& r_)
-		:	RenderingPass(r_)
-		{}
-
-		bool getEnabled() const
-		{
-			return enabled;
-		}
-		bool& getEnabled()
-		{
-			return enabled;
-		}
-		void setEnabled(const bool x)
-		{
-			enabled = x;
-		}
-
-	protected:
-		bool enabled;
+public:
+	SwitchableRenderingPass(Renderer& r_)
+		: RenderingPass(r_)
+	{}
+
+	bool getEnabled() const
+	{
+		return enabled;
+	}
+	bool& getEnabled()
+	{
+		return enabled;
+	}
+	void setEnabled(const bool x)
+	{
+		enabled = x;
+	}
+
+protected:
+	bool enabled;
 };