Browse Source

Added true debug build, got rendering working

ogre-integration
Macoy Madson 1 year ago
parent
commit
2e8884edb6
12 changed files with 388 additions and 13 deletions
  1. +3
    -0
      Build.sh
  2. +11
    -0
      BuildDependencies_Debug.sh
  3. +3
    -0
      Build_Debug.sh
  4. +3
    -2
      Jamfile
  5. +4
    -0
      Jamrules
  6. +28
    -0
      build_cmake_pybullet_double_debug.sh
  7. +79
    -0
      src/DebugDraw.cpp
  8. +23
    -0
      src/DebugDraw.hpp
  9. +183
    -2
      src/Main.cpp
  10. +6
    -0
      src/PhysicsVehicle.cpp
  11. +32
    -0
      src/PhysicsWorld.cpp
  12. +13
    -9
      src/PhysicsWorld.hpp

+ 3
- 0
Build.sh View File

@ -0,0 +1,3 @@
#!/bin/sh
jam -j4

+ 11
- 0
BuildDependencies_Debug.sh View File

@ -0,0 +1,11 @@
#!/bin/sh
echo Building Bullet3...
./build_cmake_pybullet_double_debug.sh
echo Finished building Bullet3!
echo
echo Building Base2.0...
# TODO: Fix this foolishness
# TODO: Add debug build
cd ../base2.0 && jam -j4 && jam -j4 libBase20
echo Finished building Base2.0!

+ 3
- 0
Build_Debug.sh View File

@ -0,0 +1,3 @@
#!/bin/sh
jam -j4 -sDEBUG_BUILD=true

+ 3
- 2
Jamfile View File

@ -1,8 +1,9 @@
SubDir . ;
Main spargus_vehicle_prototype : src/Main.cpp
src/PhysicsVehicle.cpp
src/PhysicsWorld.cpp
src/PhysicsVehicle.cpp
src/PhysicsWorld.cpp
src/DebugDraw.cpp
;
# LinkLibraries spargus_vehicle_prototype : libBase20 ;


+ 4
- 0
Jamrules View File

@ -36,6 +36,10 @@ LINKLIBS =
# SFML
-lsfml-audio -lsfml-graphics -lsfml-window -lsfml-system
# OpenGL
-lGL
-lGLU
# Base
-Lbase2.0 -lBase20


+ 28
- 0
build_cmake_pybullet_double_debug.sh View File

@ -0,0 +1,28 @@
#!/bin/sh
cd bullet3
if [ -e CMakeCache.txt ]; then
rm CMakeCache.txt
fi
mkdir -p build_cmake_debug
cd build_cmake_debug
cmake -DBUILD_PYBULLET=ON -DBUILD_PYBULLET_NUMPY=ON -DUSE_DOUBLE_PRECISION=ON -DBT_USE_EGL=ON -DCMAKE_BUILD_TYPE=Debug .. || exit 1
make -j $(command nproc 2>/dev/null || echo 12) || exit 1
cd examples
cd pybullet
if [ -e pybullet.dylib ]; then
ln -f -s pybullet.dylib pybullet.so
fi
if [ -e pybullet_envs ]; then
rm pybullet_envs
fi
if [ -e pybullet_data ]; then
rm pybullet_data
fi
if [ -e pybullet_utils ]; then
rm pybullet_utils
fi
ln -s ../../../examples/pybullet/gym/pybullet_envs .
ln -s ../../../examples/pybullet/gym/pybullet_data .
ln -s ../../../examples/pybullet/gym/pybullet_utils .
echo "Completed build of Bullet."

+ 79
- 0
src/DebugDraw.cpp View File

