Code:
#include "main.h"
int TERRAIN::Height(BYTE *pHeightMap, int X, int Y)
{
int x = X % MAP_SIZE; // Error check our x value
int y = Y % MAP_SIZE; // Error check our y value
if(!pHeightMap) return 0; // Make sure our data is valid
return pHeightMap[x + (y * MAP_SIZE)]; // Index into our height array and return the height
}
void TERRAIN::SetTextureCoord(float x, float z)
{
// Give OpenGL the current texture coordinate for our height map
glTexCoord2f( (float)x / (float)MAP_SIZE,
- (float)z / (float)MAP_SIZE );
}
void TERRAIN::RenderHeightMap(BYTE pHeightMap[])
{
int X = 0, Y = 0; // Create some variables to walk the array with.
int x, y, z; // Create some variables for readability
float fColor = 0.0f; // Create a variable to hold our color of the polygon
if(!pHeightMap) return; // Make sure our height data is valid
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,g_Texture[0]);
glBegin( GL_QUADS ); // Render Quads
for ( X = 0; X < MAP_SIZE; X += STEP_SIZE )
for ( Y = 0; Y < MAP_SIZE; Y += STEP_SIZE )
{
// Get the (X, Y, Z) value for the bottom left vertex
x = X;
y = Height(pHeightMap, X, Y );
z = Y;
// Set the color value of the current vertice
SetTextureCoord(float(x), float(z)); glVertex3i(x, y, z); // Send this vertex to OpenGL to be rendered (integer points are faster)
// Get the (X, Y, Z) value for the top left vertex
x = X;
y = Height(pHeightMap, X, Y + STEP_SIZE );
z = Y + STEP_SIZE ;
SetTextureCoord(float(x), float(z)); glVertex3i(x, y, z); // Send this vertex to OpenGL to be rendered
// Get the (X, Y, Z) value for the top right vertex
x = X + STEP_SIZE;
y = Height(pHeightMap, X + STEP_SIZE, Y + STEP_SIZE );
z = Y + STEP_SIZE ;
SetTextureCoord(float(x), float(z)); glVertex3i(x, y, z); // Send this vertex to OpenGL to be rendered
// Get the (X, Y, Z) value for the bottom right vertex
x = X + STEP_SIZE;
y = Height(pHeightMap, X + STEP_SIZE, Y );
z = Y;
SetTextureCoord(float(x), float(z)); glVertex3i(x, y, z); // Send this vertex to OpenGL to be rendered
}
glEnd();
glDisable(GL_TEXTURE_2D);
// Reset the color
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
}
void TERRAIN::LoadRawFile(LPSTR strName, int nSize, BYTE *pHeightMap)
{
FILE *pFile = NULL;
// Let's open the file in Read/Binary mode.
pFile = fopen( strName, "rb" );
// Check to see if we found the file and could open it
if ( pFile == NULL )
{
// Display our error message and stop the function
MessageBox(NULL, "Can't find the height map!", "Error", MB_OK);
return;
}
// Here we load the .raw file into our pHeightMap data array.
// We are only reading in '1', and the size is the (width * height)
fread( pHeightMap, 1, nSize, pFile );
// After we read the data, it's a good idea to check if everything read fine.
int result = ferror( pFile );
// Check if we received an error.
if (result)
{
MessageBox(NULL, "Can't get data!", "Error", MB_OK);
}
// Close the file.
fclose(pFile);
}
whats wrong with this code, when i call the function to load the raw file, i dont get any error popup, and no errors at all when i run it, it just wont display. I wonder what i am missing....