# Visualization Library

A lightweight C++ OpenGL middleware for 2D/3D graphics

[Home] [Tutorials] [All Classes] [Grouped Classes]

- Main Page
- Related Pages
- Namespaces
- Classes
- Files

- File List
- File Members

# D:/VL/BuildSystem/VL_DISTRO/Visualization_Library_SDK/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 }
    00058 //------------------------------------------------------------------------------
    00059 void Light::apply(int index, const Camera* camera, OpenGLContext*) const
    00060 {
    00061   VL_CHECK_OGL()
    00062
    00063   if (camera)
    00064   {
    00065     glEnable (GL_LIGHT0 + index); VL_CHECK_OGL()
    00066
    00067     glMatrixMode(GL_MODELVIEW);
    00068     glPushMatrix();
    00069
    00070     // follows the given node
    00071     if ( boundTransform() )
    00072       camera->applyModelViewMatrix( boundTransform()->worldMatrix() );
    00073     else
    00074     {
    00075       // follows the camera
    00076       /*glMatrixMode(GL_MODELVIEW);*/
    00077       glLoadIdentity();
    00078     }
    00079
    00080     glLightfv(GL_LIGHT0+index, GL_AMBIENT,  mAmbient.ptr());
    00081     glLightfv(GL_LIGHT0+index, GL_DIFFUSE,  mDiffuse.ptr());
    00082     glLightfv(GL_LIGHT0+index, GL_SPECULAR, mSpecular.ptr());
    00083     glLightfv(GL_LIGHT0+index, GL_POSITION, mPosition.ptr());
    00084
    00085     glLightf(GL_LIGHT0+index, GL_SPOT_CUTOFF, mSpotCutoff);
    00086
    00087     // if its a spot light
    00088     if (mSpotCutoff != 180.0f)
    00089     {
    00090       VL_CHECK(mSpotCutoff>=0.0f && mSpotCutoff<=90.0f);
    00091       glLightfv(GL_LIGHT0+index, GL_SPOT_DIRECTION, mSpotDirection.ptr());
    00092       glLightf(GL_LIGHT0+index, GL_SPOT_EXPONENT, mSpotExponent);
    00093     }
    00094
    00095     // if positional or spot light compute the attenuation factors, that is
    00096     // attenuation is useless of directional lights.
    00097     if (mSpotCutoff != 180.0f || mPosition.w() != 0)
    00098     {
    00099       glLightf(GL_LIGHT0+index, GL_CONSTANT_ATTENUATION, mConstantAttenuation);
    00100       glLightf(GL_LIGHT0+index, GL_LINEAR_ATTENUATION, mLinearAttenuation);
    00101       glLightf(GL_LIGHT0+index, GL_QUADRATIC_ATTENUATION, mQuadraticAttenuation);
    00102     }
    00103
    00104     /*glMatrixMode(GL_MODELVIEW);*/
    00105     glPopMatrix();
    00106   }
    00107   else
    00108   {
    00109     glDisable(GL_LIGHT0 + index);
    00110   }
    00111 }
    00112 //------------------------------------------------------------------------------
    00113 void Light::bindTransform(Transform* transform)
    00114 {
    00115   mBoundTransform = transform;
    00116 }
    00117 //------------------------------------------------------------------------------
    00118 Transform* Light::boundTransform()
    00119 {
    00120   return mBoundTransform.get();
    00121 }
    00122 //------------------------------------------------------------------------------
    00123 const Transform* Light::boundTransform() const
    00124 {
    00125   return mBoundTransform.get();
    00126 }
    00127 //------------------------------------------------------------------------------

Visualization Library v2011.09.1162 Reference Documentation
Copyright 2005-2011 Michele Bosi. All rights reserved.
Updated on Thu Jan 26 2012 00:43:39.
Permission is granted to use this page to write and publish articles regarding Visualization Library.