Loading the floorplan

http://www.riemers.net/eng/Tutorials/XNA/Csharp/Series2/Loading_the_floorplan.php

We start this tutorial with something that should be very familiar by now the VertexDeclaration so we can remove this:

VertexDeclaration texturedVertexDeclaration;

In XNA Game Studio 4.0 the VertexBuffer syntax changed slightly now we don’t need to tell the graphics card the size of memory to allocate, XNA does that under the hood once again thanks to the vertex declaration changes, so now we simply tell it the type of vertex data we are sending and the amount of vertices that it will contain. So we change this line in the SetUpVertices() method:
cityVertexBuffer = new VertexBuffer(device, verticesList.Count * VertexPositionNormalTexture.SizeInBytes, BufferUsage.WriteOnly);

cityVertexBuffer.SetData (verticesList.ToArray());

to this:

cityVertexBuffer = new VertexBuffer(device, typeof(VertexPositionNormalTexture) ,verticesList.Count, BufferUsage.WriteOnly);

cityVertexBuffer.SetData(verticesList.ToArray());

Another familiar change, removing the remnants of explicitly setting the VertexDeclaration, remove this line:

texturedVertexDeclaration = new VertexDeclaration(device, VertexPositionNormalTexture.VertexElements);

Now in the Draw() method we need to change the technique that we use to draw the floor with to the new one that we added to the custom shader (effects.fx) that accepts normal data so change this:

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

to new one like this:

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

Now for the changes to the effects API in our Draw() method the lines effect.Begin(); and effect.End(); are no longer necessary!

Whilst iterating over the effect passes the methods pass.Begin() and pass.End() have been simplified into one statement pass.Apply()

As we know it is also no longer necessary to explicitly tell the graphics card the VertexDeclaration as this is done implicitly when we specify the vertex type so the following line can be removed:

device.VertexDeclaration = texturedVertexDeclaration;

Setting the vertex buffer on the graphics cards is simplified

device.Vertices[0].SetSource(cityVertexBuffer, 0, VertexPositionNormalTexture.SizeInBytes);

becomes:

device.SetVertexBuffer(cityVertexBuffer);

The DrawPrimitives call is also simplified

device.DrawPrimitives(PrimitiveType.TriangleList, 0, cityVertexBuffer.SizeInBytes / VertexPositionNormalTexture.SizeInBytes / 3);

this is now:

device.DrawPrimitives(PrimitiveType.TriangleList, 0, cityVertexBuffer.VertexCount / 3);

So all of this means the loop

effect.Begin();
foreach (EffectPass pass in effect.CurrentTechnique.Passes)
{
pass.Begin();
device.VertexDeclaration = texturedVertexDeclaration;
device.Vertices[0].SetSource(cityVertexBuffer, 0, VertexPositionNormalTexture.SizeInBytes);
device.DrawPrimitives(PrimitiveType.TriangleList, 0, cityVertexBuffer.SizeInBytes / VertexPositionNormalTexture.SizeInBytes / 3);
pass.End();
}
effect.End();

is now a much smaller and nicer:

foreach (EffectPass pass in effect.CurrentTechnique.Passes)

{

pass.Apply();

device.SetVertexBuffer(cityVertexBuffer);

device.DrawPrimitives(PrimitiveType.TriangleList, 0, cityVertexBuffer.VertexCount / 3);

}

If you’re using the Reach graphics profile, We need to do something new there also, add the line

device.SamplerStates[0] = SamplerState.LinearClamp;

after pass.Apply() The reason we need to do this is to change the texture address mode to Clamp to allow using texture sizes that are not powers of two.

And that’s it for this tutorial, next up creating the 3D city.

Advertisements

2 thoughts on “Loading the floorplan

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