   Make Light Fade As it Moves Away
  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.// LIGHTING VARIABLESfloat light_x = 20.0f;// LIGHT0float 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 };// LIGHT1float 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 1glLightfv( 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_CONSTANT_ATTENUATION, 2.0 );//glLightf( GL_LIGHT1, GL_LINEAR_ATTENUATION, 1.0f );//glLightf( GL_LIGHT1, GL_QUADRATIC_ATTENUATION, 0.2 );//glLightf( GL_LIGHT1, GL_SPOT_CUTOFF, 45.0f );//glLightfv( GL_LIGHT1, GL_SPOT_DIRECTION, spot_direction );//glLightf(GL_LIGHT1, GL_SPOT_EXPONENT, 2.0f);glEnable(GL_LIGHTING);glEnable(GL_NORMALIZE);//glEnable(GL_COLOR_MATERIAL);//glEnable(GL_LIGHT0);glEnable(GL_LIGHT1);}float angle = 0.0f;void hDisplay(){ShowCursor(false);glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);glMatrixMode(GL_MODELVIEW);glLoadIdentity();fpCamera->Render();glPushMatrix();//glRotatef(angle*5, 0.0f, 1.0f, 0.0f);light_position[0] = light_x;light1_position[0] = light_x;glPushMatrix();glTranslatef( light_position[0], light_position[1], light_position[2] );glutSolidCube(0.5);glPopMatrix();glPushMatrix();glTranslatef( light1_position[0], light1_position[1], light1_position[2] );glutSolidSphere(0.5, 5, 5);glPopMatrix();glLightfv(GL_LIGHT0, GL_POSITION, light_position );glLightfv(GL_LIGHT1, GL_POSITION, light1_position );glPopMatrix();glutSolidTorus(2, 8, 30, 30);// Draw GridsglPushMatrix();DrawGrid();glTranslatef(0.0f, 100.0f, 0.0f);DrawGrid();glPopMatrix();glFlush();}[Edited by - openglJunkie on December 27, 2008 1:09:45 PM]
  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.   ;  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 foredit: this is specific to opengl native lighting only, of course. Otherwise, simply alter your own light equation to fit the need.   