PDA

View Full Version : opengl lights


starboarder2001
06-30-2003, 01:35 PM
when i setup a opengl light and use gluLookAt(...) for my viewing transformations It seems that the light position is not effected by gluLookAt(...)?

sorry if this is a stupid question i am very new to opengl and c++.

Noor
06-30-2003, 01:54 PM
did you do glEnable(GL_LIGHT0); ?
also, did you do: glEnable(GL_LIGHTING); ?

starboarder2001
06-30-2003, 02:23 PM
the lighting is working...and i calculated the normals for all the polygons. The problem is that when i move the camera with gluLookAt() the light seems to move with the camera? if i set the light position at 0,10,0 when i move the camera to 0,0,-20 the light is now at 0,10,-20.

screenshots:

(Link Is Down):sigh:

Noor
06-30-2003, 02:36 PM
i believe you need to set the position of the light using the glTranslatef() function instead of changing the light position. Try that and see if that works. Let me know about the results.

starboarder2001
06-30-2003, 03:15 PM
this is what i am doing now:

//example1
float ambientlight[4]; //ambient light
float diffuselight[4]; //diffuse light
float specularlight[4]; //specular light
float lightposition[4]; //light position

//i would setup the 4 variables above here//

glLightfv(GL_LIGHT0,GL_AMBIENT,ambientlight);
glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuselight);
glLightfv(GL_LIGHT0,GL_SPECULAR,specularlight);
glLightfv(GL_LIGHT0,GL_POSITION,lightposition);

glLightf(GL_LIGHT0,GL_CONSTANT_ATTENUATION,0.9);
glLightf(GL_LIGHT0,GL_LINEAR_ATTENUATION,0.1);
glLightf(GL_LIGHT0,GL_QUADRATIC_ATTENUATION,0.1f);

glEnable(GL_LIGHT0);


this is what i tryed:

//example2
float ambientlight[4]; //ambient light
float diffuselight[4]; //diffuse light
float specularlight[4]; //specular light

//i would setup the 4 variables above here//

glPushMatrix();

glTranslatef(0,0,10);
glLightfv(GL_LIGHT0,GL_AMBIENT,ambientlight);
glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuselight);
glLightfv(GL_LIGHT0,GL_SPECULAR,specularlight);
glLightf(GL_LIGHT0,GL_CONSTANT_ATTENUATION,0.9);
glLightf(GL_LIGHT0,GL_LINEAR_ATTENUATION,0.1);
glLightf(GL_LIGHT0,GL_QUADRATIC_ATTENUATION,0.1f);
glEnable(GL_LIGHT0);

glPopMatrix();

Example 2 didnt work.. :sigh: can you give me a small example of how to setup a light using glTranslate(..);? are you shure that is how you can set the lights position?

davepermen
06-30-2003, 03:32 PM
use glLightfv(GL_LIGHT0,GL_POSITION,lightposition) as if you would draw a point.. means you possibly draw it at the moment it would just sit in front of your cam, without translation and rotation yet..


place the function where you draw the landscape..

Dia Kharrat
06-30-2003, 03:42 PM
Nice screenshots you have, btw :)

starboarder2001
06-30-2003, 03:50 PM
ok thanks i got it working :D

Noor
06-30-2003, 04:21 PM
Good. Congratulation!



yes, very nice screenshots!

davepermen
06-30-2003, 10:14 PM
great!!

and to add to the screenshots: they are not only nice, but they where useful to detect the problem as well (if that really was the problem, of course:D)

dotanitis
06-24-2007, 03:39 PM
Hello to all, (first post),

I have the same problem and could not find the answer. As was written again when i move the camera the light is also moves. my light code is:

glPushMatrix();
glLoadIdentity();

glDisable(GL_LIGHT0); // another light that i have
glEnable(GL_LIGHTING);

glEnable(GL_COLOR_MATERIAL);

GLfloat amb = 0.0f, dif = 2.0f;

GLfloat light_ambient[] = {amb, amb, amb};
GLfloat light_diffuse[] = {dif, dif, dif};
GLfloat light_specular[] = {0.0f, 0.0f, 0.0f};
GLfloat light_position[] = {m_nLightPosX, m_nLightPosY, m_nLightPosZ, 1.0f};

glLightfv(GL_LIGHT1, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT1, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT1, GL_SPECULAR, light_specular);
glLightfv(GL_LIGHT1, GL_POSITION, light_position);

glEnable(GL_LIGHT1);

glPopMatrix();

Don't know what to do...

Reedbeta
06-24-2007, 05:43 PM
You need to re-set the light position every frame, after you've set up your modelview matrix for the frame.

Also, this thread is from almost 4 years ago. It would be better if you had created a new thread for your question.