Visualization Library

A lightweight C++ OpenGL middleware for 2D/3D graphics
[Home] [Tutorials] [All Classes] [Grouped Classes]

X:/dropbox/visualizationlibrary/src/vlGraphics/Light.cpp

Go to the documentation of this file.
00001 /**************************************************************************************/
00002 /*                                                                                    */
00003 /*  Visualization Library                                                             */
00004 /*  http://www.visualizationlibrary.org                                               */
00005 /*                                                                                    */
00006 /*  Copyright (c) 2005-2010, Michele Bosi                                             */
00007 /*  All rights reserved.                                                              */
00008 /*                                                                                    */
00009 /*  Redistribution and use in source and binary forms, with or without modification,  */
00010 /*  are permitted provided that the following conditions are met:                     */
00011 /*                                                                                    */
00012 /*  - Redistributions of source code must retain the above copyright notice, this     */
00013 /*  list of conditions and the following disclaimer.                                  */
00014 /*                                                                                    */
00015 /*  - Redistributions in binary form must reproduce the above copyright notice, this  */
00016 /*  list of conditions and the following disclaimer in the documentation and/or       */
00017 /*  other materials provided with the distribution.                                   */
00018 /*                                                                                    */
00019 /*  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND   */
00020 /*  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED     */
00021 /*  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE            */
00022 /*  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR  */
00023 /*  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES    */
00024 /*  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;      */
00025 /*  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON    */
00026 /*  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT           */
00027 /*  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS     */
00028 /*  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                      */
00029 /*                                                                                    */
00030 /**************************************************************************************/
00031 
00032 #include <vlGraphics/Light.hpp>
00033 #include <vlCore/Transform.hpp>
00034 #include <vlGraphics/Camera.hpp>
00035 #include <vlCore/Log.hpp>
00036 #include <vlCore/Say.hpp>
00037 
00038 using namespace vl;
00039 
00040 //------------------------------------------------------------------------------
00041 // Light
00042 //------------------------------------------------------------------------------
00043 Light::Light()
00044 {
00045   VL_DEBUG_SET_OBJECT_NAME()
00046   mAmbient = fvec4(0,0,0,1);
00047   mDiffuse = fvec4(1,1,1,1);
00048   mSpecular = fvec4(1,1,1,1);
00049   mPosition = fvec4(0,0,0,1);
00050   mSpotDirection = fvec3(0,0,-1);
00051   mSpotExponent = 0;
00052   mSpotCutoff = 180.0f;
00053   mConstantAttenuation  = 1.0f;
00054   mLinearAttenuation    = 0.0f;
00055   mQuadraticAttenuation = 0.0f;
00056   mBoundTransform = NULL;
00057   mEnabled = true;
00058 }
00059 //------------------------------------------------------------------------------
00060 void Light::apply(int index, const Camera* camera, OpenGLContext*) const
00061 {
00062   VL_CHECK_OGL()
00063 
00064   if (enabled())
00065   {
00066     glEnable (GL_LIGHT0 + index); VL_CHECK_OGL()
00067 
00068     glMatrixMode(GL_MODELVIEW);
00069     glPushMatrix();
00070 
00071     // follows the given node
00072     if ( boundTransform() )
00073       camera->applyModelViewMatrix( boundTransform()->worldMatrix() );
00074     else
00075     {
00076       // follows the camera
00077       /*glMatrixMode(GL_MODELVIEW);*/
00078       glLoadIdentity();
00079     }
00080 
00081     glLightfv(GL_LIGHT0+index, GL_AMBIENT,  mAmbient.ptr());
00082     glLightfv(GL_LIGHT0+index, GL_DIFFUSE,  mDiffuse.ptr());
00083     glLightfv(GL_LIGHT0+index, GL_SPECULAR, mSpecular.ptr());
00084     glLightfv(GL_LIGHT0+index, GL_POSITION, mPosition.ptr());
00085 
00086     glLightf(GL_LIGHT0+index, GL_SPOT_CUTOFF, mSpotCutoff);
00087 
00088     // if its a spot light
00089     if (mSpotCutoff != 180.0f) 
00090     {
00091       VL_CHECK(mSpotCutoff>=0.0f && mSpotCutoff<=90.0f);
00092       glLightfv(GL_LIGHT0+index, GL_SPOT_DIRECTION, mSpotDirection.ptr());
00093       glLightf(GL_LIGHT0+index, GL_SPOT_EXPONENT, mSpotExponent);
00094     }
00095 
00096     // if positional or spot light compute the attenuation factors, that is
00097     // attenuation is useless of directional lights.
00098     if (mSpotCutoff != 180.0f || mPosition.w() != 0)
00099     {
00100       glLightf(GL_LIGHT0+index, GL_CONSTANT_ATTENUATION, mConstantAttenuation);
00101       glLightf(GL_LIGHT0+index, GL_LINEAR_ATTENUATION, mLinearAttenuation);
00102       glLightf(GL_LIGHT0+index, GL_QUADRATIC_ATTENUATION, mQuadraticAttenuation);
00103     }
00104 
00105     /*glMatrixMode(GL_MODELVIEW);*/
00106     glPopMatrix();
00107   }
00108   else
00109   {
00110     glDisable(GL_LIGHT0 + index);
00111   }
00112 }
00113 //------------------------------------------------------------------------------
00114 void Light::bindTransform(Transform* transform) 
00115 { 
00116   mBoundTransform = transform; 
00117 }
00118 //------------------------------------------------------------------------------
00119 Transform* Light::boundTransform()
00120 { 
00121   return mBoundTransform.get(); 
00122 }
00123 //------------------------------------------------------------------------------
00124 const Transform* Light::boundTransform() const
00125 { 
00126   return mBoundTransform.get(); 
00127 }
00128 //------------------------------------------------------------------------------

Visualization Library 2011.09.1160 Reference Documentation
Copyright 2005-2011 Michele Bosi. All rights reserved.
Updated on Thu May 2 2013 13:40:37.
Permission is granted to use this page to write and publish articles regarding Visualization Library.