1 /***************************************************************************
2 * Copyright (C) 2008 by Scott West *
3 * westsg2@mcmaster.ca *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
22 #include <btBulletDynamicsCommon.h>
26 #define MOVE_STEP 0.05
28 GLWidget::GLWidget(QWidget
* parent
) : QGLWidget(parent
)
34 btVector3
worldMin (-100,-100,-100);
35 btVector3
worldMax ( 100, 100, 100);
36 int maxProxies
= 1024;
38 btAxisSweep3
* broadphase
=
39 new btAxisSweep3 (worldMin
, worldMax
, maxProxies
);
41 btDefaultCollisionConfiguration
* collisionConf
=
42 new btDefaultCollisionConfiguration();
44 btCollisionDispatcher
* dispatcher
=
45 new btCollisionDispatcher(collisionConf
);
47 btSequentialImpulseConstraintSolver
* solver
=
48 new btSequentialImpulseConstraintSolver
;
51 new btDiscreteDynamicsWorld(dispatcher
,broadphase
,solver
,collisionConf
);
53 btWorld
->setGravity(btVector3(0,-0.5,0));
55 objects
.push_back ( new Box ( Vector (0,20,0),
58 objects
.push_back (new Box ( Vector (-3,20,0),
61 objects
.push_back (new Box ( Vector (2,20,0),
65 btWorld
->addRigidBody (objects
[0]->getBody () );
66 btWorld
->addRigidBody (objects
[1]->getBody () );
67 btWorld
->addRigidBody (objects
[2]->getBody () );
69 btCollisionShape
* groundShape
= new btStaticPlaneShape(btVector3(0,1,0),1);
70 btDefaultMotionState
* groundMotionState
=
71 new btDefaultMotionState(btTransform(btQuaternion (0,0,0,1),
73 btRigidBody::btRigidBodyConstructionInfo
74 groundRigidBodyCI (0,groundMotionState
,groundShape
,btVector3 (0,0,0));
75 btRigidBody
* groundRigidBody
= new btRigidBody(groundRigidBodyCI
);
76 btWorld
->addRigidBody(groundRigidBody
);
88 void GLWidget::keyPressEvent (QKeyEvent
*event
)
90 btRigidBody
* b
= objects
[0]->getBody ();
92 switch (event
->key ())
101 offsety
-= MOVE_STEP
;
104 offsety
+= MOVE_STEP
;
107 offsetz
-= MOVE_STEP
;
110 offsetz
+= MOVE_STEP
;
114 b
->applyForce (btVector3 (0.0, 0.0, 10.0),
115 btVector3 (-3.0, 0.0, 0.0));
124 void GLWidget::updateGL ()
126 btWorld
->stepSimulation (1/60.0);
127 QGLWidget::updateGL ();
130 void GLWidget::paintGL ()
132 glClear (GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
135 GLfloat pos_light
[] = {-1.0, 2.0, 10.0, 0.0};
136 GLfloat dif_light
[] = {1.0f
, 1.0f
, 1.0f
, 1.0f
};
137 glLightfv (GL_LIGHT1
, GL_DIFFUSE
, dif_light
);
138 glLightfv (GL_LIGHT1
, GL_POSITION
, pos_light
);
140 gluLookAt (offsetx
, 10, 20.0,
144 for( std::vector
<Box
*>::iterator it
= objects
.begin() ;
145 it
!= objects
.end ();
155 void GLWidget::resizeGL (int w
, int h
)
157 glMatrixMode (GL_PROJECTION
);
159 glViewport (0, 0, w
, h
);
160 gluPerspective (45.0, (GLfloat
)w
/(GLfloat
)h
, 1.0, 500.0);
162 glMatrixMode (GL_MODELVIEW
);
165 void GLWidget::initializeGL ()
167 glClearColor (0.0f
, 0.0f
, 0.0f
, 1.0f
);
169 glEnable (GL_DEPTH_TEST
);
170 glDepthMask (GL_TRUE
);
172 glDepthFunc (GL_LEQUAL
);
173 glHint (GL_PERSPECTIVE_CORRECTION_HINT
, GL_NICEST
);
175 glEnable (GL_LIGHTING
);
176 glEnable (GL_LIGHT1
);
177 glShadeModel (GL_SMOOTH
);