Panagiotis Christopoulos Charitos пре 15 година
родитељ
комит
841939fa65
3 измењених фајлова са 90 додато и 22 уклоњено
  1. 2 2
      src/Physics/DebugDrawer.cpp
  2. 63 17
      src/Renderer/Dbg.cpp
  3. 25 3
      src/Renderer/Dbg.h

+ 2 - 2
src/Physics/DebugDrawer.cpp

@@ -36,7 +36,7 @@ void DebugDrawer::drawBox(const btVector3& min, const btVector3& max, const btVe
 	trf(1, 3) = (max.getY() + min.getY()) / 2.0;
 	trf(2, 3) = (max.getZ() + min.getZ()) / 2.0;
 	app->getMainRenderer().getDbg().setModelMat(trf);
-	app->getMainRenderer().getDbg().setColor(Vec4(toAnki(color), 1.0));
+	app->getMainRenderer().getDbg().setColor(toAnki(color));
 	app->getMainRenderer().getDbg().drawCube(1.0);
 }
 
@@ -55,7 +55,7 @@ void DebugDrawer::drawBox(const btVector3& min, const btVector3& max, const btTr
 	trf(2, 3) = (max.getZ() + min.getZ()) / 2.0;
 	trf = Mat4::combineTransformations(Mat4(toAnki(trans)), trf);
 	app->getMainRenderer().getDbg().setModelMat(trf);
-	app->getMainRenderer().getDbg().setColor(Vec4(toAnki(color), 1.0));
+	app->getMainRenderer().getDbg().setColor(toAnki(color));
 	app->getMainRenderer().getDbg().drawCube(1.0);
 }
 

+ 63 - 17
src/Renderer/Dbg.cpp

@@ -24,7 +24,7 @@ Dbg::Dbg(Renderer& r_, Object* parent):
 //======================================================================================================================
 void Dbg::drawLine(const Vec3& from, const Vec3& to, const Vec4& color)
 {
-	float posBuff [] = {from.x, from.y, from.z, to.x, to.y, to.z};
+	/*float posBuff [] = {from.x, from.y, from.z, to.x, to.y, to.z};
 
 	setColor(color);
 	setModelMat(Mat4::getIdentity());
@@ -32,7 +32,7 @@ void Dbg::drawLine(const Vec3& from, const Vec3& to, const Vec4& color)
 	glEnableVertexAttribArray(POSITION_ATTRIBUTE_ID);
 	glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, posBuff);
 	glDrawArrays(GL_LINES, 0, 2);
-	glDisableVertexAttribArray(POSITION_ATTRIBUTE_ID);
+	glDisableVertexAttribArray(POSITION_ATTRIBUTE_ID);*/
 }
 
 
@@ -41,7 +41,7 @@ void Dbg::drawLine(const Vec3& from, const Vec3& to, const Vec4& color)
 //======================================================================================================================
 void Dbg::renderGrid()
 {
-	Vec4 col0(0.5, 0.5, 0.5, 1.0);
+	/*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);
 
@@ -78,7 +78,7 @@ void Dbg::renderGrid()
 	glEnableVertexAttribArray(POSITION_ATTRIBUTE_ID);
 	glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, &positions[0]);
 	glDrawArrays(GL_LINES, 0, positions.size());
-	glDisableVertexAttribArray(POSITION_ATTRIBUTE_ID);
+	glDisableVertexAttribArray(POSITION_ATTRIBUTE_ID);*/
 }
 
 
@@ -87,7 +87,7 @@ void Dbg::renderGrid()
 //======================================================================================================================
 void Dbg::drawSphere(float radius, const Transform& trf, const Vec4& col, int complexity)
 {
-	setColor(col);
+	/*setColor(col);
 
 	const float twopi  = M::PI*2;
 	const float pidiv2 = M::PI/2;
@@ -154,7 +154,7 @@ void Dbg::drawSphere(float radius, const Transform& trf, const Vec4& col, int co
 	glEnableVertexAttribArray(0);
 	glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, &(positions[0][0]));
 	glDrawArrays(GL_QUAD_STRIP, 0, positions.size());
-	glDisableVertexAttribArray(0);
+	glDisableVertexAttribArray(0);*/
 }
 
 
