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);
glLoadIdentity();
glLoadMatrixf(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);
glLoadIdentity();
gluPerspective(90, width/(float) height, 0.01, 10);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
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);
glLoadIdentity();
glTranslated(0, 0, 0.05);
storeModelMatrix();
break;
case ‘s’:
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslated(0, 0, -0.05);
storeModelMatrix();
break;
case ‘a’:
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(-2, 0, 1, 0);
storeModelMatrix();
break;
case ‘d’:
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(2, 0, 1, 0);
storeModelMatrix();
break;
case ‘z’: glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslated(0, -0.05, 0);
storeModelMatrix();
break;
case ‘x’: glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslated(0, 0.05, 0);
storeModelMatrix();
break;
case ‘n’: glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslated(0.05, 0, 0);
storeModelMatrix();
break;
case ‘m’: glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
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);
glShadeModel(GL_FLAT);
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();