Could someone possibly explain what the following code does:
int imagesInTexture = 11;
if (floorPlan[x, z] == 0)
{
verticesList.Add(new VertexPositionNormalTexture(new Vector3(x, 0, -z), new Vector3(0, 1, 0), new Vector2(0, 1)));
verticesList.Add(new VertexPositionNormalTexture(new Vector3(x, 0, -z - 1), new Vector3(0, 1, 0), new Vector2(0, 0)));
verticesList.Add(new VertexPositionNormalTexture(new Vector3(x + 1, 0, -z), new Vector3(0, 1, 0), new Vector2(1.0f / imagesInTexture, 1)));
verticesList.Add(new VertexPositionNormalTexture(new Vector3(x, 0, -z - 1), new Vector3(0, 1, 0), new Vector2(0, 0)));
verticesList.Add(new VertexPositionNormalTexture(new Vector3(x + 1, 0, -z-1), new Vector3(0, 1, 0), new Vector2(1.0f / imagesInTexture, 0)));
verticesList.Add(new VertexPositionNormalTexture(new Vector3(x + 1, 0, -z), new Vector3(0, 1, 0), new Vector2(1.0f / imagesInTexture, 1)));
}
It is from this tutorial below: http://www.riemers.net/eng/Tutorials/XNA/Csharp/Series2/Loading_the_floorplan.php
It isn't clearly explained what it does, if you would like to view the texture map that comes with the tutorial I have uploaded it here: Texture Map from Tutorial.
Thanks.
The code creates the tiles to be drawn. Each tile is composed of two triangles. Triangles are defined as three sequential vertices. These triangles are then submitted to the graphics card in a vertex buffer.
VertexPositionNormalTexture
is the type of vertex data expected by the Effect
used, and it contains:
(x, 0, -z)
, where x
and z
are the coordinates of the tile.(0, 1, 0)
, i.e. directly up. All the vertices have the same normal vector because the surface is flat.(0, 1)
, the top left corner of the texture; for the second, the bottom left corner; for the third, the top right point in the roof part.The type of data depends on the effect: some may use vertex colors, more textures with independent coordinates, material properties like reflectivity, etc. However, this matters mostly when designing shaders and generating geometry on the fly (such as in this tutorial). If you use the Model
class with a built-in Effect
, everything is taken care of by the framework.
If you haven't worked with 3D graphics before, I'd suggest toying with the code: comment out either group of three to see the triangles, then play with the position and texture coordinates. Plotting the vertices on a piece of paper might also help understand the code.