# Movable lights...

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

## Recommended Posts

Hey all. This is what im trying to do now: I want the stars in my game to be the light sources and so I need to be able to keep the light source at the center of the star no matter where it moves. I'm not sure how to do this though. I've been reading in the red OGL book about how to make movable lights but I guess I just don't quite understand it yet. Any help? Thank you in advance.

##### Share on other sites
Quote:
 Original post by ahlywogThis is what im trying to do now: I want the stars in my game to be the light sources and so I need to be able to keep the light source at the center of the star no matter where it moves.
How many stars are there in your game? Be aware that most graphics hardware is limited to 8 lights.

Quote:
 I'm not sure how to do this though. I've been reading in the red OGL book about how to make movable lights but I guess I just don't quite understand it yet.
def set_light_pos(light, x, y, z):	pos = [x, y, z, 1.0]	glLightfv(GL_LIGHT0 + light, GL_POSITION, pos)
However, remember that lights are specified in eye space, so you probably want to set the light positions before applying a view matrix.

##### Share on other sites
Per map there will probably be a couple stars. Only the nearest star will have a light source and the stars will be spaced enough for this not to be an issue.

I understand the setting of the GL_POSITION part. That's easy. It's the setting the light position before applying a view matrix that I don't quite understand.

##### Share on other sites
Quote:
 Original post by ahlywogI understand the setting of the GL_POSITION part. That's easy. It's the setting the light position before applying a view matrix that I don't quite understand.
Actually, I think I just messed you up there - specifying in eye space seems to mean that you do need to have a view matrix setup. Perhaps someone less confused than me should weigh in here...

Presumably, you call glLoadIdentity() right after you clear the color/depth buffer, and then at some time later you setup a view matrix (either with gluLookAt, or you own matrix math). If this is the case, you should be able set your light positions right after setting the view matrix (before doing any other matrix operations to render objects).

The other (perhaps simpler) method is to setup your view matrix as normal, glTranlatef() to the light position, and set the light position to [0,0,0,1].

##### Share on other sites
Can anyone give an example?

##### Share on other sites
Light positions are specified in object space just like for vertices. They are multiplied by the modelview matrix, just like vertices.

Whatever transform you have applied to your object will apply to the light as well but you must call glLight.

glTranslatef(x, y, z);
glLightfv(GL_LIGHT0, GL_POSITION, pos);
DrawMyObject1();

glRotatef(x, y, z);
glLightfv(GL_LIGHT1, GL_POSITION, pos);
DrawMyObject2();

//and now render all other objects that will receive the lighting
glEnable(GL_LIGHTING);
DrawOther();
glDisable(GL_LIGHTING);

##### Share on other sites
See that's what I've been trying to do. Unless I'm doing it wrong. I set the X,Y,Z of the light to that of the star.

http://s298.photobucket.com/albums/mm265/ahlywog/?action=view¤t=lighting.png

As I said before the light is set to the X/Y/Z of the star (the big yellow thing.) But, clearly, it's not working properly as the light is showing up from a source below the objects.

1. 1
2. 2
3. 3
Rutin
20
4. 4
khawk
14
5. 5

• 9
• 11
• 11
• 23
• 12
• ### Forum Statistics

• Total Topics
633655
• Total Posts
3013186
×