Go to the source code of this file.
Functions | |
void | initHeightField () |
Initialize the height field. | |
void | drawHeightField (float x, float y, int w, int h) |
Draw the height field. |
|
Draw the height field. The field is placed in the XY plane centered in (0,0,0). A transformation is set up so that the height field position (x,y) is moved into the origin, and w x h tiles around this position are drawn. Each tile covers g_cellsize square units in OpenGL coordinates. Definition at line 109 of file heightfield.cpp. References renderGroundQuad(), TEX_ROCK, TEX_SNOW, and texture(). Referenced by renderScene().
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 } |
|
Initialize the height field. The field data is loaded as g_hfW*g_hfH continuous raw floats from the file. Definition at line 15 of file heightfield.cpp. References g_hf, g_hfH, g_hfW, g_strHeightFieldFile, and g_strResourceDir. Referenced by initGL().
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 } |