# Thread: DOH! Need help stapling texture to terrain map properly.

1. ## DOH! Need help stapling texture to terrain map properly.

Ok, I have found no really good explanation of how to properly calculate texture coordinates for this problem ... mebbe someone can help me out.

I have a variable size terrain whose dimensions as passed to glVertex3f are :

X Axis : -xMinT to xMaxT
Y Axis : -yMinT to yMaxT

(Note Z is up in this system)

So I want to staple the texture onto the terrain map with a 1:1 correspondence and rely on scaling the texture matrix to distort it at run time.

My last effort went like this ...

Compute the scaling functions ...
Code :
```	// Compute function to map range [xMinT,xMaxT] onto [0,1]
m_TexCoordSlope[0] = 1.0f/float(xMaxT-xMinT);
m_TexCoordIntercept[0] = -float(xOffset)*m_TexCoordSlope[0];

// Compute function to map range [yMinT,yMaxT] onto [0,1]
m_TexCoordSlope[1] = 1.0f/float(yMaxT-yMinT);
m_TexCoordIntercept[1] = -float(yOffset)*m_TexCoordSlope[1];```
and my function to place a vertex and staple its texture coordinate ...
Code :
```void TerrainRenderer: : placeVertex(float x, float y, float z)
{
double z2 = z*m_ScaledElevationSlope+m_ScaledElevationIntercept;
setHeightColor(z);
glVertex3f(x,y,z2);

float xt = m_TexCoordSlope[0]*x + m_TexCoordIntercept[0];
float yt = m_TexCoordSlope[1]*y + m_TexCoordIntercept[1];

glTexCoord2f(xt,yt);
}```
The code that places the vertex is ...
Code :
```glMatrixMode(GL_TEXTURE);
glPushMatrix();
glScalef(m_TexStretch[0],m_TexStretch[1],1);
glMatrixMode(GL_MODELVIEW);

// ...

placeVertex(terrainX, terrainY, terrainZ);

// ...

glMatrixMode(GL_TEXTURE);
glPopmatrix();
glMatrixMode(GL_MODEL_VIEW);

// ...```
Well, I end up with a really nicely textured terrain but that texture is repeated over the terrain roughly 100 times in each direction when m_TexStretch[i] = 1. I want that case to result in the texture being stapled to the corners with a ONE TO ONE match of the texture to the terrain map.

Does anyone see what I am doing wrong? Or, are the better ways to do this?

Ice out

[This message has been edited by Iceman (edited 09-21-2002).]

2. ## Re: DOH! Need help stapling texture to terrain map properly.

oops

[This message has been edited by Iceman (edited 09-21-2002).]

3. ## Re: DOH! Need help stapling texture to terrain map properly.

oops

[This message has been edited by Iceman (edited 09-21-2002).]

oops

5. ## Re: DOH! Need help stapling texture to terrain map properly.

s_tex = x_coord/terrain_x_size;
t_tex = 1.0-z_coord/terrain_z_size;

This assumes that the y axis points up.

There's no need for all the texture matrix stuff.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•