PDA

View Full Version : my light is busted...



ngill
07-13-2000, 09:44 PM
well... I hate to do this but... here's the code... I get NO effect whether GL_LIGHT0 is turned off/on!! What is the matter?
#include <GL/glut.h>

GLvoid InitGL()
{
glViewport(0,0,640,480);
glClearColor(0.0f,0.0f,0.0f,1.0f);
glShadeModel(GL_SMOOTH);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f,4.0/3.0,0.1f,100.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

GLfloat ambientLight[]={0.3f, 0.3f, 0.3f, 1.0f};
GLfloat diffuseLight[]={0.7f, 0.7f, 0.7f, 1.0f};
GLfloat specularLight[]={1.0f, 1.0f, 1.0f, 0.0f};
GLfloat specref[]={1.0f, 1.0f, 1.0f, 1.0f};
GLfloat spotdir[]={0.0f,0.0f,-1.0f};

glLightfv(GL_LIGHT0,GL_AMBIENT,ambientLight);
glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuseLight);
glLightfv(GL_LIGHT0,GL_SPECULAR,specularLight);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT,ambientLight );

GLfloat lightPos[]={0.0f, 0.0f, 0.0f, 0.0f};
glLightfv(GL_LIGHT0,GL_POSITION,lightPos);
glLightf(GL_LIGHT0,GL_SPOT_CUTOFF,60.0f);
glLightf(GL_LIGHT0,GL_SPOT_EXPONENT,100.0f);
glLightfv(GL_LIGHT0,GL_SPOT_DIRECTION,spotdir);

glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);

glColor3f(0.0f,0.0f,1.0f);

glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT_AND_BACK,GL_AMBIENT_AND_D IFFUSE);
glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,specref );
glMaterialf(GL_FRONT_AND_BACK,GL_SHININESS,128);
}

GLvoid DrawScene()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glBegin(GL_TRIANGLES);
glNormal3f(0.0f,0.0f,1.0f);
glVertex3f(-1.0f,-1.0f,-5.0f);
glVertex3f( 0.0f, 1.0f,-5.0f);
glVertex3f( 1.0f,-1.0f,-5.0f);
glEnd();
glPopMatrix();
glutSwapBuffers();
}

int main(int numargs, char **argv)
{
glutInitWindowPosition(0,0);
glutInitWindowSize(640,480);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH|GLUT_RG BA);
glutCreateWindow(argv[0]);

InitGL();
glutDisplayFunc(DrawScene);

glutMainLoop();

return 0;
}


[This message has been edited by ngill (edited 07-14-2000).]

Relic
07-14-2000, 08:08 AM
And I love to do this ;-)
See comments behind //

I get NO effect whether GL_LIGHT0 is turned off/on!! What is the matter?
#include <GL/glut.h>
GLvoid InitGL() // add void
{
glViewport(0,0,640,480);
glClearColor(0.0f,0.0f,0.0f,1.0f); // alpha = 1.0 necessary?
glShadeModel(GL_SMOOTH); // default

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f,4.0/3.0,0.1f,100.0f); // takes double arguments
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// Remember glLight positions are transformed by this matrix, too.

GLfloat ambientLight[]={0.3f, 0.3f, 0.3f, 1.0f};
GLfloat diffuseLight[]={0.7f, 0.7f, 0.7f, 1.0f};
GLfloat specularLight[]={1.0f, 1.0f, 1.0f, 0.0f};
GLfloat specref[]={1.0f, 1.0f, 1.0f, 1.0f};
GLfloat spotdir[]={0.0f,0.0f,-1.0f}; // BTW, default.

glLightfv(GL_LIGHT0,GL_AMBIENT,ambientLight);
glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuseLight);
glLightfv(GL_LIGHT0,GL_SPECULAR,specularLight);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT,ambientLight );


GLfloat lightPos[]={0.0f, 0.0f, 0.0f, 0.0f}; // Error, not a vector or a point! Use GLfloat lightPos[]={0.0f, 0.0f, 0.0f, 1.0f}; and see the top vertex being lit http://www.opengl.org/discussion_boards/ubb/wink.gif
glLightfv(GL_LIGHT0,GL_POSITION,lightPos); glLightf(GL_LIGHT0,GL_SPOT_CUTOFF,60.0f);
glLightf(GL_LIGHT0,GL_SPOT_EXPONENT,100.0f);
glLightfv(GL_LIGHT0,GL_SPOT_DIRECTION,spotdir);

glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);

glColor3f(0.0f,0.0f,1.0f);

