Back to index

supertuxkart  0.5+dfsg1
track.hpp
Go to the documentation of this file.
00001 //  $Id: track.hpp 2111 2008-05-31 07:04:30Z cosmosninja $
00002 //
00003 //  SuperTuxKart - a fun racing game with go-kart
00004 //  Copyright (C) 2004 Steve Baker <sjbaker1@airmail.net>
00005 //
00006 //  This program is free software; you can redistribute it and/or
00007 //  modify it under the terms of the GNU General Public License
00008 //  as published by the Free Software Foundation; either version 2
00009 //  of the License, or (at your option) any later version.
00010 //
00011 //  This program is distributed in the hope that it will be useful,
00012 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //  GNU General Public License for more details.
00015 //
00016 //  You should have received a copy of the GNU General Public License
00017 //  along with this program; if not, write to the Free Software
00018 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00019 
00020 #ifndef HEADER_TRACK_H
00021 #define HEADER_TRACK_H
00022 
00023 #ifdef __APPLE__
00024 #  include <OpenGL/gl.h>
00025 #else
00026 #  ifdef WIN32
00027 #    define WIN32_LEAN_AND_MEAN
00028 #    include <windows.h>
00029 #  endif
00030 #  include <GL/gl.h>
00031 #endif
00032 #include <plib/sg.h>
00033 #include <plib/ssg.h>
00034 #include <string>
00035 #include <vector>
00036 #include "btBulletDynamicsCommon.h"
00037 #include "material.hpp"
00038 #include "triangle_mesh.hpp"
00039 #include "music_information.hpp"
00040 
00041 class Track
00042 {
00043 private:
00044     float                    m_gravity;
00045     std::string              m_ident;
00046     std::string              m_screenshot;
00047     std::string              m_top_view;
00048     std::vector<MusicInformation*> m_music;
00049     std::vector<float>       m_start_x, m_start_y, m_start_z, m_start_heading;
00050     std::string              m_herring_style;
00051     std::string              m_description;
00052     std::string              m_designer;
00053     std::string              m_filename;
00054     ssgBranch*               m_model;
00055     TriangleMesh*            m_track_mesh;
00056     TriangleMesh*            m_non_collision_mesh;
00057     // The next two variables are for AI improvements: the AI sometimes does
00058     // not estimate curve speed and/or angle correctly, resulting in too much
00059     // braking. These factors are used to adjust this.
00060     float                    m_AI_angle_adjustment;
00061     float                    m_AI_curve_speed_adjustment;
00062 public:
00063     enum RoadSide{ RS_DONT_KNOW = -1, RS_LEFT = 0, RS_RIGHT = 1 };
00064 
00065     //An enum is not used for the QUAD_TRI_* constants because of limitations
00066     //of the conversion between enums and ints.
00067     static const int QUAD_TRI_NONE;
00068     static const int QUAD_TRI_FIRST;
00069     static const int QUAD_TRI_SECOND;
00070 
00071     static const int UNKNOWN_SECTOR;
00072 
00073     struct SegmentTriangle
00074     {
00075         int segment;
00076         int triangle;
00077 
00078         SegmentTriangle
00079         (
00080             int _segment,
00081             int _triangle
00082         ) : segment(_segment), triangle(_triangle) {};
00083     };
00084 
00085     std::string m_name;
00086     sgVec4      m_sky_color;
00087     bool        m_use_fog;
00088     sgVec4      m_fog_color;
00089     float       m_fog_density;
00090     float       m_fog_start;
00091     float       m_fog_end;
00092     sgVec3      m_sun_position;   
00093     sgVec4      m_ambient_col;
00094     sgVec4      m_specular_col;
00095     sgVec4      m_diffuse_col;
00096 
00100     class sgVec3Wrapper
00101     {
00102     private:
00103         sgVec3 vec;
00104 
00105     public:
00106         sgVec3Wrapper(const sgVec3& o)
00107         {
00108             sgCopyVec3(vec, o);
00109         }
00110 
00111         operator const float* () const
00112         {
00113             return vec;
00114         }
00115 
00116         operator float* ()
00117         {
00118             return vec;
00119         }
00120     };
00121     //FIXME: Maybe the next 4 vectors should be inside an struct and be used
00122     //from a vector of structs?
00123     //FIXME: should the driveline be set as a sgVec2?
00124     std::vector<sgVec3Wrapper> m_driveline;
00125     std::vector<SGfloat> m_distance_from_start;
00126     std::vector<SGfloat> m_path_width;
00127     std::vector<SGfloat> m_angle;
00128 
00129        //Left and Right drivelines for overhead map rendering.
00130        //(Should probably be private as they are only use internally right now)
00131     std::vector<sgVec3Wrapper> m_left_driveline;
00132     std::vector<sgVec3Wrapper> m_right_driveline;
00133 
00134     sgVec2 m_driveline_min;
00135     sgVec2 m_driveline_max;
00136 
00137 
00138     float m_total_distance;
00139     static const float NOHIT;
00140 
00141     float m_track_2d_width,  // Width and heigth of the 2d display of the track
00142           m_track_2d_height;
00143     float m_scale_x,        // Scaling to fit track into the size determined by
00144           m_scale_y;        // track2dWidth/Heightheigth
00145     bool m_do_stretch;      // 2d track display might be stretched to fit better
00146 
00147                        Track             (std::string filename,float w=100,
00148                                           float h=100, bool stretch=1);
00149                       ~Track             ();
00150     void               cleanup           ();
00151     void               addDebugToScene   (int type                    ) const;
00152     void               draw2Dview        (float x_offset,
00153                                           float y_offset              ) const;
00154     void               drawScaled2D      (float x, float y, float w,
00155                                           float h                     ) const;
00156 
00157     void               findRoadSector    (const sgVec3 XYZ, int *sector) const;
00158     int                findOutOfRoadSector(const sgVec3 XYZ,
00159                                            const RoadSide SIDE,
00160                                            const int CURR_SECTOR
00161                                            ) const;
00162     int                spatialToTrack    (sgVec3 dst,
00163                                           const sgVec2 POS,
00164                                           const int SECTOR            ) const;
00165     void               trackToSpatial    (sgVec3 xyz, const int SECTOR) const;
00166     void               loadTrackModel    ();
00167     bool               isShortcut        (const int OLDSEC, const int NEWSEC) const;
00168     void               addMusic          (MusicInformation* mi)
00169                                                   {m_music.push_back(mi);       }
00170     ssgBranch*         getModel          () const {return m_model;              }
00171     float              getGravity        () const {return m_gravity;            }
00172     float              getTrackLength    () const {return m_total_distance;     }
00173     const std::string& getIdent          () const {return m_ident;              }
00174     const char*        getName           () const {return m_name.c_str();       }
00175     void               startMusic        () const;
00176     const std::string& getFilename       () const {return m_filename;           }
00177     const sgVec3& getSunPos              () const {return m_sun_position;       }
00178     const sgVec4& getAmbientCol          () const {return m_ambient_col;        }
00179     const sgVec4& getDiffuseCol          () const {return m_diffuse_col;        }
00180     const sgVec4& getSpecularCol         () const {return m_specular_col;       }
00181     const bool&   useFog                 () const {return m_use_fog;            }
00182     const sgVec4& getFogColor            () const {return m_fog_color;          }
00183     const float&  getFogDensity          () const {return m_fog_density;        }
00184     const float&  getFogStart            () const {return m_fog_start;          }
00185     const float&  getFogEnd              () const {return m_fog_end;            }
00186     const float&  getAIAngleAdjustment   () const {return m_AI_angle_adjustment;}
00187     const float&  getAICurveSpeedAdjustment() const {return m_AI_curve_speed_adjustment;}
00188     const sgVec4& getSkyColor            () const {return m_sky_color;          }
00189     const std::string& getDescription    () const {return m_description;        }
00190     const std::string& getDesigner       () const {return m_designer;           }
00191     const std::string& getTopviewFile    () const {return m_top_view;           }
00192     const std::string& getScreenshotFile () const {return m_screenshot;         }
00193     const std::vector<SGfloat>& getWidth () const {return m_path_width;         }
00194     const std::string& getHerringStyle   () const {return m_herring_style;      }
00195     void               getStartCoords    (unsigned int pos, sgCoord* coords) const;
00196     void  getTerrainInfo(const btVector3 &pos, float *hot, btVector3* normal, 
00197                          const Material **material) const;
00198     void createPhysicsModel              ();
00199     void               glVtx             (sgVec2 v, float x_offset, float y_offset) const
00200     {
00201         glVertex2f(
00202             x_offset+(v[0]-m_driveline_min[0])*m_scale_x,
00203             y_offset+(v[1]-m_driveline_min[1])*m_scale_y);
00204     }
00205 
00206 private:
00207     void  loadTrack                      (std::string filename);
00208     void  herring_command                (sgVec3 *xyz, char htype, int bNeedHeight);
00209     void  loadDriveline                  ();
00210     void  readDrivelineFromFile          (std::vector<sgVec3Wrapper>& line,
00211                                          const std::string& file_ext      );
00212     void  convertTrackToBullet           (ssgEntity *track, sgMat4 m);
00213 
00214     float pointSideToLine(const sgVec2 L1, const sgVec2 L2,
00215                           const sgVec2 P ) const;
00216     int   pointInQuad(const sgVec2 A, const sgVec2 B,
00217                       const sgVec2 C, const sgVec2 D, const sgVec2 POINT ) const;
00218     void  getMusicInformation(std::vector<std::string>&             filenames, 
00219                               std::vector<MusicInformation*>& m_music   );
00220 }
00221 ;   // class Track
00222 
00223 #endif