Thursday 28 June 2012

Simple Bumped Terrain with Water







Right, decided to move this component from the engine and into my Generic XNA section, I know what you are thinking, "I have gone through all your bump mapped terrain stuff, I don't need this as a generic drawable game component!!", well that may be the case, but I have put a few fixes in here to do with the terrains tangent generation and also some of the lighting in the terrain shader I wrote. So you might find it of some use after all. I have also tidied it up, the one in the engine does the job but I really did leave it in a right mess.

As to the Ocean code, I have included it in here as I have updated the class so that this too is a full drawable game component.
OK, on to what I have done here. I found that the way the terrain use to calculate its Tangent data, in some lighting conditions, you would get an odd effect where the edge of your terrain would get lit brighter than anywhere else. This was because the loop that generates the Normal and Tangent data starts at 1 and ends at the the terrain width/height -1 in both the x and y loops. So what happens is you end up with all your vertices's at the edges having crappy tangent/normal data and so get this odd edge effect which looks like this.




As you can see in this image the edge is far too bright.

Also the book I got the shader code from was quite old and used the old DirectX draw order and so used the Z as the Up/Down axis and the Y as the depth axis, this lead to some odd lighting (IMhO) so I "think" I have fixed this in the shader by just altering the lighting swizzle.

I have also tidied up the PickTerrain method and made it a little neater to read and a little bit more usable (from a user point of view). So you could now use this functionality so your player or even you can graphically create your own terrain and see it exactly as it is rendered.

As I said before I have also reworked the Ocean class so that it is now a drawable game component. The only thing you have to really look out for is the order you add the objects to the Component list if you want to use transparent water. Just make sure you set the draw order higher than the terrain so that the terrain is drawn first. Also cleaned up some of the render state settings used to get the transparency, turns out I did not need half of them...

So now you have terrain class that can be dropped into any XNA project, and an ocean shader that is now pretty much self autonomous.

Controls:
WSAD - Translate Camera
Mouse - Rotate Camera
Left Click - Raise Terrain
Right Click - Lower Terrain
F1 to F12 - alter Ocean parameters
F1 & F2 - Switch Alpha On/Off
F3 & F4 - Alter Ocean Color
F5 to F10 - Vary wave amplitude, frequency and bump height
F11 & F12 - Sparkle On/Off
Esc - Exit

GenericXNAExampleTerrain.zip

No comments:

Post a Comment