Issue with lighting intensity

Okey, I’m fairly new to openGL but not to programming but now I’m faced with an issue that I cannot get a grip on.

I created a stationary light after which I draw a simple plain flat surface. The light is in front of the surface and I’m looking right at the surface with the light in between me and the surface.

Now the problem is that the further I position the light away from the object the brighter the object gets illuminated. And when the light is almost in front of the plane it’s completely dark.
I have no idea what the problem is with my lighting setup.

Including below a part of my source. This is a snippet from the paint function.



    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();
    glPushMatrix( );

    glRotatef(360.0f - rotX, 1.0f, 0.0f, 0.0f);
    glRotatef(360.0f - rotY, 0.0f, 1.0f, 0.0f);
    glTranslatef(-posX, -posY, -posZ);

    GLfloat light1_position [] = {posTest, 50, 50, 1}; // Static light
    GLfloat light1_diffuse [] = { 1.0, 1.0, 1.0, 1.0 };
    glLightfv ( GL_LIGHT1, GL_POSITION, light1_position );
    glLightfv ( GL_LIGHT1, GL_DIFFUSE, light1_diffuse );
    glEnable(GL_LIGHT1);

    GLfloat diff [] = { 0.7f , 0.5f , 0.0f, 1.0f };
    glMaterialfv ( GL_FRONT, GL_DIFFUSE, diff );

    glBegin(GL_QUADS);
    glNormal3f(1.0, 0.0, 0.0); //Back
    glVertex3f(0.0, 0.0, 0.0);
    glVertex3f(0.0, 100.0, 0.0);
    glVertex3f(0.0, 100.0, 100.0);
    glVertex3d(0.0, 0.0, 100.0);

    /*glNormal3f(-1.0, 0.0, 0.0); // Front
    glVertex3f(100.0, 0.0, 0.0);
    glVertex3f(100.0, 0.0, 100.0);
    glVertex3f(100.0, 100.0, 100.0);
    glVertex3f(100.0, 100.0, 0.0);

    glNormal3f(0.0, -1.0, 0.0); // Top
    glVertex3f(0.0, 100.0, 100.0);
    glVertex3f(100.0, 100.0, 100.0);
    glVertex3f(100.0, 100.0, 0.0);
    glVertex3f(0.0, 100.0, 0.0);

    glNormal3f(0.0, 1.0, 0.0); // Bottom
    glVertex3f(0.0, 0.0, 0.0);
    glVertex3f(0.0, 0.0, 100.0);
    glVertex3f(100.0, 0.0, 100.0);
    glVertex3f(100.0, 0.0, 0.0);

    glNormal3f(0.0, 0.0, -1.0); // Left
    glVertex3f(0.0, 0.0, 100.0);
    glVertex3f(0.0, 100.0, 100.0);
    glVertex3f(100.0, 100.0, 100.0);
    glVertex3f(100.0, 0.0, 100.0);

    glNormal3f(0.0, 0.0, 1.0); // Right
    glVertex3f(0.0, 0.0, 0.0);
    glVertex3f(0.0, 100.0, 0.0);
    glVertex3f(100.0, 100.0, 0.0);
    glVertex3f(100.0, 0.0, 0.0);*/

    glEnd();


The light position gets transformed by what is the current modelview matrix (so it ends up in eye space). This is often the cause of the light not working as expected. If you change the modelview the light is “moved” as well. Have you tried setting the desired position of the light earlier, for instance after you load in the identify matrix?

Since the light has to be stationary in the scene it’s position has to be set after the modelview matrix has been formed (if I’m not mistaken).
I tried setting a light before forming the modelview matrix (creating a moving light with the same settings) but the problem still remains. The closer the light gets to the object I want to render the darker it becomes, and if I put the light at large distance the object becomes very bright. While I expect behaviour the other way around.
I’m probably missing some very basic parameters in setting up my light, screwing up the way openGL calculates the intensity of the light

The issue seems to be solved.
Lighting was a mess because my surfaces weren’t tesselated enough. This made lighting go crazy when it came to close to the surface. After creating some more tesselation on my surfaces the lighting behaves as expected.