vrsode/VrsTest.cpp

Go to the documentation of this file.
00001 #include <ode/ode.h>
00002 
00003 #include <vrs/vrs.h>
00004 #include <vrs/glut/glutcanvas.h>
00005 #include <vrs/polygonsetbuilder.h>
00006 
00007 #include "Car3DS.h"
00008 #include "CarSimple.h"
00009 #include "random_utils/LogManager.h"
00010 #include "random_utils/ThreadSafeCanvas.h"
00011 #include "PhysicsManager.h"
00012 #include "PhysicsBody.h"
00013 #include "PhysicsJointBall.h"
00014 #include "SamplePlane.h"
00015 #include "SampleBox.h"
00016 #include "PlaneShape.h"
00017 #include "BoxShape.h"
00018 
00019 using namespace VRS;
00020 using namespace vrsode;
00021 
00022 #define VRSODE_MULTITHREAD
00023 
00024 class VrsTest : public SharedObj
00025 {
00026 public:
00027     VrsTest();
00028 private:
00029     void keyEvent();
00030 
00031     SO<BehaviorCallback>    m_keyCallBack;
00032     SO<random_utils::ThreadSafeCanvas>  m_canvas;
00033     SO<SceneThing>          m_rootNode;
00034 
00035     SO<CarSimple>           m_car;
00036 };
00037 
00038 SO<SceneThing>          g_rootNode;
00039 
00040 VrsTest::VrsTest()
00041 {
00042     // initialize glut and create canvas for this application
00043     m_canvas = new random_utils::ThreadSafeCanvas(
00044         "VrsTest", 640, 480, GLCanvas::RGBDDS);
00045 
00046     // create scene graph root
00047     m_rootNode = new SceneThing();
00048     g_rootNode = m_rootNode;
00049 
00050     // attach some background color
00051     m_rootNode->append(new BackgroundGL(Color(0.2),
00052                        Color(0.9), BackgroundGL::TopToBottom));
00053 
00054     // attach some nice green color as default
00055     m_rootNode->append(new ShapeMaterialGL(
00056             Color(0.5, 1.0, 0.5), Color(0.15, 0.3, 0.15)));
00057 
00058     m_rootNode->append(new TransparencyTechniqueGL());
00059 
00060     PhysicsManager::get();
00061 
00062 //     PhysicsManager::get()->lock();
00063 
00064     // create a big box, which is the ground
00065     SO<SceneThing> boxGround = new SceneThing(m_rootNode);
00066     boxGround->append(new Box(Vector(-82,-80,-82), Vector(82,0,82)));
00067     SO<CollisionBody> boxGroundPhy = new CollisionBody(boxGround, CS_Box);
00068     boxGroundPhy->surface()->setBounce(1.0);
00069     boxGroundPhy->surface()->setBounceVelocity(0.0);
00070     boxGroundPhy->setDebugNode();
00071 
00072     // create walls
00073     SO<SceneThing> wall[4];
00074     SO<BoxShape> wallShape[4];
00075     SO<CollisionBody> wallPhy[4];
00076 
00077     wallShape[0] = new BoxShape(Bounds(Vector(80,0,80),Vector(81,30,-80)));
00078     wallShape[1] = new BoxShape(Bounds(Vector(-80,0,80),Vector(-81,30,-80)));
00079     wallShape[2] = new BoxShape(Bounds(Vector(80,0,80),Vector(-80,30,81)));
00080     wallShape[3] = new BoxShape(Bounds(Vector(80,0,-80),Vector(-80,30,-81)));
00081 
00082     for(int i = 0; i < 4; i++)
00083     {
00084         wall[i] = new SceneThing(m_rootNode);
00085         wallPhy[i] = new CollisionBody(wall[i], wallShape[i]);
00086         wallPhy[i]->setDebugNode();
00087     }
00088 
00089     // create a box,
00090     SO<SceneThing> box = new SceneThing(m_rootNode);
00091     SO<BoxShape> boxShape = new BoxShape(
00092             Bounds(Vector(-1,-20,-20), Vector(1,20,20)));
00093     SO<CollisionBody> boxPhy = new SampleBox(box, boxShape);
00094     boxPhy->setPosition(10,20.1,0);
00095     boxPhy->surface()->setBounce(1.0);
00096     boxPhy->surface()->setBounceVelocity(0.0);
00097     boxPhy->setDebugNode();
00098 
00099 //     for(int i = 0; i < 160; i+=40)
00100 //         for(int j = 0; j < 160; j+=40)
00101 //         {
00102 //             SO<SceneThing> ball = new SceneThing(m_rootNode);
00103 //             ball->append(new Sphere(1.0));
00104 //             SO<PhysicsBody> ballPhy = new PhysicsBody(ball, CS_Sphere);
00105 //             ballPhy->setPosition(-80 + i,30,-80 + j);
00106 //             ballPhy->surface()->setBounce(1.0);
00107 //             ballPhy->surface()->setBounceVelocity(0.0);
00108 //             ballPhy->setAngularVelocity(VRS::Vector(0.0,0.0,-5.0));
00109 //             ballPhy->setDebugNode();
00110 //             ballPhy->setCollisionPartnerCategories(2);
00111 //         }
00112 
00113     // create a crate
00114     SO<SceneThing> crate = new SceneThing(m_rootNode);
00115     crate->append(new Box(Vector(-4,-5,-40), Vector(4,0.1,40)));
00116     SO<CollisionBody> cratePhy = new CollisionBody(crate, CS_Box);
00117     cratePhy->setPosition(0, 0, -5);
00118     cratePhy->setRotation(0,0,1,18);
00119 
00120 //     // create a sample plane
00121 //     SO<SceneThing> plane = new SceneThing(m_rootNode);
00122 //     SO<PlaneShape> planeShape = new PlaneShape(Vector(0,-1,0),-50);
00123 //     SO<CollisionBody> planePhy = new SamplePlane(plane, planeShape);
00124 
00125     // create a car
00126     SO<SceneThing> car = new SceneThing(m_rootNode);
00127     m_car = new CarSimple(car);
00128 
00129     // collision groups
00130     // the ball does not collide with the plane
00131     boxPhy->setCollisionCategory(1);
00132 
00133 //     PhysicsManager::get()->unlock();
00134 
00135     // create a camera
00136     SO<SceneThing> view = new SceneThing();
00137     view->append(new Camera(
00138         new Perspective(72, 0.1, 200.0),
00139         new LookAt(Vector(-70,6,40), Vector(0,0,0))));
00140 
00141     // let there be light
00142     view->append(new DistantLight(Vector(1,1,1)));
00143     view->append(new PointLight(Vector(0,1,-2)));
00144 
00145     m_canvas->append(view);
00146     m_canvas->append(m_rootNode);
00147 
00148     // create and connect behaviors
00149     m_canvas->append(new TrackBall(m_rootNode));
00150 
00151     // add keyboard callback
00152     m_canvas->append(m_keyCallBack = new BehaviorCallback());
00153     m_keyCallBack->setCanvasCallback(
00154             new MethodCallback<VrsTest>(this, &VrsTest::keyEvent));
00155     m_keyCallBack->activate();
00156 
00157 #ifdef VRSODE_MULTITHREAD
00158     PhysicsManager::get()->setStepTime(0.005);
00159     PhysicsManager::get()->setStepSize(0.02);
00160 
00161     // PhysicsManager::get()->setPerformanceAutoAdjust(true);
00162     PhysicsManager::startThread();
00163 #endif
00164 }
00165 
00166 void dropBall(int p);
00167 
00168 void
00169 VrsTest::keyEvent()
00170 {
00171     KeyEvent* event = VRS_Cast(KeyEvent, m_keyCallBack->currentCanvasEvent());
00172 
00173 //     PhysicsManager::get()->lock();
00174 
00175     if(event != NULL && event->pressed ())
00176         switch(event->keyCode())
00177         {
00178 //             case Key::Escape: exit(0); break;
00179             case Key::Up: m_car->accelerate(1.0); break;
00180             case Key::Down: m_car->accelerate(-0.2); break;
00181             case Key::Left: m_car->steerLeft(1.0); break;
00182             case Key::Right: m_car->steerRight(1.0); break;
00183 //             case Key::End: m_car->nos(1.0); break;
00184             case Key::Escape: m_car->handBreak(true); break;
00185             case Key::End: dropBall(0); break;
00186         }
00187     if(event != NULL && event->released())
00188         switch(event->keyCode())
00189         {
00190             case Key::Up: m_car->accelerate(0.0); break;
00191             case Key::Down: m_car->accelerate(0.0); break;
00192             case Key::Left: m_car->steerLeft(0.0); break;
00193             case Key::Right: m_car->steerRight(0.0); break;
00194             case Key::Escape: m_car->handBreak(false); break;
00195         }
00196 
00197 //     PhysicsManager::get()->unlock();
00198 
00199     m_canvas->postForRedisplay();
00200 }
00201 
00202 #ifndef VRSODE_MULTITHREAD
00203 void
00204 singleThreadPhysicsUpdate(int p)
00205 {
00206     PhysicsManager::get()->update(0.05);
00207     glutTimerFunc(50, singleThreadPhysicsUpdate, 0);
00208 }
00209 #endif
00210 
00211 void
00212 dropBall(int p)
00213 {
00214     PhysicsManager::get()->lock();
00215     {
00216         SO<SceneThing> ball = new SceneThing(g_rootNode);
00217         ball->append(new Sphere(1.0));
00218         SO<PhysicsBody> ballPhy = new PhysicsBody(ball, CS_Box);
00219         ballPhy->setPosition(-40,30,-2);
00220         ballPhy->surface()->setBounce(1.0);
00221         ballPhy->surface()->setBounceVelocity(0.0);
00222         ballPhy->setAngularVelocity(VRS::Vector(0.0,0.0,-5.0));
00223         ballPhy->setDebugNode();
00224         // ballPhy->setCollisionPartnerCategories(2);
00225     }
00226     /*
00227     {
00228         SO<SceneThing> ball = new SceneThing(g_rootNode);
00229         ball->append(new Sphere(1.0));
00230         SO<PhysicsBody> ballPhy = new PhysicsBody(ball, CS_Sphere);
00231         ballPhy->setPosition(-40,30,2);
00232         ballPhy->surface()->setBounce(1.0);
00233         ballPhy->surface()->setBounceVelocity(0.0);
00234         ballPhy->setAngularVelocity(VRS::Vector(0.0,0.0,-5.0));
00235         ballPhy->setDebugNode();
00236         ballPhy->setCollisionPartnerCategories(2);
00237     }
00238     */
00239     PhysicsManager::get()->unlock();
00240 
00241     glutTimerFunc(1800, dropBall, 0);
00242 
00243 }
00244 
00245 
00246 // don't let SDL rename our main
00247 #ifdef __APPLE__
00248   #undef main
00249 #endif
00250 
00251 int
00252 main(int p_argc, char** p_argv)
00253 {
00254     glutInit(&p_argc, p_argv);
00255     SDL_Init(SDL_INIT_EVENTTHREAD | SDL_INIT_TIMER);
00256 
00257 
00258     SO<VrsTest> application = new VrsTest;
00259 
00260 #ifndef VRSODE_MULTITHREAD
00261     glutTimerFunc(20, singleThreadPhysicsUpdate, 0);
00262 #endif
00263 
00264     glutTimerFunc(1000, dropBall, 0);
00265     glutMainLoop();
00266 
00267     PhysicsManager::get()->stop();
00268     PhysicsManager::get()->release();
00269 
00270     SDL_Quit();
00271     return 0;
00272 }
00273 
00274 /* trash code
00275 
00276     m_rootNode->append(new ConstantDetail(32, 64));
00277 
00278     for(int j = -200; j < 200; j+= 60)
00279     {
00280         for(int i = -200; i < 200; i+= 60)
00281         {
00282             SO<SceneThing> boxHouse = new SceneThing(m_rootNode);
00283             boxHouse->append(new VRS::ConstantDetail(1, 1));
00284             boxHouse->append(new Box(Vector(i,-10,j), Vector(i+10,20,j+10)));
00285 
00286             SO<CollisionBody> boxHousePhy =
00287                     PhysicsManager::get()->createCollisionBody(boxHouse, CS_Box);
00288 
00289             boxHousePhy->setPosition(0,-1,0);
00290             boxHousePhy->setDebugNode();
00291         }
00292     }
00293 
00294     m_car = new Car3DS(car);
00295 
00296     // create an mesh from scratch
00297     PhysicsBody* phyPlane = new PhysicsBody(
00298         new PolygonSet(PolygonSet::Quads,
00299             makeIterator(
00300                 Vector(-1.0, 0.0,  1.0),
00301                 Vector( 1.0, 0.0,  1.0),
00302                 Vector( 1.0, 0.0, -1.0),
00303                 Vector(-1.0, 0.0, -1.0)),
00304             0, 0,
00305             makeIterator(
00306                 Color(0.1),
00307                 Color(0.1),
00308                 Color(0.9),
00309                 Color(0.9))));
00310 
00311     childNode->append(phyPlane->polygonSet());
00312 
00313     SO<Car3DS>              m_car;
00314 */
00315 

Generated on Fri May 11 21:01:58 2007 for Random Racer by  doxygen 1.5.1