@ -0,0 +1,79 @@
#include "DebugDraw.hpp"
#include <GL/glu.h>
#include <SFML/OpenGL.hpp>
#include <iostream>
void DebugDraw::drawLine(const btVector3& from, const btVector3& to, const btVector3& fromColor,
const btVector3& toColor)
{
glBegin(GL_LINES);
glColor3d(fromColor.getX(), fromColor.getY(), fromColor.getZ());
glVertex3d(from.getX(), from.getY(), from.getZ());
glColor3d(toColor.getX(), toColor.getY(), toColor.getZ());
glVertex3d(to.getX(), to.getY(), to.getZ());
glEnd();
}
void DebugDraw::drawLine(const btVector3& from, const btVector3& to, const btVector3& color)
{
drawLine(from, to, color, color);
}
void DebugDraw::drawSphere(const btVector3& p, btScalar radius, const btVector3& color)
{
glColor3d(color.getX(), color.getY(), color.getZ());
int slices = 16;
int stacks = 16;
GLUquadric* sphereQuadric = gluNewQuadric();
gluSphere(sphereQuadric, radius, slices, stacks);
// TODO Position it!
// p.getX(), p.getY(), p.getZ()
}
void DebugDraw::drawBox(const btVector3& bbMin, const btVector3& bbMax, const btVector3& color)
{
glColor3d(color.getX(), color.getY(), color.getZ());
// TODO
// glDrawStrokedCube(AxisAlignedBox3f(Vec3f(bbMin.getX(), bbMin.getY(), bbMin.getZ()),
// Vec3f(bbMax.getX(), bbMax.getY(), bbMax.getZ())));
}
void DebugDraw::drawContactPoint(const btVector3& PointOnB, const btVector3& normalOnB,
btScalar distance, int lifeTime, const btVector3& color)
{
glColor3d(color.getX(), color.getY(), color.getZ());
glBegin(GL_LINES);
// From
glVertex3d(PointOnB.getX(), PointOnB.getY(), PointOnB.getZ());
// To
glVertex3d(normalOnB.getX() + PointOnB.getX(), normalOnB.getY() + PointOnB.getY(),
normalOnB.getZ() + PointOnB.getZ());
glEnd();
}
void DebugDraw::reportErrorWarning(const char* warningString)
{
std::cout << warningString << std::endl;
}
void DebugDraw::draw3dText(const btVector3& location, const char* textString)
{
// TODO
// TextLayout textDraw;
// textDraw.clear((0, 0, 0, 0));
// textDraw.setColor(1, 1, 1));
// textDraw.setFont(Font("Arial", 16));
// textDraw.addCenteredLine(textString);
// glDraw(glTexture(textDraw.render()), Vec2f(location.getX(), location.getY()));
}
void DebugDraw::setDebugMode(int newDebugMode)
{
debugMode = newDebugMode;
}
int DebugDraw::getDebugMode() const
{
return debugMode;
}

+ 23
- 0
src/DebugDraw.hpp View File

@ -0,0 +1,23 @@
#pragma once
#include "LinearMath/btIDebugDraw.h"
#include "btBulletDynamicsCommon.h"
class DebugDraw : public btIDebugDraw
{
int debugMode;
public:
virtual ~DebugDraw() = default;
virtual void drawLine(const btVector3& from, const btVector3& to, const btVector3& fromColor,
const btVector3& toColor);
virtual void drawLine(const btVector3& from, const btVector3& to, const btVector3& color);
virtual void drawSphere(const btVector3& p, btScalar radius, const btVector3& color);
virtual void drawBox(const btVector3& bbMin, const btVector3& bbMax, 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 debugMode);
virtual int getDebugMode() const;
};

+ 183
- 2
src/Main.cpp View File

