Terrain lighting


In this tutorial we are going to do pretty much the same changes as in the last tutorial, but I will repeat them, albeit in less detail, since we worked on the World Space code which dealt with two triangles in the last tutorial and now we are going back to the terrain code.

Again we need to replace the custom vertex struct with the updated version, So the full code for the VertexPositionColorNormal struct is:

public struct VertexPositionColorNormal : IVertexType


public Vector3 vertexPosition;

public Color vertexColor;

public Vector3 vertexNormal;

public readonly static VertexDeclaration VertexDeclaration = new VertexDeclaration


new VertexElement( 0, VertexElementFormat.Vector3, VertexElementUsage.Position, 0 ),

new VertexElement( sizeof(float) * 3, VertexElementFormat.Color, VertexElementUsage.Color, 0 ),

new VertexElement( sizeof(float) * 3 + 4, VertexElementFormat.Vector3, VertexElementUsage.Normal, 0 )


//The constructor for the custom vertex. This allows similar

//initialization of custom vertex arrays as compared to arrays of a

//standard vertex type, such as VertexPositionColor.

public VertexPositionColorNormal(Vector3 position, Color color, Vector3 normal)


vertexPosition = position;

vertexColor = color;

vertexNormal = normal;


//Public method for accessing the Position component of this custom vertex.

public Vector3 Position


get { return vertexPosition; }

set { vertexPosition = value; }


//Public method for accessing the Color component.

public Color Color


get { return vertexColor; }

set { vertexColor = value; }


//Public method for accessing the Normal component.

public Vector3 Normal


get { return vertexNormal; }

set { vertexNormal = value; }


//Implement the IVertexType interface so that we can get the vertex

//declaration straight from our custom vertex!

VertexDeclaration IVertexType.VertexDeclaration


get { return VertexDeclaration; }



We can ignore the part where we are told to update this line:

myVertexDeclaration = new VertexDeclaration(device, VertexPositionNormalColored.VertexElements);

since we don’t need to worry about vertex declarations any more as they are handled in our custom struct now, just remove this line.

Now we’ll need to change:

VertexPositionNormalColored[] vertices;


VertexPositionColorNormal[] vertices;

One change Riemer forgot to mention in this tutorial was to update the vertex array declaration to the custom vertex struct so change this line:

vertices = new VertexPositionColor[terrainWidth * terrainHeight];

to this:

vertices = new VertexPositionColorNormal[terrainWidth * terrainHeight];

We will use this line as stated in the tutorial:

device.DrawUserIndexedPrimitives(PrimitiveType.TriangleList, vertices, 0, vertices.Length, indices, 0, indices.Length / 3);

in previous tutorials we had to specify the vertex declaration as the last parameter, again this is taken care of so we don’t need that any more.

And two other changes which were left out of the tutorial, but are in the code at the bottom of the page are as follows:

In the Draw() method we must add:


otherwise we wont see the lighting effects 🙂 and while we set this don’t forget to change:

effect.CurrentTechnique = effect.Techniques[“Colored”];


effect.CurrentTechnique = effect.Techniques[“ColoredNormal”];

also we need to change the position of the camera in the SetUpCamera() method to:

viewMatrix = Matrix.CreateLookAt(new Vector3(0, 100, 100), new Vector3(0, 0, 0), new Vector3(0, 1, 0));

And thats all for this tutorial, next up VertexBuffer & IndexBuffer.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s