PDA

View Full Version : Anyone here familiar with OpenGL?



Khosan
2008-11-04, 10:23 PM
I could use a hand with something. I'm muddling about with lighting and having some difficulties. Point lights not giving off any lighting, spotlight not changing direction when I change the direction vector, some other things.

Here's what I have at the moment. Everything between works.

GLfloat pointLight[] = { 0.0, 0.0, 1.0, 1.0 }; //Point light's position
GLfloat pointDiffuse[] = { 0.0, 0.0, 1.0, 1.0 };

GLfloat distantLight[] = { -1.0, 1.0, -1.0, 0.0 }; //Distant light's position
GLfloat distantDiffuse[] = { 0.0, 1.0, 0.0, 1.0 };

GLfloat spotDirection[] = { 1.0, -1.0, -1.0 };
GLfloat spotDiffuse[] = { 1.0, 0.0, 0.0, 1.0 };
GLfloat spotExponent = 10.0;
GLfloat spotAngle = 45.0;

...

void myDraw()
{
// Clear the screen
glClearColor( 0.0, 0.0, 0.0, 1.0);
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

// Draw polygonal mesh
glPushMatrix();
pm.Draw();
glLightfv( GL_LIGHT0, GL_POSITION, pointLight );
glLightfv( GL_LIGHT0, GL_DIFFUSE, pointDiffuse );

glLightfv( GL_LIGHT1, GL_POSITION, distantLight );
glLightfv( GL_LIGHT1, GL_DIFFUSE, distantDiffuse );

glLightfv( GL_LIGHT2, GL_AMBIENT, spotDiffuse );
glLightfv( GL_LIGHT2, GL_DIFFUSE, spotDiffuse );
glLightf( GL_LIGHT2, GL_SPOT_EXPONENT, spotExponent );
glLightf( GL_LIGHT2, GL_SPOT_CUTOFF, spotAngle );

glutSolidSphere( 10.0, 100, 100 );
glPopMatrix();

// Swap buffers
glutSwapBuffers();
}

I missed class the day we handled lighting in OpenGL, so maybe I'm just missing some of the things that would be obvious now had I not been sick. Distant light works exactly as it should. Point light doesn't do anything, really. Spotlight 'works' but I use that term loosely, the direction vector doesn't have any noticeable effect.

EDIT: I'm also cheating on the polygonal mesh for now. I've got something else that handles that, but I'm using the solid sphere for convenience.

EDIT: Solved one mystery. Blue point light wasn't showing up because it was inside the sphere. Whoops. Still working on the spotlight, it doesn't seem to want to work as anything other than another directional light that comes from the front.

EDIT: Figured it out. Had to call in my brother. 'Twas a stupid error (for one, I deleted the directional setting somewhere along the line), involving mistyped 4th variables.