PDA

View Full Version : My light transforms but it shouldn't.



wodzu
15-03-2011, 07:46 AM
Hi guys.

I am learning about lights in OpenGL and I have problem that my light is not fixed.
By a fixed light I mean, the light that stays at the same point on the scene, no matter where the camera is.
I don't know why, I think I did eferything that OpenGL manual suggested.

Take a look at the code:



procedure TMyApplication.DoRun;
var
Screen: PSDL_Surface;
Event: PSDL_Event;
LoopStop: Boolean;
Entity: TEntity;
Camera: TCamera;
const
LightPosition: array[0..3] of GLfloat = (0.0, 0.0, -1.0, 0.0);
WhiteLight: array[0..3] of GLfloat = (1.0, 1.0, 1.0, 1.0);
LightModelAmbient: array[0..3] of GLfloat = (0.1, 0.1, 0.1, 1.0);
begin
SDL_INIT(SDL_INIT_VIDEO);

SDL_GL_SETATTRIBUTE(SDL_GL_RED_SIZE, 8);
SDL_GL_SETATTRIBUTE(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SETATTRIBUTE(SDL_GL_BLUE_SIZE, 8);
SDL_GL_SETATTRIBUTE(SDL_GL_DEPTH_SIZE, 24);
SDL_GL_SETATTRIBUTE(SDL_GL_DOUBLEBUFFER, 1);
SDL_ENABLEKEYREPEAT(1,0);

Screen := SDL_SETVIDEOMODE(800, 600, 0, SDL_OPENGL);
IF Screen = NIL THEN HALT;
SDL_WM_SetCaption( 'Event test', nil );
New(Event);

glCLEARCOLOR(0.0, 0.0, 0.0, 0.0);
glVIEWPORT(0,0,800,600);
glMATRIXMODE(GL_PROJECTION);
glLOADIDENTITY;
gluPERSPECTIVE(45.0, 800.0/600.0, 0.1, 1000.0);
glMATRIXMODE(GL_MODELVIEW);
glLOADIDENTITY;
glCLEAR(GL_COLOR_BUFFER_BIT);
glENABLE(GL_CULL_FACE);
glShadeModel(GL_SMOOTH);

glLightfv(GL_LIGHT0, GL_POSITION, LightPosition);
glLightfv(GL_LIGHT0, GL_DIFFUSE, WhiteLight);
glLightfv(GL_LIGHT0, GL_SPECULAR, WhiteLight);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, LightModelAmbient);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);

while LoopStop = False do
begin
if SDL_POLLEVENT(Event) = 1 then
case Event^.type_ of
SDL_KEYDOWN:
begin
case Event^.key.keysym.sym of
SDLK_ESCAPE:
LoopStop := True;
SDLK_a: Camera.MoveForward(-1);
SDLK_z: Camera.MoveForward(1);
SDLK_UP: Camera.RotateAroundOX(1);
SDLK_DOWN: Camera.RotateAroundOX(-1);
SDLK_LEFT: Camera.RotateAroundOY(-1);
SDLK_RIGHT: Camera.RotateAroundOY(1);
end;
end;
end;

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPERSPECTIVE(45.0, 800.0/600.0, 0.1, 1000.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity;
glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);

glRotatef(Camera.Rotation.X, 1, 0, 0);
glRotatef(Camera.Rotation.Y, 0, 1, 0);
glTranslatef(-Camera.Position.X, -Camera.Position.Y, -Camera.Position.Z);
Entity.Draw; //draws the one visible object

glLoadIdentity;
glLightfv(GL_LIGHT0, GL_POSITION, LightPosition);
SDL_GL_SWAPBUFFERS;
end;
end;


I've stripped some non important parts from the code for clearity. I am loading identity matrix before calling glLightfv() but still, the light is moving when I move my camera. You can try it yourself with the attached program. Sorry for the inconvenience with moving the camera( it is not framerate independant).
If you press left or right arrow you will see that the light moves along with the camera.

Keyes are:
a, z: zoom in zoom out
arrows: rotates camera
esc: closes the application


Thanks for your time and help.

User137
15-03-2011, 09:41 AM
You are setting light position after you have rendered. Try swapping lines like this:

glRotatef(Camera.Rotation.X, 1, 0, 0);
glRotatef(Camera.Rotation.Y, 0, 1, 0);
glTranslatef(-Camera.Position.X, -Camera.Position.Y, -Camera.Position.Z);
glLightfv(GL_LIGHT0, GL_POSITION, LightPosition); // Set light after camera
Entity.Draw; //draws the one visible object

wodzu
15-03-2011, 10:07 AM
You are setting light position after you have rendered. Try swapping lines like this:

glRotatef(Camera.Rotation.X, 1, 0, 0);
glRotatef(Camera.Rotation.Y, 0, 1, 0);
glTranslatef(-Camera.Position.X, -Camera.Position.Y, -Camera.Position.Z);
glLightfv(GL_LIGHT0, GL_POSITION, LightPosition); // Set light after camera
Entity.Draw; //draws the one visible object

Thanks mate, it works!