#### Archived

This topic is now archived and is closed to further replies.

# Lights again

This topic is 5583 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey... Iam trying to get spot and pointlights working. I had a look at the gl Reference and got this code.

// Is this Matrix stuff needed? I read something about the position coordinates being transformed by the ModelView Matrix?

glMatrixMode(GL_MODELVIEW);
glPushMatrix();

glEnable(  GL_LIGHTING );

GLfloat light1_diffuse[] = { 1.0, 0.0, 0.0, 1.0 };
GLfloat light1_position[] = { 278.0, 1282.0, 80.0, 1.0 };
GLfloat light1_dir[] = { 0,0, -1};

glLightfv(GL_LIGHT0, GL_DIFFUSE, light1_diffuse);

glLightfv(GL_LIGHT0, GL_POSITION, light1_position);

glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 45.0);
glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, light1_dir);
glLightf(GL_LIGHT0, GL_SPOT_EXPONENT, 2.0);

glLightModelfv(GL_LIGHT_MODEL_AMBIENT,light1_ambient);
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT,GL_AMBIENT_AND_DIFFUSE);

glEnable(  GL_LIGHT0   );

glEnable(GL_COLOR_MATERIAL);

glPopMatrix();

(The code is called once in the initialization of my engine) The problem is that I cant see any sign of light at the location I passed to the glLight* function. It should be a spot at 278 1282 80 pointing down and leaving a red spot on the ground... At this location, theres nothing. If I go away from the spot to an 'outdoor' area of my map, I can see a very dark red shimmer on the skybox. Thats why I guess the lights position isnt transmitted correctly to gl ... Any ideas? TIA [edited by - Dtag on February 1, 2003 3:32:46 AM] [edited by - Dtag on February 1, 2003 3:33:34 AM]

##### Share on other sites
I guess that''s normal.
Light position is stored in eye coordinates at the time you set the position. In other words, if your camera moves or rotates you can''t "set the light position in the initialization code".

In your rendering code, you probably have something like :

void render()
{
glClear(...);
glMatrixMode(GL_MODELVIEW);

setup_camera(); /* for instance, gluLookAt(...) */

render_objects();

flush(); // for instance glutPostRedisplay();
}

The solution is to setup the light position *after* the camera has been setup :

void render()
{
glClear(...);
glMatrixMode(GL_MODELVIEW);

setup_camera(); /* for instance, gluLookAt(...) */

glLightfv(GL_LIGHT0, GL_POSITION, light1_position); /* NEW */
glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, light1_dir); /* NEW */

render_objects();

flush(); // for instance glutPostRedisplay();
}

Note that the other light initialization can be done "once in the initialization of your engine" because only the light position and direction are dependant of the camera position and orientation.

##### Share on other sites
Hi
Thanks for your answer. The light looks alot better now. Altough the function to set the light intensity doesnt seem to work. No matter what parameter I pass to the glLight* function ( using GL_SPOT_EXPONENT ) the intensity doesnt change. Another problem is the attentuation... I want a bright light that attentuates very slowly at first and very quickly at end. So QUADRATIC_ATTENTUATION would be the right thing for me but changing the quadratic attentuation value doesnt change the behaviour of the light at all. If i try other attentuation variables and use LINEAR for example, the light is completely gone, if I try CONSTANT, the light is darker everywhere. Any ideas?
Thanks

##### Share on other sites
GL_SPOT_EXPONENT does not really change the light "intensity". It changes how focused is the spot.

I''m not sure QUADRATIC is the kind of attenuation you look for. Quadratic is 1/d^2, so it means that is starts decreasing very soon, and later takes longer to attenuate.

Also, a common mistake is not to tesselate your geometry enough. Remember that lighting is performed PER-VERTEX in OpenGL. It''s perfect for directional lights, it''s more than ok for ponctual lights, but it''s awful for spotlights.
If you''re trying to illuminate a single quad, you''ll never notice the "spot shape". Hmm. I''m not certain to be clear. Feel free to ask for explanations.

##### Share on other sites
Well Iam looking for a method to draw the lights that are specified in the entities part of BSP files. Theyre simple point lights afaik. Should I use the normal glLights for that or do I need to look for something else? If so, how do i adjust intensity etc?

##### Share on other sites
glLight is the way to go. Other lighting method require advanced OpenGL stuff which can perform awesome effects but are much more difficult to master and also needs much more GPU or CPU usage.

What does your scene looks like actually ? Is it a set of quads, or a set of 3D models (imported from 3DS or Milkshape for instance) ?

##### Share on other sites
Ive got models that are imported from 3dsmax or whatever can export MD3 models. My world is a BSP Tree that contains nodes that contain Triangle Fans. Want to take a look at it? I could upload it on my webserver and link it here?

##### Share on other sites
I''d be glad to see it, but unless you''re developing a Linux application I doubt I can run it

Anyway, it''s good news that you''re importing 3D models. It generally means that models have a decent polygon count, which is more than useful for spotlights (according to my previous remark).

##### Share on other sites
Ok here you go ( you need all of the files )
http://www.pay4hits.de/maps.zip
http://www.pay4hits.de/xaero.zip
http://www.pay4hits.de/models.zip
http://www.pay4hits.de/engine30.exe
unzip/copy all of them to a directory of your choice and run

The light is outside of the hall on the yard. You can see that the light is very wide spread. Another thing i noticed that the models in the back hall got some strange illuminations ( look at laras weapon?! )

EDIT: I forgot a file:
http://www.pay4hits.de/media.zip

[edited by - Dtag on February 1, 2003 10:57:39 AM]

Still there?!

• 9
• 23
• 10
• 19