View Full Version : Lighting calculation - changing

03-05-2002, 06:19 AM
Is it possible to change the lighting calculation performed by OpenGL? I'm trying to simulate sensor responses that have an entirely physics based model. The model requires that the sensor response be a complex function of the incidence angle.

Any ideas?

03-05-2002, 07:21 AM
Chapter 5 of the redbook deals with lighting. At the end of the chapter, it goes into the maths behind OpenGL's lighting model. You can look at it to see if it has what you want.

03-05-2002, 11:41 PM
I am familiar with the lighting equation used by OpenGL. Neglecting emmission and ambient properties it uses the dot product of the normal vector at the vertex and the relative direction of the light for diffuse or the relative direction of the viewpoint for specular components. This is then attenuated based on the constant, linear and quadratic attenuation factors.

The model I am trying to simulate requires that the intensity of the light from a material is of a form ~ A(theta+C)^B where theta is the incidence angle and A, B and C are different for each material/object.

The question is can this be changed, or will other 'tweaks' have to be used to get the desired effects.

If tweaks are required is it possible to access the normal vectors of an object per polygon in order to change, say, the emissive properties to give the required intensity dynamically calculated each frame.

03-06-2002, 05:28 AM
Lighting intensity is also affected by the length of the normal used. Typically normals should be of length 1, but I've noticed that if you set it longer the intensity increases. I don't know if this is a bug or intentional but you can use this in limited circumstances to get the desired effect. It should work if you have one light source, but if you have more than one, they both will act on the same normal.

Then again you could design your own lighting model http://www.opengl.org/discussion_boards/ubb/smile.gif

03-06-2002, 05:53 AM
I only have one light source - being the sensor antenna. How do you access the normals of an object in order to change their length?

Writing a new lighting model? Sounds interesting but it can't be a quick job. How would you even start such a task?

03-06-2002, 06:36 AM
Since you have a need for a completely different model, it would seem the only solution is for you to implement it rather than trying to fake it with the OpenGL model.
If you understand the OpenGL lighting model, it shouldn't be all that hard to create your own.

03-06-2002, 06:54 AM
Hmmm. OK, so I see I'm going to have to buy a new pile of coffee for the next week/month's work.

I understand how the light calc works but I am not getting how I can change it. I don't see what I could change in the lighting calc.

How could you go about doing this?

03-06-2002, 07:35 AM
Maybe I should be clearer.

Are you saying that I should redefine the mathematics of lighting as the red book calls it? Or that I should change the material and lighting properties?

I don't see that changing the lighting properties/materials etc can do what I want. So can you change the maths in model or replace the model entirely?

03-06-2002, 07:43 AM
Yes, I believe what DFrey is saying is that you can effectively replace OpenGL's lighting by doing your own calculations in software. Then supply the computed colour in a glColor*() call I would presume.
Of course, I could have misinterpreted what DFrey said.

03-06-2002, 08:28 AM
I only have one light source - being the sensor antenna. How do you access the normals of an object in order to change their length?

When drawing stuff with lighting you have to specify at least one normal for each surface. This is done with glNormal(). You're supposed to enter a normalised vector (e.g glNormal3d(0, 1, 0)) but I've successfully entered non normalised vectors as well (e.g. glNormal3d(0, 5, 0) and get what appears to be a reflected intensity that's greater than expected (in the example this was 5 times expected).

So theoretically, with one light source, you would just multiply your regular normal by your expected intensity to get the desired result
vector v = (0, 1, 0)
double Intensity = 5
Normal = v * Intensity // = (0, 5, 0)

03-06-2002, 08:32 AM
OpenGL's lighting model is per vertex. So you can basically replace it with anything you can imagine by disabling OpenGL light alltoghether, calculating a alight intensity i (according to your own model) at every vertex and submitting the color (i,i,i) along with the vertex.

If you have hardware capable of vertex shaders, you might want to have a look into that as well. Performance would be greatly increased in a hardware implementation - though I'm not sure whether power functions are available in vertex shaders.

03-07-2002, 01:55 AM
If the light position changes, ie the sensor moves, then the colors would have to be recalculated as a function of the vertices and the light/sensor position. How can I access the vertices in order to recalculate the colour?

03-07-2002, 05:50 AM
Redraw the scene, using the updated light position.

03-07-2002, 06:47 AM
That's not quite what I'm asking, perhaps I phrased my question badly.

I can use glNormal and glColor to set the vertices and the colour but how do I find out the normals in the first place? I've looked at a few examples on the web and the lighting model looks easy enough to implement if I know the normals and hence the angles and so forth between the light and the normal and the observer/viewpoint.

Maybe I'm missing something, I don't know the vertex normals as I didn't set them its done when the objects are loaded in.

Cheers so far guys.

03-07-2002, 07:35 AM
search the forum, calculating normals is a really common question

03-07-2002, 09:08 AM
Back to lighting intensity. I think we were wrong about doing your own lighting calculations or messing with the length of normals. Based on this post http://www.opengl.org/discussion_boards/ubb/Forum3/HTML/005763.html in the advanced forum, you can specify the desired intensity. The resulting colour only gets clamped after ligthing calculations are performed for a colour. That makes sense since your monitor can't show greater than one anyway.

Oops, forgot what the original post was about. Sorry.

[This message has been edited by Furrage (edited 03-07-2002).]

03-07-2002, 11:26 PM
Cheers guys I'll go try some stuff http://www.opengl.org/discussion_boards/ubb/smile.gif

03-08-2002, 03:19 AM
thelamberto, if you're supplying the normals to OpenGL you obviously have access to them, so using them to calculate lighting using your own algorithm shouldn't be a problem. If the viewpoint changes, you'll need to recompute the lighting. That's what OpenGL does as well. If you don't know how lighting is implemented internally in OpenGL i suggest you read this tutorial (http://www.flipcode.com/tutorials/tut_swlighting.shtml) .