#### Archived

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

# Lights again

This topic is 5488 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?!

##### Share on other sites
Sorry. I left my computer for the week-end. Currently I''m still downloading zips because my provider tends to be lazy. I''ll give feedback asap.

##### Share on other sites
Ah, finally I got it working under linux
It looks pretty good, even though I guess it''s just a beginning.
Obviously the models have more faces than enough to support spotlights correctly. I sincerely doubt the low polygon count will be a problem in that case
Keep up the good work !

ps: I''ll try to run it with Windows. Probably I''m missing a few things (like the sky box which is not textured).

##### Share on other sites
Yep It doesnt support shaders yet ( and skies are obviously shaders ). Did you see the red pointlight on the yard? it doesnt look focused or bright or something. Do you have any idea how to fix it?

##### Share on other sites
Actually I wouldn''t call skybox a shader. A shader is a kind of material property. Skybox is an object in its own. These are two different concepts in my opinion.
Though, skies often use shaders in order to increase realism.

As for your red light, if you mean the light that follows every player I think it has to do with your rendering function.
Probably you''re not calling glLight(GL_POSITION) at the right place. If you call it in the beginning of the rendering function of every model, then that''s the kind of result I would expect.

In other words, you gotta check if glLight(GL_POSITION) is called once per frame or not. If you''re not sure, you can log info into a file or the console.

##### Share on other sites
I know I post this a lot, but ohh well

have a look at this post I made a good 3 months ago.

| - Project-X - my mega project.. yup, still cracking along - | - adDeath - an ad blocker I made - | - email me - |

##### Share on other sites
I do call it at the right place ( after camera is set up ) but the light looks just crappy ( have a look - not bright, not focused ) and I cant find the right properties to set those things... I willhave a look at the pos later today rip im at work atm..

As for the skybox: I didnt care much about it yet so im not sure how exactly it is drawn. I just guessed its a shader because all the textures that didnt draw properly so far were shaders

[edited by - Dtag on February 4, 2003 1:58:40 AM]

##### Share on other sites
Hmm I dont really see the connection between your post and my question... I dont have any problems positioning it.. Its just the appearance... intensity, how focused it is etc... And also why it is bugged ( laras weapon?! ( in my sample ))

[edited by - Dtag on February 4, 2003 11:05:57 AM]

##### Share on other sites
I don''t think that lighting is messed up. I mean, OpenGL is doing the right thing if I understand right what you''re doing.
If I were you, I''d try to move/rotate the spotlight (eg change the GL_POSITION and GL_SPOT_DIRECTION values) and see what happens.

##### Share on other sites
Didnt you see laras weapon being completely illuminated while nothing in the area around her was illuminated?

##### Share on other sites
If the light is local to the model, it''s normal. Though, if there is one unique light in the world, then I agree it''s dumb.
I''m not surprised by lara''s light. Not more than the main player''s light not the quake boss light.

##### Share on other sites
I showed you the settings of the light before. The light is a static location which is out on the yard. So why are you not surprised by this behaviour of the light?

##### Share on other sites
Could you please give an idea of the world dimensions, in OpenGL units ? For instance, what is the height of a character, approximatively ?

##### Share on other sites
Y Size: ~2400
X Size: ~1000
Z: Dunno like 250 maybe? No idea

Character Height: 64
Character Width: 32

##### Share on other sites
Ok, so you would like your spotlight to be around the middle of the scene, and pointing down. Ang this light would be unique (no other light). Am I right ?

##### Share on other sites
Nope its neither a spotlight nor is it pointing down. Its a point light initialized in the X Middle, Upper Y area. ( Yard )