Skip to main content
GameDev.net gamedev.net
🔒 Locked 🎮 Unity

Wierd lighting problem

Started by INVERSED Jan 19, 2005 at 10:13 AM 1 replies 800+ views
Original Post
INVERSED
INVERSED
Hey everybody. I've been staring at this for a couple of hours now, and I can't seem to figure it out. I'm having a problem with my lighting such that as you move the camera the lighting changes. This gives an effect where vertices will turn completly black as you pan around. It looks like [edit: ah, I didn't know the board supported html?] The code which I use looks like


//The Render Function

void IGraphicDevice::ExecuteRenderList(){
	for( std::list<QueueableT>::iterator it = vRenderList.begin(); it != vRenderList.end(); it++ ){
		PushMatrix( (*it).lMat );
		
		CRenderable * pRenderable = (*it).pRenderable;


		//setup lights
		const LocalLightList & vLights = *(*it).pLights;
		for( std::list<LightQueueable>::const_iterator l = vLights.begin(); l != vLights.end(); l++ ){
			SetLight( (*l).pLight );
		}

		//render
		pRenderable->Render();



		//clear lights
		ClearLight();
		PopMatrix();
	}
}





In that function, for every item that need rendering, I push it's transform matrix onto the stack, and then, for every light, I call SetLight, then I render the object and pop the matrix


//The SetLight function

void COGLGraphicDevice::SetLight( CSceneLight * pLight ){
	assert( pLight );

	CVector4 vPos;
	switch( pLight->GetType() ){
		case CSceneLight::LightType_Directional:
			vPos = pLight->GetDirection();
			vPos.w = 0.0f;
			break;
		case CSceneLight::LightType_Point:
			vPos = pLight->GetPosition();
			vPos.w = 1.0f;
			break;
		case CSceneLight::LightType_Spot:
			vPos = pLight->GetPosition();
			vPos.w = 1.0f;
			glLightf( GL_LIGHT0 + uCurrentLightsUsed, GL_SPOT_CUTOFF, pLight->GetCutoff() );
			glLightf( GL_LIGHT0 + uCurrentLightsUsed, GL_SPOT_EXPONENT, pLight->GetExponent() );
			break;
	}

	//set the color
	glEnable(  GL_LIGHT0 + uCurrentLightsUsed );
	//glLightfv( GL_LIGHT0 + uCurrentLightsUsed, GL_AMBIENT, pLight->GetAmbient() );
	glLightfv( GL_LIGHT0 + uCurrentLightsUsed, GL_DIFFUSE, pLight->GetDiffuse() );
	glLightfv( GL_LIGHT0 + uCurrentLightsUsed, GL_SPECULAR, pLight->GetSpecular() );
	glLightfv( GL_LIGHT0 + uCurrentLightsUsed, GL_POSITION, vPos );


	uCurrentLightsUsed++;
}





The setLight function sets the position and color of each light I'm using. Am I missing something? Also, while I'm here, if I attempt to set the ambient color for the light, It seems to be lighting everything, despite the fact my ambient was set to a 10% gray. any ideas what causes that? Thanks for the help. [Edited by - INVERSED on January 19, 2005 5:53:24 PM]
Write more poetry.http://www.Me-Zine.org
Solias
Solias
Ok that confused me at first, PushMatrix() looks a lot like glPushMatrix() at first glance. I'm guessing your PushMatrix applies the object's transform? This means that you are defining your lights in object space, since they are affected by the model view matrix. You probably want to define them in world space (before the object space transform). This would cause the lights to be inconsistant, but they shouldn't change when you move the camera. That actually sounds like defining the light in camera space (before the camera transform).

In regards to your second question, each light contributes it's ambient value in addition to the light model ambient value you set earlier.

From MSDN:

In RGBA mode, the lighted color of a vertex is the sum of the material emission intensity, the product of the material ambient reflectance and the lighting model full-scene ambient intensity, and the contribution of each enabled light source. Each light source contributes the sum of three terms: ambient, diffuse, and specular. The ambient light source contribution is the product of the material ambient reflectance and the light's ambient intensity. The diffuse light source contribution is the product of the material diffuse reflectance, the light's diffuse intensity, and the dot product of the vertex's normal with the normalized vector from the vertex to the light source. The specular light source contribution is the product of the material specular reflectance, the light's specular intensity, and the dot product of the normalized vertex-to-eye and vertex-to-light vectors, raised to the power of the shininess of the material. All three light source contributions are attenuated equally based on the distance from the vertex to the light source and on light source direction, spread exponent, and spread cutoff angle. All dot products are replaced with zero if they evaluate to a negative value.


Finally to create links you need to use standard html link tags
INVERSED
INVERSED
Not that this will ever help anybody, but just for the sake of completion, found the issue. I was forgetting to clear my list of lights and ended up turning all eight lights on apparently, so my flbu, not OGL's... grr... I've been working on this all day.
Write more poetry.http://www.Me-Zine.org

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.