glEnable(GL_COLOR_MATERIAL); // Oops, never call enable before you have set a glColorMaterial on your own, or you might bust your materials without noticing if you don't use GL_AMBIENT_AND_DIFFUSE. Well, so in your case it's ok.
glColorMaterial(GL_FRONT_AND_BACK,GL_AMBIENT_AND_D IFFUSE); // You have no two sided lighting, so no need to specify front and back (performance!)
glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,specref );
glMaterialf(GL_FRONT_AND_BACK,GL_SHININESS,128); // Use lower values for bigger highlight.
}

GLvoid DrawScene()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glBegin(GL_TRIANGLES);
glNormal3f(0.0f,0.0f,1.0f);
glVertex3f(-1.0f,-1.0f,-5.0f); // Very low tesselation for shininess of 128. http://www.opengl.org/discussion_boards/ubb/smile.gif
glVertex3f( 0.0f, 1.0f,-5.0f);
glVertex3f( 1.0f,-1.0f,-5.0f);
glEnd();
glPopMatrix();
glutSwapBuffers();
}

int main(int numargs, char **argv)
{
glutInitWindowPosition(0,0);
glutInitWindowSize(640,480);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH|GLUT_RG BA);
glutCreateWindow(argv[0]);

InitGL();
glutDisplayFunc(DrawScene);

glutMainLoop();

return 0;
}

Does it work now?


[This message has been edited by Relic (edited 07-14-2000).]

ngill
07-14-2000, 01:49 PM
Thanks a lot relic! I was trying to acheive the spot light effect, and made even a few more erros like the not a vector (spotlight) / point thing...

By the way, where did you learn all the stuff about not calling glEnable(GL_COLOR_MATERIAL), stuff like that? Superbible didn't mention it, but I am guessing it's "common sense"...

ngill
07-14-2000, 02:00 PM
another question... does the which vectors MUST have length of 1 unit except normals for polygons? does the spotdirection only supposed to have the length of one? or do ALL vectors (including lightpos [4th value 0.0]) are supposed to have length of 1?

Relic
07-15-2000, 12:05 PM
"which vectors MUST have length of 1 unit except normals for polygons? does the spotdirection only supposed to have the length of one? or do ALL vectors (including lightpos [4th value 0.0]) are supposed to have length of 1? "

The general rule is OpenGL does not care for the normalization as long as GL_NORMALIZE is disabled (default). BUT, the lighting calculations written in the specs are all defined for unit vectors.
So for best perfomance you should care for unit vectors and leave GL_NORMALIZE disabled. If you have no normalized vectors available, you have to bench what is faster, doing the normalization yourself or let OpenGL do it. On HW geometry latter could be faster.
With glScale somewhere in the program and lighting you MUST enable GL_NORMALIZE if you want correct lighting.

"By the way, where did you learn all the stuff about not calling glEnable(GL_COLOR_MATERIAL), stuff like that? Superbible didn't mention it, but I am guessing it's "common sense"... "

You mean telling all my tricks? I wouldn't call it commen sense, but experience. http://www.opengl.org/discussion_boards/ubb/biggrin.gif
About five years professional programming experience with OpenGL under Windows NT have sharpened my eye for these things now. http://www.opengl.org/discussion_boards/ubb/smile.gif

ngill
07-25-2000, 01:27 PM
sorry for resurrecting old topic, but after covering some other topics, I still can't get SPOT lighting to work correctly... All the polygons turn white! It doesn't seem right... here's a sub 100 line program...
what am I doing wrong?

#include <GL/glut.h>

GLfloat lightPos[]={0.0f, 5.0f, 0.0f, 0.0f};

GLfloat ambient[]={0.2f, 0.2f, 0.2f, 1.0f};
GLfloat specref[]={1.0f, 1.0f, 1.0f, 1.0};

GLfloat ambientL[]={0.25f, 0.25f, 0.25f, 1.0f};
GLfloat diffuseL[]={0.45f, 0.45f, 0.45f, 1.0f};
GLfloat specularL[]={1.0f, 1.0f, 1.0f, 1.0f};

void InitGL()
{
GLfloat spotdir[]={0.0f,-1.0f,0.0f};

glLightModelfv(GL_LIGHT_MODEL_AMBIENT,ambient);

glLightfv(GL_LIGHT0,GL_AMBIENT,ambientL);
glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuseL);
glLightfv(GL_LIGHT0,GL_SPECULAR,specularL);
glLightfv(GL_LIGHT0,GL_POSITION,lightPos);
glLightfv(GL_LIGHT0,GL_SPOT_DIRECTION,spotdir);
glLightf(GL_LIGHT0,GL_SPOT_CUTOFF,60.0f);
glLightf(GL_LIGHT0,GL_SPOT_EXPONENT,128.0f);

