PDA

View Full Version : Lighting for a newbie...



Abened
23-10-2003, 10:16 AM
I tried to add lighting in a GLXTreem project but there are some things I don't understand.
First, if i increase the ConstantAttenuation variable, i don't see a thing.
That's the same with the 2 others (i tried to set them to 0.001 but it didn't changed a thing).
So how to use theese variables and what are they supposed to do ?

Then, i've got only one light wich is located at the same position as the camera. I don't understant why some walls are lighted and others don't. See the screenshot :
http://perso.club-internet.fr/jbe/GLXL.jpg
Why is a far wall lighted up instead of those which are near the light ?

Finally, i can't get an ambient light to light up every side of my map:
If set positional to false, and the poisition (which is in this case the direction ?) is set to 1,1,1. In this case the wall which are in fact at -1 aren't lighted up. (this isn't clear i know... sorry)

Answers anyone ?

Abened

Andreaz
23-10-2003, 05:39 PM
Okey, lighting is avery big subject, i'll start with a section from a opengl document. This explains how the opengl lighting is computed, will add the most important things after the document.

Real−World and OpenGL Lighting
When you look at a physical surface, your eye?¢_Ts perception of the color depends on the distribution of
photon energies that arrive and trigger your cone cells, as described in "Color Perception." Those
photons come from a light source or combination of sources, some of which are absorbed and some of
which are reflected by the surface. In addition, different surfaces may have very different propertiesA_
some are shiny, and preferentially reflect light in certain directions, while others scatter incoming light
equally in all directions. Most surfaces are somewhere in between.
OpenGL approximates light and lighting as if light can be broken into red, green, and blue components.
Thus, the color of light sources is characterized by the amount of red, green, and blue light they emit,
and the material of surfaces is characterized by the percentage of the incoming red, green, and blue
components that are reflected in various directions. The OpenGL lighting equations are just an
approximation, but one that works fairly well and can be computed relatively quickly. If you desire a
more accurate (or just different) lighting model, you have to do your own calculations in software. Such
software can be enormously complex, as a few hours of reading any optics textbook should convince
you.
In the OpenGL lighting model, the light in a scene comes from several light sources that can
individually be turned on and off. Some light comes from a particular direction or position, and some
light is generally scattered about the scene. For example, when you turn on a light bulb in a room, most
of the light comes from the bulb, but some light comes after bouncing off one, two, three, or more walls.
This bounced light (called ambient) is assumed to be so scattered that there is no way to tell its original
direction, but it disappears if a particular light source is turned off.
Finally, there might be a general ambient light in the scene that comes from no particular source, as if
it had been scattered so many times that its original source is impossible to determine.
In the OpenGL model, the light sources have an effect only when there are surfaces that absorb and
reflect light. Each surface is assumed to be composed of a material with various properties. A material
might emit its own light (like headlights on an automobile), it might scatter some incoming light in all
directions, and it might reflect some portion of the incoming light in a preferential direction like a
mirror or shiny surface.
The OpenGL lighting model considers the lighting to be divided into four independent components:
emitted, ambient, diffuse, and specular. All four components are computed independently, and then
added together.
Emitted, Ambient, Diffuse, and Specular Light
Emitted light is the simplestA_it originates from an object and is unaffected by any light sources.
The ambient component is the light from that source that?¢_Ts been scattered so much by the environment
that its direction is impossible to determineA_it seems to come from all directions. Backlighting in a
room has a large ambient component, since most of the light that reaches your eye has bounced off
many surfaces first. A spotlight outdoors has a tiny ambient component; most of the light travels in the
same direction, and since you?¢_Tre outdoors, very little of the light reaches your eye after bouncing off
other objects. When ambient light strikes a surface, it?¢_Ts scattered equally in all directions.
Diffuse light comes from one direction, so it?¢_Ts brighter if it comes squarely down on a surface than if it
barely glances off the surface. Once it hits a surface, however, it?¢_Ts scattered equally in all directions, so
it appears equally bright, no matter where the eye is located. Any light coming from a particular
position or direction probably has a diffuse component.
Finally, specular light comes from a particular direction, and it tends to bounce off the surface in a
preferred direction. A well−collimated laser beam bouncing off a high−quality mirror produces almost
100 percent specular reflection. Shiny metal or plastic has a high specular component, and chalk orcarpet has almost none. You can think of specularity as shininess.
Although a light source delivers a single distribution of frequencies, the ambient, diffuse, and specular
components might be different. For example, if you have a white light in a room with red walls, the
scattered light tends to be red, although the light directly striking objects is white. OpenGL allows you
to set the red, green, and blue values for each component of light independently.
Material Colors
The OpenGL lighting model makes the approximation that a material?¢_Ts color depends on the
percentages of the incoming red, green, and blue light it reflects. For example, a perfectly red ball
reflects all the incoming red light and absorbs all the green and blue light that strikes it. If you view
such a ball in white light (composed of equal amounts of red, green, and blue light), all the red is
reflected, and you see a red ball. If the ball is viewed in pure red light, it also appears to be red. If,
however, the red ball is viewed in pure green light, it appears black (all the green is absorbed, and
there?¢_Ts no incoming red, so no light is reflected).
Like lights, materials have different ambient, diffuse, and specular colors, which determine the
ambient, diffuse, and specular reflectances of the material. A material?¢_Ts ambient reflectance is
combined with the ambient component of each incoming light source, the diffuse reflectance with the
light?¢_Ts diffuse component, and similarly for the specular reflectance and component. Ambient and
diffuse reflectances define the color of the material and are typically similar if not identical. Specular
reflectance is usually white or gray, so that specular highlights end up being the color of the light
source?¢_Ts specular intensity. If you think of a white light shining on a shiny red plastic sphere, most of
the sphere appears red, but the shiny highlight is white.
RGB Values for Lights and Materials
The color components specified for lights mean something different than for materials. For a light, the
numbers correspond to a percentage of full intensity for each color. If the R, G, and B values for a light?¢_Ts
color are all 1.0, the light is the brightest possible white. If the values are 0.5, the color is still white, but
only at half intensity, so it appears gray. If R=G=1 and B=0 (full red and green with no blue), the light
appears yellow.
For materials, the numbers correspond to the reflected proportions of those colors. So if R=1, G=0.5,
and B=0 for a material, that material reflects all the incoming red light, half the incoming green, and
none of the incoming blue light. In other words, if an OpenGL light has components (LR, LG, LB), and a
material has corresponding components (MR, MG, MB), then, ignoring all other reflectivity effects, the
light that arrives at the eye is given by (LR*MR, LG*MG, LB*MB).
Similarly, if you have two lights, which send (R1, G1, B1) and (R2, G2, B2) to the eye, OpenGL adds the
components, giving (R1+R2, G1+G2, B1+B2). If any of the sums are greater than 1 (corresponding to a
color brighter than the equipment can display), the component is clamped to 1.



