# Visualization Library

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

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

- Main Page
- 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.com                                               */
    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(int light_index)
    00044 {
    00045   VL_DEBUG_SET_OBJECT_NAME()
    00046   if (light_index<0 && light_index>7)
    00047     Log::error( Say("Light index %n out of range. The light index must be between 0 and 7.\n") << light_index );
    00048   VL_CHECK(light_index>=0 && light_index<8)
    00049   mLightIndex = light_index;
    00050   mAmbient = fvec4(0,0,0,1);
    00051   mDiffuse = fvec4(1,1,1,1);
    00052   mSpecular = fvec4(1,1,1,1);
    00053   mPosition = fvec4(0,0,0,1);
    00054   mSpotDirection = fvec3(0,0,-1);
    00055   mSpotExponent = 0;
    00056   mSpotCutoff = 180.0f;
    00057   mConstantAttenuation   = 1.0f;
    00058   mLinearAttenuation    = 0.0f;
    00059   mQuadraticAttenuation = 0.0f;
    00060   mFollowedTransform = NULL;
    00061 }
    00062 //------------------------------------------------------------------------------
    00063 void Light::setLightIndex(int light_index)
    00064 {
    00065   if (light_index<0 && light_index>7)
    00066     Log::error( Say("Light index %n out of range. The light index must be between 0 and 7.\n") << light_index );
    00067   VL_CHECK(light_index>=0 && light_index<8)
    00068   mLightIndex = light_index;
    00069 }
    00070 //------------------------------------------------------------------------------
    00071 void Light::apply(const Camera* camera, OpenGLContext* ctx) const
    00072 {
    00073   if (camera)
    00074   {
    00075     glEnable (GL_LIGHT0 + lightIndex());
    00076
    00077     glMatrixMode(GL_MODELVIEW);
    00078     glPushMatrix();
    00079
    00080     // follows the given node
    00081     if ( followedTransform() )
    00082       camera->applyModelViewMatrix( followedTransform()->worldMatrix() );
    00083     else
    00084     {
    00085       // follows the camera
    00086       /*glMatrixMode(GL_MODELVIEW);*/
    00087       glLoadIdentity();
    00088     }
    00089
    00090     glLightfv(GL_LIGHT0+lightIndex(), GL_AMBIENT,  mAmbient.ptr());
    00091     glLightfv(GL_LIGHT0+lightIndex(), GL_DIFFUSE,  mDiffuse.ptr());
    00092     glLightfv(GL_LIGHT0+lightIndex(), GL_SPECULAR, mSpecular.ptr());
    00093     glLightfv(GL_LIGHT0+lightIndex(), GL_POSITION, mPosition.ptr());
    00094
    00095     glLightf(GL_LIGHT0+lightIndex(), GL_SPOT_CUTOFF, mSpotCutoff);
    00096
    00097     // if its a spot light
    00098     if (mSpotCutoff != 180.0f)
    00099     {
    00100       VL_CHECK(mSpotCutoff>=0.0f && mSpotCutoff<=90.0f);
    00101       glLightfv(GL_LIGHT0+lightIndex(), GL_SPOT_DIRECTION, mSpotDirection.ptr());
    00102       glLightf(GL_LIGHT0+lightIndex(), GL_SPOT_EXPONENT, mSpotExponent);
    00103     }
    00104
    00105     // if positional or spot light compute the attenuation factors, that is
    00106     // attenuation is useless of directional lights.
    00107     if (mSpotCutoff != 180.0f || mPosition.w() != 0)
    00108     {
    00109       glLightf(GL_LIGHT0+lightIndex(), GL_CONSTANT_ATTENUATION, mConstantAttenuation);
    00110       glLightf(GL_LIGHT0+lightIndex(), GL_LINEAR_ATTENUATION, mLinearAttenuation);
    00111       glLightf(GL_LIGHT0+lightIndex(), GL_QUADRATIC_ATTENUATION, mQuadraticAttenuation);
    00112     }
    00113
    00114     /*glMatrixMode(GL_MODELVIEW);*/
    00115     glPopMatrix();
    00116   }
    00117   else
    00118   {
    00119     glDisable(GL_LIGHT0 + lightIndex());
    00120   }
    00121 }
    00122 //------------------------------------------------------------------------------
    00123 void Light::followTransform(Transform* transform)
    00124 {
    00125   mFollowedTransform = transform;
    00126 }
    00127 //------------------------------------------------------------------------------
    00128 Transform* Light::followedTransform()
    00129 {
    00130   return mFollowedTransform.get();
    00131 }
    00132 //------------------------------------------------------------------------------
    00133 const Transform* Light::followedTransform() const
    00134 {
    00135   return mFollowedTransform.get();
    00136 }
    00137 //------------------------------------------------------------------------------

Visualization Library v2011.05.1142 Reference Documentation
Copyright 2005-2011 Michele Bosi. All rights reserved.
Updated on Thu May 26 2011 22:51:24.
Permission is granted to use this page to write and publish articles regarding Visualization Library.