Thanks for your help, dimebolt.
Simple things first: I have no idea why that second image isn't showing up. I uploaded it to rapidshare just like I did with the first screen, and the first one works...
Here is a new link that works!
Now to the problem: Things look allright when I don't move the light ( it starts at 0, 0, 0 ). I can then move the camera around and do everything I want to and it all looks fine.
But when I move the light around, it suddenly starts to flicker and very soon all is black.
Here is more code, that you requested:
I initialize a light like this:// initialize a lightfloat ambient[] = { 1.0f, 0.0f, 0.0f, 1.0f };float diffuse[] = { 1.0f, 0.0f, 0.0f, 1.0f };float specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };float position[] = { 0.0f, 0.0f, 0.0f };lgt.init ( ambient, diffuse, specular );lgt.set_position ( position );lgt.toggle ( true );
Those are the definitions of the functions used above:void light::init ( float ambient[], float diffuse[], float specular[] )// initializes a light{ glLightfv ( GL_LIGHT1, GL_AMBIENT, ambient ); glLightfv ( GL_LIGHT1, GL_DIFFUSE, diffuse ); glLightfv ( GL_LIGHT1, GL_SPECULAR, specular ); glLightModeli ( GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE ); glEnable ( GL_LIGHT1 );}void light::toggle ( void )// toggles a light on or off{ if ( mode == true ) // if light is activated { glDisable ( GL_LIGHT1 ); mode = false; } else // if light is deactivated { glEnable ( GL_LIGHT1 ); mode = true; }}void light::toggle ( bool new_mode )// switch a light to a specific state{ mode = new_mode; if ( mode == true ) // if light is activated glEnable ( GL_LIGHT1 ); else // if light is activated glDisable ( GL_LIGHT1 );}void light::set_position ( float light_position[] )// updates the position of the light{ // reset the matrix glLoadIdentity ( ); // position the camera cam.update ( ); // move the light glLightfv ( GL_LIGHT1, GL_POSITION, light_position );}
Here is my object::draw ( ) function, that draws models from 3ds files:[source lang="cpp]void object::draw ( )// draws the object normally to the screen{ // reset the matrix glLoadIdentity ( ); // position the camera cam.update ( ); // update acceleration values update_position ( ); // move and rotate to place the object glTranslatef ( position.x, position.y, position.z ); glRotatef ( rotation.x, 1.0f, 0.0f, 0.0f ); glRotatef ( rotation.y, 0.0f, 1.0f, 0.0f ); glRotatef ( rotation.z, 0.0f, 0.0f, 1.0f ); // set up render modes glColorMask ( 1, 1, 1, 1 ); glColor4f ( 1.0f, 1.0f, 1.0f, 1.0f ); glDisable ( GL_BLEND ); glDisable ( GL_CLIP_PLANE0 ); glEnable ( GL_DEPTH_TEST ); glDisable ( GL_STENCIL_TEST ); if ( sphere_mapped == true ) // if the object is sphere mapped { // enable sphere mapping glEnable ( GL_TEXTURE_GEN_S ); glEnable ( GL_TEXTURE_GEN_T ); } else // if the object is texture mapped { // disable shere mapping glDisable ( GL_TEXTURE_GEN_S ); glDisable ( GL_TEXTURE_GEN_T ); } // activate texture surface_texture.activate ( ); // push the matrix glPushMatrix ( ); // scale glScalef ( size, size, size ); for ( int i = 0; i < actor.num_polygons; i++ ) // loop through each polygon { float vertex1[3], vertex2[3], vertex3[3]; // prepare vertex data vertex1[0] = actor.vertex[actor.polygon.a ].x; vertex1[1] = actor.vertex[actor.polygon.a ].y; vertex1[2] = actor.vertex[actor.polygon.a ].z; vertex2[0] = actor.vertex[actor.polygon.b ].x; vertex2[1] = actor.vertex[actor.polygon.b ].y; vertex2[2] = actor.vertex[actor.polygon.b ].z; vertex3[0] = actor.vertex[actor.polygon.c ].x; vertex3[1] = actor.vertex[actor.polygon.c ].y; vertex3[2] = actor.vertex[actor.polygon.c ].z; // get the face normal get_face_normal ( actor.normal, vertex1, vertex2, vertex3 ); // multiply it by -1 actor.normal[0] *= -1; actor.normal[1] *= -1; actor.normal[2] *= -1; cgGLSetParameter3f ( vertexPosition, vertex1[0], vertex1[1], vertex1[2] ); cgGLSetParameter3f ( vertexNormal, actor.normal[0], actor.normal[1], actor.normal[2] ); /*glBegin ( GL_LINES ); glVertex3f ( actor.vertex[actor.polygon.a ].x, actor.vertex[actor.polygon.a ].y, actor.vertex[actor.polygon.a ].z ); glVertex3f ( vertex1[0], vertex1[1], vertex1[2] ); glVertex3f ( actor.vertex[actor.polygon.b ].x, actor.vertex[actor.polygon.b ].y, actor.vertex[actor.polygon.b ].z ); glVertex3f ( vertex2[0], vertex2[1], vertex2[2] ); glVertex3f ( actor.vertex[actor.polygon.c ].x, actor.vertex[actor.polygon.c ].y, actor.vertex[actor.polygon.c ].z ); glVertex3f ( vertex3[0], vertex3[1], vertex3[2] ); glEnd ( );*/ // begin drawing polygon glBegin ( GL_TRIANGLES ); // activate face normal glNormal3f ( actor.normal[0], actor.normal[1], actor.normal[2] ); // draw first vertex glTexCoord2f ( actor.mapcoord[actor.polygon.a].u, actor.mapcoord[actor.polygon.a].v ); glVertex3f ( actor.vertex[actor.polygon.a ].x, actor.vertex[actor.polygon.a ].y, actor.vertex[actor.polygon.a ].z ); // draw second vertex glTexCoord2f ( actor.mapcoord[actor.polygon.b].u, actor.mapcoord[actor.polygon.b].v ); glVertex3f ( actor.vertex[actor.polygon.b].x, actor.vertex[actor.polygon.b].y, actor.vertex[actor.polygon.b].z ); // draw third vertex glTexCoord2f ( actor.mapcoord[actor.polygon.c].u, actor.mapcoord[actor.polygon.c].v ); glVertex3f ( actor.vertex[actor.polygon.c].x, actor.vertex[actor.polygon.c].y, actor.vertex[actor.polygon.c].z ); // done drawing polygon glEnd ( ); } // pop the matrix glPopMatrix ( ); // disable sphere mapping glDisable ( GL_TEXTURE_GEN_S ); glDisable ( GL_TEXTURE_GEN_T );}
Do you need any more code parts?