PDA

View Full Version : How to implement point lights?



Punchey
02-09-2001, 06:34 AM
I'm wanting to draw a point light. Or, in other words, I'm moving a little light source around and I want it to emmit light around it but not infinitely so. I just want it to cast light on objects that are nearby only. How can this be done in OpenGL?

DFrey
02-09-2001, 12:22 PM
Main thing is to set the light's w value to 1.0 (other values are possible but for simplicity choose 1.0). When w is 0, OpenGL makes the light a directional light. When w is not 0 then it makes the light either a point light (if GL_SPOT_CUTOFF is 180) or a spotlight (if GL_SPOT_CUTOFF is in [0,90]).

Punchey
02-09-2001, 01:07 PM
Great! Thanks! So, real quickly, what's the syntax to set those SPOT_CUTOFF params?
Is it glLightf(GL_LIGHTx,GL_SPOT_CUTOFF,num)?

BTW, it looks like you're becoming my own personal OpenGL tutor. :-) Anyway, thanks!

[This message has been edited by Punchey (edited 02-09-2001).]

[This message has been edited by Punchey (edited 02-09-2001).]

DFrey
02-09-2001, 01:19 PM
If I wanted light 0 to be a spotlight with a 20 degree field I use:
glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 20);

If I wanted light 0 to be a point light, I would use:
glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 180);

180 is the default angle so you normally don't need to set GL_SPOT_CUTOFF for point lights.

And of course the above code works as indicated only when w is not 0.

Punchey
02-09-2001, 02:10 PM
Okay, I tried all of this and found that while doing what you said will make it a positional light, it does not cause the amplitude of the light effect on more distant objects to diminish. The only way I figured out how to accomplish this is to set the GL_QUADRATIC_ATTENUATION to something like 0.001f. Anywhere far from this value lead to undesireable results either too much light or way to little. I suppose this depends on the scaling of your world. But is this the one and only method by which you can achieve lighting dependant upon the distance from the lightsource? Or is there a better way?

Humus
02-10-2001, 04:39 AM
No, attenuation is the way to go.

Punchey
02-12-2001, 07:42 AM
Why should I preffer no attenuation? I need a light, the effects of which grow more weak as the distance from the light to the object increases. I don't want a light that stretches on infinitely. I'm doing an outer space thing and I have a missile that casts light but I don't want the missile to be as bright as a freakin' star so I need the light intensity to fall off as distance grows. What is the reason I would preffer no attenuation and is there a better way to achieve this effect perhaps without GL_LIGHTs?

Eric
02-12-2001, 07:46 AM
Originally posted by Punchey:
Why should I preffer no attenuation?

Humus wrote: "No, attenuation is the way to go" and not "No attenuation is the way to go".

He answered your question: "Is there a best way ?".

The answer is: "No.". And the comment is: "Attenuation is the way to go.".

Regards.

Eric

P.S.: Humus, was it you that asked all those questions to nVidia at SharkyExtreme ???

j
02-12-2001, 07:46 AM
Humus said that attenuation _is_ the way to go.

If quadratic attenuation isn't working very well, you can also factor in linear attenuation.

j

Punchey
02-12-2001, 07:57 AM
Oh, I see now... sorry, I feel like an idiot. As it was once said on this board, show me the opening in the ground so I may get into it. :-) In that case, thanks! I guess that answered my questions... except, what's this about linear attenuation? I couldn't get that to work right when I was trying it. But then again, I probably didn't try a parameter that would work for my case. Also, probably due to poor drivers, I tried my attenuated version and it didn't seem to attenuate AT ALL on my Voodoo3. You think this is yet another example of HORRIBLE OpenGL support by 3DFX?

DFrey
02-12-2001, 09:05 AM
The attentuation model is quadratic in nature meaning it has a quadratic term, a linear term and a constant term. You can set the coefficient for each term.
Oh and here is a method I found handy for determining the terms with little need for fine tuning. Using Paint Shop Pro, (and I would assume Photoshop would work too), I can create the type of gradient I want and save it as a 256 x 1 raw grayscale picture. Then feed that raw data into your favorite statistics package to calculate the quadratic regression and then give the coefficients so calculated to OpenGL (after rescaling them).

[This message has been edited by DFrey (edited 02-12-2001).]

Humus
02-12-2001, 09:23 AM
Originally posted by Eric:
P.S.: Humus, was it you that asked all those questions to nVidia at SharkyExtreme ???


Yes, it was me.
I like their forums, and their "Ask the industry" topics are really nice.

Punchey
02-12-2001, 09:27 AM
DFrey, What sort of statistics packages are there that are good/cheap/free? I don't have one. I suppose I should get one eh?

Humus
02-12-2001, 09:49 AM
Originally posted by Punchey:
Oh, I see now... sorry, I feel like an idiot. As it was once said on this board, show me the opening in the ground so I may get into it. :-) In that case, thanks! I guess that answered my questions... except, what's this about linear attenuation? I couldn't get that to work right when I was trying it. But then again, I probably didn't try a parameter that would work for my case. Also, probably due to poor drivers, I tried my attenuated version and it didn't seem to attenuate AT ALL on my Voodoo3. You think this is yet another example of HORRIBLE OpenGL support by 3DFX?

