# Calculate correct tU/tV coords for terrain

This topic is 4973 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am curious if what I do to calculate the coords is correct: fTexDelta = 2.0f / mapsize tU/tV (tileU * ( (float)(x + (pBoundingCoords[0].x)) * fTexDelta * 0.5f)) (tileV * (1.0f - (float)(z + (pBoundingCoords[0].z )) * fTexDelta * 0.5f)) This comes from a for loop when generating the vertices, x and z are the equivalent x,y values on the heightmap bmp.

##### Share on other sites
hmmm, not sure how you are doing it right now but what you can usually do is the following

float uDelta = MaxU / mapMaxX;  // stepping (1 map unit = uv units)float vDelta = MaxV / mapMaxZ;// uv at a point then becomesu = (uDelta * x);v = (vDelta * z);

HTH

##### Share on other sites
Well presumably you want your heightmap texture to exactly fit your heightmap vertex grid.
So:
for(int x=0;x<=MapWidth;++x)for(int y=0;y<=MapWidth;++y){ vertex->x = x; vertex->y = y; vertex->u = x/mapWidth; vertex->v = y/mapWidth; }
I use the "<=" because if your map size is 256, you need 257 vertices for the edge.

I actually just set u=x, v=y and use a texcoord scaling matrix. This means I can apply a colourmap stretched over the whole terrain for large-scale detail and detail maps for close detail, using only one set of texture coordinates with two different matrices; this reduces your vertex size.

##### Share on other sites
Well if it is possible for anyone to read this snippet. This is part of a function I use to add the vertices to each leaf. I didn't bother to touch it up so anyone could read it, but it is still fairly simplistic. The last 2 parameters of AddVertice are the tU and tV coords.

The fTexDelta = 2.0f / 256.0f. This is the only part I do not get, since I did get this calculation trick from somewhere else.

But as I continued to research it, if I set tileU and tileV to 2 in the calculation below you will cleary see the seperation on the terrain (if you use the right texture) at the center of the terrain.

//Fill the vertices
for (int z = 0; z <= vSize.z; z++)
{
for (int x = 0; x <= vSize.x; x++)
{
iCurrVertex = (z * iVertexJump) + x;
iHeightIndex = (int)(((vUnScaledLeftCorner.z + z) * (pHeightmap->Height() + 1.0f)) + (vUnScaledLeftCorner.x + x));

iCurrVertex,
Vector
(
(float)(x + (pBoundingCoords[0].x / m_fScaleXZ)) * m_fScaleXZ,
pHeightmap->GetTableIndex(iHeightIndex),
(float)(z + (pBoundingCoords[0].z / m_fScaleXZ)) * m_fScaleXZ
),
(tileV * ( (float)(x + (pBoundingCoords[0].x / m_fScaleXZ)) * fTexDelta * 0.5f)),
(tileU * (1.0f - (float)(z + (pBoundingCoords[0].z / m_fScaleXZ)) * fTexDelta * 0.5f))
);

}
}

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 13
• 14
• 10
• 11
• 13
• ### Forum Statistics

• Total Topics
634096
• Total Posts
3015484
×