Page 1 of 1

OpenGL DebugDraw example?

Posted: Tue Jan 31, 2017 11:24 am
by paulggriffiths
I'm using code off the net but the debug draw uses deprecated functions.
Where is a modern version example to?

Heres the deprecated code:

Code: Select all

//// Helper class; draws the world as seen by Bullet.
//// This is very handy to see it Bullet's world matches yours.
//// This example uses the old OpenGL API for simplicity, 
//// so you'll have to remplace GLFW_OPENGL_CORE_PROFILE by
//// GLFW_OPENGL_COMPAT_PROFILE in glfwWindowHint()
//// How to use this class :
//// Declare an instance of the class :
//// BulletDebugDrawer_DeprecatedOpenGL mydebugdrawer;
//// dynamicsWorld->setDebugDrawer(&mydebugdrawer);
//// Each frame, call it :
//// mydebugdrawer.SetMatrices(ViewMatrix, ProjectionMatrix);
//// dynamicsWorld->debugDrawWorld();
//
//class BulletDebugDrawer_DeprecatedOpenGL : public btIDebugDraw{
//public:
//	void SetMatrices(glm::mat4 pViewMatrix, glm::mat4 pProjectionMatrix){
//		glUseProgram(0); // Use Fixed-function pipeline (no shaders)
//		glMatrixMode(GL_MODELVIEW);
//		glLoadMatrixf(&pViewMatrix[0][0]);
//		glMatrixMode(GL_PROJECTION);
//		glLoadMatrixf(&pProjectionMatrix[0][0]);
//	}
//	virtual void drawLine(const btVector3& from,const btVector3& to,const btVector3& color){
//		glColor3f(color.x(), color.y(), color.z());
//		glBegin(GL_LINES);
//			glVertex3f(from.x(), from.y(), from.z());
//			glVertex3f(to.x(), to.y(), to.z());
//		glEnd();
//	}
//	virtual void drawContactPoint(const btVector3 &,const btVector3 &,btScalar,int,const btVector3 &){}
//	virtual void reportErrorWarning(const char *){}
//	virtual void draw3dText(const btVector3 &,const char *){}
//	virtual void setDebugMode(int p){
//		m = p;
//	}
//	int getDebugMode(void) const {return 3;}
//	int m;
//};
Thanks.

Re: OpenGL DubugDraw example?

Posted: Thu Feb 02, 2017 2:47 am
by paulggriffiths
Not to worry, I implimented my own.

Re: OpenGL DubugDraw example?

Posted: Thu Feb 02, 2017 11:58 am
by benelot
Can you post it here? Maybe anyone else would be interested in it. It does not matter if it is messy or similar, anything helps usually. Also you might get corrections to it if there is anything wrong with it.

Re: OpenGL DubugDraw example?

Posted: Sun Feb 05, 2017 10:54 pm
by jimjamjack
I'm currently trying to get this to work myself :p
How did you get it to work in an OpenGL 3 context? :)

Re: OpenGL DubugDraw example?

Posted: Mon Feb 06, 2017 3:41 am
by paulggriffiths
Sure, here you go:

Code: Select all

// Helper class; draws the world as seen by Bullet.
// This is very handy to see it Bullet's world matches yours
// How to use this class :
// Declare an instance of the class :
// 
// dynamicsWorld->setDebugDrawer(&mydebugdrawer);
// Each frame, call it :
// mydebugdrawer.SetMatrices(ViewMatrix, ProjectionMatrix);
// dynamicsWorld->debugDrawWorld();
GLuint VBO, VAO;
class BulletDebugDrawer_OpenGL : public btIDebugDraw {
public:
	void SetMatrices(glm::mat4 pViewMatrix, glm::mat4 pProjectionMatrix) 
	{
		glUniformMatrix4fv(glGetUniformLocation(lineShader.Program, "projection"), 1, GL_FALSE, glm::value_ptr(pProjectionMatrix));
		glUniformMatrix4fv(glGetUniformLocation(lineShader.Program, "view"), 1, GL_FALSE, glm::value_ptr(pViewMatrix));
	}

	virtual void drawLine(const btVector3& from, const btVector3& to, const btVector3& color) 
	{
		// Vertex data
		GLfloat points[12];

		points[0] = from.x();
		points[1] = from.y();
		points[2] = from.z();
		points[3] = color.x();
		points[4] = color.y();
		points[5] = color.z();

		points[6] = to.x();
		points[7] = to.y();
		points[8] = to.z();
		points[9] = color.x();
		points[10] = color.y();
		points[11] = color.z();

		glDeleteBuffers(1, &VBO);
		glDeleteVertexArrays(1, &VAO);
		glGenBuffers(1, &VBO);
		glGenVertexArrays(1, &VAO);
		glBindVertexArray(VAO);
		glBindBuffer(GL_ARRAY_BUFFER, VBO);
		glBufferData(GL_ARRAY_BUFFER, sizeof(points), &points, GL_STATIC_DRAW);
		glEnableVertexAttribArray(0);
		glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), 0);
		glEnableVertexAttribArray(1);
		glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
		glBindVertexArray(0);

		glBindVertexArray(VAO);
		glDrawArrays(GL_LINES, 0, 2);
		glBindVertexArray(0);

	}
	virtual void drawContactPoint(const btVector3 &, const btVector3 &, btScalar, int, const btVector3 &) {}
	virtual void reportErrorWarning(const char *) {}
	virtual void draw3dText(const btVector3 &, const char *) {}
	virtual void setDebugMode(int p) {
		m = p;
	}
	int getDebugMode(void) const { return 3; }
	int m;
};
Heres the fragment shader for it:

