2
0
Эх сурвалжийг харах

Commiting new version of Dbg. Its way faster

Panagiotis Christopoulos Charitos 13 жил өмнө
parent
commit
3cc700c9c8

+ 13 - 9
include/anki/renderer/DebugDrawer.h

@@ -41,24 +41,28 @@ public:
 	{
 	{
 		crntCol = Vec3(col);
 		crntCol = Vec3(col);
 	}
 	}
-	void setModelMatrix(const Mat4& modelMat);
-	void setViewProjectionMatrix(const Mat4& m)
-	{
-		vpMat = m;
-	}
+	void setModelMatrix(const Mat4& m);
+	void setViewProjectionMatrix(const Mat4& m);
 	/// @}
 	/// @}
 
 
+	void flush();
+
 private:
 private:
 	ShaderProgramResourcePointer prog;
 	ShaderProgramResourcePointer prog;
 	static const U MAX_POINTS_PER_DRAW = 256;
 	static const U MAX_POINTS_PER_DRAW = 256;
-	Array<Vec3, MAX_POINTS_PER_DRAW> positions;
-	Array<Vec3, MAX_POINTS_PER_DRAW> colors;
-	Mat4 modelMat;
+	Mat4 mMat;
 	Mat4 vpMat;
 	Mat4 vpMat;
-	U pointIndex;
+	Mat4 mvpMat;
+	U vertexPointer;
 	Vec3 crntCol;
 	Vec3 crntCol;
+
+	Array<Vec3, MAX_POINTS_PER_DRAW> clientPositions;
+	Array<Vec3, MAX_POINTS_PER_DRAW> clientColors;
+	Array<Mat4, MAX_POINTS_PER_DRAW> clientMatrices;
+
 	Vbo positionsVbo;
 	Vbo positionsVbo;
 	Vbo colorsVbo;
 	Vbo colorsVbo;
+	Vbo matricesVbo;
 	Vao vao;
 	Vao vao;
 
 
 	/// This is a container of some precalculated spheres. Its a map that
 	/// This is a container of some precalculated spheres. Its a map that

+ 1 - 2
shaders/Dbg.glsl

@@ -2,8 +2,7 @@
 
 
 layout(location = 0) in vec3 position;
 layout(location = 0) in vec3 position;
 layout(location = 1) in vec3 color;
 layout(location = 1) in vec3 color;
-
-uniform mat4 modelViewProjectionMat;
+layout(location = 2) in mat4 modelViewProjectionMat;
 
 
 out vec3 vColor;
 out vec3 vColor;
 
 

+ 6 - 177
src/renderer/Dbg.cpp

