# Algorithm for UV Coords generation for grid

I have a landscape which is an array of vertices, and a matching array of texcoords.

I want to have an algorithm too generate the ST/UV coords for the grid.

an example of what i think the algorithm should look like;

``````for ( long y = 0; y &lt; height; y++ )
{
for ( long x = 0; x &lt; width; x+=6 )
{
texcoords[y*(width*3)+x].s = ?;
texcoords[y*(width*3)+x].t = ?;
texcoords[y*(width*3)+x+1].s = ?;
texcoords[y*(width*3)+x+1].t = ?;
texcoords[y*(width*3)+x+2].s = ?;
texcoords[y*(width*3)+x+2].t = ?;
texcoords[y*(width*3)+x+3].s = ?;
texcoords[y*(width*3)+x+3].t = ?;
texcoords[y*(width*3)+x+4].s = ?;
texcoords[y*(width*3)+x+4].t = ?;
texcoords[y*(width*3)+x+5].s = ?;
texcoords[y*(width*3)+x+5].t = ?;
}
}``````

Ahem … if I understood well, this is fair easy …

Without any kind of optimisation :

for ( long y = 0; y < height; y++ )
{
for ( long x = 0; x < width; x++ )
{
texcoords[ywidth+x].s = (float)x/(float)(width-1);
texcoords[y
width+x].t = (float)y/(float)(height-1);
}
}

texture coordinates are from 0.0 to 1.0.

[This message has been edited by paddy (edited 05-26-2001).]

The pronblem is, my array contains three points for every polygon, and each polygon is reversed;

even polygons;
(0)
|
|
(1)–(2)

odd polygons;
(1)–(0)
\ |
\ |
(2)

So the algorithm has to be cleverer to accomodate this?

Just asign the plane you want to your texture coords.

XY plane:
s=x
t=y

or…

XZ plane:
s=x
t=z

or…whatever you choose.

Once the texture coords are set up, then scale them to the appropriate size.

About your reversed polygon thing. It doesn’t matter because you’ll be assigning the texture coordinates the same forward and reversed vertices.

Of course!
So simple I could’nt see it Cheers.

Not to blame anyone but … I think there is an easier way to store an array for a landscape 