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 …

// 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 …

void TerrainRenderer: : placeVertex(float x, float y, float z)
double z2 = z*m_ScaledElevationSlope+m_ScaledElevationIntercept;

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


The code that places the vertex is …


// …

placeVertex(terrainX, terrainY, terrainZ);

// …


// …

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?

Thanks in advance …

Ice out

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


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


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


How about for each vertex:

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.