2
votes

I am trying to do normal mapping on flat surface but I can't get any noticeable result :(

My shader

http://pastebin.com/raEvBY92

For my eye, shader looks fine, but it doesn't render desired result( https://dl.dropbox.com/u/47585151/sss/final.png). All values are passed.Normals,tengents and binormals are computed correctly when I create the grid,I have checked that! Here are screens of ambient,diffuse,specular and bump map.

https://dl.dropbox.com/u/47585151/sss/ambient.png

https://dl.dropbox.com/u/47585151/sss/bumpMap.png

https://dl.dropbox.com/u/47585151/sss/diffuse.png

https://dl.dropbox.com/u/47585151/sss/specular.png They seems to be legit... The bump map,which is the result of (bump=normalize(mul(bump, input.WorldToTangentSpace)) definitely looks correct,but doesn't have any impact on end result. Maybe I don't understand the different spaces idea or I changed the order of matrix multiplication.By world matrix I understand the position and orientation of the grid,which never changes and it is identity matrix.Only view matrix changes and represents camera position and orientation in own space.

Where is my mistake?

1

1 Answers

1
votes

First of all, if you're having a problem, it's a good ideo to comment everything out, which doesn't belong to this. The whole lightcomputation with ambient, specular or even the diffusetexture isn't interesting at this moment. With

output.color=float4(diffuse ,1);

You can focus on your problem and see clearly what change, if you change something in you code.

If your quad lies in the xy-plane with z=0, you should change your lightvector, he wouldn't work. Generally I use for testing purpose a diagonal vector (like normalize(1,-1,1)) to prevent a parallel direction to my object.

When I look over your code it seems to me, that you didn't get the idea of the different spaces, as how you thought ;) The basic idea of normalmapping is to give additional information about the surface with additional normals. They are saved in a normalmap, so encoded to rgb, where b is usually the up-vector. Now you must fit them into your 3D-world, because they aren't in the world space, but in the tangent space (tangent space = surface space of the triangle). Because this transformation is more complex, the normal computation goes the other way round. You transform with the normal,binormal and tangent as a matrix your lightvector and viewvector from world space into tangent space (you are mapping the axis of world space xyz to tnb - tangent,normal,binormal, the order can be wrong I usually swap them until it works ;) ). With your line

bump = normalize(mul(bump, input.WorldToTangentSpace));

you try to transform you normal in tangent space to tangent space. Change this, so you transform the view and the lightvector in the vertexshader into tangent space and pass the transformed vectors to the pixelshader. There you can do the lightcomputation in tangent space. Maybe read an additional tutorial to normalmapping, then you will get this working! :)

PS: If youre finished with the basic lighting, your specular computation seems to have some errors, too.

float3 reflect = normalize(2*diffuse*bump-LightDirection);

This line seems to should compute the halfway-vector, but therefore you need the viewvector and shouldn't use a lightingstrength like diffuse. But a tutorial can explain this in more detail than me now.