Updated the to use static libraries too.
[blobbuild.git] / glwidget.cpp
blob53e27c4753bfda19d39cc9622f40d07614833a2e
1 /***************************************************************************
2 * Copyright (C) 2008 by Scott West *
3 * westsg2@mcmaster.ca *
4 * *
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. *
9 * *
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. *
14 * *
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 ***************************************************************************/
20 #include "glwidget.h"
22 #include <btBulletDynamicsCommon.h>
24 #include <iostream>
26 #define MOVE_STEP 0.05
28 GLWidget::GLWidget(QWidget* parent) : QGLWidget(parent)
30 setFocus ();
31 grabKeyboard ();
32 resize (800,600);
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;
50 btWorld =
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),
56 Vector (1,1,3)
57 ) );
58 objects.push_back (new Box ( Vector (-3,20,0),
59 Vector (1,2,2)
60 ) );
61 objects.push_back (new Box ( Vector (2,20,0),
62 Vector (1,1,2)
63 ) );
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),
72 btVector3 (0,-1,0)));
73 btRigidBody::btRigidBodyConstructionInfo
74 groundRigidBodyCI (0,groundMotionState,groundShape,btVector3 (0,0,0));
75 btRigidBody* groundRigidBody = new btRigidBody(groundRigidBodyCI);
76 btWorld->addRigidBody(groundRigidBody);
78 offsetx = 0.0;
79 offsety = 0.0;
80 offsetz = 0.0;
84 GLWidget::~GLWidget()
88 void GLWidget::keyPressEvent (QKeyEvent *event)
90 btRigidBody* b = objects[0]->getBody ();
92 switch (event->key ())
94 case Qt::Key_Left:
95 offsetx += MOVE_STEP;
96 break;
97 case Qt::Key_Right:
98 offsetx -= MOVE_STEP;
99 break;
100 case Qt::Key_Up:
101 offsety -= MOVE_STEP;
102 break;
103 case Qt::Key_Down:
104 offsety += MOVE_STEP;
105 break;
106 case Qt::Key_W:
107 offsetz -= MOVE_STEP;
108 break;
109 case Qt::Key_S:
110 offsetz += MOVE_STEP;
111 break;
112 case Qt::Key_Space:
113 b->activate ();
114 b->applyForce (btVector3 (0.0, 0.0, 10.0),
115 btVector3 (-3.0, 0.0, 0.0));
116 break;
117 default:
118 break;
121 updateGL();
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);
133 glLoadIdentity ();
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,
141 0.0, 0.0, 0.0,
142 0.0, 1.0, 0.0);
144 for( std::vector<Box*>::iterator it = objects.begin() ;
145 it != objects.end ();
146 ++it)
148 (*it)->render ();
155 void GLWidget::resizeGL (int w, int h)
157 glMatrixMode (GL_PROJECTION);
158 glLoadIdentity ();
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);