Back to index

supertuxkart  0.5+dfsg1
scene.cpp
Go to the documentation of this file.
00001 //
00002 //  SuperTuxKart - a fun racing game with go-kart
00003 //  Copyright (C) 2004 SuperTuxKart-Team
00004 //
00005 //  This program is free software; you can redistribute it and/or
00006 //  modify it under the terms of the GNU General Public License
00007 //  as published by the Free Software Foundation; either version 2
00008 //  of the License, or (at your option) any later version.
00009 //
00010 //  This program is distributed in the hope that it will be useful,
00011 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 //  GNU General Public License for more details.
00014 //
00015 //  You should have received a copy of the GNU General Public License
00016 //  along with this program; if not, write to the Free Software
00017 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00018 
00019 #include <plib/ssg.h>
00020 #include "material_manager.hpp"
00021 #include "material.hpp"
00022 #include "camera.hpp"
00023 #include "track.hpp"
00024 #include "world.hpp"
00025 #include "user_config.hpp"
00026 
00027 #include "btBulletDynamicsCommon.h"
00028 #ifdef __APPLE__
00029 #  include <GLUT/glut.h>
00030 #else
00031 #  include <GL/glut.h>
00032 #endif
00033 
00034 #include "scene.hpp"
00035 
00036 Scene *scene = 0;
00037 
00038 Scene::Scene() :
00039 m_scenegraph(new ssgRoot)
00040 {
00041 }
00042 
00043 //-----------------------------------------------------------------------------
00044 Scene::~Scene ()
00045 {
00046     delete m_scenegraph;
00047 }
00048 
00049 //-----------------------------------------------------------------------------
00050 void Scene::clear ()
00051 {
00052     if(m_scenegraph != 0)
00053     {
00054         m_scenegraph->removeAllKids();
00055     }
00056 
00057     for (Cameras::iterator i = m_cameras.begin(); i != m_cameras.end(); ++i)
00058         delete *i;
00059 
00060     m_cameras.clear();
00061 }
00062 
00063 //-----------------------------------------------------------------------------
00064 
00065 Camera *Scene::createCamera(int playerId, const Kart* kart)
00066 {
00067   Camera *cam = new Camera(playerId, kart);
00068 
00069   m_cameras.push_back(cam);
00070 
00071   return cam;
00072 }
00073 
00074 //-----------------------------------------------------------------------------
00075 void Scene::add(ssgEntity *kid)
00076 {
00077     m_scenegraph->addKid( kid );
00078 }
00079 
00080 //-----------------------------------------------------------------------------
00081 void Scene::remove(ssgEntity *kid)
00082 {
00083     m_scenegraph->removeKid( kid );
00084 }
00085 
00086 //-----------------------------------------------------------------------------
00087 void Scene::reset()
00088 {
00089     for (Cameras::iterator i = m_cameras.begin(); i != m_cameras.end(); ++i)
00090     {
00091         (*i)->reset();
00092     }
00093 }   // reset
00094 //-----------------------------------------------------------------------------
00095 void Scene::draw(float dt)
00096 {
00097     glEnable ( GL_DEPTH_TEST ) ;
00098 
00099     const Track* TRACK = world->m_track;
00100 
00101     ssgGetLight ( 0 ) -> setPosition ( TRACK->getSunPos() ) ;
00102     ssgGetLight ( 0 ) -> setColour ( GL_AMBIENT , TRACK->getAmbientCol()  ) ;
00103     ssgGetLight ( 0 ) -> setColour ( GL_DIFFUSE , TRACK->getDiffuseCol() ) ;
00104     ssgGetLight ( 0 ) -> setColour ( GL_SPECULAR, TRACK->getSpecularCol() ) ;
00105 
00106     if (TRACK->useFog())
00107     {
00108         glEnable ( GL_FOG ) ;
00109 
00110         glFogf ( GL_FOG_DENSITY, TRACK->getFogDensity() ) ;
00111         glFogfv( GL_FOG_COLOR  , TRACK->getFogColor() ) ;
00112         glFogf ( GL_FOG_START  , TRACK->getFogStart() ) ;
00113         glFogf ( GL_FOG_END    , TRACK->getFogEnd() ) ;
00114         glFogi ( GL_FOG_MODE   , GL_EXP2   ) ;
00115         glHint ( GL_FOG_HINT   , GL_NICEST ) ;
00116 
00117         /* Clear the screen */
00118         glClearColor (TRACK->getFogColor()[0],
00119                       TRACK->getFogColor()[1],
00120                       TRACK->getFogColor()[2],
00121                       TRACK->getFogColor()[3]);
00122     }
00123     else
00124     {
00125         /* Clear the screen */
00126         glClearColor (TRACK->getSkyColor()[0],
00127                       TRACK->getSkyColor()[1],
00128                       TRACK->getSkyColor()[2],
00129                       TRACK->getSkyColor()[3]);
00130     }
00131 
00132     glClear      ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ) ;
00133 
00134     for (Cameras::iterator i = m_cameras.begin(); i != m_cameras.end(); ++i)
00135     {
00136         (*i)->update(dt);
00137         (*i)->apply ();
00138 
00139         if(!user_config->m_bullet_debug)
00140         {
00141             // Use this for faster profiling by disabling drawing the scene graph
00142             //if(!user_config->m_profile)ssgCullAndDraw ( m_scenegraph );
00143             ssgCullAndDraw ( m_scenegraph );
00144         }
00145         else
00146         {
00147             // Use bullets debug drawer
00148             GLfloat light_ambient[] = { 0.0, 0.0, 0.0, 1.0 };
00149             GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
00150             GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 };
00151             /*       light_position is NOT default value       */
00152             GLfloat light_position0[] = { 1.0, 1.0, 1.0, 0.0 };
00153             GLfloat light_position1[] = { -1.0, -1.0, -1.0, 0.0 };
00154             
00155             glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
00156             glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
00157             glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
00158             glLightfv(GL_LIGHT0, GL_POSITION, light_position0);
00159             
00160             glLightfv(GL_LIGHT1, GL_AMBIENT, light_ambient);
00161             glLightfv(GL_LIGHT1, GL_DIFFUSE, light_diffuse);
00162             glLightfv(GL_LIGHT1, GL_SPECULAR, light_specular);
00163             glLightfv(GL_LIGHT1, GL_POSITION, light_position1);
00164             
00165             glEnable(GL_LIGHTING);
00166             glEnable(GL_LIGHT0);
00167             glEnable(GL_LIGHT1);
00168             
00169             glShadeModel(GL_SMOOTH);
00170             glEnable(GL_DEPTH_TEST);
00171             glDepthFunc(GL_LESS);
00172             
00173             glClearColor(0.8,0.8,0.8,0);
00174             
00175             glMatrixMode(GL_PROJECTION);
00176             glLoadIdentity();
00177             float f=2.0f;
00178             glFrustum(-f, f, -f, f, 1.0, 1000.0);
00179             
00180             btVector3 pos;
00181             sgCoord *c = world->getKart(race_manager->getNumKarts()-1)->getCoord();
00182             gluLookAt(c->xyz[0], c->xyz[1]-5.f, c->xyz[2]+4,
00183                       c->xyz[0], c->xyz[1],     c->xyz[2],
00184                       0.0f, 0.0f, 1.0f);
00185             glMatrixMode(GL_MODELVIEW);
00186             
00187             for (World::Karts::size_type i = 0 ; i < race_manager->getNumKarts(); ++i)
00188             {
00189                 Kart *kart=world->getKart((int)i);
00190                 if(!kart->isEliminated()) kart->draw();
00191             }
00192             world->getPhysics()->draw();
00193         }   //  bullet_debug
00194     }   // for cameras
00195 
00196     if (TRACK->useFog())
00197     {
00198         glDisable ( GL_FOG ) ;
00199     }
00200 
00201     glViewport ( 0, 0, user_config->m_width, user_config->m_height ) ;
00202 }
00203