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
00012 static float *g_hf = NULL;
00013
00015 void initHeightField()
00016 {
00017
00018 int W = g_hfW;
00019 int H = g_hfH;
00020 assert( W>0 && W*H > 0 );
00021
00022 if (g_hf != NULL) {
00023 delete[] g_hf;
00024 }
00025
00026
00027 g_hf = new float[W*H];
00028 memset( g_hf,0, W*H*sizeof(float) );
00029
00030
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
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
00078 setDiffuseMaterialColor( transparent_alpha );
00079
00080
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
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
00112
00113
00114
00115 glEnable( GL_AUTO_NORMAL );
00116
00117
00118 glBindTexture( GL_TEXTURE_2D, texture( TEX_SNOW ));
00119 glEnable( GL_TEXTURE_2D );
00120
00121
00122 renderGroundQuad( w,h );
00123
00124
00125
00126 glDepthFunc( GL_LEQUAL );
00127
00128
00129 glDisable( GL_TEXTURE_2D );
00130 glBindTexture( GL_TEXTURE_2D, texture( TEX_ROCK ));
00131 glEnable( GL_TEXTURE_2D );
00132
00133
00134
00135 glBlendEquation( GL_ADD );
00136 glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
00137
00138
00139 glEnable( GL_BLEND );
00140 renderGroundQuad( w,h );
00141
00142
00143
00144 glDepthFunc( GL_LESS );
00145 glDisable( GL_BLEND );
00146 glDisable( GL_TEXTURE_2D );
00147 glDisable( GL_AUTO_NORMAL );
00148 }