2
\$\begingroup\$

I have a grid of vertices that I'm using as the ground for my game in XNA. I have a effect file / shader which handles texturing and lighting. Right now I have it so the texture tiles across each face of the grid, but the texture i'm using is 4096x4096 and i'd like to have it completely stretched across all the faces of this grid instead of repeating itself over each face. How would I go about doing this?

Thanks

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Assuming the following:

  • (0,0) is the top-left coordinate.
  • Each vertex in the grid is equidistant in x and y.

float u, v;
for( int Row(0); Row <= NumberOfRows; ++Row )
{
    for( int Col(0); Col <= NumberOfCols; ++Col)
    {
        u = Row / float(NumberOfRows);
        v = Col / float(NumberOfCols);

        // Store u and v
     }
}

This code creates UV coordinates with a constant increment from 0 to 1 in both x and y. This will allow you to map one square texture over the full grid. All you need to remember is that texture coordinates rely on the smallest extremity being equal to 0 and the largest extremity being equal to 1 of the object your mapping.

\$\endgroup\$
7
  • \$\begingroup\$ The loop code is off by one using "<=", its processing 1 row and column more than actually is there. \$\endgroup\$ Commented Jul 13, 2013 at 18:31
  • \$\begingroup\$ If a grid has 3 columns there are 4 lines, it was intentional. \$\endgroup\$
    – Syntac_
    Commented Jul 13, 2013 at 23:05
  • \$\begingroup\$ Alright thanks, although I'm a little confused about how I would pass the UV's into the shader. I'm using graphicsDevice.DrawUserIndexedPrimitives to draw my grid. Would I just store the UV's in an array and pass the array across to the shader? \$\endgroup\$ Commented Jul 13, 2013 at 23:11
  • \$\begingroup\$ Could you tell me how you are creating the vertices and index list for the grid? Or are you importing a model? \$\endgroup\$
    – Syntac_
    Commented Jul 13, 2013 at 23:31
  • \$\begingroup\$ I have a class which creates a grid starting along the X axis then going down. I specify the tile amount along the X and Y axis and the size of each tile. I then take the vertice and index list from it and draw it using the UserIndexedPrimitives. \$\endgroup\$ Commented Jul 14, 2013 at 2:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .