Diffuse shading / Bug

I’ve noticed that when I shade an object with diffuse reflection, there is sometimes a colour flipping effect that could be a bug… if you compile the code below you can see the colour flipping occuring on the low-res sphere above. I don’t think there should be any change in surface colour because the light isn’t moving relative to the objects.

I’d really like to get rid of this colour changing artefact… any suggestions?

(I’m using Mesa 3.4.2-10 / Redhat)

Thanks,

Josh.

#include <GL/glut.h>

GLfloat light_diffuse[] = {1.0, 0.0, 0.0, 1.0}; /* Red diffuse light. /
GLfloat light_specular[] = {0.0, 0.0, 0.0, 0.0};
GLfloat light_ambient[] = {0.0, 0.0, 0.0, 0.0};
GLfloat light_position[] = {1.0, 1.0, 1.0, 0.0}; /
Infinite light location. */
float theta = 0;

void
display(void)
{ float s, c;

s = sin(theta);
c = cos(theta);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(5c, 0.0, 5s,
0.0, 0.0, 0.0,
0.0, 1.0, 0.);
// Put infinite light source
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
// Draw spheres
glTranslatef(0,1,0);
glutSolidSphere(1,3,2);
glTranslatef(0,-2,0);
glutSolidSphere(1,20,20);

glutSwapBuffers();
}

void
init(void)
{
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);

glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);

glEnable(GL_DEPTH_TEST);

glMatrixMode(GL_PROJECTION);
gluPerspective(40.0, 1.0, 1.0, 10.0);
}

void idle() {
theta += 0.01;
glutPostRedisplay();
}

int
main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutCreateWindow(“Sphere”);
glutDisplayFunc(display);
glutIdleFunc(idle);
glShadeModel(GL_FLAT);
init();
glutMainLoop();
return 0; }

Your code looks correct to me. And works fine on my Redhat 9 box with Geforce2MX and NVIDIA driver 4363. Perhaps someone can check it out using Mesa. Btw what graphics board do you use?

I tried this code with several configurations:
[ul][li] Mesa 3, RH8: Lighting looks wrong and changes as the scene rotates[] nVidia’s GL (4363): Looks correct and stable[] Mesa 5: Also looks correct[/ul][/li]I found that if I change the light to be positional and moved it outside of the objects (10,10,10,1) the scene seems to look right in Mesa 3.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.