I'm new to OpenGL. I'm studying OpenGL 2.1 and I'm trying to make a little program to test the GL_SPOT_CUTOFF property, but when I set a value between 0.0-90.0, the light doesn't work and everything is dark.
The code:
void lightInit(void) {
GLfloat light0Position[] = {0.0,0.0,2.0,1.0};
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 45.0);
glLightfv(GL_LIGHT0, GL_POSITION, light0Position);
}
void reshapeFunc(int w, int h) {
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h)
glOrtho(-4, 4, -4*(GLfloat)h/(GLfloat)w, 4*(GLfloat)h/(GLfloat)w, -4.0, 4.0);
else
glOrtho(-4*(GLfloat)w/(GLfloat)h,4*(GLfloat)w/(GLfloat)h, -4, 4, -4, 4.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0,0,0,0,0,-1,0,1,0);
}
void displayFunc(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// center sphere
glutSolidSphere(1, 100,100);
// right sphere
glPushMatrix();
glTranslatef(3,0,0);
glutSolidSphere(1, 100,100);
glPopMatrix();
// left sphere
glPushMatrix();
glTranslatef(-3,0,0);
glutSolidSphere(1, 100,100);
glPopMatrix();
glutSwapBuffers();
}
void keyboardFunc(unsigned char key, int x, int y) {
if (key==27) {
exit(EXIT_SUCCESS);
}
}
int main(int argc, char** argv) {
//freeglut init and windows creation
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(600, 500);
glutInitWindowPosition(300, 100);
glutCreateWindow("OpenGL");
//glew init and errors check
GLenum err = glewInit();
if (GLEW_OK != err) {
fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
return 1;
}
fprintf(stdout, "---GLEW %s---\n\n", glewGetString(GLEW_VERSION));
//general settings
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_SMOOTH);
glEnable(GL_DEPTH_TEST);
//light settings
lightInit();
//callback functions
glutDisplayFunc(displayFunc);
glutReshapeFunc(reshapeFunc);
glutKeyboardFunc(keyboardFunc);
glutMainLoop();
return 0;
}
This code produces this image:
If I delete glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 45.0);
, the next image is produced:
Is there some kind of bug ?
GLfloat light0SpotDirection[] = {0.0,0.0,-1.0};
andglLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, light0SpotDirection);
to my code to give it a try, but it produced the same results. \$\endgroup\$