Implemented debug draw for bullet.

This commit is contained in:
Faerbit 2015-03-13 15:30:21 +01:00
parent 4d6ee7093f
commit ab6d2e4ffd
9 changed files with 168 additions and 0 deletions

9
data/shader/debug.fsh Normal file
View File

@ -0,0 +1,9 @@
#version 150
in vec3 vColor;
out vec4 oColor;
void main() {
oColor = vec4(vColor, 1.0);
}

13
data/shader/debug.vsh Normal file
View File

@ -0,0 +1,13 @@
#version 150
uniform mat4 viewProjectionMatrix;
in vec3 aPosition;
in vec3 aColor;
out vec3 vColor;
void main() {
vColor = aColor;
gl_Position = viewProjectionMatrix * vec4(aPosition, 1.0);
}

64
game/debugDraw.cc Normal file
View File

@ -0,0 +1,64 @@
#include "debugDraw.hh"
DebugDraw::DebugDraw() : debug_mode(0) {
data = std::vector<float>();
}
void DebugDraw::setDebugMode(int debugMode) {
this->debug_mode = debugMode;
}
int DebugDraw::getDebugMode() const {
return debug_mode;
}
std::vector<float>* DebugDraw::getData() {
return &data;
}
void DebugDraw::drawLine (const btVector3 &from, const btVector3 &to, const btVector3 &color) {
data.push_back(from.getX());
data.push_back(from.getY());
data.push_back(from.getZ());
data.push_back(color.getX());
data.push_back(color.getY());
data.push_back(color.getZ());
data.push_back(to.getX());
data.push_back(to.getY());
data.push_back(to.getZ());
data.push_back(color.getX());
data.push_back(color.getY());
data.push_back(color.getZ());
}
void DebugDraw::drawLine (const btVector3 &from, const btVector3 &to, const btVector3 &fromColor,
const btVector3 &toColor) {
data.push_back(from.getX());
data.push_back(from.getY());
data.push_back(from.getZ());
data.push_back(fromColor.getX());
data.push_back(fromColor.getY());
data.push_back(fromColor.getZ());
data.push_back(to.getX());
data.push_back(to.getY());
data.push_back(to.getZ());
data.push_back(toColor.getX());
data.push_back(toColor.getY());
data.push_back(toColor.getZ());
}
void DebugDraw::drawContactPoint (const btVector3 &PointOnB, const btVector3 &normalOnB,
btScalar distance, int lifeTime, const btVector3 &color){
}
void DebugDraw::reportErrorWarning (const char *warningString) {
printf("%s\n", warningString);
}
void DebugDraw::draw3dText (const btVector3 &location, const char *textString) {
}
void DebugDraw::clearData() {
this->data = std::vector<float>();
}

25
game/debugDraw.hh Normal file
View File

@ -0,0 +1,25 @@
#pragma once
#include "LinearMath/btIDebugDraw.h"
#include <ACGL/OpenGL/Objects.hh>
class DebugDraw : public btIDebugDraw {
int debug_mode;
public:
DebugDraw();
void setDebugMode(int debugMode);
int getDebugMode() const;
std::vector<float>* getData();
void drawLine (const btVector3 &from, const btVector3 &to, const btVector3 &color);
void drawLine (const btVector3 &from, const btVector3 &to, const btVector3 &fromColor,
const btVector3 &toColor);
void reportErrorWarning (const char *warningString);
void clearData();
// the following functions don't do anything but are defined to
// get the class not to be abstract
void drawContactPoint (const btVector3 &PointOnB, const btVector3 &normalOnB,
btScalar distance, int lifeTime, const btVector3 &color);
void draw3dText (const btVector3 &location, const char *textString);
private:
std::vector<float> data;
};

View File

@ -6,6 +6,7 @@
#include <functional> #include <functional>
#include <ACGL/OpenGL/Creator/ShaderProgramCreator.hh> #include <ACGL/OpenGL/Creator/ShaderProgramCreator.hh>
#include "LinearMath/btIDebugDraw.h"
using namespace ACGL::OpenGL; using namespace ACGL::OpenGL;
@ -27,6 +28,7 @@ Graphics::Graphics(glm::uvec2 windowSize, float nearPlane,
gameStart = false; gameStart = false;
renderShadows = true; renderShadows = true;
renderFlames = true; renderFlames = true;
renderDebug = false;
} }
Graphics::Graphics() { Graphics::Graphics() {
@ -109,6 +111,20 @@ void Graphics::init(Level* level) {
flamePostShader = ShaderProgramCreator("flame_post") flamePostShader = ShaderProgramCreator("flame_post")
.attributeLocations(fullscreen_quad->getAttributeLocations()).create(); .attributeLocations(fullscreen_quad->getAttributeLocations()).create();
debug_ab = SharedArrayBuffer(new ArrayBuffer());
debug_ab->defineAttribute("aPosition", GL_FLOAT, 3);
debug_ab->defineAttribute("aColor", GL_FLOAT, 3);
debug_vao = SharedVertexArrayObject(new VertexArrayObject());
debug_vao->attachAllAttributes(debug_ab);
debug_vao->setMode(GL_LINES);
debugShader = ShaderProgramCreator("debug")
.attributeLocations(debug_vao->getAttributeLocations()).create();
debugDrawer = DebugDraw();
level->getPhysics()->getWorld()->setDebugDrawer(&debugDrawer);
depth_directionalMaps = std::vector<SharedTexture2D>(3); depth_directionalMaps = std::vector<SharedTexture2D>(3);
framebuffer_directional = std::vector<SharedFrameBufferObject>(3); framebuffer_directional = std::vector<SharedFrameBufferObject>(3);
@ -508,6 +524,21 @@ void Graphics::render(double time)
glBlitFramebuffer(0, 0, windowSize.x, windowSize.y, 0, 0, windowSize.x, windowSize.y, glBlitFramebuffer(0, 0, windowSize.x, windowSize.y, 0, 0, windowSize.x, windowSize.y,
GL_COLOR_BUFFER_BIT, GL_NEAREST); GL_COLOR_BUFFER_BIT, GL_NEAREST);
} }
if (renderDebug) {
debugDrawer.setDebugMode(btIDebugDraw::DBG_DrawWireframe);
level->getPhysics()->getWorld()->debugDrawWorld();
debugDrawer.setDebugMode(btIDebugDraw::DBG_NoDebug);
unsigned int data_count = debugDrawer.getData()->size();
float* debugData = new float[data_count];
for (unsigned int i = 0; i<data_count; i++) {
debugData[i] = debugDrawer.getData()->at(i);
}
debug_ab->setDataElements(data_count/6, debugData);
debugDrawer.clearData();
debugShader->use();
debugShader->setUniform("viewProjectionMatrix", lightingViewProjectionMatrix);
debug_vao->render();
}
} }
} }
@ -662,3 +693,12 @@ bool Graphics::getRenderShadows() {
bool Graphics::getRenderFlames() { bool Graphics::getRenderFlames() {
return renderFlames; return renderFlames;
} }
void Graphics::setRenderDebug(bool state) {
renderDebug = state;
}
bool Graphics::getRenderDebug() {
return renderDebug;
}

View File

@ -7,6 +7,7 @@
#include <ACGL/OpenGL/Objects/FrameBufferObject.hh> #include <ACGL/OpenGL/Objects/FrameBufferObject.hh>
#include "level.hh" #include "level.hh"
#include "debugDraw.hh"
using namespace ACGL::OpenGL; using namespace ACGL::OpenGL;
@ -26,8 +27,10 @@ class Graphics {
void renderLoadingScreen(); void renderLoadingScreen();
void setRenderShadows(bool state); void setRenderShadows(bool state);
void setRenderFlames(bool state); void setRenderFlames(bool state);
void setRenderDebug(bool state);
bool getRenderShadows(); bool getRenderShadows();
bool getRenderFlames(); bool getRenderFlames();
bool getRenderDebug();
private: private:
void bindTextureUnits(); void bindTextureUnits();
void updateLights(); void updateLights();
@ -77,6 +80,12 @@ class Graphics {
float loadingScreenHeight; float loadingScreenHeight;
bool renderShadows; bool renderShadows;
bool renderFlames; bool renderFlames;
bool renderDebug;
DebugDraw debugDrawer;
SharedArrayBuffer debug_ab;
SharedVertexArrayObject debug_vao;
SharedShaderProgram debugShader;
}; };
#endif #endif

View File

@ -29,6 +29,9 @@ static void keyCallback(GLFWwindow* _window, int _key, int, int _action, int)
if (_key == GLFW_KEY_F6 && _action == GLFW_PRESS) { if (_key == GLFW_KEY_F6 && _action == GLFW_PRESS) {
app.getGraphics()->setRenderFlames(!app.getGraphics()->getRenderFlames()); app.getGraphics()->setRenderFlames(!app.getGraphics()->getRenderFlames());
} }
if (_key == GLFW_KEY_F7 && _action == GLFW_PRESS) {
app.getGraphics()->setRenderDebug(!app.getGraphics()->getRenderDebug());
}
} }
static void mouseCallback(GLFWwindow* window, int button, int action, int) { static void mouseCallback(GLFWwindow* window, int button, int action, int) {

View File

@ -710,3 +710,7 @@ void Physics::kill() //delete dynamically allocated memory
//feel like a good little programmer because everything is clean //feel like a good little programmer because everything is clean
} }
btDynamicsWorld* Physics::getWorld() {
return world;
}

View File

@ -79,6 +79,7 @@ class Physics {
bool playerWithObject(); bool playerWithObject();
void activateEndgame(); void activateEndgame();
void forcePlayer(glm::vec3 newPosition); void forcePlayer(glm::vec3 newPosition);
btDynamicsWorld* getWorld();
struct positionConstraint{btRigidBody* body; float strength; btVector3 position;}; struct positionConstraint{btRigidBody* body; float strength; btVector3 position;};