First I initialize the Physics world like this :
Code: Select all
void initPhysics()
{
broadphase = new btDbvtBroadphase();
collisionConfiguration = new btDefaultCollisionConfiguration();
dispatcher = new btCollisionDispatcher(collisionConfiguration);
solver = new btSequentialImpulseConstraintSolver;
dynamicsWorld = new btDiscreteDynamicsWorld(dispatcher, broadphase, solver, collisionConfiguration);
dynamicsWorld->setGravity(btVector3(0, -10, 0));
//groundShape = new btStaticPlaneShape(btVector3(0, 1, 0), 1);
groundShape = new btBoxShape(btVector3(5, 5, 5));
fallShape = new btBoxShape(btVector3(1, 1, 1));
//fallShape = new btSphereShape(1);
// Orientation and Position of Ground
groundMotionState = new btDefaultMotionState(btTransform(btQuaternion(0, 0, 0, 1), btVector3(0, -3, 0)));
btRigidBody::btRigidBodyConstructionInfo groundRigidBodyCI(0, groundMotionState, groundShape, btVector3(0, 0, 0));
groundRigidBody = new btRigidBody(groundRigidBodyCI);
dynamicsWorld->addRigidBody(groundRigidBody);
// Orientation and Position of Falling body
fallMotionState = new btDefaultMotionState(btTransform(btQuaternion(0, 0, 0, 1), btVector3(-1, 5, 0)));
btScalar mass = 1;
btVector3 fallInertia(0, 0, 0);
fallShape->calculateLocalInertia(mass, fallInertia);
btRigidBody::btRigidBodyConstructionInfo fallRigidBodyCI(mass, fallMotionState, fallShape, fallInertia);
fallRigidBody = new btRigidBody(fallRigidBodyCI);
dynamicsWorld->addRigidBody(fallRigidBody);
bulletDebugugger.setDebugMode(btIDebugDraw::DBG_DrawWireframe);
dynamicsWorld->setDebugDrawer(&bulletDebugugger);
}
Code: Select all
#pragma once
#include <LinearMath/btIDebugDraw.h>
class CDebugDraw :
public btIDebugDraw
{
private:
int m_debugMode;
public:
CDebugDraw(void);
virtual ~CDebugDraw(void);
struct _LINE {
btVector3 from;
btVector3 to;
_LINE(btVector3 f, btVector3 t) {
from = f;
to = t;
}
};
std::vector<_LINE> LINES;
struct _COLOR {
btVector3 col;
_COLOR(btVector3 c) {
col = c;
}
};
std::vector<_COLOR> COLORS;
GLuint vao, vbo[2];
virtual void drawLine(const btVector3& from,const btVector3& to,const btVector3& color);
virtual void drawContactPoint(const btVector3& PointOnB,const btVector3& normalOnB,btScalar distance,int lifeTime,const btVector3& color);
virtual void reportErrorWarning(const char* warningString);
virtual void draw3dText(const btVector3& location,const char* textString);
virtual void setDebugMode(int m_debugMode);
virtual int getDebugMode() const;
void doDrawing();
void cleanDrawing();
};
Code: Select all
void CDebugDraw::drawLine(const btVector3& from,const btVector3& to,const btVector3& color)
{
LINES.push_back(_LINE(from, to));
COLORS.push_back(_COLOR(color));
}
void CDebugDraw::doDrawing()
{
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glGenBuffers(2, vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
glBufferData(GL_ARRAY_BUFFER, LINES.size() * sizeof(_LINE), &LINES[0], GL_STATIC_DRAW);
glVertexAttribPointer((GLuint)0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vbo[1]);
glBufferData(GL_ARRAY_BUFFER, COLORS.size() * sizeof(_COLOR), &COLORS[0], GL_STATIC_DRAW);
glVertexAttribPointer((GLuint)1, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(1);
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
void CDebugDraw::cleanDrawing()
{
// delete buffers
glDeleteBuffers(2, vbo);
glDeleteBuffers(1, &vao);
}
Code: Select all
// Debug drawing
glBindVertexArray(bulletDebugugger.vao);
glDrawArrays(GL_LINES, 0, bulletDebugugger.LINES.size());
// Clear vectors
bulletDebugugger.LINES.clear();
bulletDebugugger.COLORS.clear();
The "triangle" seems to fall from top and stop at the position shown in the screenshot attached. The color of some lines changes from blue to red.
But there are weird lines in the window, I guess that is due to the Debug Drawing function. I think I am doing something wrong in my code.
Can someone guide me where am I doing wrong ?
Has anyone implemented a debug drawer using VAO and/or VBO in OpenGL 4 ?