home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / demos / audio / drive / Stretch.h < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  6.5 KB  |  221 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. // Stretch.h - definition of the stretch abstract class
  19. //
  20. // A road is a doubly-linked list of stretches. Each stretch
  21. //
  22. //
  23. //
  24. //
  25. //
  26. //
  27. //
  28. //
  29. //
  30. //
  31. //////////////////////////////////////////////////////////////////////
  32.  
  33. #ifndef STRETCH_H
  34. #define STRETCH_H
  35.  
  36. #include "Defines.h"
  37. #include <Inventor/SbPList.h>
  38.  
  39. class Car;
  40.  
  41. // stretch types
  42. #define STRAIGHT    1
  43. #define CURVE        2
  44. #define LOOP        3
  45. #define CONNECT        4
  46.  
  47. // positions across a stretch
  48. // XXX ordered such because Dynamics do not use middle for flag representation
  49. #define LEFT 0
  50. #define RIGHT 1
  51. #define MIDDLE 2
  52.  
  53. // stripe size relative to road
  54. const float STRIPE_WIDTH = .02;
  55. const float STRIPE_HEIGHT = .05;
  56.  
  57. // terrain = terrain_width * .5*road_width
  58. #define TERRAIN_WIDTH 10.0
  59.  
  60. // offset from side of road for trees & poles
  61. #define SCENERY_OFFSET 5.0;
  62.  
  63. typedef struct {
  64.     SbVec3f normal;
  65.     SbVec3f left;
  66.     SbVec3f middle;
  67.     SbVec3f right;
  68. } StretchData;
  69.  
  70.  
  71. class Stretch {
  72.  
  73. public:
  74.  
  75.     Stretch(Stretch *prev, Stretch *next);
  76.     
  77.     virtual ~Stretch();
  78.  
  79.     // draws all "visible" stretches, starting with this one
  80.     // at the given marker.
  81.     // display_mode is RENDER_FILLED or RENDER_WIREFRAME
  82.     // ignore_car is a car _not_ to draw, usually the local car
  83.     // when viewing from inside the car
  84.     void draw(int start_marker, int display_mode, Car *ignore_car) const;
  85.  
  86.     // call this before calling draw
  87.     void init_draw() const { _drawn = 0; };
  88.  
  89.     const Stretch * get_next() const { return _next; };
  90.     const Stretch * get_prev() const { return _prev; };
  91.     
  92.     float get_turn_angle() { return _turn_angle; };
  93.     float get_hill_angle() { return _hill_angle; };
  94.     float get_step() const { return _step; };
  95.     float get_length() const { return _length; };
  96.     float get_width() const { return _width; };
  97.  
  98.     const SbVec3f get_left(int i) const { return _data[i].left; };
  99.     const SbVec3f get_middle(int i) const { return _data[i].middle; };
  100.     const SbVec3f get_right(int i) const { return _data[i].right; };
  101.     const SbVec3f get_normal(int i) const { return _data[i].normal; };
  102.     int get_count() const { return _num_markers; };
  103.  
  104.     const SbVec3f get_left_median(int i) const 
  105.         { return _median_data[i].left; };
  106.     const SbVec3f get_right_median(int i) const 
  107.         { return _median_data[i].right; };
  108.     const StretchData get_left_terrain(int i) const 
  109.         { return _left_terrain_data[i]; };
  110.     const StretchData get_right_terrain(int i) const 
  111.         { return _right_terrain_data[i]; };
  112.  
  113.     const Stretch * get_next_stretch(int marker, int &next_marker) const;
  114.     const Stretch * get_prev_stretch(int marker, int &prev_marker) const;
  115.     const SbVec3f get_direction(int marker, int side) const;
  116.     const SbVec3f get_side_direction(int marker, int side) const;
  117.  
  118.     // for this stretch, at current marker, returns the marker offset
  119.     // for position in the range of 0.0-1.0. If the position is on
  120.     // the next marker, returns TRUE; returns FALSE if on this marker.
  121.     // 0.0 means at the beginning of the segment, 1.0 at the end.
  122.     // XXX had to do *offset instead of &offset. Was getting NaN's.
  123.     virtual Boolean get_marker_offset(
  124.         int marker, const SbVec3f position, float *offset);
  125.         
  126.     // Returns the middle of the LEFT or RIGHT side of the
  127.     // road at the given marker
  128.     SbVec3f road_pos(int side, int marker) const;
  129.  
  130.     // Add or remove a car to the list of cars on this stretch
  131.     void add_car(const Car *car) {
  132.         _car_list->append((void *)car);
  133.     };
  134.     void remove_car(const Car *car) {
  135.         int i = _car_list->find((void *)car);
  136.         if (i == -1)
  137.             fprintf(stderr,"Attempt to remove non-existant car %x\n",car);
  138.         else
  139.             _car_list->remove(i);
  140.     };
  141.  
  142.     // XXX Do not panic. These are only temporary until the creation
  143.     // of a genuine Scenery class.
  144.     const SbVec3f get_pole_pos() const {return _pole_pos;};
  145.     const SbVec3f get_tree1_pos() const {return _tree1_pos;};
  146.     const SbVec3f get_tree2_pos() const {return _tree2_pos;};
  147.  
  148. protected:
  149.  
  150.     /// protected data members
  151.     
  152.     Stretch * _prev;
  153.     Stretch * _next;
  154.  
  155.     int _type;    // type of the stretch
  156.  
  157.     // all these values are in world units, i.e. feet or meters
  158.     float _width;
  159.     float _length;
  160.     float _step;        // polygon step, in world units
  161.     
  162.     float _turn_angle;    // radians, positive curves left
  163.     float _turn_radius;
  164.     
  165.     float _hill_angle;    // radians, positive curves up
  166.     float _hill_radius;
  167.     
  168.     float _bank_angle;    // radians, positive tilts to left, start to end
  169.  
  170.     // offset from origin to end of road;
  171.     SbVec3f _offset;
  172.  
  173.     // change in pitch/roll from origin to end of road
  174.     SbRotation _orientation;
  175.     
  176.     static int _drawn; // depth in the rendering recursion
  177.     static int _ahead; // how many stretches to draw ahead of current
  178.  
  179.     StretchData * _data;
  180.     StretchData * _median_data;
  181.     StretchData * _left_terrain_data;
  182.     StretchData * _right_terrain_data;
  183.             
  184.     int _num_markers; // number of vertices down one edge of stretch
  185.     int _num_stripes; // number of median stripes
  186.  
  187.     // The stretch is in charge of drawing cars that are on it.
  188.     // This is a list of cars on this stretch.
  189.     SbPList * _car_list;
  190.  
  191.     // XXX scenery hacks until we get a real scenery class
  192.     SbVec3f _pole_pos;
  193.     SbVec3f _tree1_pos;
  194.     SbVec3f _tree2_pos;
  195.  
  196.     // protected member functions
  197.     
  198.     void set_next(Stretch *s) { _next = s; };
  199.     void set_prev(Stretch *s) { _prev = s; };
  200.  
  201.     void init_scenery();
  202.     void draw_scenery(int display_mode) const;
  203.     void draw_terrain(int start_marker, int display_mode) const;
  204.     void draw_asphalt(int start_marker, int display_mode) const;
  205.     void draw_stripes(int start_marker, int display_mode) const;
  206.     void draw_cars(Car *ignore_car, int display_mode) const;
  207.         // draws cars on this stretch
  208.  
  209.     const SbVec3f get_offset() const { return _offset; };
  210.     const SbRotation get_orientation() const { return _orientation; };
  211.     void rotate_bank(int i, float bank);
  212.     void compute_step();
  213.     void allocate_data();
  214.     void find_previous(SbVec3f &prev_offset, SbRotation &prev_orientation);
  215.     void match_ends();
  216.     void transform_data(SbVec3f prev_offset, SbRotation prev_orientation);
  217. };
  218.  
  219. #endif
  220.     
  221.