Friday 12 December 2008

XNA Terrain rendering - lesson 2

So far, so good. New we move to step 4 of Riemer's excellent tutorials. During this step, the triangle coordinates are adjusted and the camera is introduced.

So, what I did is create a RiemersCamera component that implements the camera. This is a drawable component, and when it draws, it sets up the view and projection of the effect. The important lesson here is that the order in which the components are created is significant. Create the camera first -- it sets up the effect. Components that follow the camera uses this effect. This way there is no reason for the other components to be aware of the camera. Neat!

A disadvantage of these two components is that they share an effect. The effect has only one instance, and changes to the effect is propagated from one control to another.

To mitigate this situation, I created a base control RiemersEffectComponent that encapsulates the loading of the effect. At least the process coupling is a bit more explicit.

Step 5 of Riemer's series is about rotating and translating the world matrix. Easily done on the RiemersCamera. And step 6 is all about introducing DrawUserIndexedPrimitives. For this I created a new component called RiemersIndices, starting with a copy of ReimersTriangle.

At last in step 6 we get a glimpse of terrain. Although the final drawing is not much. The idea is the following: the terrain is a number of dots, organised in a equally spaced matrix. Each dot has Y-value that indicates its height. The dots are then connected to draw the terrain. You guessed it -- all this is done in the RiemersTerrain component.

Step 7 shows how the simple code in the previous steps becomes much more interesting. The only change is to load the height data from a heightmap. Step 8 gives a bit more interactivity and allows you to control the view via the keyboard.

The last two steps raised an issue for my components. The camera controls the view, but the terrain knows its size. So I added a TranslationMatrix as a public property to the RiemerCamera. Then passed the camera to the constructor of RiemersTerrain. After the terrain loads the heightmap, it adjusts the view by specifying the tranlation matrix value.

Step 8 adds colour. To determine the water levels, the height of the heightmap is determined and releative water levels are set. There is also a need to clear the Z-Buffer (However, on my machine I did not notice the anomaly that is mentioned in the tutorial).

This is a good place to end the lesson. We have moved from triangles to heightmappped wire frame to a basic coloured terrain. Some simple game components are taking form, thanks to Riemer!

No comments:

Post a Comment