Normal Maps


really basic question, I’m afraid:

I’ve been messing around with vertex-displacement mapping to create distorted meshes. I’m now thinking about attempting to light these meshes.

I’ve so far managed get my vertices to be displaced by using a sampler2D in the Vertex Shader etc.
I’ve also worked out how to create a normal map from the same image, which I was planning to pass into the Fragment shader as a separate texture, and use it to apply lighting there.

This is all fine, but the complicating factor is, I’m using the basic parametric formula for a sphere to bend a flat mesh around into a ball, with the luminosity information from the displacement map modulating the radius of the ball, creating this kind of thing (note that in this example, I mirrored the displacement image on the X and Y axes to get a symmetrical effect).

I’m now wondering how I can use the information in the normal map to generate correct normals for lighting my radially-displaced mesh.

Anyone any ideas?


since the above normalmap is in tangent space (i think thats the right term, ie most colors are close to blue, vec3(0,0,1) )
u will need to supply or calculate the normals etc yourself per vertex.

the only way u dont need to calc the normals is if your normalmap is in ‘worldspace’ ie the colors are all the rainbow colors in the vec3(-1,-1,-1) -> vec3(1,1,1) range

Hi zed,

thanks for getting back to me!
Is there any way I can pre-calculate the normals in the correct ranges, based on the displacement texture? Or transform the existing normal map into the correct format within the fragment shader? Maybe using gl_NormalMatrix?


OK, worked it out:
You just need to take the color values of each texel in the Fragment shader, and multiply them by 2, then subtract 1, to get them in the correct range.

Works now


I should have said ‘works now on a plane mesh’.

I’m still not sure if this will work for my mesh that has been distorted into a sphere. I imagine I also have to transform the normals in some way so they’re still accurate.

Or, am I wrong?


This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.