PDA

View Full Version : Trouble with light



Pedja
09-04-2008, 10:09 AM
I'm using this code for ambient light:


GLfloat light1_ambient[] = { 0.2, 0.2, 0.2, 1.0 };
GLfloat light1_diffuse[] = { 0.0, 1.0, 1.0, 1.0 };
GLfloat light1_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat light1_position[] = { 20.0, 0.0, 0.0};
GLfloat spot_direction[] = { 1.0, 0.0, 0.0 };
glLightfv(GL_LIGHT1, GL_AMBIENT, light1_ambient);
glLightfv(GL_LIGHT1, GL_DIFFUSE, light1_diffuse);
glLightfv(GL_LIGHT1, GL_SPECULAR, light1_specular);
glLightfv(GL_LIGHT1, GL_POSITION, light1_position);
glLightf(GL_LIGHT1, GL_CONSTANT_ATTENUATION, 1.5);
glLightf(GL_LIGHT1, GL_LINEAR_ATTENUATION, 0.5);
glLightf(GL_LIGHT1, GL_QUADRATIC_ATTENUATION, 0.2);
glLightf(GL_LIGHT1, GL_SPOT_CUTOFF, 45.0);
glLightfv(GL_LIGHT1, GL_SPOT_DIRECTION, spot_direction);
glLightf(GL_LIGHT1, GL_SPOT_EXPONENT, 2.0);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT1);

but my scene is almost completely dark except when I do not put the line glEnable(GL_LIGHTING).What am I doing wrong?

ZbuffeR
09-04-2008, 11:33 AM
Either you use glColor and forgot this :
glEnable(GL_COLOR_MATERIAL);

Or you scaled the scene without normalizing the normals. In this case just use :
glEnable(GL_NORMALIZE);

EDIT : try without attenuation, by commenting out these lines:
glLightf(GL_LIGHT1, GL_CONSTANT_ATTENUATION, 1.5);
glLightf(GL_LIGHT1, GL_LINEAR_ATTENUATION, 0.5);
glLightf(GL_LIGHT1, GL_QUADRATIC_ATTENUATION, 0.2);

Pedja
09-05-2008, 01:59 AM
Ok there is improvement but my scene now looks like this:
http://img143.imageshack.us/img143/401/mysceneaq9.th.jpg
Can you tell me why?

ZbuffeR
09-05-2008, 02:31 AM
1) please don't link to a thumbnail ! Impossible to see anything.
2) try with a white texture.
3) use shaders to avoind clamping the color too early

Pedja
09-05-2008, 03:32 AM
Thanks!Problem solved.I checked the sphere normals again and corrected the problem.