OpenGL illumination on 3d objects problem

Discussion in 'Mac Programming' started by vicman28, Nov 26, 2008.

  1. vicman28 macrumors newbie

    Joined:
    Nov 26, 2008
    #1
    Hi everyone, i have been doing some openGL programming on my powerbook and when i started with the lighting i got some problems. First off i tried to illuminate a sphere with two lights to "mimic" a pool ball but there seemed to be a problem with the light, i had to rotate the sphere and only half of it hat the diffuse and specular lights on it, the other half was just plain color. On top of that when the sphere rotated the change between one illuminated side and the other came with a weird swoop thing that i did not like. My first impression was the normals that i forgot to calculate so i tried using the Solid Teapot that (so i have read) is the only primitive with precalculated normals. The teapot shows even weirder behavior. With the teapot not only lighting seems weird, but the teapot has a cut in the middle, and when it turns around you can see through it as if the handle of the teapot had no alpha but the body of the teapot had some, this happens with all the faces close to the camera and i don't know what is going on, any help would be appreciated. Below i add my display method, i tried disabling face culling but don't see any improvement.

    ****************************************************************
    void Display()
    {
    rot+=.01;

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    GLfloat light[]={1,1,1,1};
    glLightfv(GL_LIGHT0,GL_DIFFUSE,light);
    GLfloat light2[]={1,1,1,1};
    glLightfv(GL_LIGHT0,GL_SPECULAR,light2);
    GLfloat material[]={1,0,0
    ,1};
    glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,material);
    GLfloat material2[]={1,1,1,1};
    glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,material2);
    glMaterialf(GL_FRONT_AND_BACK,GL_SHININESS,45);
    GLfloat pos[]={13,5,-10,1};
    glLightfv(GL_LIGHT0,GL_POSITION,pos);
    glLightfv(GL_LIGHT1,GL_DIFFUSE,light);
    glLightfv(GL_LIGHT1,GL_SPECULAR,light2);

    glDisable(GL_CULL_FACE);

    GLfloat pos1[]={-5,-5,5,1};
    glLightfv(GL_LIGHT1,GL_POSITION,pos1);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glRotatef(rot,1,1,0);
    //glutSolidSphere(0.3,10,10);
    glutSolidTeapot(.3);




    glutSwapBuffers();
    glutPostRedisplay();

    }
    ****************************************************************
     
  2. lazydog macrumors 6502a

    Joined:
    Sep 3, 2005
    Location:
    Cramlington, UK
    #2
    Hi

    The 'see through' problem sounds like you haven't enabled depth tests, ie add this:-

    Code:
    glEnable( GL_DEPTH_TEST ) ;
    
    The lighting problems looks like it's because you haven't enabled light0 and light1:-

    Code:
    glEnable( GL_LIGHT0 ) ;
    glEnable( GL_LIGHT1 ) ;
    
    Also, you might want to change your colour material mode to GL_AMBIENT_AND_DIFFUSE.

    b e n
     
  3. vicman28 thread starter macrumors newbie

    Joined:
    Nov 26, 2008
    #3
    Ty for the fast reply, that is on the Init() in my code, i do the depth test and the enable lights there as in :

    ****************************************************************

    void Init()
    {
    glClearColor(0,0,0,1);
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glEnable(GL_LIGHT1);

    }

    ****************************************************************

    sorry i just showed my display, tough everything was there. Also i'm using GL_PROJECTION in the reshape method. I would like to know why you chose the GL_AMBIENT_AND_DIFFUSE i mean i understand it gives a better color match but didn't know it existed :p, tried using it but have the same problem with the teapot.
     
  4. vicman28 thread starter macrumors newbie

    Joined:
    Nov 26, 2008
    #4
    Pics

    Some pics of the behavior. Best regards.
     

    Attached Files:

  5. lazydog macrumors 6502a

    Joined:
    Sep 3, 2005
    Location:
    Cramlington, UK
    #5
    How about your glutInitDisplayMode(), have you assigned a depth buffer?

    Code:
    glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH ) ;
     
    The only other thing I can think of is you've set your camera up so that the part of the teapot falls behind your camera's near clip plane.

    As for the lighting, I tried your code and it worked fine for me. Oh one thing I added and forgot to say was:-

    Code:
    glEnable( GL_COLOR_MATERIAL ) ;
    
    b e n
     
  6. vicman28 thread starter macrumors newbie

    Joined:
    Nov 26, 2008
    #6
    Worked like a charm

    Ty very much the problem was with the glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH); didn't use the Depth buffer, never even crossed my mind thanks a lot for the help :D
     

Share This Page