Jump to content
  • Advertisement
Sign in to follow this  

OpenGL Make Light Fade As it Moves Away

This topic is 3585 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok, I'm tired and fixing to go to sleep after I post this; So, I'm reading the OpenGL RedBook and one of the exercises wants me to make the light fade as it gets further away. Ok, with the code it kind of does, but the specular remains no matter how far away it is. I'm still new to this so I don't understand what all goes on with most of the properties: This is just some sample code, no need to read through it if you don't want. I just need the idea of how to accomplish this goal.
float light_x = 20.0f;

				// LIGHT0
float mat_ambient[] = { 0.2f, 0.2f, 0.2f, 1.0f };
float mat_diffuse[] = { 1.0f, 1.0f, 0.0f, 1.0f };
float mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
float mat_shininess[] = { 50.0f };
float light_position[] = { 20.0f, 2.0f, 10.0f, 0.0f };
				// LIGHT1
float light1_ambient[] = { 0.2f, 0.2f, 0.2f, 1.0 };
float light1_diffuse[] = { 0.0f, 0.0, 1.0, 1.0 };
float light1_specular[] = { 1.0, 1.0, 1.0, 1.0 };
float light1_position[] = { -20.0, 2.0, 10.0, 0.0 };
float spot_direction[] = { -1.0, -1.0, 1.0 };

void hLighting()
	glLightModelfv( GL_LIGHT_MODEL_AMBIENT, mat_ambient );
	glLightModelf( GL_LIGHT_MODEL_LOCAL_VIEWER, 1.0f );

	glMaterialfv( GL_FRONT, GL_SPECULAR, mat_specular );
	glMaterialfv( GL_FRONT, GL_SHININESS, mat_shininess );

	glLightfv( GL_LIGHT0, GL_AMBIENT, mat_ambient );
	glLightfv( GL_LIGHT0, GL_DIFFUSE, mat_diffuse );
	glLightfv( GL_LIGHT0, GL_SPECULAR, mat_diffuse );
	glLightfv( GL_LIGHT0, GL_SHININESS, mat_shininess );
	glLightfv( GL_LIGHT0, GL_POSITION, light_position );

	// LIGHT 1
	glLightfv( GL_LIGHT1, GL_AMBIENT, light1_ambient );
	glLightfv( GL_LIGHT1, GL_DIFFUSE, light1_diffuse );
	glLightfv( GL_LIGHT1, GL_SPECULAR, light1_specular );
	glLightfv( GL_LIGHT1, GL_POSITION, light1_position );

	//glLightf( GL_LIGHT1, GL_LINEAR_ATTENUATION, 1.0f );
	//glLightf( GL_LIGHT1, GL_SPOT_CUTOFF, 45.0f );
	//glLightfv( GL_LIGHT1, GL_SPOT_DIRECTION, spot_direction );
	//glLightf(GL_LIGHT1, GL_SPOT_EXPONENT, 2.0f);


float angle = 0.0f;

void hDisplay()



		//glRotatef(angle*5, 0.0f, 1.0f, 0.0f);
		light_position[0] = light_x;
		light1_position[0] = light_x;

			glTranslatef( light_position[0], light_position[1], light_position[2] );

			glTranslatef( light1_position[0], light1_position[1], light1_position[2] );
			glutSolidSphere(0.5, 5, 5);

		glLightfv(GL_LIGHT0, GL_POSITION, light_position );
		glLightfv(GL_LIGHT1, GL_POSITION, light1_position );

	glutSolidTorus(2, 8, 30, 30);

	// Draw Grids
		glTranslatef(0.0f, 100.0f, 0.0f);


[Edited by - openglJunkie on December 27, 2008 1:09:45 PM]

Share this post

Link to post
Share on other sites
Well you should not comment out setting the attenuation properties, as these define how light behaves as you move away from it.

Your lights properties will be multiplied by an attenuation factor to compute the influence on a surface.
This attenuation factor is
1 / (constantAttenuation plus distance * linearAttenuation plus distance^2 * quadricAttenuation)

(dunno why it eats up my "plus" operator)

That means you'll probably want to set these properties (for simple fading perhaps just linear attenuation) to values different from zero.

Share this post

Link to post
Share on other sites
the w component of the lightposition alters its 'sort'
from msdn reference;

If the w component of the position is 0, the light is treated as a directional source. Diffuse and specular lighting calculations take the light's direction, but not its actual position, into account, and attenuation is disabled. Otherwise, diffuse and specular lighting calculations are based on the actual location of the light in eye coordinates, and attenuation is enabled. The initial position is (0, 0, 1, 0); thus, the initial light source is directional, parallel to, and in the direction of the -z axis.

iow, setting it to 0 will illuminate regardless distance, 1 will take distance into account.

I believe this is the effect youre looking for

edit: this is specific to opengl native lighting only, of course. Otherwise, simply alter your own light equation to fit the need.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!