home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / demos / audio / drive / Car.c++ < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  3.5 KB  |  164 lines

  1. /*
  2.  * Copyright 1992-1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. //////////////////////////////////////////////////////////////////////
  18. // Car.c++ - implementationtion of the car class
  19. //////////////////////////////////////////////////////////////////////
  20.  
  21. #include <Inventor/Sb.h>
  22. #include <Inventor/nodes/SoDrawStyle.h>
  23. #include <Inventor/nodes/SoSeparator.h>
  24. #include <Inventor/actions/SoGLRenderAction.h>
  25. #include "Car.h"
  26. #include "Engine.h"
  27. #include "Dynamics.h"
  28. #include "Driver.h"
  29. #include "Road.h"
  30. #include "Stretch.h"
  31. #include "Render.h"
  32. extern "C" {
  33. #include "shapes.h"
  34. }
  35. #include "so.h"
  36.  
  37.  
  38. // Pointer to the driver that is on this workstaion
  39. Driver * Car::_local_driver = NULL;
  40.  
  41.  
  42. Car::Car(Simulation *sim, SbString carfile, Road *r, int type)
  43. {
  44.     // set up the car as a Inventor graph
  45.     _car_root = new SoSeparator;
  46.     _car_root->ref();
  47.  
  48.     _style = new SoDrawStyle;
  49.     _style->style = SoDrawStyle::FILLED;
  50.  
  51.     // read in the car database
  52.     _car_body = readSceneGraph(carfile.getString());
  53.  
  54.     _car_root->addChild(_style);
  55.     _car_root->addChild(_car_body);
  56.  
  57.     // simulation that this car is in
  58.     _simulation = sim;
  59.     
  60.     // physical information about the car
  61.     // XXX extract from car custom node
  62.     _wheelbase = 35.0/2.0;
  63.     _width = 15.0/2.0;
  64.     _height = 11.0/2.0;
  65.  
  66.     // driver position relative to center of car
  67.     _driver_position.setValue(-1.5,4.0,0.0);
  68.  
  69.     // road that this car is on
  70.     _road = r;
  71.  
  72.     _type = type;
  73.     
  74.     /// components of the car
  75.  
  76.     // only local cars have a driver
  77.     if (_type == LOCAL_CAR)
  78.         // keep driver construction first    
  79.         _local_driver = _driver = new Driver(this); 
  80.     else
  81.         _driver = NULL;
  82.         
  83.     _engine = new Engine(this);
  84.     _dynamics = new Dynamics(this);
  85.  
  86.     _render_action = new SoGLRenderAction(
  87.         SbVec2s(10,10), // dummy for now
  88.         TRUE); // XXX do inherit GL state
  89.  
  90.     if (_type == LOCAL_CAR)
  91.     {
  92.         _material = RED_METAL;
  93.         _color = 0xFF;
  94.     }
  95.     else if (_type == ROBOT_CAR)
  96.     {
  97.         _material = GREY_METAL;
  98.         _color = 0x7f7f7f;
  99.     }
  100.     // XXX else if (_type == NETWORK_CAR)
  101.     //    _color = retrieve from network
  102. }
  103.  
  104.  
  105. Car::~Car()
  106. {
  107.     if (_driver) delete _driver;
  108.     delete _engine;
  109.     delete _dynamics;
  110.     delete _render_action;
  111. }
  112.  
  113.  
  114. void Car::update(Boolean ok_to_sleep)
  115. {
  116.     switch (_type)
  117.     {
  118.         case LOCAL_CAR:
  119.             _driver->update(ok_to_sleep);
  120.             break;
  121.         case ROBOT_CAR:
  122.             _dynamics->update();
  123.             break;
  124.     }
  125. }
  126.  
  127.  
  128. void Car::setDrawStyle(int mode)
  129. {
  130.     if (mode == RENDER_FILLED)
  131.         _style->style = SoDrawStyle::FILLED;
  132.     else
  133.         _style->style = SoDrawStyle::LINES;
  134. }
  135.  
  136.  
  137. void Car::draw(int mode)
  138. {
  139.     Boolean backface_state;
  140.     
  141.     if (mode == RENDER_FILLED)
  142.     {
  143.         backface_state = getbackface();
  144.         static Boolean first_time = TRUE;
  145.         
  146.         backface(TRUE);
  147.         lmbind(MATERIAL, _material);
  148.     }
  149.     else
  150.         cpack(_color);
  151.  
  152.     if (_local_driver) {        
  153.         _render_action->setWindowSize(_local_driver->getViewWindowSize());
  154.         _render_action->apply(_car_root);
  155.     }
  156.  
  157.     if (mode == RENDER_FILLED)
  158.     {
  159.         backface(backface_state);
  160.         lmbind(MATERIAL, 0);
  161.     }
  162. }
  163.  
  164.