Saturday, 13 December 2008

XNA Terrain rendering - lesson 3

Riemers tutorial, step 9 is all about the basics of lighting. It starts by adding a normal to each surface. Given a normal, a light source and view point, the amount of light reflected by the surface is computed. A new vertex structure called VertexPositionNormalColored is created to keep the normal. The light source is send to Riemers effect. Riemers next step applies these changes to the terrain.

In my code, the RiemerTerrain component keeps and calculates the light information is kept on the RiemerCamera component.

The final step on Riemer's series is very interesting. It is an optimization. Essentially memory is allocated on the GPU and values are copied to the GPU only once. The call to the graphics primitive is updated to use that memory instead. This means during draw, there is not a lot of communication to the graphics card. A great technique for optimising the rendering of static geometry.

Although Riemers advanced terrain series assumes knowledge of the HLSL series; I will start with it without walking through the HLSL series (I did that one quite a while ago -- and I am now sorry I did not create a blog for it). This means I have to prepare my current code to be aligned with Riemers. This was actually quite easy -- there is a new height map and a new effects file.

The first advanced tutorial step is all about creating a better camera. This was relatively easy -- I created a new component called RiemersFirstPersonCamera by inheriting from the RiemersCamera class created a few days ago. Had to refactor a bit -- and moved the angle relates functionality to a another derivative of RiemersCamera called RiemersRotatingCamera. Quite easily done. Now we have a camera that allows us explore the terrain nicely. The good old WASD with mouse control.

The second step uses a texture to draw the terrain. I copied RiemersTerrain to RiemersTexturedTerrain and found a number of small differences. One notable difference was the removal of the indices and vertexes from the private member list of the class. There is no need to keep a copy of this data since it is generated right into the graphics card memory.

The idea of the texture is relatively simple. When seen from above, each dot on the map maps to some XY coordinate on the texture.

There was one more change that is important from the component perspective. Previously, the camera decided on the current technique of the effect. But now, the current technique is chosen by the terrain component. The primary reason for this change is that the terrain owns the texture, and the texture is a parameter for the technique (albeit implicitly). I am not convinced that this is in fact the best place for technique selection -- maybe I'll change it back later.

Again, the end of another lesson; and we have a nice look and feel. That is a little green patch to look at, and an intuitive camera that creates the feel.

No comments:

Post a Comment