Olie
Olie

Reputation: 24675

Why doesn't my open gl ambient lighting work?

I got most of my lighting working the way I want, but I seem to be completely missing any ambient lighting. That is, the away-from-the-sun sides of things are completely black (I was hoping for 50% ambient), whereas the toward-the-sun sides seem lit as I'd expect.

Here's my lighting setup code (edited from original, see note below):

- (void) setupLighting
{
    const float amb = 2.0;
    const float LightAmbient[][4]  = {  { amb, amb, amb, 1.0f },
                                        { amb, amb, amb, 1.0f }
                                    };
    const float LightDiffuse[] [4] = {  { 1.0f, 1.0f, 1.0f, 1.0f },
                                        { 1.0f, 1.0f, 1.0f, 1.0f }
                                    };
    const float LightPosition[][4] = {  { 1.0f,  4.0f, 2.0f, 0.0f },
                                        { 0.0f, 10.0f, 0.0f, 1.0f }
                                    };

    glLightfv(GL_LIGHT0, GL_AMBIENT, LightAmbient[0]);
    glLightfv(GL_LIGHT0, GL_DIFFUSE, LightDiffuse[0]);
    glLightfv(GL_LIGHT0, GL_POSITION, LightPosition[0]);

    glLightfv(GL_LIGHT1, GL_AMBIENT, LightAmbient[1]);
    // etc., snip -- no LIGHT1 for this round

    glColorMaterial (GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
    glEnable(GL_COLOR);
    //glEnable(GL_COLOR_MATERIAL);

    glEnable(GL_LIGHT0);
    //glEnable(GL_LIGHT1);
    glEnable(GL_LIGHTING);
}

(NOTE: LIGHT1 commented out for now -- I'm just trying to get LIGHT0 to do what I want.)

Not sure if it matters, but I'm drawing unit-aligned, texture-mapped unit cubes (a la MineCraft) using glDrawArrays(). All my normals seem correct; if I leave lighting off, everything draws correctly (just flat-lit.)

Any hints?

Thanks!

Edit (Added screenshot.)

Here's a screenshot of the problem

Edit Per comments, I bumped ambient values up to 2.0, and now it looks like what I'd expect for about 50% ambient. That seems whacky -- I thought lighting values were pinned at [0, 1]...? What fundamental concept about ambient light am I not grokking?

Upvotes: 0

Views: 5159

Answers (1)

Christian Rau
Christian Rau

Reputation: 45948

In your code, you didn't enable GL_COLOR_MATERIAL. So if you did not set the ambient material color (by calling glMaterial with GL_AMBIENT) its default is (0.2, 0.2, 0.2), which should result in your screenshot when multiplied by (1,1,1) (the light's ambient).

Also, there's no need to use color values greater than 1 as these are clamped to [0,1] anyway.

Upvotes: 2

Related Questions