Back to index

extremetuxracer  0.5beta
render_util.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 "gl_util.h"
00023 #include "render_util.h"
00024 #include "textures.h"
00025 #include "game_config.h"
00026 #include "player.h"
00027 
00028 #include "stuff.h"
00029 
00030 #include "ppgltk/ui_theme.h"
00031 
00032 /*
00033  * Constants 
00034  */
00035 
00036 /* Distance by which to push back far clip plane, to ensure that the
00037    fogging plane is drawn (m) */
00038 #define FAR_CLIP_FUDGE_AMOUNT 5
00039 
00040 static const pp::Color textColor(0.0, 0.0, 0.0);
00041 
00042 void reshape( int w, int h )
00043 {
00044     double far_clip_dist;
00045 
00046     setparam_x_resolution( w );
00047     setparam_y_resolution( h );
00048     glViewport( 0, 0, (GLint) w, (GLint) h );
00049     glMatrixMode( GL_PROJECTION );
00050     glLoadIdentity();
00051 
00052     far_clip_dist = getparam_forward_clip_distance() + FAR_CLIP_FUDGE_AMOUNT;
00053 
00054     gluPerspective( getparam_fov(), (double)w/h, NEAR_CLIP_DIST, 
00055                   far_clip_dist );
00056 
00057     glMatrixMode( GL_MODELVIEW );
00058 } 
00059 
00060 void flat_mode()
00061 {
00062     set_gl_options( TEXT );
00063 
00064     glMatrixMode( GL_PROJECTION );
00065     glLoadIdentity();
00066     glOrtho( -0.5, 639.5, -0.5, 479.5, -1.0, 1.0 );
00067     glMatrixMode( GL_MODELVIEW );
00068     glLoadIdentity();
00069 }
00070 
00071 void draw_overlay() {
00072     glColor4f( 0.0, 0.0, 1.0, 0.1 );
00073     glRecti( 0, 0, 640, 480 );
00074 } 
00075 
00076 void clear_rendering_context()
00077 {
00078     glDepthMask( GL_TRUE );
00079     glClearColor( theme.background.r,
00080                 theme.background.g,
00081                 theme.background.b,
00082                 theme.background.a );
00083     glClearStencil( 0 );
00084     glClear( GL_COLOR_BUFFER_BIT 
00085             | GL_DEPTH_BUFFER_BIT 
00086             | GL_STENCIL_BUFFER_BIT );
00087 }
00088 
00089 /* 
00090  * Sets the material properties
00091  */
00092 void set_material( const pp::Color diffuse, const pp::Color specular,
00093                       const double specular_exp )
00094 {
00095   GLfloat mat_amb_diff[4];
00096   GLfloat mat_specular[4];
00097 
00098   /* Set material color (used when lighting is on) */
00099   mat_amb_diff[0] = diffuse.r;
00100   mat_amb_diff[1] = diffuse.g;
00101   mat_amb_diff[2] = diffuse.b;
00102   mat_amb_diff[3] = diffuse.a; 
00103   glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, mat_amb_diff );
00104 
00105   mat_specular[0] = specular.r;
00106   mat_specular[1] = specular.g;
00107   mat_specular[2] = specular.b;
00108   mat_specular[3] = specular.a;
00109   glMaterialfv( GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular );
00110 
00111   glMaterialf( GL_FRONT_AND_BACK, GL_SHININESS, specular_exp );
00112 
00113   /* Set standard color */
00114   glColor4f( diffuse.r, diffuse.g, diffuse.b, diffuse.a);
00115 } 
00116 
00117 
00118 void draw_billboard( Player& plyr, 
00119                    pp::Vec3d center_pt, double width, double height, 
00120                    bool use_world_y_axis, 
00121                    pp::Vec2d min_tex_coord, pp::Vec2d max_tex_coord )
00122 {
00123     pp::Vec3d pt;
00124     pp::Vec3d x_vec;
00125     pp::Vec3d y_vec;
00126     pp::Vec3d z_vec;
00127 
00128     x_vec.x = plyr.view.inv_view_mat.data[0][0];
00129     x_vec.y = plyr.view.inv_view_mat.data[0][1];
00130     x_vec.z = plyr.view.inv_view_mat.data[0][2];
00131 
00132     if ( use_world_y_axis ) {
00133        y_vec = pp::Vec3d( 0, 1, 0 );
00134        x_vec = projectIntoPlane( y_vec, x_vec );
00135        x_vec.normalize();
00136        z_vec = x_vec^y_vec;
00137     } else {
00138        y_vec.x = plyr.view.inv_view_mat.data[1][0];
00139        y_vec.y = plyr.view.inv_view_mat.data[1][1];
00140        y_vec.z = plyr.view.inv_view_mat.data[1][2];
00141        z_vec.x = plyr.view.inv_view_mat.data[2][0];
00142        z_vec.y = plyr.view.inv_view_mat.data[2][1];
00143        z_vec.z = plyr.view.inv_view_mat.data[2][2];
00144     }
00145 
00146     glBegin( GL_QUADS );
00147     {
00148        pt = center_pt+((-width/2.0)*x_vec);
00149        pt = pt+((-height/2.0)*y_vec);
00150        glNormal3f( z_vec.x, z_vec.y, z_vec.z );
00151        glTexCoord2f( min_tex_coord.x, min_tex_coord.y );
00152        glVertex3f( pt.x, pt.y, pt.z );
00153 
00154        pt = pt + (width*x_vec);
00155        glTexCoord2f( max_tex_coord.x, min_tex_coord.y );
00156        glVertex3f( pt.x, pt.y, pt.z );
00157 
00158        pt = pt - (height*y_vec);
00159        glTexCoord2f( max_tex_coord.x, max_tex_coord.y );
00160        glVertex3f( pt.x, pt.y, pt.z );
00161 
00162        pt = pt - (-width *x_vec);
00163        glTexCoord2f( min_tex_coord.x, max_tex_coord.y );
00164        glVertex3f( pt.x, pt.y, pt.z );
00165     }
00166     glEnd();
00167 }