Replicating the OpenGL lighting equation

Started by
4 comments, last by Monder 20 years, 3 months ago
In my 3D engine I''m planning to offer several lighting options. In one of them OpenGL will do the lighting and in another I do it manually by setting vertex colours before sending all the data off to OpenGL. Now as I want these two lighting methods to look the same I tried to clone the OpenGL lighting equation for my manual method. However when performing lighting using the two methods the manual one looks about twice as bright as the OpenGL one. I''m fairly sure the manual calculations that set the vertex colours individually are the same as the OpenGL ones, though mine do not include attenuation, I have set GL_CONSTANT_ATTENUATION set to 1.0 and GL_LINEAR_ATTENUATION and GL_QUADRATIC_ATTENUATION set to 0.0 for the light. Are there any gotchas I should watch out for that may be causing the difference between the two lighting methods? For example does OpenGL do anything to the colour array before applying lighting?
Advertisement
are you taking into account materials at all? Default material reflectance for ambient lights is (0.2, 0.2, 0.2, 1.0) and default material reflectance for directional lights is (0.8, 0.8, 0.8, 1.0) so these may be adjusting your colours when using OpenGL lighting.

edit: typo!

[edited by - FReY on January 5, 2004 4:25:39 AM]
do unto others... and then run like hell.
Well I''m setting all material parameters using glMaterialfv, so I believe I set up the material stuff correctly
After a quick google search I found the following in a .pdf assignment paper, get it from here: http://www.ics.uci.edu/~gopi/ICS186AW03/lab5.pdf

The OpenGL equation for lighting is:

Vertex color = (material emission) + (global ambient)*(material ambient) +
Sigma(1/( k1+ k2*d + k3*d^2)) * [(light ambient)*(material ambient) + (max{L.N, 0})*(light
diffuse)*(material diffuse) + (max{H.N,0})^n *(light specular)*(material specular) ]

where H is your half vector for specular lighting and n is your specular power (or shininess in this case). d seems to be the distance of your vertex from the light and k1,k2,k3 are attenuation factors. I think the sigma is there for all the possible lights in the scene.

So if you''ve implemented this equation exactly then I have no idea of what the problem is. Maybe you could get clues from reading Chapter 5 of the Red book.
do unto others... and then run like hell.
Well when setting all the light colours to 0 apart from the diffuse colour which I set to 0.5, 0.5, 0.5, 1.0 (RGBA) my implementation gives an unevenly lit model (which is what it should be for diffuse light) but OpenGL gives an evenly lit model. I've set GL_LIGHT_MODEL_LOCAL_AMBIENT to 0.0, 0.0, 0.0, 1.0 so ambient light can't be affecting it. And it won't be a driver bug as I'm using the latest NVidia drivers.

Is there any specific order you have to specify parameters in and enable lights? For example do you have to specify light parameters before enabling the light?

[edited by - Monder on January 5, 2004 10:57:09 AM]
AHHHHHH!!

Found the problem I was specifying a directional light, not a point light, I thought 1.0 as the W coord of the light position specified a point light and 0.0 was directional, but it''s the other way round. Ahh well thanks for the help guys.

This topic is closed to new replies.

Advertisement