Sign in to follow this  
Medo Mex

Terrain Normal Map

Recommended Posts

Medo Mex    891

I'm trying to apply bump map to my terrain using Pixel Shader.

 

I found a tutorial, but it's using BiNormal and Tangent, I don't have both in my terrain mesh, I only have Position, TexCoord (UV) and Normal.

 

http://rastertek.com/tertut15.html

 

Can someone give me idea on how can I convert the code to use Position, TexCoord and Normal only?

 

Here is my Vertex Shader "IN" structure:

struct VERTEX_IN
{
    float3 Pos : POSITION;
    float2 UV : TEXCOORD0;
    float3 Normal : NORMAL;
};

 

Share this post


Link to post
Share on other sites
Burnt_Fyr    1665

You cannot bump objects in a pixel shader without a tangent and bitangent, as they describe the mapping from tangent space to object space so that normal can be brought from tangent space into object/world/view space.

 

You already have what you need to calculate the tangents and bitangents for your mesh, you just need to do it.

 

http://www.terathon.com/code/tangent.html

 

This describes the math behind process. It's heavy on the linear algebra, but provides source as well.

Share this post


Link to post
Share on other sites
Medo Mex    891

@Burnt_Fyr: I created some lights (such as point light) that use bump mapping with meshes other than the terrain, and it works, I don't use BiNormal or Tangent.

 

So did you mean I can't create bump mapping on any mesh without Binormal/Tangent calculation? Because I already did.

 

BTW, I tried the directional light on my terrain, but it doesn't work on the terrain as expected.

 

So I think probably the terrain bump map should be calculated differently...

Edited by Medo3337

Share this post


Link to post
Share on other sites
Medo Mex    891

After working for sometime, here is what I got

 

I'm NOT sure if this is bump map, if it's bump map, how do I improve it to make it more realistic?

 

[attachment=16999:terrain.png]

Share this post


Link to post
Share on other sites
DwarvesH    510

Hi!

 

That does look like a bump mapping effect, only it is kind of weak. It is hard to tell if it is correct without seeing the textures and the bump map. What kind of light are you using? A directional light?

 

But what I actually wanted to say was that you can compute the the tangents on the CPU like Burnt_Fyr said. I did that for ages. But eventually my terrain got so big and real time terrain editing such a major feature, that doing it on the CPU was a major performance bottleneck. And also RAM use was a major issue. 

 

So now I am approximating them in the vertex shader:

float3 n = normalize(mul(inNormal, world));
float3 c1 = cross(inNormal, float3(0, 0, 1));
float3 c2 = cross(inNormal, float3(0, 1, 0));

// Calculate tangent
float3 t = (distance(c1, 0) > distance(c2, 0)) ? c1 : c2;
float3 b = cross(inNormal, t);

float3x3 tbnMatrix = float3x3(t.x, b.x, n.x,
	                      t.y, b.y, n.y,
	                      t.z, b.z, n.z);

This produces almost identical results with the CPU version and all you need is the terrain normal. It gives the best results for well behaved continuous heightmap. This is also my not too clever way of asking for feedback on this computation smile.png.

Share this post


Link to post
Share on other sites
Medo Mex    891

@DwarvesH: Yes I'm using Directional Light.

 

Basically, I'm trying to create commercial quality terrain, how do I make the bump map look realistic? I just applied a random bump map to the terrain in the screenshot above.

Share this post


Link to post
Share on other sites
Migi0027    4628

Well, to make it even more realistic, you could try to implement Parallax Occlusion mapping.

 

An example of that would be that you have a small area of dirt, and you wanted some wheel tracks, without adding more vertices, indices...

 

I made a small experiment with it a while ago, you can see it here if you wish: (Just 4 vertices and 6 indices. A plane basically)

 

http://cuboidzone.wordpress.com/2013/05/10/pom-parallax-occlusion-mapping-cuboid-engine/

Share this post


Link to post
Share on other sites
Migi0027    4628

And to make it look more realistic, you could generate a bump map for each texture layer.

 

I know that several bump map generators exist, just don't remember their names.

 

Too lazy to Google it right now. happy.png

Share this post


Link to post
Share on other sites
DwarvesH    510

I would start with something simple, like http://www.dhpoware.com/demos/xnaTerrainNormalMapping.html.

 

After you fully understand that, going from those simple shaders to something really complicated that makes your normals pop will take you quite a while, but it is easier if you start with the basics.

 

Obtaining something of "commercial quality" will take you months at least, and something like FarCry 3 years. I'm slowly getting there: http://www.youtube.com/watch?v=5A6BEEeGnLw

 

So maybe you should look into an existing engine so that you can concentrate on the gameplay. Or maybe you do wish to write an OKish 10-25k lines of code terrain engine. 

Share this post


Link to post
Share on other sites
Medo Mex    891

How do I make the Directional Light EVERYWHERE on the terrain? I want the terrain to show light everywhere so the bump map never disappear.

Edited by Medo3337

Share this post


Link to post
Share on other sites
DwarvesH    510

AFAIK, normal mapping only works where the diffuse component of the light, the one that caries the bump effect is not zero, so it doesn't work on polygons that are facing away or are almost facing away from the light source.

 

You can use ambient bump mapping approximation or spherical harmonics that sample the normal map, but I don't think this is necessary for terrain. In most of the cases, you won't have faces are perpendicular on the direction of the light and you will have a non-zero diffuse contribution as long as the light source is not very low.

Share this post


Link to post
Share on other sites
pbobzebuilder    387

For a terrain & if your normal map is computed from a heightmap, you can use world space normal maps. Your normalmap will only be used on the terrain (tangent space allow you to use your normal map on any geometry).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this