Advertisement Jump to content
Sign in to follow this  

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.

If you intended to correct an error in the post then please contact us.

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 this post

Link to post
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 becomes
u = (uDelta * x);
v = (vDelta * z);


Share this post

Link to post
Share on other sites
Well presumably you want your heightmap texture to exactly fit your heightmap vertex grid.
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 this post

Link to post
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));

(float)(x + (pBoundingCoords[0].x / m_fScaleXZ)) * m_fScaleXZ,
(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))


Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!