Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

Light.cpp

Go to the documentation of this file.
00001 
00002 /*
00003     TEDDY - General graphics application library
00004     Copyright (C) 1999-2002  Timo Suoranta
00005     tksuoran@cc.helsinki.fi
00006 
00007     This library is free software; you can redistribute it and/or
00008     modify it under the terms of the GNU Lesser General Public
00009     License as published by the Free Software Foundation; either
00010     version 2.1 of the License, or (at your option) any later version.
00011 
00012     This library is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015     Lesser General Public License for more details.
00016 
00017     You should have received a copy of the GNU Lesser General Public
00018     License along with this library; if not, write to the Free Software
00019     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020 
00021     $Id: Light.cpp,v 1.5 2002/01/17 18:57:37 tksuoran Exp $
00022 */
00023 
00024 
00025 #include "Teddy/Graphics/Device.h"
00026 #include "Teddy/Materials/Light.h"
00027 #include "Teddy/Graphics/View.h"
00028 #include "Teddy/PhysicalComponents/Projection.h"
00029 #include "Teddy/SysSupport/Timer.h"
00030 #include "Teddy/Scenes/Camera.h"
00031 #include "Teddy/SysSupport/StdIO.h"
00032 #include "Teddy/SysSupport/Messages.h"
00033 using namespace Teddy::Graphics;
00034 using namespace Teddy::Materials;
00035 using namespace Teddy::PhysicalComponents;
00036 using namespace Teddy::Scenes;
00037 using namespace Teddy::SysSupport;
00038 
00039 
00040 namespace Teddy     {
00041 namespace Materials {
00042 
00043 
00048 unsigned int Light::light_id    [8] = { GL_LIGHT0, GL_LIGHT1, GL_LIGHT2, GL_LIGHT3, GL_LIGHT4, GL_LIGHT5, GL_LIGHT6, GL_LIGHT7 };
00049 int          Light::light_status[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
00050 
00051 /*static*/ const unsigned long Light::TYPE_DIRECTIONAL = 0;
00052 /*static*/ const unsigned long Light::TYPE_POSITIONAL  = 1;
00053 
00054 
00055 void Light::setType( const unsigned long type ){
00056     this->type = type;
00057 }
00058 
00060 Light::Light( const std::string &name ):Model(name){
00061     type = Light::TYPE_POSITIONAL;
00062     orbit_active = false;
00063     id = 10000;
00064     for( int i=0; i<8; i++ ){
00065         if( light_status[i] == 0 ){
00066             light_status[i] = 1;
00067             id = i;
00068             break;
00069         }
00070     }
00071     if( id == 10000 ){
00072         printf( "Light::Light could not allocate GL light id\n" );
00073         return;
00074     }
00075     ambient  = Color::BLACK;
00076     diffuse  = Color::WHITE;
00077     specular = Color::BLACK;
00078 
00079     float pos[4];
00080 
00081     pos[0] = 0;  //  position.r;
00082     pos[1] = 0;  //  position.s;
00083     pos[2] = 0;  //  position.t;
00084     pos[3] = 1;
00085 
00086     View::check();
00087     glLightfv( light_id[id], GL_POSITION, pos );
00088     View::check();
00089 }
00090 
00091 
00092 void Light::setAttenuation( const float constant, const float linear, const float quadratic ){
00093     View::check();
00094     constant_attenuation  = constant;
00095     linear_attenuation    = linear;
00096     quadratic_attenuation = quadratic;
00097     glLightf( light_id[id], GL_CONSTANT_ATTENUATION,  constant_attenuation  );
00098     glLightf( light_id[id], GL_LINEAR_ATTENUATION,    linear_attenuation    );
00099     glLightf( light_id[id], GL_QUADRATIC_ATTENUATION, quadratic_attenuation );
00100     View::check();
00101 }
00102 
00103 
00104 void Light::setSpotCutOff( const float cutoff_angle ){
00105     View::check();
00106     spot_cutoff_angle = cutoff_angle;
00107     glLightf( light_id[id], GL_SPOT_CUTOFF, spot_cutoff_angle );
00108     View::check();
00109 }
00110 
00111 void Light::setSpotExponent( const float exponent ){
00112     spot_exponent = exponent;
00113     glLightf( light_id[id], GL_SPOT_EXPONENT, spot_exponent );
00114 }
00115 
00116 void Light::setSpotDirection( Vector spot_direction ){
00117     this->spot_direction = spot_direction;
00118 }
00119 
00120 
00121 void Light::orbit( float radius, float speed, int axis ){
00122     orbit_active = true;
00123     orbit_radius = radius;
00124     orbit_speed  = speed;
00125     orbit_axis   = axis;
00126 }
00127 
00128 
00130 void Light::setAmbient( const Color &a ){
00131     ambient = a;
00132     dmsg( M_LWS, "Light::setAmbient ambient  % 8.4f, % 8.4f, % 8.4f", ambient.rgba[0], ambient.rgba[1], ambient.rgba[2] );
00133 }
00134 
00135 
00137 void Light::setDiffuse( const Color &d ){
00138     diffuse = d;
00139 }
00140 
00141 
00143 void Light::setSpecular( const Color &s ){
00144     specular = s;
00145 }
00146 
00147 
00149 void Light::enable(){
00150     View::check();
00151     glEnable( light_id[id] );
00152     View::check();
00153 }
00154 
00155 
00157 void Light::disable(){
00158     View::check();
00159     glDisable( light_id[id] );
00160     View::check();
00161 }
00162 
00163 
00165 /*virtual*/ void Light::applyLight( Projection *p ){
00166     if( id==10000 ){ return; }
00167 
00168     Camera *camera = p->getCamera();
00169     float   pos[4];
00170 
00171     View::check(); glPushMatrix();
00172     camera->doCamera( p, true );
00173 
00174     if( type == Light::TYPE_POSITIONAL ){
00175         Vector  vec    = getPosition();
00176         pos[0] = vec.v[0];
00177         pos[1] = vec.v[1];
00178         pos[2] = vec.v[2];
00179         pos[3] = 1;
00180 
00181         float dir[4];
00182 
00183         dir[0] = spot_direction[0];
00184         dir[1] = spot_direction[1];
00185         dir[2] = spot_direction[2];
00186 
00187         View::check(); glLightfv( light_id[id], GL_POSITION,       pos );
00188         View::check(); glLightfv( light_id[id], GL_SPOT_DIRECTION, dir );
00189         View::check(); glLightfv( light_id[id], GL_AMBIENT,        ambient.rgba );
00190         View::check(); glLightfv( light_id[id], GL_DIFFUSE,        diffuse.rgba );
00191         View::check(); glLightfv( light_id[id], GL_SPECULAR,       specular.rgba );
00192     }else{
00193         Vector  vec = this->getAttitude().getViewAxis();
00194         pos[0] = vec.v[0];
00195         pos[1] = vec.v[1];
00196         pos[2] = vec.v[2];
00197         pos[3] = 0;
00198         View::check(); glLightfv( light_id[id], GL_POSITION, pos           );
00199         View::check(); glLightfv( light_id[id], GL_AMBIENT,  ambient.rgba  );
00200         View::check(); glLightfv( light_id[id], GL_DIFFUSE,  diffuse.rgba  );
00201         View::check(); glLightfv( light_id[id], GL_SPECULAR, specular.rgba );
00202     }
00203 
00204     View::check(); glPopMatrix();
00205     View::check();
00206 }
00207 
00208 
00209 };  //  namespace Materials
00210 };  //  namespace Teddy
00211 
00212