Movable lights...

Started by
5 comments, last by ahlywog 15 years, 4 months ago
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.
Advertisement
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]

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.
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]

Can anyone give an example?
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);
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);
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.

This topic is closed to new replies.

Advertisement