Sign in to follow this  

Light direction for Bumpmapping

Recommended Posts

Hello, I have created a universe, the sun is at 0/0/0, and 30 planets rotating around it. Now I added some DOT3-Bumpmapping, but I am not sure about the correct direction. So is the following code correct, when 0/0/0 is the sun which is the light source, and planet[i] is the current planet?
        m_vLight.x := planet3d[i].x;
        m_vLight.y := planet3d[i].y;
        m_vLight.z := planet3d[i].z;
        D3DXVec3Normalize(m_vLight, m_vLight);
        dwFactor := VectortoRGBA(m_vLight, 0.0);
        SetTexture(0, planet3d[i].bumptexture);
        SetRenderState(D3DRS_TEXTUREFACTOR, dwFactor);
        SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
        SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_TFACTOR);
        SetTextureStageState(0, D3DTSS_COLOROP,  D3DTOP_DOTPRODUCT3);
Thanks a lot, Firle

Share this post

Link to post
Share on other sites
The light vector needs to be in the same space as the normals encoded in your normal map texture. The dot product operation doesn't make any sense for lighting if the vectors aren't in the same space (or at least a related space).

So if the normals in the normal map are in object space, your light vector should be in object space too.

Light vectors are usually specified in world space.

To rotate the light vector from world space into object space, multiply it by the inverse of the top left 3x3 portion of the world matrix used for the object ("world matrix" really means "object space to world space matrix").

Tip: if the world matrix for your object is only made up from rotations and translations, transposing it is the same (but cheaper) as inverting it.

Only the top left 3x3 part of the matrix should be used to transform the light vector because the vector represents a direction rather than a position.

If the normals in the normal map are in a different space, you must transform the light vector into that space (if that space is tangent space, then the vector will be different for every vertex so you'll need to transform it on a per vertex basis).

Also, do remember that the light vector should be negated (i.e. for a directional light representing the sun, the vector should point towards the sun, not from it).

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