glColorMaterial(GL_FRONT,GL_AMBIENT_AND_DIFFUSE);
glMaterialfv(GL_FRONT, GL_SPECULAR,specref);
glMateriali(GL_FRONT, GL_SHININESS,128);

glEnable(GL_CULL_FACE);
glEnable(GL_LIGHTING);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);

glClearColor(0.0f,0.0f,0.0f);
glColor3f(1.0f,0.0f,0.0f);
glViewport(0,0,640,480);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
GLfloat aspectratio(4.0/3.0);
gluPerspective(45.0,aspectratio,0.1,100.0);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

void DrawMainScene()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(0,20,20,0,0,0,0,1,0);

static const GLfloat size(10.0);

glPushMatrix();
glFrontFace(GL_CW);
glColor3f(1.0f,0.0f,0.0f);
glBegin(GL_QUADS);
glNormal3f(0.0f,1.0f,0.0f);
for (GLfloat detaz=-size;detaz<size;detaz+=0.4)
for (GLfloat detax=-size;detax<size;detax+=0.4)
{
glVertex3f(detax,-0.8f,detaz);
glVertex3f(detax+0.5,-0.8f,detaz);
glVertex3f(detax+0.5,-0.8f,detaz+0.5);
glVertex3f(detax,-0.8f,detaz+0.5);
}
glEnd();
glFrontFace(GL_CCW);
glPopMatrix();

glPushMatrix();
glPushAttrib(GL_LIGHTING_BIT);
glDisable(GL_LIGHTING);
glTranslatef(lightPos[0],lightPos[1],lightPos[2]);
glColor3f(1.0f,1.0f,0.0f);
glutSolidSphere(0.2f,15,15);
glPopAttrib();
glPopMatrix();

glutSwapBuffers();
}

int main(int argc, char** argv)
{
glutInitWindowPosition(0,0);
glutInitWindowSize(640,480);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutCreateWindow(argv[0]);

InitGL();

glutDisplayFunc(DrawMainScene);
glutMainLoop();

return 0;
}


[This message has been edited by ngill (edited 07-25-2000).]

Relic
07-26-2000, 12:28 PM
Ok, to the rescue....

I have removed all things I haven't commented.
Haven't checked the program's output though.
The error seems to be in the first line of code.

GLfloat lightPos[]={0.0f, 5.0f, 0.0f, 0.0f}; // With fourth component == 0.0 this is a vector! Spotlights need positions. Try GLfloat lightPos[]={0.0f, 5.0f, 0.0f, 1.0f};

BTW, and check if the lightPos[]={0.0f, 1.0f, 0.0f, 0.0f}; is darker. If yes this answers the question if GL_NORMALIZE has to be enabled for direction vectors

(GL_LIGHT0,GL_POSITION,lightPos); // Spot lights are point lights, you specified a directional light! That doesn't fit together.

glLightfv(GL_LIGHT0,GL_SPOT_DIRECTION,spotdir); // no spotlight defined, no effect.
glLightf(GL_LIGHT0,GL_SPOT_CUTOFF,60.0f); // dito
glLightf(GL_LIGHT0,GL_SPOT_EXPONENT,128.0f); // dito. Once again, you will get a very tiny highliht and you'll probably miss it, if the tesselation is not fine enough!
glMateriali(GL_FRONT, GL_SHININESS,128); // Same here, this is the maximum allowed value. Try the default of 1.0 until you have your spotlight working.
glEnable(GL_COLOR_MATERIAL); // You only use the red color material in this program. As long as you won't specify a color-per-vertex this is overkill.
glClearColor(0.0f,0.0f,0.0f); // No Compiler error? glClearColor has four parameters.
// glColor3f(1.0f,0.0f,0.0f); // Initialized in DrawMainSzene below.

void DrawMainScene()
glColor3f(1.0f,0.0f,0.0f); // Ok, ambient and diffuse material initialized to red.
glNormal3f(0.0f,1.0f,0.0f); // Ok, all normals point up.
glBegin(GL_QUADS); // Move this here, if all vertices get the same normal, the driver has less things to do between begin-and


Tip: I would suggest a highly tesselated sphere for your next experiments,
because its easier to catch the highlights and makes interpretation of
light positions and direction vectors much simpler.