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 );
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));
}
}
}