Skip to content

Commit

Permalink
funkcni zaklad fyziky + debug jeji debug kresleni
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasKimer committed Nov 30, 2011
1 parent 527e298 commit a5286c4
Show file tree
Hide file tree
Showing 8 changed files with 211 additions and 48 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*.so

# VS2010 stuff
Release/*
Debug/*
ipch/*
*.sdf
Expand Down
128 changes: 85 additions & 43 deletions Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,6 @@ using namespace std;

#define WALK_SPEED 0.1f

// pole pomocnych car k vykresleni - caru definuji dva body a barva
struct LINE {
LINE(glm::vec3 a, glm::vec3 b, glm::vec3 color) : a(a), b(b), color(color) {};

glm::vec3 a;
glm::vec3 b;
glm::vec3 color;
};
vector<LINE> lines;

GLuint linesVBO;
GLuint linesEBO;


////////////////////////////////////////////////////////////////////////////////

Expand Down Expand Up @@ -54,6 +41,8 @@ void Game::onInit()
#if 1
BaseModel* chairs = container->load3DS("models/chairs.3ds");
BaseModel* e112 = container->load3DS("models/e112.3ds");
BaseModel* car = container->load3DS("models/car.3ds");
BaseModel* wheel = container->load3DS("models/wheel.3ds");

/*
CachedModel* e112 = CachedModel::load("models/e112.3ds~");
Expand All @@ -71,11 +60,11 @@ void Game::onInit()
if (1) {
container->addModel("e112", e112);
glm::mat4 modelmat = glm::scale(glm::vec3(0.2));
container->queueDraw(e112, modelmat);
e112QueueItem = container->queueDraw(e112, modelmat);
}

// vykresli zidle
{
/* {
glm::mat4 scale = glm::scale(glm::vec3(0.2));
glm::mat4 rows[] = {
glm::translate(scale, glm::vec3(-740, 19, -70)),
Expand Down Expand Up @@ -103,7 +92,7 @@ void Game::onInit()
}
}
}

*/
// pro kazde svetlo v kontejneru pridat kouli, ktera ho znazornuje
{
BaseModel* sphere = container->load3DS("models/sphere.3ds");
Expand All @@ -119,8 +108,24 @@ void Game::onInit()
container->queueDraw(sphere, mat);
}
}

{
container->addModel("car", car);
carQueueItem = container->queueDraw(car);
}

{
container->addModel("wheel", wheel);
for (unsigned int i = 0; i < 4; i++)
{
wheelQueueItem[i] = container->queueDraw(wheel);
}
}
#endif

// zapamatovat si frontu
drawingQueue = &container->getDrawingQueue();

cout << "- constructing scene" << endl;

// vyrobit scenu
Expand All @@ -136,7 +141,14 @@ void Game::onInit()

ShaderManager::loadProgram("line");

drawLine(glm::vec3(1, 1, 1), glm::vec3(10, 10, 10), glm::vec3(1, 1, 1));
cout << "- initializing physics" << endl;

physics = new Physics();
btTransform transform;
transform.setIdentity();
transform.setOrigin(btVector3(0, 3, 1));
// physics->AddRigidBody(5., transform, new btBoxShape(btVector3(0.75,0.75,0.75)))->setAngularVelocity(btVector3(1,1,1));
physics->AddStaticModel(drawingQueue->at(e112QueueItem).model, 0.2f, false);
}


Expand All @@ -148,28 +160,63 @@ void Game::onWindowRedraw(const GameTime & gameTime)

handleActiveKeys(gameTime);

physics->StepSimulation(gameTime.Elapsed() * 0.001f);

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);

glDepthFunc(GL_LESS);

drawingQueue->at(carQueueItem).matrix = physics->GetCar()->GetWorldTransform();


for (int i = 0; i < physics->GetCar()->GetVehicle()->getNumWheels(); i++)
{
btScalar m[16];
physics->GetCar()->GetVehicle()->updateWheelTransform(i, true); //synchronize the wheels with the (interpolated) chassis worldtransform
physics->GetCar()->GetVehicle()->getWheelInfo(i).m_worldTransform.getOpenGLMatrix(m); //draw wheels (cylinders)

drawingQueue->at(wheelQueueItem[i]).matrix = mat4_from_arr(m);

if (i == 1 || i == 3)
drawingQueue->at(wheelQueueItem[i]).matrix = glm::rotate(drawingQueue->at(wheelQueueItem[i]).matrix, 180.f, 0.f, 1.f, 0.f);

//PhysicsDebugDraw::DrawCylinder(m, physics->GetCar()->GetVehicle()->getWheelInfo(i).m_wheelsRadius, physics->GetCar()->GetVehicle()->getWheelInfo(i).m_wheelsRadius/2);
}