Position and Attenuation
As previously mentioned, you can choose whether to have a light source that?¢_Ts treated as though it?¢_Ts
located infinitely far away from the scene or one that?¢_Ts nearer to the scene. The first type is referred to
as an directional light source; the effect of an infinite location is that the rays of light can be considered
parallel by the time they reach an object. An example of a real−world directional light source is the sun.
The second type is called a positional light source, since its exact position within the scene determines
the effect it has on a scene and, specifically, the direction from which the light rays come. A desk lamp
is an example of a positional light source. You can see the difference between directional and positional
lights in Figure J−16 .
The light used in Example 6−1 is a directional one:
GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
glLightfv(GL_LIGHT0, GL_POSITION, light_position);As shown, you supply a vector of four values (x, y, z, w) for the GL_POSITION parameter. If the last
value, w, is zero, the corresponding light source is a directional one, and the (x, y, z) values describe its
direction. This direction is transformed by the modelview matrix just as it would be if it described a
normal vector. By default, GL_POSITION is (0, 0, 1, 0), which defines a directional light that points
along the negative z−axis. (Note that nothing prevents you from creating a directional light with the
direction of (0, 0, 0), but such a light won?¢_Tt help you much.)
If the w value is nonzero, the light is positional, and the (x, y, z) values specify the location of the light
in homogeneous object coordinates (see Appendix G ). This location is transformed by the modelview
matrix and stored in eye coordinates. See "Controlling a Light?¢_Ts Position and Direction" for more
information about how to control the transformation of the light?¢_Ts location. Also, by default, a positional
light radiates in all directions, but you can restrict it to producing a cone of illumination by defining the
light as a spotlight. The next section, "Spotlights," explains how to define a light as a spotlight.
Note: Remember that the colors across the face of a smooth−shaded polygon are determined by the
colors calculated for the vertices. Because of this, you probably want to avoid using large
polygons with local lightsA_if you locate the light near the middle of the polygon, the vertices
might be too far away to receive much light, so the whole polygon will look darker than you
intended. To avoid this problem, break up the large polygon into smaller ones.
For real−world lights, the intensity of light decreases as distance from the light increases. Since a
directional light is infinitely far away, it doesn?¢_Tt make sense to attenuate its intensity over distance, so
attenuation is disabled for a directional light. However, you might want to attenuate the light from a
positional light. OpenGL attenuates a light source by multiplying the contribution of that source by an
attenuation factor:




- End of doc -

One very important thing to remember is that opengl is using "per face lighting" and not "per pixel lighting" as mentioned above.

The attenuation factors is describing how fast the lighting disapears, i have used it in the terrain demo, and the effect of the different settings is very easy to spot there.Remember that in GLX jou must call TGLXLightItem.Update to save the lightsettings to opengl.


note aswell as the normals of the planes is wery important, if your'e using drawplane in glx's primitives the normal always points at a certain direction so you must rotate the plane accordingly.

When looking at your screenshot the first thing i think you should change is the attenuation constants, skip the constant one, but try setting the linear to say 0.1 (depending on the size of the scene), but just experiment with that variable, you can also try the quatric attenuation but this is very sensive.

Yes, when setting positional to false the position comes the direction. However i'm not 100% sure about how this works eather, never tried it myself. (Just implemented into glx according to specification :lol: )

Try aswell rendering where the light is position with GLXLightItem.DrawCube to see that it's in the right place.

Hope you'll get something usefull out of this post, otherwise just tell and i will try to explain it more.

Andreaz
23-10-2003, 08:59 PM
Btw, must say the program looks really good. Nice work :D