Back to index

extremetuxracer  0.5beta
tux_shadow.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 "tux_shadow.h"
00023 #include "gl_util.h"
00024 #include "model_hndl.h"
00025 #include "hier.h"
00026 #include "phys_sim.h"
00027 #include "textures.h"
00028 
00029 #include "game_config.h"
00030 
00031 #define SHADOW_HEIGHT 0.1
00032 
00033 void draw_tux_shadow()
00034 {
00035     if ( ! getparam_draw_tux_shadow() ) 
00036        return;
00037        
00038        pp::Matrix model_matrix;
00039     char *tux_root_node_name;
00040     scene_node_t *tux_root_node;
00041               
00042     set_gl_options( TUX_SHADOW ); 
00043        
00044        /* 
00045        * Make the shadow darker if the stencil buffer is active 
00046        */
00047        
00048        if(getparam_stencil_buffer()){
00049        glColor4f( 0.0,0.0,0.0,0.3 );
00050        }else{
00051        glColor4f( 0.0,0.0,0.0,0.1 );
00052        }
00053        
00054     model_matrix.makeIdentity();
00055 
00056     tux_root_node_name = ModelHndl->get_tux_root_node();
00057 
00058     if ( get_scene_node( tux_root_node_name, &tux_root_node ) != TCL_OK ) {
00059        check_assertion( 0, "couldn't find tux's root node" );
00060     } 
00061 
00062     traverse_dag_for_shadow( tux_root_node, model_matrix );
00063 }
00064 
00065 void traverse_dag_for_shadow( scene_node_t *node, pp::Matrix model_matrix )
00066 {
00067     pp::Matrix new_model_matrix;
00068     scene_node_t *child;
00069 
00070     check_assertion( node != NULL, "node is NULL" );
00071 
00072     new_model_matrix=model_matrix*node->trans;
00073 
00074     if ( node->geom == Sphere && node->render_shadow ) {
00075        draw_shadow_sphere( new_model_matrix );
00076     } 
00077 
00078     child = node->child;
00079     while (child != NULL) {
00080 
00081         traverse_dag_for_shadow(child, new_model_matrix);
00082 
00083         child = child->next;
00084     } 
00085 }
00086 
00087 void draw_shadow_sphere( pp::Matrix model_matrix )
00088 {
00089     double theta, phi, d_theta, d_phi, eps, twopi;
00090     double x, y, z;
00091     int div = getparam_tux_shadow_sphere_divisions();
00092     
00093     eps = 1e-15;
00094     twopi = M_PI * 2.0;
00095 
00096     d_theta = d_phi = M_PI / div;
00097 
00098     for ( phi = 0.0; phi + eps < M_PI; phi += d_phi ) {
00099        double cos_theta, sin_theta;
00100        double sin_phi, cos_phi;
00101        double sin_phi_d_phi, cos_phi_d_phi;
00102 
00103        sin_phi = sin( phi );
00104        cos_phi = cos( phi );
00105        sin_phi_d_phi = sin( phi + d_phi );
00106        cos_phi_d_phi = cos( phi + d_phi );
00107         
00108         if ( phi <= eps ) {
00109 
00110             glBegin( GL_TRIANGLE_FAN );
00111               draw_shadow_vertex( 0., 0., 1., model_matrix );
00112 
00113                 for ( theta = 0.0; theta + eps < twopi; theta += d_theta ) {
00114                   sin_theta = sin( theta );
00115                   cos_theta = cos( theta );
00116 
00117                     x = cos_theta * sin_phi_d_phi;
00118                   y = sin_theta * sin_phi_d_phi;
00119                     z = cos_phi_d_phi;
00120                   draw_shadow_vertex( x, y, z, model_matrix );
00121                 } 
00122 
00123               x = sin_phi_d_phi;
00124               y = 0.0;
00125               z = cos_phi_d_phi;
00126               draw_shadow_vertex( x, y, z, model_matrix );
00127             glEnd();
00128 
00129         } else if ( phi + d_phi + eps >= M_PI ) {
00130             
00131             glBegin( GL_TRIANGLE_FAN );
00132               draw_shadow_vertex( 0., 0., -1., model_matrix );
00133 
00134                 for ( theta = twopi; theta - eps > 0; theta -= d_theta ) {
00135                   sin_theta = sin( theta );
00136                   cos_theta = cos( theta );
00137 
00138                     x = cos_theta * sin_phi;
00139                     y = sin_theta * sin_phi;
00140                     z = cos_phi;
00141                   draw_shadow_vertex( x, y, z, model_matrix );
00142                 } 
00143                 x = sin_phi;
00144                 y = 0.0;
00145                 z = cos_phi;
00146               draw_shadow_vertex( x, y, z, model_matrix );
00147             glEnd();
00148 
00149         } else {
00150             
00151             glBegin( GL_TRIANGLE_STRIP );
00152                 
00153                 for ( theta = 0.0; theta + eps < twopi; theta += d_theta ) {
00154                   sin_theta = sin( theta );
00155                   cos_theta = cos( theta );
00156 
00157                     x = cos_theta * sin_phi;
00158                     y = sin_theta * sin_phi;
00159                     z = cos_phi;
00160                   draw_shadow_vertex( x, y, z, model_matrix );
00161 
00162                     x = cos_theta * sin_phi_d_phi;
00163                     y = sin_theta * sin_phi_d_phi;
00164                     z = cos_phi_d_phi;
00165                   draw_shadow_vertex( x, y, z, model_matrix );
00166                 } 
00167                 x = sin_phi;
00168                 y = 0.0;
00169                 z = cos_phi;
00170               draw_shadow_vertex( x, y, z, model_matrix );
00171 
00172                 x = sin_phi_d_phi;
00173                 y = 0.0;
00174                 z = cos_phi_d_phi;
00175               draw_shadow_vertex( x, y, z, model_matrix );
00176 
00177             glEnd();
00178 
00179         } 
00180     } 
00181 
00182 } 
00183 
00184 void draw_shadow_vertex( double x, double y, double z, 
00185                       pp::Matrix model_matrix )
00186 {
00187     pp::Vec3d pt;
00188     double old_y;
00189     pp::Vec3d nml;
00190 
00191     pt = pp::Vec3d( x, y, z );
00192     pt = model_matrix.transformPoint( pt );
00193 
00194     old_y = pt.y;
00195     
00196     nml = find_course_normal( pt.x, pt.z );
00197     pt.y = find_y_coord( pt.x, pt.z ) + SHADOW_HEIGHT;
00198 
00199     if ( pt.y > old_y ) 
00200        pt.y = old_y;
00201 
00202     glNormal3f( nml.x, nml.y, nml.z );
00203     glVertex3f( pt.x, pt.y, pt.z );
00204 }