Ok I haven’t used materials, and this may be the reason it doesn’t work. I then went and tried to use materials, but i just end up with darkness
GLfloat no_mat[] = {0.0f, 0.0f, 0.0f, 1.0f};
GLfloat mat_ambient[] = {0.7f, 0.7f, 0.7f, 1.0f};
GLfloat mat_ambient_color[] = {0.8f, 0.8f, 0.2f, 1.0f};
GLfloat mat_diffuse[] = {0.1f, 0.5f, 0.8f, 1.0f};
GLfloat mat_specular[] = {1.0f, 1.0f, 1.0f, 1.0f};
GLfloat no_shininess = 0.0f;
GLfloat low_shininess = 5.0f;
GLfloat high_shininess = 100.0f;
GLfloat mat_emission[] = {0.3f, 0.2f, 0.2f, 0.0f};
void RenderHeightMap(BYTE pHeightMap[]) // This Renders The Height Map As Quads
{
int X = 0, Y = 0; // Create Some Variables To Walk The Array With.
int x, y, z; // Create Some Variables For Readability
int i; // Counter
char buffer[200];
int height[4]; //Y value of the 4 vertices making up each quad
if(!pHeightMap) return; // Make Sure Our Height Data Is Valid
float pointCoords[4][3]; //The xyz of the points outlining the quad
float normal[3];
//////////////////////////////////////////////
///First Pass - Render whole world in grass(!)
//////////////////////////////////////////////
glDisable(GL_BLEND); //Disable blending
for ( X = 0; X < (MAP_SIZE-STEP_SIZE); X += STEP_SIZE )
{
for ( Y = 0; Y < (MAP_SIZE-STEP_SIZE); Y += STEP_SIZE )
{
height[0] = Height(pHeightMap, X, Y );
height[1] = Height(pHeightMap, X, Y + STEP_SIZE );
height[2] = Height(pHeightMap, X + STEP_SIZE, Y + STEP_SIZE );
height[3] = Height(pHeightMap, X + STEP_SIZE, Y );
glBindTexture(GL_TEXTURE_2D, terrain[1].texture); //Bind the grass texture
// Get The (X, Y, Z) Value For The Bottom Left Vertex
pointCoords[0][0] = X;
pointCoords[0][1] = Height(pHeightMap, X, Y );
pointCoords[0][2] = Y;
//Top Left
pointCoords[1][0]= X;
pointCoords[1][1] = Height(pHeightMap, X, Y + STEP_SIZE );
pointCoords[1][2]= Y + STEP_SIZE;
//Top Right
pointCoords[2][0] = X + STEP_SIZE;
pointCoords[2][1] = Height(pHeightMap, X + STEP_SIZE, Y + STEP_SIZE );
pointCoords[2][2] = Y + STEP_SIZE;
//Bottom Right
pointCoords[3][0] = X + STEP_SIZE;
pointCoords[3][1] = Height(pHeightMap, X + STEP_SIZE, Y );
pointCoords[3][2] = Y;
FindNormal(pointCoords[0][0], pointCoords[0][1], pointCoords[0][2],
pointCoords[1][0], pointCoords[1][1], pointCoords[1][2],
pointCoords[2][0], pointCoords[2][1], pointCoords[2][2], normal);
/* sprintf(buffer, "Normal:
%f
%f
%f", normal[0], normal[1], normal[2]);
MessageBox(NULL, buffer, "normal", MB_OK);*/
if(bRender) // What We Want To Render (if polygon mode)
glBegin( GL_QUADS ); // Render Polygons
else
glBegin( GL_LINES ); // Render Lines Instead (grid, no filler)
glMaterialfv(GL_FRONT, GL_AMBIENT, no_mat);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, no_mat);
glMaterialf(GL_FRONT, GL_SHININESS, no_shininess);
glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
glNormal3f(normal[0], normal[1], normal[2]);
glTexCoord2f(0,0);
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
glVertex3f(pointCoords[0][0],pointCoords[0][1], pointCoords[0][2]);
glNormal3f(normal[0], normal[1], normal[2]);
glTexCoord2f(1,0);
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
glVertex3f(pointCoords[1][0],pointCoords[1][1], pointCoords[1][2]);
glNormal3f(normal[0], normal[1], normal[2]);
glTexCoord2f(1,1);
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
glVertex3f(pointCoords[2][0],pointCoords[2][1], pointCoords[2][2]);
glNormal3f(normal[0], normal[1], normal[2]);
glTexCoord2f(0,1);
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
glVertex3f(pointCoords[3][0],pointCoords[3][1], pointCoords[3][2]);
glEnd();
}
} //end of For x
///////////////////
///////Second Pass
///////Blend the rock texture with the grass to create "gradual rockification"
///////////////////
glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE);
glEnable(GL_BLEND); //Enable blending
glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); //Our method of blendation
for ( X = 0; X < (MAP_SIZE-STEP_SIZE); X += STEP_SIZE )
{
for ( Y = 0; Y < (MAP_SIZE-STEP_SIZE); Y += STEP_SIZE )
{
height[0] = Height(pHeightMap, X, Y );
height[1] = Height(pHeightMap, X, Y + STEP_SIZE );
height[2] = Height(pHeightMap, X + STEP_SIZE, Y + STEP_SIZE );
height[3] = Height(pHeightMap, X + STEP_SIZE, Y );
glBindTexture(GL_TEXTURE_2D, terrain[2].texture);
pointCoords[0][0] = X;
pointCoords[0][1] = Height(pHeightMap, X, Y );
pointCoords[0][2] = Y;
//Top Left
pointCoords[1][0]= X;
pointCoords[1][1] = Height(pHeightMap, X, Y + STEP_SIZE );
pointCoords[1][2]= Y + STEP_SIZE;
//Top Right
pointCoords[2][0] = X + STEP_SIZE;
pointCoords[2][1] = Height(pHeightMap, X + STEP_SIZE, Y + STEP_SIZE );
pointCoords[2][2] = Y + STEP_SIZE;
//Bottom Right
pointCoords[3][0] = X + STEP_SIZE;
pointCoords[3][1] = Height(pHeightMap, X + STEP_SIZE, Y );
pointCoords[3][2] = Y;
FindNormal(pointCoords[0][0], pointCoords[0][1], pointCoords[0][2],
pointCoords[1][0], pointCoords[1][1], pointCoords[1][2],
pointCoords[2][0], pointCoords[2][1], pointCoords[2][2], normal);
if(bRender) // What We Want To Render (if polygon mode)
glBegin( GL_QUADS ); // Render Polygons
else
glBegin( GL_LINES ); // Render Lines Instead (grid, no filler :D )
glMaterialfv(GL_FRONT, GL_AMBIENT, no_mat);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, no_mat);
glMaterialf(GL_FRONT, GL_SHININESS, no_shininess);
glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
glNormal3f(normal[0], normal[1], normal[2]);
glTexCoord2f(0,0);
glColor4f(1.0f, 1.0f, 1.0f, SetAlpha(Height(pHeightMap, X, Y )));
glVertex3f(pointCoords[0][0],pointCoords[0][1], pointCoords[0][2]);
glNormal3f(normal[0], normal[1], normal[2]);
glTexCoord2f(1,0);
glColor4f(1.0f, 1.0f, 1.0f, SetAlpha(Height(pHeightMap, X, Y + STEP_SIZE )));
glVertex3f(pointCoords[1][0],pointCoords[1][1], pointCoords[1][2]);
glNormal3f(normal[0], normal[1], normal[2]);
glTexCoord2f(1,1);
glColor4f(1.0f, 1.0f, 1.0f, SetAlpha(Height(pHeightMap, X, Y + STEP_SIZE )));
glVertex3f(pointCoords[2][0],pointCoords[2][1], pointCoords[2][2]);
glNormal3f(normal[0], normal[1], normal[2]);
glTexCoord2f(0,1);
glColor4f(1.0f, 1.0f, 1.0f, SetAlpha(Height(pHeightMap, X, Y + STEP_SIZE )));
glVertex3f(pointCoords[3][0],pointCoords[3][1], pointCoords[3][2]);
glEnd();
}
} //end of For x
///////////////
///Third pass can come here
///////////////
glDisable(GL_BLEND); //No more blending
glBindTexture(GL_TEXTURE_2D, NULL); // Clear the bound texture (for prosterity)
}