Back to index

extremetuxracer  0.5beta
fog.cpp
Go to the documentation of this file.
00001 /* 
00002  * PPRacer 
00003  * Copyright (C) 2004-2005 Volker Stroebel <volker@planetpenguin.de>
00004  *
00005  * Copyright (C) 1999-2001 Jasmin F. Patry
00006  * 
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  * 
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  * 
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00020  */
00021 
00022 #include "fog.h"
00023 #include "gl_util.h"
00024 #include "tcl_util.h"
00025 
00026 #include "game_config.h"
00027 
00028 
00029 FogPlane fogPlane;
00030 
00031 void 
00032 FogPlane::reset()
00033 {
00034     m_isOn = true;
00035     m_mode = GL_LINEAR;
00036     m_density = 0.005;
00037     m_start = 0.0;
00038     m_end = getparam_forward_clip_distance();
00039     init_glfloat_array( 4, m_color, 1.0, 1.0, 1.0, 1.0 );
00040 }
00041 
00042 void 
00043 FogPlane::setup()
00044 {
00045     if ( !m_isOn || getparam_disable_fog() ) {
00046               glDisable( GL_FOG );
00047               return;       
00048     }
00049 
00050     glEnable( GL_FOG );
00051 
00052     glFogi( GL_FOG_MODE, m_mode );
00053     glFogf( GL_FOG_DENSITY, m_density );
00054     glFogf( GL_FOG_START, m_start );
00055     glFogf( GL_FOG_END, m_end );
00056     glFogfv( GL_FOG_COLOR, m_color );
00057 
00058     if ( getparam_nice_fog() ) {
00059               glHint( GL_FOG_HINT, GL_NICEST );
00060     } else {
00061               glHint( GL_FOG_HINT, GL_FASTEST );
00062     }
00063        
00064 }
00065 
00066 static int fog_cb (ClientData cd, Tcl_Interp *ip, 
00067                  int argc, CONST84 char *argv[]) 
00068 {
00069     double tmp_arr[4];
00070     double tmp_dbl;
00071     bool error = false;
00072     
00073     if (argc < 2) {
00074               error = true;
00075     }
00076 
00077     NEXT_ARG;
00078 
00079     while ( !error && argc > 0 ) {
00080 
00081        if ( strcmp( "-on", *argv ) == 0 ) {
00082            fogPlane.setEnabled();
00083        } else if ( strcmp( "-off", *argv ) == 0 ) {
00084            fogPlane.setEnabled(false);
00085        } else if ( strcmp( "-mode", *argv ) == 0 ) {
00086            NEXT_ARG;
00087            if ( argc == 0 ) {
00088                      error = true;
00089                      break;
00090            }
00091 
00092            if ( strcmp( "exp", *argv ) == 0 ) {
00093                      fogPlane.setMode(GL_EXP);
00094            } else if ( strcmp( "exp2", *argv ) == 0 ) {
00095                      fogPlane.setMode(GL_EXP2);
00096            } else if ( strcmp( "linear", *argv ) == 0 ) {
00097                      fogPlane.setMode(GL_LINEAR);
00098            } else {
00099                      print_warning( TCL_WARNING, "tux_fog: mode must be one of "
00100                             "`exp', `exp2', or `linear'" );
00101                      error = true;
00102            }
00103        } else if ( strcmp( "-density", *argv ) == 0 ) {
00104            NEXT_ARG;
00105            if ( argc == 0 ) {
00106                      error = true;
00107                      break;
00108            }
00109               if ( Tcl_GetDouble ( ip, *argv, &tmp_dbl ) == TCL_ERROR ) {
00110                      error = true;
00111                      break;
00112            }
00113            fogPlane.setDensity(tmp_dbl);
00114        } else if ( strcmp( "-start", *argv ) == 0 ) {
00115            NEXT_ARG;
00116            if ( argc == 0 ) {
00117                      error = true;
00118                      break;
00119            }
00120            if ( Tcl_GetDouble ( ip, *argv, &tmp_dbl ) == TCL_ERROR ) {
00121                      error = true;
00122                      break;
00123            }
00124            fogPlane.setStart(tmp_dbl);
00125        } else if ( strcmp( "-end", *argv ) == 0 ) {
00126            NEXT_ARG;
00127            if ( argc == 0 ) {
00128                      error = true;
00129                      break;
00130            }
00131            if ( Tcl_GetDouble ( ip, *argv, &tmp_dbl ) == TCL_ERROR ) {
00132                      error = true;
00133                      break;
00134            }
00135            fogPlane.setEnd(tmp_dbl);
00136        } else if ( strcmp( "-color", *argv ) == 0 ) 
00137        {
00138            NEXT_ARG;
00139            if ( argc == 0 ) {
00140                      error = true;
00141                      break;
00142            }
00143            if ( get_tcl_tuple ( ip, *argv, tmp_arr, 4 ) == TCL_ERROR ) {
00144                      error = true;
00145                      break;
00146            }
00147            copy_to_glfloat_array( fogPlane.getColor(), tmp_arr, 4 );
00148        } else {
00149            print_warning( TCL_WARNING, "tux_fog: unrecognized "
00150                         "parameter `%s'", *argv );
00151        }
00152 
00153        NEXT_ARG;
00154     }
00155 
00156     if ( error ) {
00157        print_warning( TCL_WARNING, "error in call to tux_fog" );
00158        Tcl_AppendResult(
00159            ip, 
00160            "\nUsage: tux_fog [-on|-off] "
00161            "[-mode [exp|exp2|linear]] "
00162            "[-density <value>] "
00163            "[-start <value>] "
00164            "[-end <value>] "
00165            "[-color { r g b a }] ",
00166            (char *) 0 );
00167        return TCL_ERROR;
00168     }
00169     
00170     return TCL_OK;
00171 }
00172 
00173 void
00174 FogPlane::registerCallbacks( Tcl_Interp *ip )
00175 {
00176     Tcl_CreateCommand (ip, "tux_fog", fog_cb,  0,0);
00177 }