Lesson 34 adding textures to the terrain

Started by
3 comments, last by DeathwishCRO 14 years ago
Greetings all, I'm quite new to open gl (I've done some OSG, delta 3d coding), so i was wondering how to add a texture to the terrain so it looks more realistic. I've gone through the texturing tutorial on NeHe but had no success so far. I understand that i need to setup the texture similar to the seting up on the faces of poligons, (top/bottom- left/right), but am not sure how exactly to do so? Any help would be appreciated
Advertisement
the easiest way to do so would be to stretch a single texture across the whole thing.
the simplest way to do that would be to in the function RenderHeightMap
and before each
glVertex3i(x, y, z);

call this line
glTexCoord2f((float)x*(1/MAP_SIZE),(float)y*(1/MAP_SIZE);

This will give the proper texture coordinates
Now all you need is to bind the proper texture and enable texturing, but i leave that to you.
Thank you for your reply, however Im wondering if the height of the heigthmaped terrain does not influence the texturing?

I've added the functions from the texturing tutorial,and enabled texturing in glinit

I've modified the renderheightmap function like so
void RenderHeightMap(BYTE pHeightMap[]) // This Renders The Height Map As Quads
{
glBindTexture(GL_TEXTURE_2D, texture[0]); // Select Our Texture NEW
int X = 0, Y = 0; // Create Some Variables To Walk The Array With.
int x, y, z; // Create Some Variables For Readability

if(!pHeightMap) return; // Make Sure Our Height Data Is Valid

if(bRender) // What We Want To Render
glBegin( GL_QUADS ); // Render Polygons
else
glBegin( GL_LINES ); // Render Lines Instead

for ( X = 0; X < (MAP_SIZE-STEP_SIZE); X += STEP_SIZE )
for ( Y = 0; Y < (MAP_SIZE-STEP_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 Vertex
SetVertexColor(pHeightMap, x, z);
glTexCoord2f((float)x*(1/MAP_SIZE),(float)y*(1/MAP_SIZE));
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 ;

// Set The Color Value Of The Current Vertex
SetVertexColor(pHeightMap, x, z);
glTexCoord2f((float)x*(1/MAP_SIZE),(float)y*(1/MAP_SIZE));
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 ;

// Set The Color Value Of The Current Vertex
SetVertexColor(pHeightMap, x, z);
glTexCoord2f((float)x*(1/MAP_SIZE),(float)y*(1/MAP_SIZE));
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;

// Set The Color Value Of The Current Vertex
SetVertexColor(pHeightMap, x, z);
glTexCoord2f((float)x*(1/MAP_SIZE),(float)y*(1/MAP_SIZE));
glVertex3i(x, y, z); // Send This Vertex To OpenGL To Be Rendered
}
glEnd();

glColor4f(1.0f, 1.0f, 1.0f, 1.0f); // Reset The Color
}

It all compiles normaly but no texture is shown :/
Here is my solution folder if it helps show you where am i Blundering
http://rapidshare.com/files/372982652/NEHE_Lesson34_heightmap_loading.7z.html

best regards
well it turns out that you do have texturing, i have two changed you might want to make though

1. this line will work much better than the one i gave you before
glTexCoord2f((float)x/MAP_SIZE,(float)z/MAP_SIZE);

2. remove SetVertexColor(pHeightMap, x, z); , you won't need that anymore since it makes the texture a lot darker
Works fine, thank you very much. Totaly forgot bout the setvertex function
Best regards, Daniel

This topic is closed to new replies.

Advertisement