Advertisement

Cg Per Pixel Lighting - wrong lighting

Started by November 04, 2004 08:46 AM
20 comments, last by RipTorn 19 years, 11 months ago
Sorry for bringing this topic up again, I'm aware it's old. However, here's the solution (short version - if you don't have matrix class and everything).

Light and eye position should be transformed from world to camera space, as RipTorn said. Eye position is always (0, 0, 0).
Light is transformed that way: light_pos = matrix * (light - camera)
where 'light_pos' is light position in camera space, 'matrix' is modelview matrix, 'light' is light position in world space and 'camera' (eye) is camera world space position.

gluLookAt (cx, cy, cz,...);glGetFloatv (GL_MODELVIEW_MATRIX, matrix); // float matrix [16]// float n [3]: holds '(light - camera)'n [0] = light [0] - cx;n [1] = light [1] - cy;n [2] = light [2] - cz;// float light_pos [3]: camera space light positionlight_pos [0] = matrix [0] * n [0] + matrix [4] * n [1] + matrix [8] * n [2];light_pos [1] = matrix [1] * n [0] + matrix [5] * n [1] + matrix [9] * n [2];light_pos [2] = matrix [2] * n [0] + matrix [6] * n [1] + matrix[10] * n [2];cgGLSetParameter3fv (light_position, light_pos);cgGLSetParameter3fv (eye_position, eye); // float eye [3]: eye = 0
indeed that should work.

effectivly your doing:

pass in data for matrix M into glLookAt, sets modelview matrix to inverse of M.

so your subtracting M.position (12,13,14) and then multiplying by transpose(M as 3x3)... so yeah. should work fine :)

the mathematical purist in me (which is on holiday at the moment) would normally be throwing a tantrum but who cares.

Thats nice to know everything has worked out well. Will be nice to see what becomes of this [smile] - although thats never going to happen on this laptop.... [crying]

This topic is closed to new replies.

Advertisement