PDA

View Full Version : DOH! Need help stapling texture to terrain map properly.



Iceman
09-21-2002, 10:53 AM
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_ScaledElevationIntercep t;
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 ...



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?

Thanks in advance ...

Ice out


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

Iceman
09-21-2002, 11:40 AM
oops

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

Iceman
09-21-2002, 11:41 AM
oops

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

Iceman
09-21-2002, 11:42 AM
oops

dorbie
09-22-2002, 02:15 AM
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.