0
\$\begingroup\$

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:

enter image description here

If I delete glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 45.0);, the next image is produced:

enter image description here

Is there some kind of bug ?

\$\endgroup\$
  • \$\begingroup\$ Look at this related question, does it give you the info you need? \$\endgroup\$ – user39686 Jan 21 '15 at 23:29
  • \$\begingroup\$ Nop. I didn't set GL_SPOT_DIRECTION because I wanted it to be as default (0.0,0.0,-1.0). When I read the post that you linked above, I added GLfloat light0SpotDirection[] = {0.0,0.0,-1.0}; and glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, light0SpotDirection); to my code to give it a try, but it produced the same results. \$\endgroup\$ – Facundo Laffont Jan 21 '15 at 23:58
  • \$\begingroup\$ That post has fully functioning code demonstrating how to do this, minus what was fixed in the accepted answer. You can derive any changes to fix your own issue from it. Also, the accepted answer has the best piece of advice: "If you are learning OpenGL now, I strongly recommend that you do not learn that stuff at all. OpenGL lighting is part of the fixed function pipeline and deprecated since OpenGL 3.0. You should use the programmable pipeline ("shaders") nowadays." \$\endgroup\$ – user39686 Jan 22 '15 at 0:04
  • \$\begingroup\$ I copied the entire code plus the correction but it didn't work :(. I think I'm going to start using shaders instead. Thanks anyway. \$\endgroup\$ – Facundo Laffont Jan 22 '15 at 0:24
  • 1
    \$\begingroup\$ You should really move away from deprecated OpenGL stuff. Check this site out for modern OpenGL tutorials. \$\endgroup\$ – Soapy May 22 '15 at 14:23
0
\$\begingroup\$

Code works for me, exactly as presented in the question (after adding the missing #include lines). (Ubuntu 14.10, AMD Radeon HD 5870, running the latest Catalyst drivers)

Middle sphere is hit by the spotlight, left and right spheres are not.

enter image description here

Whatever your problem is, it's presumably not the code (although I haven't tested this on any devices other than the one mentioned above. Possible that there's an "undefined behaviour" bug which happens to work nicely under my driver but which isn't working so nicely under yours).

\$\endgroup\$
  • \$\begingroup\$ Thanks, Trevor. The code works fine on another computer. I guess it's simply a compatibility problem. \$\endgroup\$ – Facundo Laffont Jun 30 '16 at 3:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.