Debug draw Aabbs moving in a different direction

DaleGriff
Posts: 4
Joined: Sun Mar 16, 2014 10:45 pm

Debug draw Aabbs moving in a different direction

Post by DaleGriff »

My dilemma, what could possible cause the debugdraw Aabbs to shoot across the screen from left to right when the rendered boxes fall from top to bottom of the screen. Other than plugging the debug draw into the bullet physics world I can't see of any parameters that I could set (or accidentally may have set) that causes this to happen......


Hi folks,
I've been learning opengl and bullet recently.
I've had success with a few dozen textured cubes falling onto a surface from a height and colliding, looked good.

I'm using glfw to draw the window.

I seem to be extracting the model matrices from bullet fine that I then use to update the MVP matrix that I send to the shader. So each cube's rotation and translation is correct.

But I'm totally lost as to what the debug draw is doing? I've ended up stripping the program down to a few rows of falling cubes and viewing the Aabb generated by drawline. Now whilst my cube is falling from top to bottom of the screen the wireframe representation falls in from the left had side??? It's as if bullet debug world is doing everything at 90 degrees counterclockwise to my falling textured cubes.

I'm baffled by this seeing as I'm getting the matrix out of bullet to send to the shader? so I'm not sure why the debugger doesn't seem to tally.

I really try not to ask for help but have been stumped on this for the last week.

Anyway, I'd really appreciate any suggestions

This is some of the code.

Code: Select all

	// Create and compile our GLSL program from the shaders
	GLuint programID = LoadShaders( ".\\..\\Debug\\SimpleVertexShader.vertexshader", ".\\..\\Debug\\SimpleFragmentShader.fragmentshader");

	//Get a handle for our MVP uniform; This is our link to send stuff into the matrix.
	GLuint MatrixID = glGetUniformLocation(programID, "MVP");	//in the vertex shader
	GLuint Texture = loadBMP_custom("BrickWall.bmp");

	//Get a handle for our "myTextureSampler" uniform
	GLuint TextureID = glGetUniformLocation(programID, "myTextureSampler" );	//in the fragment shader

	static const GLushort vertex_indices[] =
        {
            0, 1, 2,
            2, 1, 3,

            2, 3, 4,
            4, 3, 5,

            4, 5, 6,
            6, 5, 7,

            6, 7, 0,
            0, 7, 1,
            6, 0, 2,
            2, 4, 6,
            7, 5, 3,
            7, 3, 1
        };

        static const GLfloat vertex_positions[] =
        {
            -1.00f, -1.00f, -1.00f,
            -1.00f,  1.00f, -1.00f,
             1.00f, -1.00f, -1.00f,
             1.00f,  1.00f, -1.00f,

             1.00f, -1.00f,  1.00f,
             1.00f,  1.00f,  1.00f,
            -1.00f, -1.00f,  1.00f,
            -1.00f,  1.00f,  1.00f,
        };

#pragma region uv
	static const GLfloat uv_positions[] ={

			1.00f, 1.00f,
            1.00f,  0.00f,
            0.00f, 1.00f,
            0.00f,  0.00f,

            1.00f, 0.00f,
            1.00f,  1.00f,
            0.00f, 0.00f, 
            0.00f,  1.00f

	};
	