// vykreslit scenu
scene->draw();

// vykreslit fyziku
physics->DebugDrawWorld();

/*for (int i = 0; i < 100000; i++)
{
physics->GetDebugDrawer()->drawLine(btVector3(0,0,0), btVector3(5,5,5), btVector3(1,0,0));
physics->GetDebugDrawer()->drawLine(btVector3(6,6,6), btVector3(11,11,11), btVector3(0,1,0));
}*/

drawLines(physics->GetDebugDrawer()->GetLines());

// ---------------------------------------

#if 0
// vykresleni car -----------------------
SDL_GL_SwapBuffers();
}

void Game::drawLines(vector<PhysicsDebugDraw::LINE> & lines)
{
// vykresleni car -----------------------
glDisable(GL_CULL_FACE);

vector<GLfloat> vertices;
vector<GLuint> indices;
unsigned int indexI = 0;

for (vector<LINE>::iterator it = lines.begin(); it != lines.end(); it++)
for (vector<PhysicsDebugDraw::LINE>::iterator it = lines.begin(); it != lines.end(); it++)
{
LINE l = (*it);
PhysicsDebugDraw::LINE l = (*it);
vertices.push_back(l.a.x);
vertices.push_back(l.a.y);
vertices.push_back(l.a.z);
Expand Down Expand Up @@ -197,7 +244,7 @@ void Game::onWindowRedraw(const GameTime & gameTime)

// vrcholy
glEnableVertexAttribArray(activeBinding.positionAttrib);
glVertexAttribPointer(activeBinding.positionAttrib, 3, GL_FLOAT, GL_FALSE, 0, (void*)&(vertices.begin()));
glVertexAttribPointer(activeBinding.positionAttrib, 3, GL_FLOAT, GL_FALSE, 0, (void*)&(vertices.at(0)));

// modelova matice
glm::mat4 modelmat = glm::mat4(1.0);
Expand All @@ -221,29 +268,10 @@ void Game::onWindowRedraw(const GameTime & gameTime)

// kresleni car
glDrawElements(GL_LINES, indices.size(), GL_UNSIGNED_INT, (void*)&(indices.at(0)));
#endif


// ---------------------------------------

SDL_GL_SwapBuffers();
}





void Game::drawLine(glm::vec3 a, glm::vec3 b, glm::vec3 color)
{
LINE l(a, b, color);
lines.push_back(l);
lines.clear();
}






void Game::handleActiveKeys(const GameTime & gameTime)
{
bool wDown = ( find(activeKeys.begin(), activeKeys.end(), SDLK_w) != activeKeys.end() );
Expand All @@ -262,7 +290,17 @@ void Game::handleActiveKeys(const GameTime & gameTime)
float z = ( (-1.0f * sDown) + (1.0f * wDown) ) * f_step;

camera.Move(x, 0.0f, z);


if ( find(activeKeys.begin(), activeKeys.end(), SDLK_UP) != activeKeys.end() )
physics->GetCar()->Forward();
if ( find(activeKeys.begin(), activeKeys.end(), SDLK_DOWN) != activeKeys.end() )
physics->GetCar()->Backward();
if ( find(activeKeys.begin(), activeKeys.end(), SDLK_b) != activeKeys.end() )
physics->GetCar()->HandBrake();
if ( find(activeKeys.begin(), activeKeys.end(), SDLK_LEFT) != activeKeys.end() )
physics->GetCar()->TurnLeft();
if ( find(activeKeys.begin(), activeKeys.end(), SDLK_RIGHT) != activeKeys.end() )
physics->GetCar()->TurnRight();
}


Expand Down Expand Up @@ -290,6 +328,10 @@ void Game::onKeyDown(SDLKey key, Uint16 mod)
else
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
}

if (key == SDLK_RETURN) {
physics->GetCar()->Reset();
}
}


