Lighting (screens inside)...

Started by
12 comments, last by d h k 18 years, 6 months ago
Hello everybody, this thread actually started in the Maths area, because I was sure, that my problem with lighting in OpenGL comes from some wrong normal calculation, but it seems like that is not the case. Thus I continue the thread here. I load objects as .3ds models into my application. I already checked the CW or CCW orientation of the triangles, but it turned out that they are consistent. I calculate the normals from my model data ( which is also correct, since I use the same data to actually draw my models ) and then I try to add lighting ( ambient, diffuse and specular for now ). My light is initially at position 0, 0, 0. When it is there, everything looks fine. The correct faces are lit up or not lit up according to their position/orientation. This is how it looks when the light is at the origin and everything is fine ( the light is at the origin of the particle system ). Now when I move my light in any direction, the faces start to flicker and sometimes wrong faces are lit up etc. Actually pretty much all the lighting messes up big time. This is how it looks when the lighting is messed up. And when I move the light even further away from the origin, the whole diffuse color ( which is the light component that messes up by the way ) disappears, and everything is lit only using the ambient color. So, this is how it looks now. I don't know what could be causing this problem. Here are the relevant code parts: Part of my initialization code...

	// enable texture mapping
	glEnable ( GL_TEXTURE_2D );

	// enable smooth shading
	glShadeModel ( GL_SMOOTH );

	// enable lighting
	glEnable ( GL_LIGHTING );

	// enable normalizing
	glEnable ( GL_NORMALIZE );

	glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
	glEnable(GL_COLOR_MATERIAL);

	// set background color
	glClearColor ( 0.6f, 0.6f, 0.6f, 1.0f );

	// depth buffer setup
	glClearDepth ( 1.0f );

	// initialize a light
	float ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
	float diffuse[] = { 1.0f, 0.0f, 0.0f, 1.0f };
	float specular[] = { 12.0f, 12.0f, 12.0f, 12.0f };
	lgt.init ( ambient, diffuse, specular );
	lgt.toggle ( true );
}


My complete object::draw ( ) function...

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] );

		// 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 );
}


My light class definitions ( that I used in the initialization code 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 );
}



My normal calculations ( should be correct though )...

void cross_product ( float *c,float a[3], float b[3] )
// finds the cross product of two vectors
{  
	c[0] = a[1] * b[2] - b[1] * a[2];
	c[1] = a[2] * b[0] - b[2] * a[0];
	c[2] = a[0] * b[1] - b[0] * a[1];
}

void normalize ( float * vect )
// scales a vector to a length of 1
{
	float length;
	int a;

	length = ( float ) sqrt ( pow ( vect[0], 2 ) + pow ( vect[1], 2 ) + pow ( vect[2], 2 ) );

	for ( a = 0; a < 3; ++a )
	// divides vector by its length to normalize
	{
		vect[a] /= length;
	}
}

void get_face_normal ( float *norm, float pointa[3], float pointb[3], float pointc[3] )
// gets the normal of a face
{
	float vect[2][3];
	int a,b;
	float point[3][3];

	for ( a = 0; a < 3; ++a )
	// copies points into point[][]
	{
		point[0][a]=pointa[a];
		point[1][a]=pointb[a]; 
		point[2][a]=pointc[a];
	}

	for ( a = 0; a < 2; ++a )
	// calculates vectors from point[0] to point[1]
	{                       
		for ( b = 0; b < 3; ++b )
		// and point[0] to point[2]
		{
			vect[a] = point[2-a] - point[0];      
		}
	}

	// calculates vector at 90° to to 2 vectors
	cross_product ( norm, vect[0], vect[1] );

	// makes the vector length 1
	normalize ( norm );
}


Can anybody help me? What is wrong? [Edited by - d h k on October 8, 2005 9:01:30 AM]
Advertisement
Sorry to bump this thread, but it was just about to slip away from the first page. And considering there where ~60 views and not one single reply, I suppose I didn't express my problem correct.

Is anything not clear to you or do you guys just don't have any idea of what could be wrong? I know I posted four sections of code, but they're not that long ( together maybe 100 lines maximum ).

Again I am sorry if I seem annoying with this problem, but I really need help for this. It's one of the last technical steps for this application.
First off, I suggest you draw your normals to make sure they're correct. They probably are, but it pays to make sure you're debugging something thats actually broken. :) Simply drawing lines for every vertex between vertexPos and vertexPos+normal*scale (just tweek 'scale' until the lines are of sensible length) will let you instantly tell if they're correct or not.

However I think your problem is with your order of operations. For hardware lights you want to do:

- clear screen
- set projection matrix
- set camera position (in modelview matrix)
- enable/set lights
- for each object
-- push modelview matrix
-- position / rotate object via modelview
-- render object
-- pop modelview matrix

The key difference is that you seem to be setting the lights, then setting the camera, which is the wrong way of doing things. When lights are specified they are transformed using the current set of matrices into screen space. Instead do view and camera setup first, then lights.

Hope that helps.
Thanks for your reply.

First of all I checked the normals and they look fine.

Then I tried to follow your advice, but it seems I still have problems with it. I guess since the problem is the order, the problem is in the global draw function, that determines the order:

This is the way I used to have this function set up:
void draw_world ( void )// draws the world{	// clear screen buffer, depth buffer and stencil buffer	glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT );	// reset the matrix	glLoadIdentity ( );	float lgt_pos[] = { prt_system.position.x, prt_system.position.y, prt_system.position.z };	lgt.set_position ( lgt_pos );	// draw the gui	draw_gui ( );	// and draw the map	map.draw ( );	// then draw the objects	draw_objects ( );}