@ -3,6 +3,11 @@
#include "graphics/graphics.hpp"
#include "input/input.hpp"
#include <GL/glu.h>
#include <SFML/OpenGL.hpp>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include "PhysicsVehicle.hpp"
#include "PhysicsWorld.hpp"
@ -30,8 +35,124 @@ void windowResizeCB(float width, float height)
{
WindowWidth = width;
WindowHeight = height;
// Is this necessary?
glViewport(0,0, width, height);
}
struct Camera
{
float camPos[3] = {0, -10, 0};
float camRot[3] = {0, 0, 0};
float camTranslate[3] = {0, 0, -10};
window& win;
float add = 1;
float prevY = 0;
float prevX = 0;
sf::RenderWindow* winBase = nullptr;
Camera(window& winOwner) : win(winOwner)
{
winBase = win.getBase();
win.getBase()->setMouseCursorVisible(false);
sf::Mouse::setPosition(sf::Vector2i(win.getWidth() / 2, win.getHeight() / 2), *winBase);
}
void UpdateInput()
{
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))
{
/* yrotrad = (yrot / 180 * 3.141592654f);
xrotrad = (xrot / 180 * 3.141592654f);
xpos += float(sin(yrotrad)) ;
zpos -= float(cos(yrotrad)) ;
* */
camTranslate[0] += float(sin(camRot[1] / 180 * 3.141592654));
camTranslate[2] -= float(cos(camRot[1] / 180 * 3.141592654));
// camPos[1]=-10;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
{
camTranslate[0] -= float(sin(camRot[1] / 180 * 3.141592654));
camTranslate[2] += float(cos(camRot[1] / 180 * 3.141592654));
// camTranslate[2]=50*win.GetFrameTime();
// camPos[1]=-10;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A))
{
camTranslate[0] -= float(cos(camRot[1] / 180 * 3.141592654));
camTranslate[2] -= float(sin(camRot[1] / 180 * 3.141592654));
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))
{
camTranslate[0] += float(cos(camRot[1] / 180 * 3.141592654));
camTranslate[2] += float(sin(camRot[1] / 180 * 3.141592654));
}
// camRot[1]=(10*(in->GetMouseX()*win.GetWidth()/10) + 400)/win.GetWidth();
camRot[1] += (sf::Mouse::getPosition(*winBase).x - prevX) / 5 /**winBase.GetWidth()/3600*/;
camRot[0] += (sf::Mouse::getPosition(*winBase).y - prevY) / 5;
prevX = sf::Mouse::getPosition(*winBase).x;
prevY = sf::Mouse::getPosition(*winBase).y;
if (prevX <= 100 || prevX >= 200)
{
sf::Mouse::setPosition(sf::Vector2i(winBase->getSize().x / 2, winBase->getSize().y / 2), *winBase);
prevX = winBase->getSize().x / 2;
}
if (prevY <= 100 || prevY >= 200)
{
sf::Mouse::setPosition(sf::Vector2i(winBase->getSize().x / 2, winBase->getSize().y / 2), *winBase);
prevY = win.getHeight() / 2;
}
}
void UpdateStart()
{
UpdateInput();
// Set camera
if (camRot[0] < -90)
camRot[0] = -90;
if (camRot[0] > 100)
camRot[0] = 100;
// Make the degree positive
if (camRot[1] < 0)
camRot[1] += 360;
// Clamp the degree to 0-360
if (camRot[1] >= 360)
camRot[1] -= 360;
if (camRot[1] <= -360)
camRot[1] += 360;
// Calculate the slope of the movement vector
/*camTranslate[0] *=sin(camRot[1]);
camTranslate[2] *=cos(camRot[1]);*/
// Add the translation vector
camPos[0] += camTranslate[0];
camPos[1] += camTranslate[1];
camPos[2] += camTranslate[2];
// Define the camera matrix
glLoadIdentity();
gluLookAt(0, 0, 1, 0, 0, -1, 0, 1, 0);
glRotatef(camRot[0], 1, 0, 0);
glRotatef(camRot[1], 0, 1, 0);
glRotatef(camRot[2], 0, 0, 1);
glTranslatef(-camPos[0], camPos[1], -camPos[2]);
glPushMatrix();
}
void UpdateEnd()
{
// Reset translation vector
camTranslate[0] = 0;
camTranslate[1] = 0;
camTranslate[2] = 0;
glPopMatrix();
}
};
int main()
{
std::cout << "Spargus Vehicle Prototype\n";
@ -41,13 +162,64 @@ int main()
//
PhysicsWorld physicsWorld;
PhysicsVehicle vehicle(physicsWorld);
window mainWindow(WindowWidth, WindowHeight, "Spargus Vehicle Prototype", &windowResizeCB);
initializeWindow(mainWindow);
inputManager input(&mainWindow);
PhysicsVehicle vehicle(physicsWorld);
// OpenGL world setup
int groundCallList = -1;
{
glEnable(GL_DEPTH_TEST);
// glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
float pos[4] = {5, 0, -1, 1};
glLightfv(GL_LIGHT0, GL_POSITION, pos);
glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 0.1f);
// glEnable(GL_CULL_FACE);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60, mainWindow.getWidth() / mainWindow.getHeight(), 1, 500);
glRotatef(0, 0, 1, 0);
// Floor
glMatrixMode(GL_MODELVIEW);
{
groundCallList = glGenLists(1);
glNewList(groundCallList, GL_COMPILE);
glBegin(GL_QUADS);
// Ground
glNormal3f(0, 1, 0);
glVertex3f(-50, -5, 0);
glNormal3f(0, 1, 0);
glVertex3f(50, -5, 0);
glNormal3f(0, 1, 0);
glVertex3f(50, -5, -400);
glNormal3f(0, 1, 0);
glVertex3f(-50, -5, -400);
glEnd();
glEndList();
}
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Lighting
float col[4] = {1, 1, 1, 0.0};
glLightfv(GL_LIGHT0, GL_DIFFUSE, col);
// glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 25);
// Fog
glEnable(GL_FOG);
glFogi(GL_FOG_MODE, GL_LINEAR);
glFogfv(GL_FOG_COLOR, col);
glFogf(GL_FOG_START, 200);
glFogf(GL_FOG_END, 300);
}
////////////////////////////////////////////////////////////////////////////////
// Game loop
@ -58,11 +230,20 @@ int main()
timer frameTimer;
frameTimer.start();
Camera cam(mainWindow);
while (!mainWindow.shouldClose() && !input.isPressed(inputCode::Escape))
{
cam.UpdateStart();
vehicle.Update(lastFrameTime);
physicsWorld.Update(lastFrameTime);
glCallList(groundCallList);
physicsWorld.DebugRender();
cam.UpdateEnd();
// Finished physics update and drawing; send it on its way
mainWindow.update();


+ 6
- 0
src/PhysicsVehicle.cpp View File

@ -2,6 +2,8 @@
#include "PhysicsWorld.hpp"
#include <iostream>
////////////////////////////////////////////////////////////////////////////////
// Vehicle constants
//
@ -189,4 +191,8 @@ void PhysicsVehicle::Update(float deltaTime)
vehicle->setSteeringValue(gVehicleSteering, wheelIndex);
wheelIndex = 1;
vehicle->setSteeringValue(gVehicleSteering, wheelIndex);
const btVector3& carLinearVelocity = carChassis->getLinearVelocity();
std::cout << "Vehicle linear velocity: " << carLinearVelocity.getX() << ", "
<< carLinearVelocity.getY() << ", " << carLinearVelocity.getZ() << "\n";
}

