Hi,
How comes when i enable lighting in openGL, everything in the window goes black.

Fredy

06-20-2001, 04:04 AM

You must setup the lights.

Zeus_666

06-20-2001, 06:00 AM

Ok, set up a light with all the properties, two questions.

1, Can i set the actual co-ordinates of the light source so for example, it is in a room.

2, All the colours of my objects have changed, how i get my original colours to stick.

MCote79

06-20-2001, 06:25 AM

1) You can put the light anywhere you want.

2) Make sure that the light is white. Otherwise different colors might be getting relected back.

Zeus_666

06-20-2001, 06:28 AM

How do you place the light as the values in the position variable say were the light points?

Thanks

harsman

06-20-2001, 06:47 AM

Use 1.0 for the fourth coordinate (W). Then you specif a position instead of a direction.

Zeus_666

06-20-2001, 08:12 AM

Ok,
Read the chapter on lighting in the red book and i am completly confused about the whole lighting thing, help would be most appreciated.

Deiussum

06-20-2001, 09:38 AM

Here's how to setup a few different types of light that might explain things a bit better.

1) Positional light. This type of light is given a position and emits light in all directions. To do this you use glLightfv with the GL_POSITION. The 4th value in the vector you give MUST have a non-zero value (usually 1.0 is used.) For instance...

float lightpos[] = {0.0, 2.0, 0.0, 1.0};

glLightfv(GL_LIGHT0, GL_POSITION, lightpos);

2) A directional light. This type of light only has a direction and is treated as though the light is at an infinite position so that the basically all light beams are parallel to each other in the scene. (Kind of like the light from the sun. You move a few feet and the angle between you and the sun is essentially the same.) To do this, you use GL_POSITION and specify 0 for the 4th value in the vector. For instance...

float lightdir[] = {0.0, 0.71, 0.71, 0.0};

glLightfv(GL_LIGHT0, GL_POSITION, lightdir);

3) A spot light, which has both a direction and a position. You basically combine GL_POSITION with a non-0 value for the 4th element of the coordinate with GL_SPOT_DIRECTION. There is also a cutoff angle on a spot light that you can use to control the "tightness" of the spotlight beam. Like so...

There are a bunch of other values that can be changed for lights, but these are probably some of the most common you will run into. You can read more about the other things you can set in MSDN or the Red Book.

Zeus_666

06-21-2001, 03:51 AM

Ok,
Let's start with Positional light.

I have a flat rectangle which is used as the floor, around all the edges are walls. Just inside the world walls, is a big building with many rooms inside.

I set a Positional light at 0, 2, 0, 0.
I start at 0, 0, -1.

The wall behind me is always black, dark while the wall at the far side of the map is light, very very light, am i doing something wrong.

Zeus_666

06-21-2001, 04:00 AM

Hi,
I am trying to get a light at the front of the building only pointing in throught the door.
This is were the user first starts. If anyone can do this for me or show me, i would be very very grateful. This lighting is starting to get on my nerves. Thanks.

I didn't check your code there but in reply to the previous post, you need to set the w coordinate to 1, not 0 to position the light. Also, it is always a good idea to have a very low level ambient light set up just to be sure that everything is drawing. If you really want to get the full effect, then the ambient light can be taken out when you finish fiddling.

bsperlin

06-21-2001, 04:29 AM

This is just a wild guess since I didn't really study your code, but bit seems that you have to set the front and back of each rectangle and decide which will be lit. There is a command to choose which side will be lit depending on whether you are describing the vertices in a clockwise or counterclockwise manner.
You need code similar to: glFrontFace( GL_CCW )
and then pay attention to the order of the vertices in each rectangle.
Barry

Deiussum

06-21-2001, 07:38 AM

I took a quick look at your code and ran it. Didn't notice anything obviously wrong with the code, but running the program makes me think that maybe the normals are messed up.

Also, when you say you just want the light going through the door it sounds like you might be thinking the light will just shine through the door and that the walls will block out light making things dark. This is not the case. OpenGL does not have any knowledge of "objects" so it performs lighting calculations for each vertex without taking any previously drawn primitives into account. (i.e. it won't automatically do shadows for you.)

I believe I see where you are getting messed up on your normal data as well.

You are using vertex arrays and do not have the same number of normals as you have vertices. You also seem to have triangles and quads in different arrays, yet you have all the normals in a single array. When you use vertex arrays in OpenGL it works something like this...

Say your array has 1 triangle and you want to draw the indices 0,1,2. Internally the vertex arrays will get drawn something like so

As you see, the indices for the normal array have to match up with the indices in the vertex array. This could very easily explain the weird lighting you are seing because normals play a big part in lighting calculations.

Zeus_666

06-21-2001, 07:56 AM

Hi,
I have a surface normal for each face, should i have one for each set of vertices, each corner of a face?

DFrey

06-21-2001, 08:21 AM

When using vertex arrays and normal arrays, you need one normal per vertex.

Zeus_666

06-21-2001, 08:31 AM

I've used a function to generate a surface normal for each quad, triangle by using 3 sets of vertices.

B**A
* *
* *
C***

How do i calculate a surface normal for each vertex set?

DFrey

06-21-2001, 08:46 AM

Ok, for smooth shading, each vertex normal is the normalized sum of all triangle normals of the triangles that share that vertex. However, for objects in which the angle between adjacent faces is large, e.g. a cube or box, you should not add the normals. In that instance, you would need to duplicate the vertex, once with one normal, and one or more other times with other normals.