Main Page | Namespace List | Class Hierarchy | Class List | File List | Class Members | File Members

heightfield.cpp

Go to the documentation of this file.
00001 #include <GL/gl.h>
00002 #include <assert.h>
00003 #include <string.h>
00004 #include <stdio.h>
00005 
00006 #include "heightfield.h"
00007 #include "config.h"
00008 #include "texture.h"
00009 
00010 
00011 // Height field data
00012 static float *g_hf = NULL;
00013 
00015 void initHeightField()
00016 {
00017   // Validate params
00018   int W = g_hfW;
00019   int H = g_hfH;
00020   assert( W>0 && W*H > 0 );
00021   // Clear old data
00022   if (g_hf != NULL) {
00023     delete[] g_hf;
00024   }
00025 
00026   // Init new data
00027   g_hf = new float[W*H];
00028   memset( g_hf,0, W*H*sizeof(float) );
00029 
00030   // Read data from file
00031   char filename[500];
00032   strcpy( filename, g_strResourceDir );
00033   strcat( filename, g_strHeightFieldFile );
00034   printf( "Reading height field %s\n", filename );
00035 
00036   FILE *file = fopen( filename, "rb" );
00037   assert( file != NULL );
00038   int n;
00039   if ( (n=fread( g_hf, sizeof(float), W*H, file )) != W*H) {
00040     printf( "Error loading height field, only %i floats read.", n ); 
00041   }
00042   else {
00043     printf( "read %i floats ... ", n );
00044   }
00045 }
00046 
00047 
00049 float gethf( int x, int y ) {
00050   assert( x>=0 && x<g_hfW );
00051   assert( y>=0 && y<g_hfH );
00052   return g_hf[ x + y*g_hfW ];
00053 }
00054 
00055 
00056 
00057 void setDiffuseMaterialColor( const float *mc )
00058 {
00059   glMaterialfv(GL_FRONT, GL_DIFFUSE, mc );
00060   glMaterialfv(GL_FRONT, GL_AMBIENT, mc );
00061   glMaterialfv(GL_FRONT, GL_SPECULAR, colorNone );
00062   glColor4fv(mc);
00063 }
00064 
00065 
00066 
00067 
00068 // render the single quad for the ground
00069 void renderGroundQuad( float w, float h )
00070 {
00071   static const float transparent_alpha[4] = {1,1,1,0};
00072   static const float opaque_alpha[4] = {1,1,1,1};
00073 
00074 
00075   glBegin( GL_QUADS );
00076 
00077   // At negative w, make transparent if alpha blending is on
00078   setDiffuseMaterialColor( transparent_alpha );
00079 
00080   // Texture coordinates have to be defined before the vertex
00081   glTexCoord2f( 0,1 );
00082   glVertex3f( -w, h, gethf(0,2) );
00083 
00084   glTexCoord2f( 0,0 );
00085   glVertex3f( -w,-h, gethf(0,0) );
00086 
00087 
00088   // At positive w, make opaque if alpha blending is on
00089   setDiffuseMaterialColor( opaque_alpha );
00090 
00091   glTexCoord2f( 1, 0 );
00092   glVertex3f( w,-h, gethf(2,0) );
00093 
00094   glTexCoord2f( 1, 1 );
00095   glVertex3f( w, h, gethf(2,2) );
00096 
00097   glEnd();
00098 }
00099 
00100 
00101 
00103 
00109 void drawHeightField( float x, float y, int w, int h )
00110 {
00111   // Automatic normals do not work correctly for a height field,
00112   // because there is no interaction between two adjacent strips.
00113   // In the solution, you will have to compute the normals of
00114   // the field yourself.
00115   glEnable( GL_AUTO_NORMAL );
00116 
00117   // Enable the grass texture
00118   glBindTexture( GL_TEXTURE_2D, texture( TEX_SNOW ));
00119   glEnable( GL_TEXTURE_2D );
00120 
00121   // Render ground once
00122   renderGroundQuad( w,h );
00123 
00124   // Allow equal depth buffer values, otherwise the quad is not rendered
00125   // a second time
00126   glDepthFunc( GL_LEQUAL );
00127 
00128   // Switch to the second texture
00129   glDisable( GL_TEXTURE_2D );
00130   glBindTexture( GL_TEXTURE_2D, texture( TEX_ROCK ));
00131   glEnable( GL_TEXTURE_2D );
00132 
00133   // Setup blending so that it blends with the framebuffer and the new
00134   // geometry using the value in the alpha channel as a weight
00135   glBlendEquation( GL_ADD );
00136   glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
00137 
00138   // Switch on alpha blending, and render ground again
00139   glEnable( GL_BLEND );
00140   renderGroundQuad( w,h );
00141 
00142 
00143   // Reset rendering state
00144   glDepthFunc( GL_LESS );
00145   glDisable( GL_BLEND );
00146   glDisable( GL_TEXTURE_2D );
00147   glDisable( GL_AUTO_NORMAL );
00148 }

Generated on Thu Jan 20 02:47:12 2005 for Projective_Texture_Effect_Demo by doxygen 1.3.6