Objects lit with OpenGL lighting fade to black on rotate (old bug I'm sure)

3 MB WMV movie showing the issue: http://vault101.co.uk/downloads/fog_glitch.wmv

It’s late/early (however you view it) and my brain is fried.
When rotating the camera left/right and even up/down any object that is lit using OpenGL lighting fades to black the further we get from an imaginary sweet spot.

I’m sure I’ve hit this issue before and can’t remember how I solved it, or what was causing it.

A quick search on this forum and another opengl one turned up nothing obvious but I’m half asleep :wink:

sample of the code - removed a load of setup junk included loading textures - hopefully this is useful in solving this, I’ve pseudo coded a heap of the tree/terrain junk as it’s very lengthy and spread across heaps of class files.
I just have my fingers crossed that I can find how I solved this last time (searches subversion repository logs) or someone can go “Ah yeah it’s cos of this…”

:wink:


pre() { // before 'running'
           ortho_mode();

           gl.glClearColor(fog_colour[0], fog_colour[1], fog_colour[2], fog_colour[3]);

            // set the fog attributes
            gl.glFogf(gl.GL_FOG_START, near_distance);
            gl.glFogf(gl.GL_FOG_END, far_distance);
            gl.glFogfv(gl.GL_FOG_COLOR, fog_colour, 0);
            gl.glFogi(gl.GL_FOG_MODE, gl.GL_EXP);
            gl.glFogf(gl.GL_FOG_DENSITY, fog_density);

            // enable the fog
            gl.glEnable(gl.GL_FOG);

            // enable the default light
            gl.glEnable(gl.GL_LIGHTING);
            gl.glEnable(gl.GL_LIGHT0);

            if (!gl.glIsEnabled(gl.GL_DEPTH_TEST)) {
                gl.glEnable(gl.GL_DEPTH_TEST);
            }
}

main_loop() {
    draw_perspective();
    draw_ortho();
}

draw_perspective() {
 if (!perspective_mode)
    perspective_mode();

gl.glLoadIdentity();

gl.glPolygonMode(gl.GL_FRONT_AND_BACK, gl.GL_FILL);
glu.gluLookAt(MEngine.g_Camera.m_vPosition.x,
                    MEngine.g_Camera.m_vPosition.y,
                    MEngine.g_Camera.m_vPosition.z,
                    MEngine.g_Camera.m_vView.x,
                    MEngine.g_Camera.m_vView.y,
                    MEngine.g_Camera.m_vView.z,
                    MEngine.g_Camera.m_vUpVector.x,
                    MEngine.g_Camera.m_vUpVector.y,
                    MEngine.g_Camera.m_vUpVector.z);

pseudo -> turn off depth testing

pseudo -> draw sky box

pseudo -> turn on depth testing

pseudo -> turn on face culling (front)

pseudo -> draw terrain using display lists

pseudo -> cull face (back)

pseudo -> draw billboards

pseudo -> draw trees using display lists

pseudo -> turn off depth testing

pseudo -> turn off face culling

}

draw_ortho() {
 if (!ortho_mode)
    ortho_mode();

  // nothing here yet but a call to ortho_mode ensures we're still pushing those matricies to avoid GL_STACK_UNDERFLOW
}

perspective_mode() {
        gl.glMatrixMode(gl.GL_PROJECTION);
        gl.glPopMatrix();
        gl.glMatrixMode(gl.GL_MODELVIEW);
        gl.glPopMatrix();
        if (!gl.glIsEnabled(gl.GL_CULL_FACE))
        gl.glEnable(gl.GL_CULL_FACE);
        if (!gl.glIsEnabled(gl.GL_DEPTH_TEST))
        gl.glEnable(gl.GL_DEPTH_TEST);
}

ortho_mode() {
        if (gl.glIsEnabled(gl.GL_CULL_FACE))
              gl.glDisable(gl.GL_CULL_FACE); // Disable Face Culling, as this won't show blended

        if (gl.glIsEnabled(gl.GL_DEPTH_TEST))
             gl.glDisable(gl.GL_DEPTH_TEST);

        gl.glMatrixMode(gl.GL_PROJECTION);
        gl.glPushMatrix();
        gl.glLoadIdentity();
        gl.glOrtho(0, SL.getInt(SL.I_SCREENWIDTH), SL.getInt(SL.I_SCREENHEIGHT), 0, 0, 1);
        gl.glMatrixMode(gl.GL_MODELVIEW);
        gl.glPushMatrix();
        gl.glLoadIdentity();
}

Ug… partly fixed. Forgot the light position. Now it’s a bit on the dark side but at least the light doesn’t change with the view.

TIP: A long stint coding is a double edged sword. Contains flashes of pure genius with some really stupid glitches to boot. yawn!

Make sure you’re calling the glLightfv for the position vector after you’ve set the modelview matrix to that of your camera. When glLightfv is called for the position, OpenGL stores the the vector after being transformed by the current modelview matrix. From the sounds of your second post, you’ve already done this though (maybe?)