#include <GL/gl.h>#include <assert.h>#include <string.h>#include <stdio.h>#include "heightfield.h"#include "config.h"#include "texture.h"Go to the source code of this file.
Functions | |
| void | initHeightField () |
| Initialize the height field. | |
| float | gethf (int x, int y) |
| Get value of the height field at certain coordinate. | |
| void | setDiffuseMaterialColor (const float *mc) |
| void | renderGroundQuad (float w, float h) |
| void | drawHeightField (float x, float y, int w, int h) |
| Draw the height field. | |
Variables | |
| float * | g_hf = NULL |
|
||||||||||||||||||||
|
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 }
|
|
||||||||||||
|
Get value of the height field at certain coordinate.
Definition at line 49 of file heightfield.cpp. References g_hf, g_hfH, and g_hfW. Referenced by renderGroundQuad().
|
|
|
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 }
|
|
||||||||||||
|
Definition at line 69 of file heightfield.cpp. References gethf(), and setDiffuseMaterialColor(). Referenced by drawHeightField().
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 }
|
|
|
Definition at line 57 of file heightfield.cpp. References colorNone. Referenced by renderGroundQuad().
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 }
|
|
|
Definition at line 12 of file heightfield.cpp. Referenced by gethf(), and initHeightField(). |
1.3.6