Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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

share|improve this question
add comment

1 Answer

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.

share|improve this answer
    
The loop code is off by one using "<=", its processing 1 row and column more than actually is there. –  Maik Semder Jul 13 '13 at 18:31
    
If a grid has 3 columns there are 4 lines, it was intentional. –  Syntac Jul 13 '13 at 23:05
    
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? –  user1157885 Jul 13 '13 at 23:11
    
Could you tell me how you are creating the vertices and index list for the grid? Or are you importing a model? –  Syntac Jul 13 '13 at 23:31
    
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. –  user1157885 Jul 14 '13 at 2:45
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.