diff --git a/data/shader/debug.fsh b/data/shader/debug.fsh new file mode 100644 index 0000000..2a3ca96 --- /dev/null +++ b/data/shader/debug.fsh @@ -0,0 +1,9 @@ +#version 150 + +in vec3 vColor; + +out vec4 oColor; + +void main() { + oColor = vec4(vColor, 1.0); +} diff --git a/data/shader/debug.vsh b/data/shader/debug.vsh new file mode 100644 index 0000000..3078e08 --- /dev/null +++ b/data/shader/debug.vsh @@ -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); +} diff --git a/game/debugDraw.cc b/game/debugDraw.cc new file mode 100644 index 0000000..88e9948 --- /dev/null +++ b/game/debugDraw.cc @@ -0,0 +1,64 @@ +#include "debugDraw.hh" + +DebugDraw::DebugDraw() : debug_mode(0) { + data = std::vector(); +} + +void DebugDraw::setDebugMode(int debugMode) { + this->debug_mode = debugMode; +} + +int DebugDraw::getDebugMode() const { + return debug_mode; +} + +std::vector* 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(); +} diff --git a/game/debugDraw.hh b/game/debugDraw.hh new file mode 100644 index 0000000..497b53d --- /dev/null +++ b/game/debugDraw.hh @@ -0,0 +1,25 @@ +#pragma once + +#include "LinearMath/btIDebugDraw.h" +#include + +class DebugDraw : public btIDebugDraw { + int debug_mode; + public: + DebugDraw(); + void setDebugMode(int debugMode); + int getDebugMode() const; + std::vector* 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 data; +}; diff --git a/game/graphics.cc b/game/graphics.cc index d762a9d..9b625bb 100644 --- a/game/graphics.cc +++ b/game/graphics.cc @@ -6,6 +6,7 @@ #include #include +#include "LinearMath/btIDebugDraw.h" using namespace ACGL::OpenGL; @@ -27,6 +28,7 @@ Graphics::Graphics(glm::uvec2 windowSize, float nearPlane, gameStart = false; renderShadows = true; renderFlames = true; + renderDebug = false; } Graphics::Graphics() { @@ -109,6 +111,20 @@ void Graphics::init(Level* level) { flamePostShader = ShaderProgramCreator("flame_post") .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(3); framebuffer_directional = std::vector(3); @@ -508,6 +524,21 @@ void Graphics::render(double time) glBlitFramebuffer(0, 0, windowSize.x, windowSize.y, 0, 0, windowSize.x, windowSize.y, 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; iat(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() { return renderFlames; } + + +void Graphics::setRenderDebug(bool state) { + renderDebug = state; +} + +bool Graphics::getRenderDebug() { + return renderDebug; +} diff --git a/game/graphics.hh b/game/graphics.hh index 2b642a2..4026594 100644 --- a/game/graphics.hh +++ b/game/graphics.hh @@ -7,6 +7,7 @@ #include #include "level.hh" +#include "debugDraw.hh" using namespace ACGL::OpenGL; @@ -26,8 +27,10 @@ class Graphics { void renderLoadingScreen(); void setRenderShadows(bool state); void setRenderFlames(bool state); + void setRenderDebug(bool state); bool getRenderShadows(); bool getRenderFlames(); + bool getRenderDebug(); private: void bindTextureUnits(); void updateLights(); @@ -77,6 +80,12 @@ class Graphics { float loadingScreenHeight; bool renderShadows; bool renderFlames; + bool renderDebug; + DebugDraw debugDrawer; + SharedArrayBuffer debug_ab; + SharedVertexArrayObject debug_vao; + SharedShaderProgram debugShader; + }; #endif diff --git a/game/main.cc b/game/main.cc index 2396c5d..e6667bb 100644 --- a/game/main.cc +++ b/game/main.cc @@ -29,6 +29,9 @@ static void keyCallback(GLFWwindow* _window, int _key, int, int _action, int) if (_key == GLFW_KEY_F6 && _action == GLFW_PRESS) { 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) { diff --git a/game/physics.cc b/game/physics.cc index a6daf4b..feab9ff 100644 --- a/game/physics.cc +++ b/game/physics.cc @@ -710,3 +710,7 @@ void Physics::kill() //delete dynamically allocated memory //feel like a good little programmer because everything is clean } + +btDynamicsWorld* Physics::getWorld() { + return world; +} diff --git a/game/physics.hh b/game/physics.hh index 3120ca3..d9f7605 100644 --- a/game/physics.hh +++ b/game/physics.hh @@ -79,6 +79,7 @@ class Physics { bool playerWithObject(); void activateEndgame(); void forcePlayer(glm::vec3 newPosition); + btDynamicsWorld* getWorld(); struct positionConstraint{btRigidBody* body; float strength; btVector3 position;};