Search code examples
vectorxnagame-engine

XNA - VertexPositionNormal & Vectors


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.


Solution

  • 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:

    • Vertex position in the 3D world. This is quite self-explanatory. For the first vertex, this is (x, 0, -z), where x and z are the coordinates of the tile.
    • Normal vector in the 3D world. This is used by the shader to calculate lighting. For example, if the surface is perpendicular to a light source, it is fully lit. For the first vertex, this is (0, 1, 0), i.e. directly up. All the vertices have the same normal vector because the surface is flat.
    • Texture coordinate in 2D space. This coordinate specifies the point in the texture that is mapped to this vertex. For the first vertex, this is (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.