Expand Down
12 changes: 10 additions & 2 deletions Game.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
#define GAME_H

#include <algorithm>
#include <vector>

#include "BaseApp.h"
#include "Scene.h"
#include "CachedModel.h"
#include "Physics.h"

class Game : public BaseApp
{
Expand All @@ -16,20 +18,26 @@ class Game : public BaseApp
void onInit();
void onWindowRedraw(const GameTime & gameTime);

// vykresli usecku mezi dvema body (souradnice jsou v prostoru sceny)
void drawLine(glm::vec3 a, glm::vec3 b, glm::vec3 color);
// vykresli usecky
void drawLines(std::vector<PhysicsDebugDraw::LINE> & lines);

void handleActiveKeys(const GameTime & gameTime);
void onKeyDown(SDLKey key, Uint16 mod);
void onMouseMove(unsigned x, unsigned y, int xrel, int yrel, Uint8 buttons);

protected:
Scene* scene;
Physics *physics;

bool mouseCaptured;

// ukazatel na frontu kreslenych objektu
std::vector<ModelContainer::DRAWINGQUEUEITEM>* drawingQueue;

// indexy polozek v kreslici fronte
unsigned int carQueueItem, e112QueueItem;
unsigned int wheelQueueItem[4]; // z pohledu ridice: 0 - predni leve, 3 - zadni prave

// pomocna promenna pro moznost kreslit wireframe (TAB)
bool drawWireframe;

Expand Down
7 changes: 5 additions & 2 deletions Physics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,9 @@ btRigidBody * Physics::AddRigidBody(float mass, const btTransform & startTransfo
return body;
}

void Physics::AddStaticModel(BaseModel * model, float scale)
void Physics::AddStaticModel(BaseModel * model, float scale, bool debugDraw)
{

for (unsigned int i = 0; i < model->getMeshes().size(); i++)
{
Mesh *m = model->getMeshes()[i];
Expand All @@ -170,6 +171,8 @@ void Physics::AddStaticModel(BaseModel * model, float scale)
tr.setIdentity();
tr.setOrigin(btVector3(0, 0, 0));

AddRigidBody(0, tr, shape);
btRigidBody *tmp = AddRigidBody(0, tr, shape); //->getCollisionFlags;
if (!debugDraw)
tmp->setCollisionFlags(tmp->getCollisionFlags() | btCollisionObject::CF_DISABLE_VISUALIZE_OBJECT);
}
}
3 changes: 2 additions & 1 deletion Physics.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ class Physics // TODO Singleton

btCollisionShape* CreateTriMeshGround();

void AddStaticModel(BaseModel *model, float scale);
void AddStaticModel(BaseModel *model, float scale, bool debugDraw = true);
PhysicsDebugDraw *GetDebugDrawer() { return m_debugDraw; }

private:

Expand Down
51 changes: 51 additions & 0 deletions PhysicsDebugDraw.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@


#include "PhysicsDebugDraw.h"


PhysicsDebugDraw::PhysicsDebugDraw(void): m_debugMode(1)
{

}

PhysicsDebugDraw::~PhysicsDebugDraw(void)
{

}

void PhysicsDebugDraw::drawLine(const btVector3& from,const btVector3& to,const btVector3& color)
{
glm::vec3 src(from.x(), from.y(), from.z());
glm::vec3 dst(to.x(), to.y(), to.z());
glm::vec3 col(color.x(), color.y(), color.z());

LINE l(src, dst, col);
lines.push_back(l);
}


/*void PhysicsDebugDraw::DrawCylinder(GLfloat *m, GLfloat radius, GLfloat height)
{
static GLUquadricObj *quadricObj = 0;
if (quadricObj == 0)
{
quadricObj = gluNewQuadric();
gluQuadricDrawStyle(quadricObj, GLU_LINE);
}
if (m)
{
glPushMatrix();
glMultMatrixf(m);
glRotatef(-90.0f, 0.0f, 1.0f, 0.0f);
glTranslatef(0.0f, 0.0f, -height/2.f);
}
glColor3f(1.f, 0.f, 1.f);
gluCylinder(quadricObj, radius, radius, height, 10, 1);
if (m)
glPopMatrix();
}*/
Loading

0 comments on commit a5286c4

Please sign in to comment.