PDA

View Full Version : Help with lighting



JWizard
11-12-2003, 02:01 PM
Hey,

I was trying to get my head around how lighting works in OpenGL and after reading some of the lighting chapter in the red book I decided to write a small test program but the results are not quite what I expected. I was expecting to see a spotlight (i.e., a "circle of light") on the quad, but the results are quite different.
Am I doing something fundamentally wrong or am I just misunderstanding the way lighting works ?

Here is the code:




#include <GLUT/glut.h>
#include <cstdio>

void initGLUT(int *argc, char **argv);
void initGL();
void setupLights();
void displayFunc();
void keyPressed(unsigned char, int, int);
void arrowKeys(int theKey, int, int);

int theWindow = 0;
bool light = true;

int main(int argc, char **argv)
{
initGLUT(&amp;argc, argv);
initGL();
setupLights();
glutMainLoop();
return 0;
}

void initGLUT(int *argc, char **argv)
{
glutInit(argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_RGBA | GLUT_DOUBLE);
glutInitWindowPosition(100, 100);
glutInitWindowSize(800, 600);
theWindow = glutCreateWindow("Lighting");
glutDisplayFunc(displayFunc);
glutKeyboardFunc(keyPressed);
glutSpecialFunc(arrowKeys);
}

void initGL()
{
glDepthFunc(GL_LEQUAL);
glEnable(GL_DEPTH_TEST);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, 4.0/3.0, 0.01, 40.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0f, 0.0f, -26.0f);
}


GLfloat light0_position[] = {0.0f, 0.0f, 6.0f, 1.0f};
GLfloat cutoff = 90.0f;

void setupLights()
{
GLfloat light0_color[] = {1.0f, 1.0f, 0.0f, 1.0f};
GLfloat exponent = 100.0f;
glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_color);
glLightfv(GL_LIGHT0, GL_POSITION, light0_position);
glLightf(GL_LIGHT0, GL_SPOT_EXPONENT, exponent);
glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, cutoff);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glShadeModel(GL_SMOOTH);
}

void displayFunc()
{
printf("(%.2f, %.2f, %.2f) : %.2f\n", light0_position[0], light0_position[1], light0_position[2], cutoff);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
GLfloat emitted[] = {1.0f, 1.0f, 1.0f};
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, emitted);
glMaterialfv(GL_FRONT, GL_SPECULAR, emitted);
glMaterialf(GL_FRONT, GL_SHININESS, 80.0f);
glNormal3f(0.0f, 0.0f, 1.0f);
glBegin(GL_QUADS);
glVertex2i(-10, -10);
glVertex2i(10, -10);
glVertex2i(10, 10);
glVertex2i(-10, 10);
glEnd();
glutSwapBuffers();
}

void keyPressed(unsigned char theKey, int, int)
{
if(theKey == 'l')
{
light = !light;
if(light) glEnable(GL_LIGHT0); else glDisable(GL_LIGHT0);
}
else if(theKey == 'o')
{
light0_position[2]+=0.1f;
}
else if(theKey == 'i')
{
light0_position[2]-=0.1f;
}
else if(theKey == 'a')
{
cutoff--;
}
else if(theKey == 's')
{
cutoff++;
}
glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, cutoff);
glLightfv(GL_LIGHT0, GL_POSITION, light0_position);
glutPostRedisplay();
}

void arrowKeys(int theKey, int, int)
{
switch(theKey)
{
case GLUT_KEY_UP:
light0_position[1]+=0.1f;
break;
case GLUT_KEY_DOWN:
light0_position[1]-=0.1f;
break;
case GLUT_KEY_LEFT:
light0_position[0]-=0.1f;
break;
case GLUT_KEY_RIGHT:
light0_position[0]+=0.1f;
break;
}
glLightfv(GL_LIGHT0, GL_POSITION, light0_position);
glutPostRedisplay();
}

chowe6685
11-12-2003, 03:57 PM
Lighting is per vertex
http://www.opengl.org/developers/code/features/KilgardTechniques/oglpitfall/oglpitfall.html

read that (#2 I beleive) it should help

JWizard
11-12-2003, 04:16 PM
Thanks a lot