@@ -16,6 +16,11 @@ void Dbg::init(const Renderer::Initializer& initializer)
 {
 {
 	enabled = initializer.dbg.enabled;
 	enabled = initializer.dbg.enabled;
 
 
+	if(!enabled)
+	{
+		return;
+	}
+
 	try
 	try
 	{
 	{
 		fbo.create();
 		fbo.create();
@@ -71,183 +76,7 @@ void Dbg::run()
 		sceneDrawer->draw(sector->getOctree());
 		sceneDrawer->draw(sector->getOctree());
 	}
 	}
 
 
-#if 0
-	drawer->setViewProjectionMatrix(r->getViewProjectionMatrix());
-	drawer->setModelMatrix(Mat4::getIdentity());
-
-	SceneNode* node = scene.findSceneNode("spot0");
-	Light* light = static_cast<Light*>(node);
-	SpotLight* slight = static_cast<SpotLight*>(light);
-
-	const Transform& trf = light->getWorldTransform();
-
-	drawer->drawLine(trf.getOrigin(),
-		trf.getOrigin() + (-trf.getRotation().getZAxis()), 
-		Vec4(1.0));
-#endif
-
-#if 0
-	// XXX
-	drawer->setViewProjectionMatrix(r->getViewProjectionMatrix());
-	drawer->setModelMatrix(Mat4::getIdentity());
-
-	Camera* camera1 = static_cast<Camera*>(scene.findSceneNode("camera1"));
-	CollisionDebugDrawer cdd(drawer.get());
-	static U ii, jj;
-	static Plane p, pp;
-
-	if(deletemeto == 0)
-	{
-		for(U i = 0; i < Is::TILES_X_COUNT; i++)
-		{
-			for(U j = 0; j < Is::TILES_Y_COUNT; j++)
-			{
-				if(r->getIs().tiles[j][i].lightsCount > 0)
-				{
-					std::cout << "--" << i << " " << j << std::endl;
-
-					p = r->getIs().tiles[j][i].planes[Frustum::FP_FAR];
-					pp = r->getIs().tiles[j][i].planes[Frustum::FP_NEAR];
-
-					ii = i;
-					jj = j;
-
-					camera1->setLocalTransform(scene.getActiveCamera().getWorldTransform());
-					camera1->update();
-
-					p.transform(camera1->getWorldTransform());
-					pp.transform(camera1->getWorldTransform());
-
-					//std::cout << scene.getActiveCamera().getWorldTransform() << std::endl;
-
-					//p.accept(cdd);
-				}
-			}
-		}
-	}
-
-	if(deletemeto == 1)
-	{
-		std::cout << "Brokolo" << std::endl;
-
-		p.accept(cdd);
-		pp.accept(cdd);
-
-	}
-
-	Plane p1 = r->getIs().tiles[jj][ii].planes[Frustum::FP_NEAR];
-	p1.transform(scene.getActiveCamera().getWorldTransform());
-	Plane p2 = r->getIs().tiles[jj][ii].planes[Frustum::FP_FAR];
-	p2.transform(scene.getActiveCamera().getWorldTransform());
-
-	//p1.accept(cdd);
-	//p2.accept(cdd);
-#endif
-
-#if 0
-	Camera* camera1 = static_cast<Camera*>(scene.findSceneNode("camera1"));
-
-	F32 fx = static_cast<PerspectiveCamera*>(camera1)->getFovX();
-	F32 fy = static_cast<PerspectiveCamera*>(camera1)->getFovY();
-
-	//std::cout << fx << " " << fy << std::endl;
-
-	Vec3 a(0.0);
-	F32 s, c;
-	sinCos(getPi<F32>() / 2 - fx / 2, s, c);
-	Vec3 b(c, 0.0, -s);
-
-	a.transform(camera1->getWorldTransform());
-	b.transform(camera1->getWorldTransform());
-
-	drawer->drawLine(a, b, Vec4(1));
-
-	sinCos(fy / 2, s, c);
-	b = Vec3(0.0, s, -c);
-	b.transform(camera1->getWorldTransform());
-
-	drawer->drawLine(a, b, Vec4(1));
-#endif
-
-#if 0
-	{
-	PerspectiveFrustum fr =
-		static_cast<const PerspectiveFrustum&>(scene.getActiveCamera().getFrustumable()->getFrustum());
-
-	fr.setTransform(Transform::getIdentity());
-
-	CollisionDebugDrawer cdd(drawer.get());
-	fr.accept(cdd);
-	}
-
-	for(U j = 0; j < 16; j++)
-	{
-		for(U i = 0; i < 1; i++)
-		{
-			Is::Tile& tile = r->getIs().tiles[j][i];
-
-			Mat4 vmat = scene.getActiveCamera().getViewMatrix();
-
-			CollisionDebugDrawer cdd(drawer.get());
-			//tile.planes[Frustum::FP_LEFT].accept(cdd);
-			//tile.planes[Frustum::FP_RIGHT].accept(cdd);
-			//tile.planes[Frustum::FP_BOTTOM].accept(cdd);
-			//tile.planes[Frustum::FP_TOP].accept(cdd);
-		}
-	}
-#endif
-
-#if 0
-	// XXX Remove these
-	CollisionDebugDrawer cdd(drawer.get());
-	Sphere s(Vec3(90.0, 4.0, 0.0), 2.0);
-	s.accept(cdd);
-
-	Camera& cam = scene.getActiveCamera();
-
-	Vec4 p0(s.getCenter(), 1.0);
-	Vec4 p1(s.getCenter() + Vec3(0.0, 2.0, 0.0), 1.0);
-	p0 = cam.getViewProjectionMatrix() * p0;
-	F32 w = p0.w();
-	p0 /= p0.w();
-	p1 = cam.getViewProjectionMatrix() * p1;
-	F32 p1w = p1.w();
-	p1 /= p1.w();
-
-	Vec4 l = cam.getViewProjectionMatrix() * Vec4(0.0, 0.0, 0.0, 1.0);
-	l /= l.w();
-
-	//std::cout << (p1 - p0).getLength() << ", " << l << std::endl;
-
-	//
-	F32 r = s.getRadius();
-	Vec3 e = cam.getWorldTransform().getOrigin();
-	Vec4 c(s.getCenter(), 1.0);
-	Vec4 a = Vec4(c.xyz() + cam.getWorldTransform().getRotation() * Vec3(r, 0.0, 0.0), 1.0);
-
-	c = cam.getViewProjectionMatrix() * c;
-	c /= c.w();
-
-	a = cam.getViewProjectionMatrix() * a;
-	a /= a.w();
-
-	Vec2 circleCenter = c.xy();
-	F32 circleRadius = (c.xy() - a.xy()).getLength();
-	//
-
-	drawer->setViewProjectionMatrix(Mat4::getIdentity());
-	drawer->setModelMatrix(Mat4::getIdentity());
-
-	/*drawer->drawLine(Vec3(p0.x(), p0.y(), 0.0),
-		Vec3(p0.x(), p0.y(), 0.0) + Vec3(2.0 / w, 0.0, 0.0),
-		Vec4(0.0, 1.0, 0.0, 0.0));
-
-	drawer->drawLine(Vec3(p0.x(), p0.y(), 0.0),
-		Vec3(p1.x(), p1.y(), 0.0), Vec4(0.0, 1.0, 1.0, 0.0));*/
-
-	drawer->drawLine(Vec3(circleCenter, 0.0),
-		Vec3(circleCenter, 0.0) + Vec3(circleRadius, 0.0, 0.0), Vec4(0.0, 1.0, 1.0, 0.0));
-#endif
+	drawer->flush();
 }
 }
 
 
 } // end namespace anki
 } // end namespace anki

