Lighting

Hi,

I’m a opengl newbie and I’m having some problems with lighting, and I haven’t found any good lighting tutorials to help me. Anyways I’m making a program on which I have an object(an sphere in this case) and I need for it to emmit lighting(sort like a star) and I need for this lighting to have a range, like after a certain distance the light starts to weaken(fade you know).
I appreciate any help.

Luiz Paulo

Make sure that you go to nehe.gamedev.net and look over tutorial #7.

I don’t have my Bluebook on me right now, but you want to set up the material properties of your sphere to GL_EMISION or something.

I think it is glMaterialfv(GL_FRONT, GL_EMISSION, vector);

Where vector is:
float vector[4];
with each component varying from 0 to 1.

SL

that is the proper function call (vector, by the way, represents the color of the object, in rgba format)

be warned, this does not add to the lighting calculations to any of the other objects. if all you have is an object with a GL_EMISSION component, and no lights, you will be able to see that object, but not any others in the scene. it simulates what the object would look like if it emits light, but does not actually make the object emit light (i think, that it just adds a color component to the object that is unaffected by ambient and diffuse lighting)

if you want the sphere to actually illuminate other objects in the scene, you are going to have to put a point light inside the sphere (dont worry, the light will pass through it, openGL does not take into account other polygons when calculating light, that is why shadows are such a pain : )

to make the light fade out over a distance, you are going to have to set its attenuation. there are three values for the attenuation: GL_CONSTANT_ATTENUATION, GL_LINEAR_ATTENUATION, GL_QUADRATIC_ATTENUATION. set them like you would any other light component:

glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, value); //light0 should be whichever light you want to change

here is the formula that gl uses to calculate the overall attenuation factor:

attFactor= 1/(constantAtt + (linearAtt * distance) + (quadradicAtt * distance squared))

that is gonna be hard to read, but hopefully, you can play around with it and figure it out…

<edit>
GL_CONSTANT_ATTENUATION defaults to 1, the other two default to 0…
</edit>

[This message has been edited by Spiral Man (edited 11-01-2001).]

Hi,

The diffuse light seens to get the job done better. but I'm still having a couple of problems. Like I haven't really been able to get attenuation work the way I want(the light really fading out from a distance). Maybe it's because my last object is at Z -2031.54f?

Also the other objects are getting light all around then, which is not what I want. they should only get light at the side which is facing the light. Can this be done with spheres(which is my case)?
Again thanks for the help.

Luiz Paulo

Originally posted by Luiz Paulo:
[b]Hi,

The diffuse light seens to get the job done better. but I'm still having a couple of problems. Like I haven't really been able to get attenuation work the way I want(the light really fading out from a distance). Maybe it's because my last object is at Z -2031.54f?

Also the other objects are getting light all around then, which is not what I want. they should only get light at the side which is facing the light. Can this be done with spheres(which is my case)?
Again thanks for the help.

Luiz Paulo[/b]

Be careful when rendering the objects.The lighting calculations are done based on the position of the vertices.So depending on the position of the vertex the lighting effect is interpolated gradually .
Just like color effects with smooth shading.