PDA

View Full Version : fixed lighting



whatman
02-21-2004, 02:27 PM
Hi, just started with opengl and im doing the rotating cube. The only problem I have is that the light seems to follow the rotation of the cube and I can not see the back of the cube when it comes into view as it is dark. I've looked in the faqs and done a search with some results, which hasn't helped me a lot.
Here is the code I have written so far, if anyone can help or point me in the right direction, I'd be grateful, cheers.

[CODE]
#include <windows.h>
#include <GL/gl.h>
#include <GL/glut.h>
#include <math.h>

GLfloat diffuseLight[] = {0.8f, 0.8f, 0.8f, 1.0f};
GLfloat specularLight[] = {1.0f, 1.0f, 1.0f, 1.0f};
GLfloat LightPosition[] = {0.0f, 0.0f, 0.0f, 1.0f};
GLfloat lightspotDirection[] = {1.0f, 1.0f, 1.0f};

GLint cube;
GLfloat angle = 0.0f;
void drawCube()
{
glPushMatrix();
glCallList(cube);
glPopMatrix();
}

void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
drawCube();
glutSwapBuffers();
}

void init()
{
glClearColor(0.0,0.0,0.0,0.0);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
glLightfv(GL_LIGHT0, GL_SPECULAR, specularLight);
glLightfv(GL_LIGHT0, GL_POSITION, LightPosition);
glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, lightspotDirection);

glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 180.0f);
glLightf(GL_LIGHT0, GL_SPOT_EXPONENT, 75.0f);
glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 1.0f);
glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 0.0f);
glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATION, 0.0f);

glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);

glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
glMaterialfv(GL_FRONT, GL_SPECULAR, specularLight);
glMateriali(GL_FRONT, GL_SHININESS, 128);
}
void myidle()
{
glLoadIdentity();
glTranslatef(0.0,0.0,-20.0);
angle = angle + 1.1f;
if(angle >= 360.0) angle = 0.0f;
glRotatef(angle, 1.0, 0.0, 1.0);
glutPostRedisplay();
}

void reshape(int width, int height)
{
GLfloat h = (GLfloat)height/(GLfloat) width;
glViewport(0,0,(GLint) width, (GLint) height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-1.0, 1.0, -h, h, 5.0, 1000.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

void setupGL()
{
glShadeModel(GL_SMOOTH);
glEnable(GL_DEPTH_TEST);
}
void makeDisplayList()
{
cube=glGenLists(1);
glNewList(cube,GL_COMPILE);
glBegin(GL_QUADS);
//draw cube with normals and colours
glEnd();
glEndList();
}


int main(int argc, char **argv)
{

glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUB LE);
glutInitWindowSize(640,480);
glutInitWindowPosition(0,0);
glutCreateWindow("Square");
setupGL();
makeDisplayList();
glutDisplayFunc(display);
glutIdleFunc(myidle);
glutReshapeFunc(reshape);
init();
glutMainLoop();

return 0;
}
[CODE]

[This message has been edited by whatman (edited 02-21-2004).]

[This message has been edited by whatman (edited 02-21-2004).]

[This message has been edited by whatman (edited 02-21-2004).]