Frage

OpenGL: no light for a simple glut cube

Hi, i'm trying to study lights in opengl and glut but I have problems quite early. I have this code for making a lighted cube in a custom position with custom camera setting:

void testApp::draw(){
    static float amb[] =  {0.4, 0.4, 0.4, 0.0};
    static float dif[] =  {1.0, 1.0, 1.0, 0.0};
    
    float light_diffuse[] = {1.0, 0.0, 1.0, 1.0}; 
    float light_position[] = {-1.0, 1.0, 1.0, 0.0}; // i tried a lot of positions here!
    
    // set camera
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(0, 200, 1200, 0,0,0, 0,1,0);

    // set lights
    glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
    glLightfv(GL_LIGHT0, GL_POSITION, light_position);
    glEnable(GL_LIGHT0);
    glEnable(GL_LIGHT1);
    glEnable(GL_LIGHTING);
    glMaterialfv(GL_FRONT, GL_AMBIENT, amb);
    glMaterialfv(GL_FRONT, GL_DIFFUSE, dif);

    // draw scene
    ofScale(50, 50, 50);
    glPushMatrix();

    glutSolidCube(2);

    // close everything
    glPopMatrix();
    glDisable(GL_LIGHT0);
    glDisable(GL_LIGHT1);
    glDisable(GL_LIGHTING); }

I see only a black cube, no light, I think the problem is the light position, I've tried a lot of positions but it does not work: black cube.

War es hilfreich?

Lösung

You are really far away from your cube. Make it smaller and move it closer. Alternatively, change your lighting like this:

float light_diffuse[] = {100.0, 0.0, 100.0, 100.0}; 
float light_position[] = {-100.0, 100.0, 1.0, 0.0};

You will hardly see any light effects on a cube. Consider using a teapot.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top