#pragma endregion
	
	glGenBuffers(1, &uv_buffer);
	glBindBuffer(GL_ARRAY_BUFFER, uv_buffer);
	glBufferData(GL_ARRAY_BUFFER, sizeof(uv_positions), uv_positions, GL_STATIC_DRAW);

	glGenBuffers(1, &position_buffer);
	glBindBuffer(GL_ARRAY_BUFFER, position_buffer);
	glBufferData(GL_ARRAY_BUFFER, sizeof(vertex_positions), vertex_positions, GL_STATIC_DRAW);

	

	glGenBuffers(1, &index_buffer);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_buffer);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER,
                     sizeof(vertex_indices),
                     vertex_indices,
                     GL_STATIC_DRAW);
	glGenBuffers(1, &uvbuffer);
	glBindBuffer(GL_ARRAY_BUFFER, uvbuffer);
	glBufferData(GL_ARRAY_BUFFER, sizeof(g_uv_buffer_data), g_uv_buffer_data, GL_STATIC_DRAW);*/

	//GLuint vertexbuffer2;
	//glGenBuffers(1, &vertexbuffer2);
	//glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer2);
	//glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data2), g_vertex_buffer_data2, GL_STATIC_DRAW); //ATTEMPTED CHANGE OF dx CODE.

		 glEnable(GL_CULL_FACE);
        // glFrontFace(GL_CW);

        glEnable(GL_DEPTH_TEST);
        glDepthFunc(GL_LEQUAL);

	BasicDemo demo;
	demo.InitializePhysics();
	//Matrices inside the game loop now//put outside again...
			glm::mat4 Model = glm::mat4(1.0f);
			
	do{		
		// Clear the screen
		glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

		// Use our shader
		glUseProgram(programID);
		//
		//Where all the above stuff lived
					// create an array of 16 floats (representing a 4x4 matrix)
				btScalar transform[16];

				computeMatricesFromInputs();
				glm::mat4 Projection = getProjectionMatrix();
				glm::mat4 View = getViewMatrix();

				// iterate through all of the objects in our world
				//This is going from bullet world to real world...
				GameObjects m_obj = demo.ReturnGameObjects();
				for(GameObjects::iterator i = m_obj.begin(); i != m_obj.end(); ++i) {
					// get the object from the iterator
					GameObject* pObj = *i;

					// read the transform
					pObj->GetTransform(transform);

				   //get model matrix in correct format
					Model = glm::make_mat4(transform);
				

					glm::mat4 MVP = Projection * View * Model;
					glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP[0][0]); //send the mvp to shader

					//Bind our texture in Texture Unit 0
					glActiveTexture(GL_TEXTURE0);
					glBindTexture(GL_TEXTURE_2D, Texture);

					//set our "myTextureSampler" sampler to user Texure Unit 0
					glUniform1i(TextureID, 0);

					//1st att vertex type buffer
					glEnableVertexAttribArray(0); //location 0 in the vertex shader
					glBindBuffer(GL_ARRAY_BUFFER, position_buffer);
					glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
					//2ND attribute buffer : uv
					glEnableVertexAttribArray(1); //location 1 in the fragment shader
					glBindBuffer(GL_ARRAY_BUFFER, uv_buffer);
					glVertexAttribPointer(
					1,		//match shader layout
					2,		//size u + v
					GL_FLOAT,
					GL_FALSE, //NORMALIZED, NO
					0,			//stride
					(void*)0	//array buffer offset
					);

					if(pObj->ReturnMass() > 0.0f)
					{
					//glDrawArrays(GL_TRIANGLES, 0, 12*3); 
					   glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_SHORT, 0);
					}
				}//end for loop

		float dt = m_clock.getTimeMilliseconds();
		// reset the clock to 0
		m_clock.reset();
		// update the scene
		demo.Update(dt/1000.0f);

		glDisableVertexAttribArray(0);
		glDisableVertexAttribArray(1);

		// Swap buffers
		glfwSwapBuffers(window);
		glfwPollEvents();

	} // Check if the ESC key was pressed or the window was closed
	while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS &&
		   glfwWindowShouldClose(window) == 0 );
The bullet physics is initialised from demo.Initialize()

Code: Select all

void BasicDemo::InitializePhysics() {
	// create the collision configuration
	m_pCollisionConfiguration = new btDefaultCollisionConfiguration();
	// create the dispatcher
	m_pDispatcher = new btCollisionDispatcher(m_pCollisionConfiguration);
	// create the broadphase
	m_pBroadphase = new btDbvtBroadphase();
	// create the constraint solver
	m_pSolver = new btSequentialImpulseConstraintSolver();
	// create the world
	m_pWorld = new btDiscreteDynamicsWorld(m_pDispatcher, m_pBroadphase, m_pSolver, m_pCollisionConfiguration);

	// create our scene's physics objects
	CreateObjects();

	// create the debug drawer
	m_pDebugDrawer = new DebugDrawer();
	m_pDebugDrawer->setDebugMode(0);
	m_pWorld->setDebugDrawer(m_pDebugDrawer);
	m_pDebugDrawer->ToggleDebugFlag(btIDebugDraw::DBG_DrawAabb);   

}


void BasicDemo::Update(float dt)
{
	if(m_pWorld)
	{
	m_pWorld -> stepSimulation(dt);
	m_pWorld->debugDrawWorld();
	}

}

void BasicDemo::ShutdownPhysics() 
{
	delete m_pWorld;
	delete m_pSolver;
	delete m_pBroadphase;
	delete m_pDispatcher;
	delete m_pCollisionConfiguration;
}