This is the new function, that I now use:
void draw_world ( void )// draws the world{	glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT );	glLoadIdentity ( );	glMatrixMode ( GL_PROJECTION );	glPushMatrix ( );	cam.update ( );	glPopMatrix ( );	glMatrixMode ( GL_MODELVIEW );	glPushMatrix ( );	float lgt_pos[] = { prt_system.position.x, prt_system.position.y, prt_system.position.z };	lgt.set_position ( lgt_pos );	draw_gui ( );	draw_objects ( );	glPopMatrix ( );}


But it still looks the very same!
In your new function, you have:
glMatrixMode ( GL_PROJECTION );
glPushMatrix ( );
cam.update ( );
glPopMatrix ( );
Assuming that cam.update sets the camera position, then that doesn't have any effect. You don't need any pushing or poping here (although a glIdentity before the camera might be a good idea).

You don't say what your new draw_objects() does. Make sure you're not still doing your object::draw in the same way - you don't want to set the camera at this point, it should already be set!

Your whole matrix setup looks convulted and error prone. You really should decide what should happen where and stick with it. It might be an idea to rip out what you've already got and add in bits slowly (following the order i listed before).

Edit: and you don't appear to be setting the camera's modelview matrix up anywhere (which should be *before* you setup lights).
You are right, the whole function is quite messed up. I cleaned it up, but now it doesn't render anything anymore...

This is the new draw_world function (that draws everything):
void draw_world ( void )// draws the world{	// clear screen	glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT );	// set projection matrix	glMatrixMode ( GL_PROJECTION );	// reset	glLoadIdentity ( );	// move for camera	cam.update ( );	// set modelview matrix	glMatrixMode ( GL_MODELVIEW );		// set lights	float lgt_pos[] = { prt_system.position.x, prt_system.position.y, prt_system.position.z };	lgt.set_position ( lgt_pos );	// draw lights	draw_objects ( );}


Then this is my draw_objects function (that draws objects):
void draw_objects ( void )// draws all objects{	obj[1].mask ( );	obj[0].draw_reflected ( );	obj[2].draw_reflected ( );	prt_system.draw_reflected ( );	obj[1].draw_blended ( );	obj[0].draw ( );	obj[2].draw ( );	prt_system.draw ( );	}


And finally this is my object::draw function (that really draws one object):
void object::draw ( )// draws the object normally to the screen{	// 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 ( );	// 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 );	// 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;		glBegin ( GL_LINES );		glVertex3f ( vertex1[0], vertex1[1], vertex1[2] );		glVertex3f ( vertex1[0]+actor.normal[0]*10.0f, vertex1[1]+actor.normal[1]*10.0f, vertex1[2]+actor.normal[2]*10.0f );		glVertex3f ( vertex2[0], vertex2[1], vertex2[2] );		glVertex3f ( vertex2[0]+actor.normal[0]*10.0f, vertex2[1]+actor.normal[1]*10.0f, vertex2[2]+actor.normal[2]*10.0f );		glVertex3f ( vertex3[0], vertex3[1], vertex3[2] );		glVertex3f ( vertex3[0]+actor.normal[0]*10.0f, vertex3[1]+actor.normal[1]*10.0f, vertex3[2]+actor.normal[2]*10.0f );		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 );}


The only point I am missing on your list is the "setting-the-cameras-modelview-matrix-up", because I am not quite sure what you meant.

My camera::update function does this:
void camera::update ( void )// updates the cameras position in the world{	// move there	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 );}


Thanks for your help though, I already rated you up. (:
That looks better, but I think your camera setup is wrong. You want your perspective setup on the projection matrix and your actual camera position on the modelview matrix. Basically something like:

glMatrixMode(GL_PROJECTION);glLoadIdentity();glPerspective( whatever settings );glMatrixMode(GL_MODELVIEW);glLoadIdentity();// the stuff you've got in camera.update to set the position// Rest of your drawing here. Looks ok now though :)

At the moment you're putting your camera orientation/position in the projection matrix, and you're not setting the projection matrix at all.
Allright, now it renders again. And is like 12x faster, too. ;) Thanks for that!

Just the original problem is still the same. The lighting screws up whenever I move the light.

This is the new and corrected draw_world function:
void draw_world ( void )// draws the world{	// clear screen	glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT );	// set projection matrix	glMatrixMode ( GL_PROJECTION );	// reset	glLoadIdentity ( );	// perspective setup	gluPerspective ( 45.0f, (GLfloat)SCREEN_WIDTH / (GLfloat)SCREEN_HEIGHT, 0.1f, 800.0f );	// set modelview matrix	glMatrixMode ( GL_MODELVIEW );	// reset	glLoadIdentity ( );	// move for camera	cam.update ( );	// set lights	float lgt_pos[] = { prt_system.position.x, prt_system.position.y, prt_system.position.z };	lgt.set_position ( lgt_pos );	// draw objects	draw_objects ( );}
Is your light::set_position still the same as your first post? You really don't want to be messing with the camera and matrices in there. :)
Yes, I'm afraid it is still the same...

Here for you to check (one line): ;)

void light::set_position ( float light_position[] )// updates the position of the light{	// move the light	glLightfv ( GL_LIGHT1, GL_POSITION, light_position );}

This topic is closed to new replies.

Advertisement