PDA

View Full Version : texture mapping: Ch 7 BOGLGP by D Astle & K Hawki

Savalia
09-30-2009, 05:45 PM
Hi, if any one can suggest a solution that would be great, I think on this topic I'm missing the obvious. In CH 7 of "Beginning OpenGL Game Programming" (1st Ed) texturing is covered, specifically glTexEnv. Dave and Kevin include a simple terrain sample and at the end of the chapter, one of the on your own exercises suggests modifying the terrain sample so that the top of the terrain high points has snow. In my attempt to complete it simple task, I have failed miserably!!! I am including th drawTerrain function how I have attempted to modify it, could anyone suggest a successful solution to my dilemma. The draw terrain and draw water code blocks are from the original solution. My approach was to copy the grass.tga file used and modify it for snow, then after the terrain block is created I would reiterate through the drawing of the terrain and try to map the snow areas to no success.
// draw the terrain
glBindTexture(GL_TEXTURE_2D, m_grassTexture);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

for (int z = 0; z < TERRAIN_SIZE - 1; ++z)
{
glBegin(GL_TRIANGLE_STRIP);
for (int x = 0; x < TERRAIN_SIZE; ++x)
{
// render two vertices of the strip at once
float scaledHeight = heightmap[z * TERRAIN_SIZE + x] / SCALE_FACTOR;
float nextScaledHeight = heightmap[(z + 1)* TERRAIN_SIZE + x] / SCALE_FACTOR;

float color = 0.5f + 0.5f * scaledHeight / MAX_HEIGHT;
float nextColor = 0.5f + 0.5f * nextScaledHeight / MAX_HEIGHT;
float snow =1.0f; // Value for white

glColor3f(color, color, color);
glTexCoord2f((GLfloat)x/TERRAIN_SIZE*8, (GLfloat)z/TERRAIN_SIZE*8);
glVertex3f(static_cast<GLfloat>(x - TERRAIN_SIZE/2), scaledHeight, static_cast<GLfloat>(z - TERRAIN_SIZE/2));

glColor3f(nextColor, nextColor, nextColor);
glTexCoord2f((GLfloat)x/TERRAIN_SIZE*8, (GLfloat)(z+1)/TERRAIN_SIZE*8);
glVertex3f(static_cast<GLfloat>(x - TERRAIN_SIZE/2), nextScaledHeight, static_cast<GLfloat>(z + 1 - TERRAIN_SIZE/2));
}
glEnd();
}

// draw the snow covered terrain
// if scaledHeight is >= SNOW_CAP the texture will become white to
// resemble snow at higher altitudes.
// draw the terrain
glBindTexture(GL_TEXTURE_2D, m_snowTexture);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

for (int z = 0; z < TERRAIN_SIZE - 1; ++z)
{
glBegin(GL_TRIANGLE_STRIP);
for (int x = 0; x < TERRAIN_SIZE; ++x)
{
// render two vertices of the strip at once
float scaledHeight = heightmap[z * TERRAIN_SIZE + x] / SCALE_FACTOR;
float nextScaledHeight = heightmap[(z + 1)* TERRAIN_SIZE + x] / SCALE_FACTOR;
float color = 0.5f + 0.5f * scaledHeight / MAX_HEIGHT;
float nextColor = 0.5f + 0.5f * nextScaledHeight / MAX_HEIGHT;
float snow =0.0f; // Value for white = 1
if(MAX_HEIGHT >= SNOW_CAP)
{
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
color = snow;
nextColor = snow;
}
glColor3f(color, color, color);
glTexCoord2f((GLfloat)x/TERRAIN_SIZE*8, (GLfloat)z/TERRAIN_SIZE*8);
glVertex3f(static_cast<GLfloat>(x - TERRAIN_SIZE/2), scaledHeight, static_cast<GLfloat>(z - TERRAIN_SIZE/2));

glColor3f(nextColor, nextColor, nextColor);
glTexCoord2f((GLfloat)x/TERRAIN_SIZE*8, (GLfloat)(z+1)/TERRAIN_SIZE*8);
glVertex3f(static_cast<GLfloat>(x - TERRAIN_SIZE/2), nextScaledHeight, static_cast<GLfloat>(z + 1 - TERRAIN_SIZE/2));
}
glEnd();
}

//draw the water
glBindTexture(GL_TEXTURE_2D, m_waterTexture);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glTexCoord2f(0.0, 0.0);
glVertex3f(-TERRAIN_SIZE/2.1f, WATER_HEIGHT, TERRAIN_SIZE/2.1f);

glTexCoord2f(TERRAIN_SIZE/4.0f, 0.0);
glVertex3f(TERRAIN_SIZE/2.1f, WATER_HEIGHT, TERRAIN_SIZE/2.1f);

glTexCoord2f(TERRAIN_SIZE/4.0f, TERRAIN_SIZE/4.0f);
glVertex3f(TERRAIN_SIZE/2.1f, WATER_HEIGHT, -TERRAIN_SIZE/2.1f);

glTexCoord2f(0.0, TERRAIN_SIZE/4.0f);
glVertex3f(-TERRAIN_SIZE/2.1f, WATER_HEIGHT, -TERRAIN_SIZE/2.1f);
glEnd();

}

Savalia
10-14-2009, 10:16 PM
Have I posted incorrectly or inappropriately?