draw a shpere that is a light source

Hello. I’m a new open gl user. I’m using GLUT with C++ at my Ubuntu Linux operating system. I would like to draw a sphere that is also a light source but i also want the light to have a specific range for example 4. How I may do that? Please describe it as detailed as possible because as I mentioned I’m new with all that stuff.
Thank you in advance!!!

i managed to do some things. But I have no idea how to specify the range of the light source.
I saw that glLightf(GL_LIGHTn, GL_SPOT_CUTOFF, angle); // angle is 0 to 180
but this has to do about the angle of projection doesn’t it?
I just want to have a light that just covers 4 units of distance and no more. How is that possible?

Take a look at the description of GL_CONSTANT_ATTENUATION, GL_LINEAR_ATTENUATION, GL_QUADRATIC_ATTENUATION on the man page for glLight. It only works for point and spot lights (directional lights have no position, so there is no way to define distance from them).

I just want to have a light that just covers 4 units of distance and no more. How is that possible?

With fixed-function OpenGL (ie: no shaders), this is not really possible. Even the constant attenuation still goes out to infinity. If you want this kind of light attenuation, you’ll have to use shaders.

You should also know that real lights do not behave like this.

Just to clarify for you, what Alfonse is saying is that there is no GL_DISTANCE_CUTOFF you can just set for fixed-function point light sources.

To crudly simulate, you could use things like clip planes to only render the lit world within ~4 units of the light and apply it. Then render the world further than 4 units of the light and render it without it. But you end up with some overhead here due to some/all batches being submitted multiple times.

Short of that, you can play with the attenuation so the light just seems to falloff around 4 units of the light, even though it is still computed for all pixels.

However, this “point light distance cut-off” concept is easily implemented and with much more flexibility with shaders (once you’ve got your lighting and shading implemented in shaders of course).

Deferred Rendering techniques make it somewhat simpler/cheaper to just light a subset of the world, but you’re talking shaders there as well. What you were talking about with drawing a sphere that is a light source sounds somewhat like you may be talking Deferred. Were you?

So I guess you need to decide, to what fidelity do you need that light cut-off distance implemented, and if it can’t be approximated crudly, are you open to using shaders? There are all sorts of solutions here.

You can try a linear attenuation of 0.25 so that no light after 4 units. This is not a clear cut, as light will go from full to black continuously.
If you need 4 units full bright and everything else dark, then do that with shaders.