+ 32
- 0
src/PhysicsWorld.cpp View File

@ -1,5 +1,12 @@
#include "PhysicsWorld.hpp"
#include "BulletDynamics/MLCPSolvers/btDantzigSolver.h"
#include "BulletDynamics/MLCPSolvers/btMLCPSolver.h"
#include "BulletDynamics/MLCPSolvers/btSolveProjectedGaussSeidel.h"
#include "LinearMath/btAlignedObjectArray.h"
#include "btBulletCollisionCommon.h"
#include "btBulletDynamicsCommon.h"
#include <iostream>
// TODO: Find out what this means
@ -30,6 +37,9 @@ PhysicsWorld::PhysicsWorld()
world = new btDiscreteDynamicsWorld(collisionDispatcher, overlappingPairCache, constraintSolver,
collisionConfiguration);
debugDrawer.setDebugMode(btIDebugDraw::DBG_DrawWireframe);
world->setDebugDrawer(&debugDrawer);
if (bulletUseMCLPSolver)
{
// for direct solver it is better to have a small A matrix
@ -42,6 +52,23 @@ PhysicsWorld::PhysicsWorld()
world->getSolverInfo().m_minimumSolverBatchSize = 128;
}
world->getSolverInfo().m_globalCfm = 0.00001;
//world->setGravity(btVector3(0,0,0));
// Ground (for testing only)
{
btVector3 groundExtents(50, 50, 50);
groundExtents[upAxisIndex] = 3;
btCollisionShape* groundShape = new btBoxShape(groundExtents);
// TODO Don't leak this
// collisionShapes.push_back(groundShape);
btTransform tr;
tr.setIdentity();
tr.setOrigin(btVector3(0, -3, 0));
localCreateRigidBody(0, tr, groundShape);
}
}
void PhysicsWorld::Update(float deltaTime)
@ -91,6 +118,11 @@ void PhysicsWorld::Update(float deltaTime)
}
}
void PhysicsWorld::DebugRender()
{
world->debugDrawWorld();
}
btRigidBody* PhysicsWorld::localCreateRigidBody(btScalar mass, const btTransform& startTransform,
btCollisionShape* shape)
{


+ 13
- 9
src/PhysicsWorld.hpp View File

@ -1,15 +1,8 @@
#pragma once
// Much of the bullet code is copied from the Forklift demo for a good starting point
#include "BulletDynamics/MLCPSolvers/btDantzigSolver.h"
#include "BulletDynamics/MLCPSolvers/btMLCPSolver.h"
#include "BulletDynamics/MLCPSolvers/btSolveProjectedGaussSeidel.h"
#include "LinearMath/btAlignedObjectArray.h"
#include "btBulletCollisionCommon.h"
#include "btBulletDynamicsCommon.h"
#include "DebugDraw.hpp"
#include "BulletDynamics/Vehicle/btRaycastVehicle.h"
// Much of the bullet code is copied from the Forklift demo for a good starting point
// extern bool bulletUseMCLPSolver;
@ -17,6 +10,12 @@
#define upAxisIndex 1
#define forwardAxisIndex 2
class btDefaultCollisionConfiguration;
class btCollisionDispatcher;
class btBroadphaseInterface;
class btConstraintSolver;
class btDiscreteDynamicsWorld;
struct PhysicsWorld
{
btDefaultCollisionConfiguration* collisionConfiguration;
@ -26,9 +25,14 @@ struct PhysicsWorld
btConstraintSolver* constraintSolver;
btDiscreteDynamicsWorld* world;
DebugDraw debugDrawer;
PhysicsWorld();
void Update(float deltaTime);
void DebugRender();
btRigidBody* localCreateRigidBody(btScalar mass, const btTransform& startTransform,
btCollisionShape* shape);
};

Loading…
Cancel
Save