@@ -163,7 +163,7 @@ void Dbg::drawSphere(float radius, const Transform& trf, const Vec4& col, int co
 //======================================================================================================================
 void Dbg::drawCube(float size)
 {
-	Vec3 maxPos = Vec3(0.5 * size);
+	/*Vec3 maxPos = Vec3(0.5 * size);
 	Vec3 minPos = Vec3(-0.5 * size);
 
 	Vec3 points [] = {
@@ -182,7 +182,7 @@ void Dbg::drawCube(float size)
 	glEnableVertexAttribArray(0);
 	glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, &(points[0][0]));
 	glDrawElements(GL_QUADS, sizeof(indeces)/sizeof(ushort), GL_UNSIGNED_SHORT, indeces);
-	glDisableVertexAttribArray(0);
+	glDisableVertexAttribArray(0);*/
 }
 
 
@@ -193,7 +193,9 @@ void Dbg::init(const RendererInitializer& initializer)
 {
 	enabled = initializer.dbg.enabled;
 
-	// create FBO
+	//
+	// FBO
+	//
 	try
 	{
 		fbo.create();
@@ -213,8 +215,19 @@ void Dbg::init(const RendererInitializer& initializer)
 		throw EXCEPTION("Cannot create debug FBO: " + e.what());
 	}
 
-	// shader
+	//
+	// Shader prog
+	//
 	sProg.loadRsrc("shaders/Dbg.glsl");
+
+	//
+	// VAO & VBOs
+	//
+	positionsVbo = new Vbo(GL_ARRAY_BUFFER, sizeof(positions), NULL, GL_DYNAMIC_DRAW, this);
+	colorsVbo = new Vbo(GL_ARRAY_BUFFER, sizeof(colors), NULL, GL_DYNAMIC_DRAW, this);
+	vao = new Vao(this);
+	const int positionAttribLoc = 0;
+	vao->attachArrayBufferVbo(*positionsVbo, positionAttribLoc, 3, GL_FLOAT, GL_FALSE, 0, NULL);
 }
 
 
@@ -273,20 +286,53 @@ void Dbg::run()
 
 
 //======================================================================================================================
-// setColor                                                                                                            =
+// setModelMat                                                                                                         =
 //======================================================================================================================
-void Dbg::setColor(const Vec4& color)
+void Dbg::setModelMat(const Mat4& modelMat)
 {
-	sProg->findUniVar("color")->setVec4(&color);
+	RASSERT_THROW_EXCEPTION(pointIndex != 0); // This means that the func called after begin and before end
+
+	Mat4 pmv = viewProjectionMat * modelMat;
+	sProg->findUniVar("modelViewProjectionMat")->setMat4(&pmv);
 }
 
 
 //======================================================================================================================
-// setModelMat                                                                                                         =
+// begin                                                                                                               =
 //======================================================================================================================
-void Dbg::setModelMat(const Mat4& modelMat)
+void Dbg::begin()
 {
-	Mat4 pmv = viewProjectionMat * modelMat;
-	sProg->findUniVar("modelViewProjectionMat")->setMat4(&pmv);
+	RASSERT_THROW_EXCEPTION(pointIndex != 0);
+}
+
+
+//======================================================================================================================
+// end                                                                                                                 =
+//======================================================================================================================
+void Dbg::end()
+{
+	RASSERT_THROW_EXCEPTION(pointIndex == 0);
+
+	positionsVbo->write(&positions[0], sizeof(Vec3) * pointIndex);
+	colorsVbo->write(&colors[0], sizeof(Vec3) * pointIndex);
+
+	vao->bind();
+	glDrawArrays(GL_LINES, 0, pointIndex);
+	vao->unbind();
+
+	// Cleanup
+	pointIndex = 0;
+	sProg->findUniVar("modelViewProjectionMat")->setMat4(&Mat4::getIdentity());
+}
+
+
+//======================================================================================================================
+// pushBackVertex                                                                                                      =
+//======================================================================================================================
+void Dbg::pushBackVertex(const Vec3& pos)
+{
+	positions[pointIndex] = pos;
+	colors[pointIndex] = crntCol;
+	++pointIndex;
 }
 

+ 25 - 3
src/Renderer/Dbg.h

@@ -8,6 +8,10 @@
 #include "Math.h"
 
 
+class Vbo;
+class Vao;
+
+
 /// Debugging stage
 class Dbg: public RenderingPass
 {
@@ -15,14 +19,13 @@ class Dbg: public RenderingPass
 		Dbg(Renderer& r_, Object* parent);
 		void init(const RendererInitializer& initializer);
 		void run();
+
 		void renderGrid();
 		void drawSphere(float radius, const Transform& trf, const Vec4& col, int complexity = 8);
 		void drawCube(float size = 1.0);
-		void setColor(const Vec4& color);
-		void setModelMat(const Mat4& modelMat);
 		void drawLine(const Vec3& from, const Vec3& to, const Vec4& color);
 
-		/// @name Setters & getters
+		/// @name Accessors
 		/// @{
 		bool isEnabled() const {return enabled;}
 		void setEnabled(bool flag) {enabled = flag;}
@@ -31,6 +34,16 @@ class Dbg: public RenderingPass
 		/// @todo add others
 		/// @}
 
+		/// @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
+		void setColor(const Vec3& col) {crntCol = col;} ///< Something like glColor
+		void setColor(const Vec4& col) {crntCol = Vec3(col);} ///< Something like glColor
+		void setModelMat(const Mat4& modelMat);
+		/// @}
+
 	private:
 		static const uint POSITION_ATTRIBUTE_ID = 0; ///< The glId of the attribute var for position in the dbg shader
 		bool enabled;
@@ -41,6 +54,15 @@ class Dbg: public RenderingPass
 		Fbo fbo;
 		RsrcPtr<ShaderProg> sProg;
 		Mat4 viewProjectionMat;
+
+		static const uint MAX_POINTS_PER_DRAW = 100;
+		Vec3 positions[MAX_POINTS_PER_DRAW];
+		Vec3 colors[MAX_POINTS_PER_DRAW];
+		uint pointIndex;
+		Vec3 crntCol;
+		Vbo* positionsVbo;
+		Vbo* colorsVbo;
+		Vao* vao;
 };