PDA

View Full Version : Problem with lighting



02-16-2002, 10:15 AM
I'm a newbie to OpenGl and am trying to implement a directional light to look down the x-axis(illuminate all objects with apositive x value)

Without implementing lighting all my objects are visible and I can move see them as soon as I try and implement some lighting everything seems to disappear. I've included the major parts of program if the problems in them. Also the function drawCube() as it's name suggests draws cubes in certain positions along the x axis. The cubes have normals declared. Can anyone see what the problem is?


GLfloat materialSpecular[] = {0.5f, 0.5f, 0.5f, 1.0f} ;
GLfloat materialShininess[] = {8.0f} ;
GLfloat materialDiffuse[] = {0.0f, 0.2f, 0.0f, 1.0f} ;
GLfloat materialAmbient[] = {0.0f, 0.2f, 0.0f, 1.0f} ;

GLfloat lightAmbient[] = {1.0f, 1.0f, 1.0f, 1.0f} ;
GLfloat lightDiffuse[] = {1.0f, 1.0f, 1.0f, 1.0f} ;
GLfloat lightSpecular[] = {1.0f, 1.0f, 1.0f, 1.0f} ;
GLfloat globalAmbient[] = {0.5f, 0.5f, 0.5f, 1.0f} ;

GLfloat light0Position[] = {-1.0f, 0.0f, 0.0f, 0.0f} ;


void orient(float ang)
{

lx = sin(ang);
lz = -cos(ang);
glLoadIdentity();
gluLookAt(jx, jy, jz,
jx + lx,jy + ly,jz + lz,
0.0f,1.0f,0.0f);
}


void moveFlat(int i)
{

jx = jx + i*(lx)*0.1;
jz = jz + i*(lz)*0.1;
glLoadIdentity();
gluLookAt(jx, jy, jz,
jx + lx,jy + ly,jz + lz,
0.0f,1.0f,0.0f);
}

void moveVertically(int i)
{
jy = jy + i*(0.1);
glLoadIdentity();
gluLookAt(jx, jy, jz,
jx + lx,jy + ly,jz + lz,
0.0f,1.0f,0.0f);
}

void initScene()
{
glEnable(GL_DEPTH_TEST) ;
glShadeModel(GL_SMOOTH) ;
glEnable(GL_NORMALIZE) ;
glEnable(GL_CULL_FACE) ;

cube_display_list = createDL();
}

void renderScene(void)
{

if (deltaMove)
moveFlat(deltaMove);
if (deltaAngle) {
angle += deltaAngle;
orient(angle);
}
if (deltaVert)
moveVertically(deltaVert);


glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glEnable(GL_LIGHTING) ;
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, globalAmbient) ;

glLightfv(GL_LIGHT0, GL_POSITION,light0Position) ;
glLightfv(GL_LIGHT0, GL_AMBIENT,lightAmbient) ;
glLightfv(GL_LIGHT0, GL_DIFFUSE,lightDiffuse) ;
glLightfv(GL_LIGHT0, GL_SPECULAR,lightSpecular) ;


glMaterialfv(GL_FRONT, GL_SPECULAR, materialSpecular) ;
glMaterialfv(GL_FRONT, GL_SHININESS, materialShininess) ;
glMaterialfv(GL_FRONT, GL_DIFFUSE, materialDiffuse) ;
glMaterialfv(GL_FRONT, GL_AMBIENT, materialAmbient) ;

drawCube();

glutSwapBuffers();


}

dutchstud
02-16-2002, 05:37 PM
i don't see glEnable(GL_LIGHT0). i might just be missing it, but if you don't have it i think you need it.