# What's wrong with this code?

Hi,

Sorry for posting this in this forum. It’s just a silly question about lighting on a cube, but I’ve been posting this question for days on various forums and newsgroups and in the beginners forum and so far noone has been able to give an answer on what’s causing the errenous lighting. Not even my lab assistant in school today. I’m using an accumulated matrix to navigate around a cube which’s put in some light. I know it’s quite cumbersome and unneccesary to deal with all the hazzles with storing away an accumulated matrix like this, but this cube example is just the problem pinpointed. I’m using this specific method to navigate around a village that I’ve modeled as an assignment for school, so if I get the lighting to behave correctly on the cube, I suppose I can apply that to the larger school-assignment. Anyway, using the code below, the sides of the cube will flicker as you move around it and if you go inside, even the inside will be lit… What am I doing wrong in my code below? Maybe I’m just missing out on something in the initprocedure?

Best regards,

Simon

#include <Gl/glut.h>

GLfloat modelview[16];
GLfloat lightPosition[] = {0, 1, 0.5, 0.0};

void display() {
GLfloat matProp[] = {1,0,0,0};
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, matProp );
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, matProp);
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, matProp);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glMatrixMode(GL_MODELVIEW);
//glLightfv(GL_LIGHT0, GL_POSITION, lightPosition); Use this line or not?

glBegin(GL_POLYGON);
glNormal3f(0,0,1);
glVertex3f(0,0,0);
glVertex3f(1,0,0); //front
glVertex3f(1,1,0);
glVertex3f(0,1,0);
glEnd();

glBegin(GL_POLYGON);
glNormal3f(1,0,0);
glVertex3f(1,0,-1);
glVertex3f(1,1,-1); //right
glVertex3f(1,1,0);
glVertex3f(1,0,0);
glEnd();

glBegin(GL_POLYGON);
glNormal3f(0,0,-1);
glVertex3f(1,0,-1);
glVertex3f(0,0,-1); //back
glVertex3f(0,1,-1);
glVertex3f(1,1,-1);
glEnd();

glBegin(GL_POLYGON);
glNormal3f(-1,0,0);
glVertex3f(0,0,-1);
glVertex3f(0,0,0); //left
glVertex3f(0,1,0);
glVertex3f(0,1,-1);
glEnd();

glBegin(GL_POLYGON);
glNormal3f(0,1,0);
glVertex3f(0,1,0); //top
glVertex3f(1,1,0);
glVertex3f(1,1,-1);
glVertex3f(0,1,-1);
glEnd();

glBegin(GL_POLYGON);
glNormal3f(0,-1,0);
glVertex3f(0,0,0);
glVertex3f(0,0,-1); //bottom
glVertex3f(1,0,-1);
glVertex3f(1, 0, 0);
glEnd();

glutSwapBuffers();
}

void myReshape(int width, int height) {
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
gluPerspective(90, width/(float) height, 0.01, 10);
glMatrixMode(GL_MODELVIEW);
gluLookAt(0,0.5,1, 0,0.5,-0.5, 0,1,0);
glGetFloatv(GL_MODELVIEW_MATRIX, modelview);
}

void storeModelMatrix() {
glMultMatrixf(modelview);
glGetFloatv(GL_MODELVIEW_MATRIX, modelview);
}

void Keyfunc(unsigned char key, int x, int y) {
switch (key) {
case ‘w’:
glMatrixMode(GL_MODELVIEW);
glTranslated(0, 0, 0.05);
storeModelMatrix();
break;
case ‘s’:
glMatrixMode(GL_MODELVIEW);
glTranslated(0, 0, -0.05);
storeModelMatrix();
break;
case ‘a’:
glMatrixMode(GL_MODELVIEW);
glRotatef(-2, 0, 1, 0);
storeModelMatrix();
break;
case ‘d’:
glMatrixMode(GL_MODELVIEW);
glRotatef(2, 0, 1, 0);
storeModelMatrix();
break;
case ‘z’: glMatrixMode(GL_MODELVIEW);
glTranslated(0, -0.05, 0);
storeModelMatrix();
break;
case ‘x’: glMatrixMode(GL_MODELVIEW);
glTranslated(0, 0.05, 0);
storeModelMatrix();
break;
case ‘n’: glMatrixMode(GL_MODELVIEW);
glTranslated(0.05, 0, 0);
storeModelMatrix();
break;
case ‘m’: glMatrixMode(GL_MODELVIEW);
glTranslated(-0.05, 0, 0);
storeModelMatrix();
break;
}
glutPostRedisplay();
}

void init() {
GLfloat specular[] = {1.0, 1.0, 1.0, 1.0};
glClearColor(0x87/(float) 0xFF, 0xCE/(float) 0xFF, 0xEB/(float) 0xFF, 0.0);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glEnable(GL_DEPTH_TEST);
glLightfv(GL_LIGHT0, GL_SPECULAR, specular);
glLightfv(GL_LIGHT0, GL_DIFFUSE, specular);
glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
}

int main(int argc, char** argv) {
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(640, 480);
glutCreateWindow(“test”);
glutReshapeFunc(myReshape);
glutDisplayFunc(display);
glutKeyboardFunc(Keyfunc);
init();
glutMainLoop();

Light position have to be traansformed by the modelview matrix, so you have to uncomment the
glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
line…

Mmmm, I thought so too, but it won’t help a bit actually. And I can’t see why! Gah!

It seems to work if you comment out the glMaterial commands. I don’t really understand why is this, it seems like some values conflict with each other…

Next time, please use the code tag. Or even edit this message to make it clearer.

GLfloat lightPosition[] = {0,1,0.5,0.0}

Since w = 0, this is a directional light and not a positional light. Is that what you want?

You should probably replace glBegin(GL_POLYGON) with glBegin(GL_QUADS). They are both correct, but the second is more clear.

You don’t call glLightModel. Maybe the flicker that you are seeing are inaccuracies in calculating the specular color.

>Next time, please use the code tag. Or even edit this message to make it clearer.

Yeah, sorry about that. The code was properly indented when I posted though, promise

>Since w = 0, this is a directional light and not a positional light. Is that what you want?

Yes.

>You should probably replace glBegin(GL_POLYGON) with glBegin(GL_QUADS). They are both correct, but the second is more clear.

Since I’m using a vector of polygons to display the village in my larger assignment, I figured I’d use the same style here.

>You don’t call glLightModel. Maybe the flicker that you are seeing are inaccuracies in calculating the specular color.

Mmm, you’re right, I don’t. But I do that in the “village-program” and it didn’t help. /

I don’t like the specular level of the light you have chosen. It is overpowering the diffuse light. This can produce the flickering light you see, especially for low poly or rather, coarse poly meshes.

Edit: Just noticed you are also using the flat shading model. That will amplify flickering I mentioned above as well.

[This message has been edited by DFrey (edited 02-25-2003).]

I agree with Catman, at the moment you’re specifying the light in… screen-space?

I had a similar problem a while ago, but i can’t remember how i solved it. It definately had something to do with either the specular or the ambient values in the material…

Try setting up the specular and ambient to black and see if you still get the problem.

Y.

I got around to running it, and simply decresing the value in the specular light or specular material removes the problem for the most part.