+ 65 - 31
src/renderer/DebugDrawer.cpp

@@ -22,19 +22,39 @@ DebugDrawer::DebugDrawer()
 {
 {
 	prog.load("shaders/Dbg.glsl");
 	prog.load("shaders/Dbg.glsl");
 
 
-	positionsVbo.create(GL_ARRAY_BUFFER, sizeof(positions), NULL,
+	positionsVbo.create(GL_ARRAY_BUFFER, sizeof(clientPositions), nullptr,
 		GL_DYNAMIC_DRAW);
 		GL_DYNAMIC_DRAW);
-	colorsVbo.create(GL_ARRAY_BUFFER, sizeof(colors), NULL, GL_DYNAMIC_DRAW);
+	colorsVbo.create(GL_ARRAY_BUFFER, sizeof(clientColors), nullptr,
+		GL_DYNAMIC_DRAW);
+	matricesVbo.create(GL_ARRAY_BUFFER, sizeof(clientMatrices), nullptr,
+		GL_DYNAMIC_DRAW);
+
 	vao.create();
 	vao.create();
-	const int positionAttribLoc = 0;
-	vao.attachArrayBufferVbo(&positionsVbo, positionAttribLoc, 3, GL_FLOAT,
-		GL_FALSE, 0, 0);
-	const int colorAttribLoc = 1;
-	vao.attachArrayBufferVbo(&colorsVbo, colorAttribLoc, 3, GL_FLOAT, GL_FALSE,
-		0, 0);
-
-	pointIndex = 0;
-	modelMat.setIdentity();
+	vao.attachArrayBufferVbo(
+		&positionsVbo, prog->findAttributeVariable("position"), 3, GL_FLOAT,
+		false, 0, 0);
+	vao.attachArrayBufferVbo(
+		&colorsVbo, prog->findAttributeVariable("color"), 3, GL_FLOAT,
+		false, 0, 0);
+
+	GLint loc =
+		prog->findAttributeVariable("modelViewProjectionMat").getLocation();
+
+	vao.attachArrayBufferVbo(
+		&matricesVbo, loc,
+		4, GL_FLOAT, false, sizeof(Mat4), 0);
+	vao.attachArrayBufferVbo(
+		&matricesVbo, loc + 1,
+		4, GL_FLOAT, false, sizeof(Mat4), sizeof(Vec4));
+	vao.attachArrayBufferVbo(
+		&matricesVbo, loc + 2,
+		4, GL_FLOAT, false, sizeof(Mat4), sizeof(Vec4) * 2);
+	vao.attachArrayBufferVbo(
+		&matricesVbo, loc + 3,
+		4, GL_FLOAT, false, sizeof(Mat4), sizeof(Vec4) * 3);
+
+	vertexPointer = 0;
+	mMat.setIdentity();
 	crntCol = Vec3(1.0, 0.0, 0.0);
 	crntCol = Vec3(1.0, 0.0, 0.0);
 }
 }
 
 
