Advertisement

Directional Lighting - I'm VERY confused!

Started by December 29, 2003 11:20 AM
5 comments, last by DalTXColtsFan 21 years, 2 months ago
OK, in a demo I'm working on I'm just trying to set up a simple directional light - I've drawn a city and I just want a light shining in a 45-degree angle in the direction of (1.0, -1.0, 0.0), that is, along the X axis but from above, meaning if the X axis pointed east it would be about 3 o'clock in the afternoon and the sun was on its way down. Shouldn't I be able to use a directional light for that? Here's what MSDN says about GL_POSITION and GL_SPOT_DIRECTION: GL_POSITION The params parameter contains four integer or floating-point values that specify the position of the light in homogeneous object coordinates. Both integer and floating-point values are mapped directly. Neither integer nor floating-point values are clamped. The position is transformed by the modelview matrix when glLight is called (just as if it were a point), and it is stored in eye coordinates. If the w component of the position is 0.0, the light is treated as a directional source. Diffuse and specular lighting calculations take the lights direction, but not its actual position, into account, and attenuation is disabled. Otherwise, diffuse and specular lighting calculations are based on the actual location of the light in eye coordinates, and attenuation is enabled. The default position is (0,0,1,0); thus, the default light source is directional, parallel to, and in the direction of the –z axis. GL_SPOT_DIRECTION The params parameter contains three integer or floating-point values that specify the direction of the light in homogeneous object coordinates. Both integer and floating-point values are mapped directly. Neither integer nor floating-point values are clamped. The spot direction is transformed by the inverse of the modelview matrix when glLight is called (just as if it were a normal), and it is stored in eye coordinates. It is significant only when GL_SPOT_CUTOFF is not 180, which it is by default. The default direction is (0,0,–1). Now, I interpreted the bolded portion to mean that if that fourth parameter is 0, the first three parameters are a vector, if it's 1 they're a location. The book "OpenGL Game Programming" seems to confirm my suspicion. Is GL_SPOT_DIRECTION completely ignored when this 4th parameter is 0? click here for a directional light screenshot If you look at the above screenshot, you'll see that I'm trying to set up a light that shines on all surfaces pointing upward or to the left, but *not* any surfaces pointing downward or to the right. That's why my SPOT_CUTOFF is 90. Is SPOT_CUTOFF also completely ignored when the 4th parameter of GL_POSITION is 0? OKAY In the process of taking all the screenshots and making sure I'd dotted all my is and crossed all my ts before posting this, I finally figured out how to make it do what I want: Click here for the one that works I guess what was confounding me is that if SPOT_CUTOFF isn't 180, directional lights seem to be useless, also, you have to specify your vector in the OPPOSITE direction that you want the light to go. I don't know why that is. hfueaklvhueaffe - in case you don't recognize that word it's somebody getting very, very confused! BTW if you think that modelling tool looks interesting and you have MFC it's downloadable here: Click here for the code Love means nothing to a tennis player

My nothing-to-write-home-about OpenGL webpage. (please pardon the popups!)

[edited by - DalTXColtsFan on December 29, 2003 12:24:20 PM] [edited by - DalTXColtsFan on December 29, 2003 12:27:09 PM] [edited by - DalTXColtsFan on December 29, 2003 12:27:57 PM]
hey friend!...don''t know if could solve some basic subjects in idrectional lihgting and spoting on opengl, but anyway here take a look at the TEXTS/Tutorials corner on this page:

http://hansa.planet-d.net

not too much but thought can aid you
Yep ;)
Advertisement
grrrrrr tripod won''t let me link you straight to a screenshot >

Just click my sig and click the second demo down, I slapped the 2 screenshots on an HTML page.

Love means nothing to a tennis player

My nothing-to-write-home-about OpenGL webpage. (please pardon the popups!)

OK, lets see if we can sort this out.

First, read the section from MSDN on GL_POSITION that you highlighted in bold again:

The default position is (0,0,1,0); thus, the default light source is directional, parallel to, and in the direction of the –z axis.

Notice that a position of (0, 0, 1, 0) gives a light in the direction of (0, 0, -1), the opposite of what you specified. This is because the situation is not simple as it is usually described. There is no special processing that means that lights with a positional w-coordinate of zero are directional and lights with a non-zero w-coordinate are positional. The directional nature of lights with zero w-coordinates is a natural consequence of homogenous coordinates. With homogenous coordinates a vertex specified at position (x, y, z, w) lies at position (x/w, y/w, z/w). When w is zero this position is infinitely far away, in direction (x, y, z). If you don''t believe it, try picking an x, y and z and seeing what position you end up with using w coordinates of 1, 0.1, 0.01, 0.001, e.t.c.

This means that the light will be infintely far away from any point in space, and still in direction (x, y, z), so any light falling on the scene arrives from direction (x, y, z) and so appears to shine in direction (-x, -y, -z).

Now, if you want a spotlight you specify the direction with GL_SPOT_DIRECTION, but with a directional light this will not gain you anything. To see why, remember that a directional light is infinitely far away from any point in space, in the same direction. If the GL_SPOT_DIRECTION is not the same direction as the direction from the light to the scene, then it is possible that the scene will not be lit at all (I don''t know how OpenGL handles the maths for this, so I''m not entirely sure what it''ll do - the important thing to realise is that a directional spotlight is generally nonsensical).

Hope this has helped and hasn''t just left you even more confused!

Enigma
It helps a little. I''m still struggling to figure out why if GL_SPOT_CUTOFF is anything other than 180 nothing gets lit.

I took (and TAUGHT as a matter of fact) calculus, and therefore understand the "limit" concept you spoke of, i.e. .01 .001 .0001 for w while x, y, and z are, for example, 2, 4, and 8 - it wouldn''t simply be infinity, infinity and infinity because there are "different ways to get to infinity" and there would therefore always be a ratio of -2, -4, -8. I didn''t word that well because I don''t want to TOTALLY relive those nightmares but I think I see what you''re getting at.

I GOT IT!!!!!!!!!

I couldn''t figure out why anything other than 180 for the spot cutoff didn''t light up a darned thing, but when you explained to me that a directional light is actually a positional light that''s infinitely far away, what I did was set my "directional light" to (-1, 1, 0, 0), and then set the GL_SPOT_DIR to (1, -1, 0, 0) and the SPOT_CUTOFF to 90

AND IT WORKED . WOOHOO

So, does it seem like you helped me more than you confused me????

Love means nothing to a tennis player

My nothing-to-write-home-about OpenGL webpage. (please pardon the popups!)

The SPOT_CUTOFF won''t affect anything if spot direction = -spot position for a directional light, because the angle from the spot direction will always be zero. I suspect if you change the spot direction slightly you''ll find a SPOT_CUTOFF value such that at or above that value all geometry is lit and below that value no geometry is lit.
diagram:
  spot_direction  ¬ /* --- ... -----x

x is a vertex, * is a directional light at position (-1, 0, 0, 0), the spotlight direction (shown by the arrow) is (1, 1, 0).
For this situation a SPOT_CUTOFF of >= 45 should result in x being lit and a SPOT_CUTOFF of < 45 should result in x being unlit. Note - this is untested. If you try it I''d be interested to know if it works!

Enigma
Advertisement
mayber it''s just me but I can''t see GL_SPOT_DIRECTION/GL_SPOT_CUTOFF, etc, having any effect on an infinite light whatsoever... (at least except from darkening the entire light)

| - My (new) little website - | - email me - |

This topic is closed to new replies.

Advertisement