It could very well be that. I had the same problem with my Matrox G400, and when I mailed them about the problem they did almost not seem to want to fix it. Probably it's because it reduces the performance a lot without a T&L unit, and isn't used by any games AFAIK.

DFrey
02-12-2001, 10:24 AM
I'm not aware of any freeware packages though I'm sure it does exist. I know there is a free 30 day trial of MatLab at http://www.mathworks.com/products/matlab/
However I do not know if the trial is crippled in anyway other than time. I myself would buy an older release of Maple or Mathematica. You can find those for very little money (if you can find them at all). I was lucky and was able to use my student discount for the full version of Maple IV a few years ago. Some cheap scientific calculators could also solve for the quadratic fit, though you'd have to enter all those data points by hand http://www.opengl.org/discussion_boards/ubb/tongue.gif.

Punchey
02-12-2001, 11:31 AM
I wondered if it slows things down... perhaps I'm better off not using it. So if no games use that technique, what technique IS commonly used for things like missiles flying past objects and illuminating them? I havn't been able to figure out lightmapping of complex-shaped objects yet. As for the G400, I'm testing with a G450 right now and it works (albiet I had to get the latest drivers).

Humus
02-12-2001, 12:19 PM
Oh, they finally got it in there? Cool ... it did never work for me, and that was even up to PD6, 6.10 was the last driver I tested before I upgraded to a Radeon.

The common tecnique used is lightmapping, even for the dynamic stuff.

Punchey
02-12-2001, 12:46 PM
Can someone point me in the direction of some good lightmapping info? All I've been able to find is info on how to do lightmapping on flat surfaces such as walls/ceilings/floors. But it seems to me that it would be VERY CPU intensive to properly use a lightmap on a more complex object. i.e. an object with alot of polys that make up a very curved surface. Or do modern game developers only use lightmaps on objects that are relatively flat such as floors?

DFrey
02-12-2001, 01:01 PM
Most modern games only use lightmaps on static geometry. On dynamic models, procedural lighting is typically used, either OpenGL's (or D3D's) lighting model, or a custom solution.

[This message has been edited by DFrey (edited 02-12-2001).]

Punchey
02-12-2001, 01:23 PM
Okay, so that raises the question: is there a way to use more than 8 lights? Is there any sort of trick that can be used to exceed this limit?

harsman
02-13-2001, 01:55 AM
Sure, for each object just enable the 8 lights that contribute most to it (you could do this for each vertex but that would be grossly inefficient). This means you take the lights intensity, distance from object and attenuation into account. You probably won't even see the difference unless you have many tiny lights over a very large object and then you could just partition the object into smaller pieces.

Punchey
02-13-2001, 05:23 AM
But ofcourse then you'd have to constantly be changing the values of the light's position back and forth within a single frame because, AFAIK, you only have the constants GL_LIGHT0-GL_LIGHT7 right? Or are there more than that? Assuming there are not, then if you needed, say, 16 lights, using the technique you described, wouldn't you have to build a list of the most significant lights for each object every frame and then for each object change light position, color, etc.? Wouldn't that be horribly slow? I've heard OpenGL state changes are very, very slow. It seems like I saw an article once about how to get past the limited number of lights in OpenGL... perhaps yours is the technique discussed in the article. I'd have to give it a try.

Elixer
02-13-2001, 08:00 AM
Why do you want to use more than 8 lights? What are you trying to do, have 30 cars all with headlights? http://www.opengl.org/discussion_boards/ubb/smile.gif

harsman
02-14-2001, 06:04 AM
Also remember that on current cards (read: GeForce) triangle performance decreases pretty quickly when you add lots of lights, so if only say four lights conttribute to an object, culling the others might give a speedup. Secondly sorting your objects after material and lights (besides texture) is a good thing.

Punchey
02-14-2001, 06:11 AM
Well, I've got several missiles that cast light... also, I saw an nVidia demo with "unlimited lights" so it said and it looks fantastic! It's a GeForce demo and it used _TONS_ of lights. So I wonder how they did it... D3D? <and everyone's eyes grow large and their hair stands on end>

DFrey
02-14-2001, 06:38 AM
Ahhh, casting an unlimited number of moving lights can be done with lightmaps. You just have to figure out which lightmaps to filter for each light (sometimes easier said than done http://www.opengl.org/discussion_boards/ubb/wink.gif).

Eric
02-14-2001, 06:56 AM
Originally posted by DFrey:
casting an unlimited number of moving lights can be done

Tss tss...

If the number was unlimited, you would never reach the end of the rendering ! http://www.opengl.org/discussion_boards/ubb/smile.gif http://www.opengl.org/discussion_boards/ubb/smile.gif http://www.opengl.org/discussion_boards/ubb/smile.gif

Regards.

Eric

P.S.: sorry...

[This message has been edited by Eric (edited 02-14-2001).]

DFrey
02-14-2001, 07:18 AM
TeeHee http://www.opengl.org/discussion_boards/ubb/tongue.gif