• Create Account

## Normal mapping dependent on view.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

2 replies to this topic

### #1jajcek  Members

274
Like
0Likes
Like

Posted 14 September 2013 - 08:13 AM

Hi,

I've got a problem that normal mapping is dependent on view. It looks like this (sorry for the watermarks): http://www.youtube.com/watch?v=-V_2Pp4kiLM&feature=youtu.be

The normal mapping is calculated in the pixel shader as follows (it is actually taken from http://stackoverflow.com/questions/5255806/how-to-calculate-tangent-and-binormal):

- vertexPosition is a position in SV_POSITION semantic

- shared vertices on in the triangles

float3 computeNormalWithTBN(float3 vertexPosition, float2 texCoord, float3 normal ) {
float3 p_dx = ddx(vertexPosition);
float3 p_dy = ddy(vertexPosition);

float2 tc_dx = ddx(texCoord);
float2 tc_dy = ddy(texCoord);

float3 t = normalize( tc_dy.y * p_dx - tc_dx.y * p_dy );
float3 b = normalize( tc_dy.x * p_dx - tc_dx.x * p_dy );

float3 n = normalize(normal);
float3 x = cross(n, t);
t = cross(x, n);
t = normalize(t);

x = cross(b, n);
b = cross(n, x);
b = normalize(b);

float4 detail = normalMap.Sample( SampleType, texCoord );
detail = (detail * 2.0f) - 1.0f;
detail *= 6.0f;
return normalize( normal + detail.x * t + detail.y * b );
}

// in main function
// ...
input.normal = computeNormalWithTBN( input.position.xyz, input.tex.xy, input.normal;
float light = saturate( dot( input.normal, float3( 0, 0.73, -0.69 ) ) );

float4 color = 0.3f;
color += light;
return color;
}

Why this happens?

Thanks for help.

### #2Hodgman  Moderators

49397
Like
0Likes
Like

Posted 14 September 2013 - 09:24 AM

What coordinate system (aka "space") is the normal in?

In the vertex shader how do you generate the normal?

Your light direction is constant (i.e. float3( 0, 0.73, -0.69 )), so I guess it's in world-space / world-coordinates.

### #3jajcek  Members

274
Like
0Likes
Like

Posted 14 September 2013 - 10:05 AM

Hey, thanks for the interest.

I think it's in world space, but I will explain how it is calculated to not make a stupid mistake.

The normal is calculated on CPU and is send to the VS. To calculate normal for the "5" vertex:

1 2 3
4 5 6
7 8 9

I calculate:

XMFLOAT3 normal;
XMVECTOR v1 = {  2, height_value_of_3 - height_value_of_7, -2 };
XMVECTOR v2 = { -2, height_value_of_1 - height_value_of_9, -2 };
XMStoreFloat3( &normal, XMVector3Normalize( XMVector3Cross( v1, v2 ) ) );
return normal;

VS only passes the normals to the PS as is.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.