Light.cpp

Go to the documentation of this file.
00001 /******************************************************************************
00002 *       SOFA, Simulation Open-Framework Architecture, version 1.0 beta 3      *
00003 *                (c) 2006-2008 MGH, INRIA, USTL, UJF, CNRS                    *
00004 *                                                                             *
00005 * This library is free software; you can redistribute it and/or modify it     *
00006 * under the terms of the GNU Lesser General Public License as published by    *
00007 * the Free Software Foundation; either version 2.1 of the License, or (at     *
00008 * your option) any later version.                                             *
00009 *                                                                             *
00010 * This library is distributed in the hope that it will be useful, but WITHOUT *
00011 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       *
00012 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
00013 * for more details.                                                           *
00014 *                                                                             *
00015 * You should have received a copy of the GNU Lesser General Public License    *
00016 * along with this library; if not, write to the Free Software Foundation,     *
00017 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.          *
00018 *******************************************************************************
00019 *                               SOFA :: Modules                               *
00020 *                                                                             *
00021 * Authors: The SOFA Team and external contributors (see Authors.txt)          *
00022 *                                                                             *
00023 * Contact information: contact@sofa-framework.org                             *
00024 ******************************************************************************/
00025 //
00026 // C++ Implementation: Light
00027 //
00028 // Description: 
00029 //
00030 //
00031 // Author: The SOFA team </www.sofa-framework.org>, (C) 2007
00032 //
00033 // Copyright: See COPYING file that comes with this distribution
00034 //
00035 //
00036 
00037 #include <sofa/component/visualmodel/Light.h>
00038 #include <sofa/component/visualmodel/LightManager.h>
00039 #include <sofa/core/ObjectFactory.h>
00040 
00041 namespace sofa
00042 {
00043 
00044 namespace component
00045 {
00046 
00047 namespace visualmodel
00048 {
00049 
00050 SOFA_DECL_CLASS(Light)
00051 
00052 SOFA_DECL_CLASS(DirectionalLight)
00053 //Register DirectionalLight in the Object Factory
00054 int DirectionalLightClass = core::RegisterObject("Directional Light")
00055 .add< DirectionalLight >()
00056 ;
00057 
00058 SOFA_DECL_CLASS(PositionalLight)
00059 //Register PositionalLight in the Object Factory
00060 int PositionalLightClass = core::RegisterObject("Positional Light")
00061 .add< PositionalLight >()
00062 ;
00063 
00064 SOFA_DECL_CLASS(SpotLight)
00065 //Register SpotLight in the Object Factory
00066 int SpotLightClass = core::RegisterObject("Spot Light")
00067 .add< SpotLight >()
00068 ;
00069 
00070 
00071 Light::Light():
00072     color(initData(&color, (Vector3) Vector3(1,1,1), "color", "Set the color of the light"))
00073 {
00074     lightID = GL_LIGHT0;
00075 }
00076 
00077 Light::~Light()
00078 {
00079 }
00080 
00081 void Light::setID(const GLint& id)
00082 {
00083     lightID = id;
00084 }
00085 
00086 void Light::init()
00087 {
00088     sofa::core::objectmodel::BaseContext* context = this->getContext();
00089     LightManager* lm = context->core::objectmodel::BaseContext::get<LightManager>();
00090     
00091     lm->putLight(this);
00092 
00093 }
00094 
00095 void Light::initVisual()
00096 {   
00097     
00098     glLightf(lightID, GL_SPOT_CUTOFF, 180.0);
00099     GLfloat c[4] = { (GLfloat) color.getValue()[0], (GLfloat)color.getValue()[1], (GLfloat)color.getValue()[2], 1.0 };
00100     glLightfv(lightID, GL_AMBIENT, c);
00101     glLightfv(lightID, GL_DIFFUSE, c);
00102     glLightfv(lightID, GL_SPECULAR, c);
00103     glLightf(lightID, GL_LINEAR_ATTENUATION, 0.0);
00104 
00105 }
00106 
00107 void Light::reinit()
00108 {
00109 
00110     initVisual();
00111 
00112 }
00113 
00114 void Light::drawLight()
00115 {
00116 
00117 }
00118 
00119 DirectionalLight::DirectionalLight():
00120     direction(initData(&direction, (Vector3) Vector3(0,0,-1), "direction", "Set the direction of the light"))
00121 {
00122 
00123 }
00124 
00125 DirectionalLight::~DirectionalLight()
00126 {
00127 
00128 }
00129 
00130 void DirectionalLight::initVisual()
00131 {
00132     Light::initVisual();
00133 
00134 }
00135 
00136 void DirectionalLight::reinit()
00137 {
00138     initVisual();
00139 }
00140 
00141 void DirectionalLight::drawLight()
00142 {
00143     Light::drawLight();
00144     GLfloat dir[4];
00145     
00146     dir[0]=(GLfloat)(direction.getValue()[0]); 
00147     dir[1]=(GLfloat)(direction.getValue()[1]); 
00148     dir[2]=(GLfloat)(direction.getValue()[2]);
00149     dir[3]=0.0; // directional 
00150     
00151     glLightfv(lightID, GL_POSITION, dir);
00152 }
00153 
00154 PositionalLight::PositionalLight():
00155     position(initData(&position, (Vector3) Vector3(-0.7,0.3,0.0), "position", "Set the position of the light")),
00156     attenuation(initData(&attenuation, (float) 0.0, "attenuation", "Set the attenuation of the light"))
00157 {
00158 
00159 }
00160 
00161 PositionalLight::~PositionalLight()
00162 {
00163 
00164 }
00165 
00166 void PositionalLight::initVisual()
00167 {
00168     Light::initVisual();
00169 
00170 }
00171 
00172 void PositionalLight::reinit()
00173 {
00174     initVisual();
00175 
00176 }
00177 
00178 void PositionalLight::drawLight()
00179 {
00180     Light::drawLight();
00181 
00182     GLfloat pos[4];
00183     pos[0]=(GLfloat)(position.getValue()[0]); 
00184     pos[1]=(GLfloat)(position.getValue()[1]); 
00185     pos[2]=(GLfloat)(position.getValue()[2]);
00186     pos[3]=1.0; // positional
00187     glLightfv(lightID, GL_POSITION, pos);
00188 
00189     glLightf(lightID, GL_LINEAR_ATTENUATION, attenuation.getValue());
00190     
00191 }
00192 
00193 
00194 SpotLight::SpotLight():
00195     direction(initData(&direction, (Vector3) Vector3(0,0,-1), "direction", "Set the direction of the light")),
00196     cutoff(initData(&cutoff, (float) 30.0, "cutoff", "Set the angle (cutoff) of the spot")),
00197     exponent(initData(&exponent, (float) 20.0, "exponent", "Set the exponent of the spot"))
00198 {
00199 
00200 }
00201 
00202 SpotLight::~SpotLight()
00203 {
00204 
00205 }
00206 
00207 void SpotLight::initVisual()
00208 {
00209     PositionalLight::initVisual();
00210 
00211 }
00212 
00213 void SpotLight::reinit()
00214 {
00215     initVisual();
00216 
00217 }
00218 
00219 void SpotLight::drawLight()
00220 {
00221     PositionalLight::drawLight();
00222 
00223     GLfloat dir[]={(GLfloat)(direction.getValue()[0]), (GLfloat)(direction.getValue()[1]), (GLfloat)(direction.getValue()[2])};
00224     glLightf(lightID, GL_SPOT_CUTOFF, cutoff.getValue());
00225     glLightfv(lightID, GL_SPOT_DIRECTION, dir);
00226     glLightf(lightID, GL_SPOT_EXPONENT, exponent.getValue());
00227     
00228 }
00229 
00230 }
00231 
00232 } //namespace component
00233 
00234 } //namespace sofa

Generated on Sun Nov 23 03:48:32 2008 for Sofa by  doxygen 1.5.7.1