XNA UK User Group
A helping hand for bedroom coders throughout the land.
Simple Bumped Terrain with Water

Blogs

RandomChaos

Syndication

http://www.youtube.com/v/a5rfeWmJo8w <p><a href="http://www.youtube.com/v/a5rfeWmJo8w">http://www.youtube.com/v/a5rfeWmJo8w</a></p>    

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


Posted Sun, Oct 21 2007 10:39 PM by Nemo Krad

Comments

Jeremy Cook wrote re: Simple Bumped Terrain with Water
on Tue, Oct 23 2007 11:55 AM

Great example.   I am still learning the whole shader thing lol, so having examples of how they are used is very helpful.

Check out the Fracture Website.

www.lucasarts.com/.../fracture

This is the future of terrain!

Nemo Krad wrote re: Simple Bumped Terrain with Water
on Tue, Oct 23 2007 12:05 PM

Fracture does look very pretty :)

Glad you are finding it of some use, I plan to expand this shader later so that is uses the SM3 fog from a previous post.

George (from Argentina) wrote re: Simple Bumped Terrain with Water
on Tue, Oct 30 2007 7:57 PM

Fantastic! Simple, easy to use, and works!

John wrote re: Simple Bumped Terrain with Water
on Wed, Oct 31 2007 7:50 PM

Great Work! but I have a problem, when i use my own Camera class, on the terrain, where have shadows, it seems to be a bit transparent.

Do you know why that happens?

(sorry for my english)

Nemo Krad wrote re: Simple Bumped Terrain with Water
on Wed, Oct 31 2007 8:24 PM

Yes, I noticed this issue when implementing the fog with the terrain in the Simple Bumped Terrain and Water with Fog and it was because I was altering the render states in the fog pass of the shader and not setting it in the first pass.

So you have two options, before you call the draw of the terrain set the render states or set them in the shader.

If you want to put it in the shader then you can do it with this:

DestBlend = One;
AlphaTestEnable = false;
AlphaBlendEnable = false;

Just be for the Vertex Shader is called.

If you want to do it on the CPU (in code) then use this:

base.GraphicsDevice.RenderState.AlphaTestEnable = false;
base.GraphicsDevice.RenderState.AlphaBlendEnable = false;
base.GraphicsDevice.RenderState.DestinationBlend = Blend.One;

If you still have the issue, send me the project and I will have a look.

John wrote re: Simple Bumped Terrain with Water
on Fri, Nov 2 2007 2:07 AM

It Works!!! Thank you!!

technique Terrain_MultiTex

{

   pass P0

   {

DestBlend = One;

AlphaTestEnable = false;

AlphaBlendEnable = false;

vertexShader = compile vs_2_0 BumpVS();

       pixelShader  = compile ps_2_0 BumpPS();

   }    

}

TrackBack wrote http://randomchaosuk.blogspot.com/
on Fri, Jan 4 2008 4:23 PM