void BasicDemo::CreateObjects()
{
	// create a ground plane
	//CreateGameObject(new btStaticPlaneShape(btVector3(0,1,0), 100), 0, btVector3(0.0f, 0.0f, 0.0f));

	//CreateGameObject(new btBoxShape(btVector3(50, -1, 50)), 0, btVector3(0.0f, 0.0f, 0.0f));
	//lets create extra boxes in a loop
	for(int i = 0; i < 20; i++)
	{
		if(i >= 0 && i < 5)
		{
			CreateGameObject(new btBoxShape(btVector3(1,1,1)), 1.0, btVector3(1.0f, 10.0f, 0.0f));
		}
		else if(i >=5 && i < 10)
		{
			CreateGameObject(new btBoxShape(btVector3(1,1,1)), 1.0, btVector3(1.0f + (i%5), 30.0f, -2.0f));
		}
		else if(i >=10 && i < 15)
		{
			CreateGameObject(new btBoxShape(btVector3(1,1,1)), 1.0, btVector3(1.0f + (i%5), 40.0f, -2.0f));
		}
		else if(i >=15 && i < 20)
		{
			CreateGameObject(new btBoxShape(btVector3(1,1,1)), 1.0, btVector3(1.0f + (i%5), 50.0f, -2.0f));
		}
		else if(i >=20 && i < 25)
		{
			CreateGameObject(new btBoxShape(btVector3(1,1,1)), 1.0, btVector3(1.0f + (i%5), 60.0f, -2.0f));
		}

	}
}
You do not have the required permissions to view the files attached to this post.
bwelch
Posts: 48
Joined: Thu Dec 12, 2013 4:04 pm

Re: Debug draw Aabbs moving in a different direction

Post by bwelch »

Did you check to see if your display uses the same axes as Bullet? In Bullet, the vertical axis is Y by default. If you got Y and Z mixed up, it could cause this sort of problem.
DaleGriff
Posts: 4
Joined: Sun Mar 16, 2014 10:45 pm

Re: Debug draw Aabbs moving in a different direction

Post by DaleGriff »

Hi bWelch, thanks for your comment. In my Drawline I have swapped the getX() and getY() terms in the drawline function and at least now the debug boxes are falling in the same direction as the rendered cube.

What is really weird for me now is that (I have down to just one cube now by the way)

1) When I create the rigid body cube object at the origin the Aabb frame and the rendered cube coincide but then as they fall the Aabb falls faster than the rendered cube so moves on ahead of it.

2) If I try and create the rigid body cube offset from the origin (so alter it's original position in bullet) then they don't coincide and the Aabb is offset from the rendered cube.???

Code: Select all

void DebugDrawer::drawLine(const btVector3 &from,const btVector3 &to, const btVector3 &color)
	{
		// draws a simple line of pixels between points.
		//not using shaders??
		// use the GL_LINES primitive to draw lines
		glBegin(GL_LINES);	
		glVertex3f(from.getY(), from.getX(), from.getZ());
		glVertex3f(to.getY(), to.getX(), to.getZ());
		glColor3f(color.getY(), color.getX(), color.getZ());
		glEnd();
	}
Not sure why I should need to have to do this???

Code: Select all

CreateGameObject(new btBoxShape(btVector3(1,1,1)), 0.1, btVector3(0.0f, 0.0f, 0.0f));
And if I put anything ofther than btVector2(0,0,0) in the above line then the Aabb is offset from the rendered cube.

As I said before what I am doing here is
*setting up bullet with initial conditions
*in a do while loop extracting the transform from bullet
* using this transform (so btscalar[16] ) to make a world matrix
* from this create an Model,view,projection matrix that I send to the shader.

So I am using the data extracted from bullet to alter the position (and orientation but not relevant as it is falling straight down) for my rendered cube.

Please help if you can as I am totally baffled with this.
You do not have the required permissions to view the files attached to this post.
DaleGriff
Posts: 4
Joined: Sun Mar 16, 2014 10:45 pm

Re: Debug draw Aabbs moving in a different direction

Post by DaleGriff »

One thing I have noticed is that if I only send the combined view projection matrix (and not the model matrix) then I need I need to put the getx() gety() in the drawline() back to their original positions.

I now get the wire frame falling as I would expect, top to bottom of the screen, however now I only have a static model of my cube because the model matrix, that is still being updated from bullet, isn't being sent to the shader.

So there seems to be some conflict with hoping to draw using shaders and then also using gl.begin() gl.end() to draw debug drawlines.

I have tried to get access to the shaders from within bebug drawer...still not sure how to get access to it using glew in that other class (when I 've already got glew running in main() ). :?
DaleGriff
Posts: 4
Joined: Sun Mar 16, 2014 10:45 pm

Re: Debug draw Aabbs moving in a different direction

Post by DaleGriff »

Cool got it working finally. Just about remembering to put #include glew.h right at the top of the code, then it lets you do it. So am drawing my textured cubes in the shader as well as the debug wireframe and/or Aabbs and no need for the conflict I was getting with the mixed use of the deprecated gl.begin() gl.end() and shaders. Cool.