Skip to main content
gamedev.net
🔒 Locked

Movable lights...

Started by ahlywog 5 replies 700+ views
Original Post
ahlywog
ahlywog
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.
swiftcoder
swiftcoder
Quote:
Original post by ahlywog
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.
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.
Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]
ahlywog
ahlywog
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.
swiftcoder
swiftcoder
Quote:
Original post by ahlywog
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.
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].
Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]
ahlywog
ahlywog
Can anyone give an example?
V-man
V-man
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);
ahlywog
ahlywog
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&current=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.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.