I am implementing normal map bump mapping and until now everything looked fine. My light source is howering above my scene of four objects, diffuse looks good, specular and bumps ditto. When I apply rotation to the scene diffuse and bumps are ok but the specular highlight starts wondering around in wrong places. I am pretty sure that I mess up my eye direction vector (which is used to calculate halfangle and later on specular contribution), I just can’t figure out how. I provide eye position to vertex shader in world coordinates and calculate eye_dir = eye_pos - gl_Vertex.xyz.
I appreciate any comments on this, as I’m sure I’m not the first poor sod going nuts over this
Sounds like your mixing coordinate systems.
gl_Vertex is in object coordinates (model space).
OpenGL skips “world” coodinates and modelview goes directly to eye coordinates (eye in the origin).
The vector from vertex to eye in that coordinate system is -(gl_ModelViewMatrix * gl_Vertex).xyz.
There are some gotchas with vertices behind the viewer (negative w after transformation) when simplifying it this way.
Did you transform the normals correctly?
thank you for replying. If I use -(gl_ModelViewMatrix * gl_Vertex).xyz as my eye_dir the specular lighting goes wrong. It’s almost correct but not quite. I must be doing something wrong elsewhere as well, perhaps fumble up the normals as you suggested.
If just before rendering an object, after all the transforms done, I glGet the modelview matrix and compute the ‘worldspace position of eye’ and provide that to uniform eye_pos, specular highlights are correct. Still, I would rather not use this method I suspect is a cheap patch for an error elsewhere. Also if I have hundreds of objects, the fetching and inversing modelview will provide unnecessary overhead.
I think it would not be a bad idea to get Eric Lengyel’s book ‘Mathematics for 3D game programming and computer graphics’
I’d do the lighting stuff in the fragment shader: it’s the common place for it.
And use gl_LightSource[i] structures instances for the half vector and such. Doing so you won’t have to multiply the light position by the modelview matrix and the eye direction will always be the same (0,0,-1).