Code: Select all

#version 330 core
in vec3 fColor;
out vec4 color;

void main()
{
    color = color;   
}
And heres the vertex shader:

Code: Select all

#version 330 core
layout (location = 0) in vec3 position;
layout (location = 1) in vec3 color;

out VS_OUT {
    vec3 color;
} vs_out;

uniform mat4 projection;
uniform mat4 view;

void main()
{
    gl_Position = projection * view * vec4(position, 1.0f);

    vs_out.color = color;
}
It isn't fast as it deletes the VBO & VAO and recreates them each frame, could do with advancing it but its ok just to get you started.

Re: OpenGL DebugDraw example?

Posted: Mon Feb 06, 2017 9:48 pm
by jimjamjack
Looks great, cheers.

Can I just ask about the "lineShader.program"? What is this exactly and where is it coming from?

Appreciate the updated code :)

Re: OpenGL DebugDraw example?

Posted: Mon Feb 13, 2017 4:33 pm
by paulggriffiths
The lineshader.program is the shader code.
Could be called anything though.

Check out learnopengl.com and go through the shaders tutorial to understand more as your not yet ready.

Re: OpenGL DebugDraw example?

Posted: Mon Feb 13, 2017 10:11 pm
by jimjamjack
I'll check out that tutorial, but I'm not too sure about drawing the OpenGL lines (It's probably simple, I've used other shaders for textures but I'm a bit lost on the shader code for a debug drawer).

Thanks anyway :)

Re: OpenGL DebugDraw example?

Posted: Sun Feb 19, 2017 3:04 pm
by benelot
If anybody of you has a complete OpenGL implementation of the BulletDebugDrawer, just post it here and we can add it to the wiki for future reference.

Re: OpenGL DebugDraw example?

Posted: Sat Jul 29, 2017 10:22 pm
by Brian Beuken
I have a pretty reasonable system I use for OpenGLES using shaders. The key point is to remember that we no longer use the glBegin and glEnd concept, but we can buffer up the lines somewhere, and after the DynamicPhysicsWorld->debugDrawWorld(); has done its thing, that buffer is full of nice lines to draw in 1 lump

heres the Draw line routine

Code: Select all

public:
	struct 
	{
		btVector3 p1;
		btVector3 p2;
			
	} typedef LineValues;


void PhysicsDraw::drawLine(const btVector3 &from,
	const btVector3 &to,
	const btVector3 &color)
{
	// draws a simple line of pixels between points but stores them for later draw
	LineValues Line;
	Line.p1 = from;
	Line.p2 = to;

	TheLines.push_back(Line);

// we don't care about colour?		
}
Add a DoDebugDraw routine after your normal updates and renders,which does the actual draw

Code: Select all

void PhysicsDraw::DoDebugDraw()
{// set up the line shader and then draw the buffer
	

	
			//load the vertex data info
		glVertexAttribPointer(this->positionLoc,  // the handle for the a_position shader attrib
			3,	// there are 3 values xyz
			GL_FLOAT, // they a float
			GL_FALSE, // don't need to be normalised
			4*sizeof(float),  // how many floats to the next one(be aware btVector3 uses 4 floats)
			(GLfloat*)&this->TheLines[0]  // where do they start as an index); // use 3 values, but add stride each time to get to the next
		);
	
		glEnableVertexAttribArray(this->positionLoc);
		glDrawArrays(GL_LINES, 0, TheLines.size()*2);
	
		
	TheLines.clear();
	
}

In your main loop after the debugDrawWorld() just set up the shaders paramaters (I use very simple shaders) call that DoDebugDraw function to display, then swap your buffers.

Code: Select all

	DynamicPhysicsWorld->debugDrawWorld();
	
	glm::mat4* Projection = TheCamera->GetProjection(); 
	glm::mat4* View = TheCamera->GetView();
	glUseProgram(PhysicsDraw::ProgramObject);
	GLint MatrixID =	glGetUniformLocation(PhysicsDraw::ProgramObject, "MVP"); // use the MVP in the simple shader
	// make the View and  Projection matrix
	glm::mat4 VP        = *Projection * *View;  // Remember order seems backwards
	glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &VP[0][0]);
	
	this->m_pPhysicsDrawer->DoDebugDraw();
and you're done
I use very simple shaders and chose to work out the VP before I send it. This will work for any shader based system, I would prefer to create a malloc buffer of a max size rather than clearing a vector every cycle, but its a debug system so speed isn't as important as function. This shader normally does simple objects so has an MVP, but you can feed it a VP just as easily, I don't feed it colour, but thats a simple addition if you must have it,.

Code: Select all

#version 100
// very simple and probably only used for debug systems
attribute vec3 a_position;
uniform mat4 MVP; 
void main()
{
	gl_Position = MVP*vec4(a_position, 1);

}
and the frag


	#version 100
	// very simple and probably likely to never be needed

precision mediump float;

void main()
{
	gl_FragColor= vec4(1.0, 1.0, 0.0, 1.0); // show a plain colour

}