Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 5 of 5

Thread: material lighting not working correctly

  1. #1
    Intern Contributor
    Join Date
    Apr 2002
    Posts
    85

    material lighting not working correctly

    This is driving me crazy! I set all materials to black and it still lights the model! Why does it do this??? I don't have any ambient light. I only have diffuse.

    Here is some code:

    void PositionLights(void)
    {

    GLfloat gLightPosition[] = {0.0f, 0.0f, 1.0f, 1.0f};
    float globalAmbient[] = {0.f, 0.f, 0.f, 1.0f};
    float ambient[] = {0.f, 0.f, 0.f, 1.0f};
    float diffuse[] = {1.f, 1.f, 1.f, 1.0f};
    float specular[] = {0.f, 0.f, 0.f, 1.0f};

    //glLightModelfv(GL_LIGHT_MODEL_AMBIENT, globalAmbient);

    //setup the one directional light
    glLightfv(GL_LIGHT0, GL_POSITION, gLightPosition);
    glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
    glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
    glLightfv(GL_LIGHT0, GL_SPECULAR, specular);

    // draw a sphere where the light is
    glDisable(GL_LIGHTING);
    glColor3f(0.75f, 0.75f, 0.0f);

    glPushMatrix();

    glTranslatef(gLightPosition[0], gLightPosition[1], gLightPosition[2]);
    glutWireSphere(0.5, 24, 24);

    glPopMatrix();

    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);


    }

    And in the drawing...

    // set the material ambient color


    materialColor[0] = 0.0f;
    materialColor[1] = 0.0f;
    materialColor[2] = 0.0f;
    materialColor[3] = 1.0f;
    glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, materialColor);

    // set the material diffuse color
    materialColor[0] = 0.0f;
    materialColor[1] = 0.0f;
    materialColor[2] = 0.0f;
    materialColor[3] = 1.0f;
    glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, materialColor);


    // set the material specular color

    materialColor[0] = 0.0f;
    materialColor[1] = 0.0f;
    materialColor[2] = 0.0f;
    materialColor[3] = 1.0f;
    glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, materialColor);

    // set the material shininess factor
    float shininess;
    shininess = 0.0f;
    glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, &shininess);

    ...

    glEnable(GL_TEXTURE_2D);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    glEnable(GL_COLOR_MATERIAL);

    // set the texture id we stored in the map user data

    CBitmapInfo* pBitmapInfo = (CBitmapInfo*) pCalRenderer->getMapUserData(0);

    if (pBitmapInfo != NULL)
    {

    glBindTexture(GL_TEXTURE_2D, pBitmapInfo->GetId() );

    }

    // set the texture coordinate buffer
    glTexCoordPointer(2, GL_FLOAT, 0, &meshTextureCoordinates[0][0]);
    glColor3f(1.0f, 1.0f, 1.0f);

    ...Then I go on to draw the polygons

    But why is my model even being lit at all?

    Thanks in advance!
    P4 2Ghz, GEFORCE4, 512 Meg RAM, WIN2000, VStudio 6.0

  2. #2
    Junior Member Regular Contributor
    Join Date
    Oct 2002
    Posts
    205

    Re: material lighting not working correctly

    You disable lighting before you draw the wireteapot. try putting glEnable(GL_LIGHTING) and glEnable(GL_LIGHT0) before the glPushMatrix();

  3. #3
    Intern Contributor
    Join Date
    Apr 2002
    Posts
    85

    Re: material lighting not working correctly

    Actually, that wireframe sphere is supposed to have lighting disabled. It represents the light position.

    The problem happens down at the bottom of the code where I draw the polygons for the model. It seems to me that it should not be lit at all because all of the materials are 0.0f

    But, it is still being lit for some reason.
    P4 2Ghz, GEFORCE4, 512 Meg RAM, WIN2000, VStudio 6.0

  4. #4
    Senior Member OpenGL Pro
    Join Date
    Jun 2000
    Location
    Shreveport, LA, USA
    Posts
    1,757

    Re: material lighting not working correctly

    With GL_COLOR_MATERIAL enabled, OpenGL gets the material color (or a portion thereof) from the current color, not from the material.

    [This message has been edited by DFrey (edited 01-27-2003).]

  5. #5
    Intern Contributor
    Join Date
    Apr 2002
    Posts
    85

    Re: material lighting not working correctly

    Yes, that was it. Thanks DFrey!
    P4 2Ghz, GEFORCE4, 512 Meg RAM, WIN2000, VStudio 6.0

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •