Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Monder

OpenGL Replicating the OpenGL lighting equation

This topic is 5312 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
Well I''m setting all material parameters using glMaterialfv, so I believe I set up the material stuff correctly

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!