@@ -145,12 +165,12 @@ void DebugDrawer::drawSphere(F32 radius, int complexity)
 
 
 	// Render
 	// Render
 	//
 	//
-	modelMat = modelMat * Mat4(Vec3(0.0), Mat3::getIdentity(), radius);
+	mMat = mMat * Mat4(Vec3(0.0), Mat3::getIdentity(), radius);
 
 
 	begin();
 	begin();
 	for(const Vec3& p : *sphereLines)
 	for(const Vec3& p : *sphereLines)
 	{
 	{
-		if(pointIndex >= MAX_POINTS_PER_DRAW)
+		if(vertexPointer >= MAX_POINTS_PER_DRAW)
 		{
 		{
 			end();
 			end();
 			begin();
 			begin();
@@ -190,45 +210,59 @@ void DebugDrawer::drawCube(F32 size)
 }
 }
 
 
 //==============================================================================
 //==============================================================================
-void DebugDrawer::setModelMatrix(const Mat4& modelMat_)
+void DebugDrawer::setModelMatrix(const Mat4& m)
 {
 {
-	ANKI_ASSERT(pointIndex == 0
+	ANKI_ASSERT(vertexPointer == 0
 		&& "The func called after begin and before end");
 		&& "The func called after begin and before end");
-	modelMat = modelMat_;
+	mMat = m;
+	mvpMat = vpMat * mMat;
+}
+
+//==============================================================================
+void DebugDrawer::setViewProjectionMatrix(const Mat4& m)
+{
+	vpMat = m;
+	mvpMat = vpMat * mMat;
 }
 }
 
 
 //==============================================================================
 //==============================================================================
 void DebugDrawer::begin()
 void DebugDrawer::begin()
 {
 {
-	ANKI_ASSERT(pointIndex == 0);
+	ANKI_ASSERT(vertexPointer == 0);
 }
 }
 
 
 //==============================================================================
 //==============================================================================
 void DebugDrawer::end()
 void DebugDrawer::end()
 {
 {
-	ANKI_ASSERT(pointIndex != 0);
+	ANKI_ASSERT(vertexPointer != 0);
+}
 
 
-	positionsVbo.write(&positions[0], 0, sizeof(Vec3) * pointIndex);
-	colorsVbo.write(&colors[0], 0, sizeof(Vec3) * pointIndex);
+//==============================================================================
+void DebugDrawer::flush()
+{
+	positionsVbo.write(&clientPositions[0], 0, sizeof(clientPositions));
+	colorsVbo.write(&clientColors[0], 0, sizeof(clientColors));
+	matricesVbo.write(&clientMatrices[0], 0, sizeof(clientMatrices));
 
 
-	Mat4 pmv = vpMat * modelMat;
 	prog->bind();
 	prog->bind();
-	prog->findUniformVariable("modelViewProjectionMat").set(pmv);
-
 	vao.bind();
 	vao.bind();
-	glDrawArrays(GL_LINES, 0, pointIndex);
-	vao.unbind();
-
-	// Cleanup
-	pointIndex = 0;
+	glDrawArrays(GL_LINES, 0, vertexPointer);
 }
 }
 
 
 //==============================================================================
 //==============================================================================
 void DebugDrawer::pushBackVertex(const Vec3& pos)
 void DebugDrawer::pushBackVertex(const Vec3& pos)
 {
 {
-	positions[pointIndex] = pos;
-	colors[pointIndex] = crntCol;
-	++pointIndex;
+	clientPositions[vertexPointer] = pos;
+	clientColors[vertexPointer] = crntCol;
+	clientMatrices[vertexPointer] = mvpMat.getTransposed();
+
+	++vertexPointer;
+
+	if(vertexPointer == MAX_POINTS_PER_DRAW)
+	{
+		flush();
+		vertexPointer = 0;
+	}
 }
 }
 
 
 //==============================================================================
 //==============================================================================