src/lightManager.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2007 by                                                 *
00003  *         Pierre-yves JEZEQUEL, Julien MICHOT, Loic MOISAN,               *
00004  *         Julien PAPILLON, Sebastien PINEAUD, Barthelemy SERRES           *
00005  *                                                                         *
00006  *   https://sourceforge.net/projects/anidam                               *
00007  *                                                                         *
00008  *   This program is free software; you can redistribute it and/or modify  *
00009  *   it under the terms of the GNU General Public License as published by  *
00010  *   the Free Software Foundation; either version 2 of the License, or     *
00011  *   (at your option) any later version.                                   *
00012  *                                                                         *
00013  *   This program is distributed in the hope that it will be useful,       *
00014  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00015  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00016  *   GNU General Public License for more details.                          *
00017  *                                                                         *
00018  *   You should have received a copy of the GNU General Public License     *
00019  *   along with this program; if not, write to the                         *
00020  *   Free Software Foundation, Inc.,                                       *
00021  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00022  ***************************************************************************/
00023 #include "lightManager.h"
00024 
00025 LightManager:: LightManager(): groupOfLights(NULL),lights(8),valid(false) {
00026   /*
00027   Initialize our list to have all NULL values. This is done so when
00028   the user calls the get_or_create_light() function, the function
00029   can properly detect whether a light has been created for a particular
00030   slot.
00031   */
00032   for(unsigned int i = 0; i < lights.size(); ++i){
00033       lights[i] = NULL;
00034   }
00035         
00036 }
00037 
00038 void LightManager::init(osg::Group* root) 
00039 {
00040   groupOfLights = root;
00041   valid = true;
00042 }
00043 
00044 osg::Light * LightManager::getOrCreateLight(LightManager::LIGHTNUMBER number) {
00045   if(valid)
00046   {
00047      if(lights[number])
00048      {
00049         return lights[number]->getLight();
00050      }
00051         
00052      //       Create a new light, and set the light number according to our parameter.
00053     osg::Light * light = new osg::Light();
00054     light->setLightNum(number);
00055 
00056         /*
00057     Create a new lightsource. This is the object which holds a light, there
00058     is a 1-1 correspondence between a light and a lightsource. The vector
00059     actually holds lightsources.
00060             
00061     Set the lightsource to contain our newly created light
00062         */
00063     osg::LightSource * lightsource = new osg::LightSource();
00064     lightsource->setLight(light);
00065        
00066     lightsource->setLocalStateSetModes(osg::StateAttribute::ON); 
00067     /*
00068     Now we need to enable the lightsource and thereby light in the SetState
00069     of the group node that will contain the lightsource.
00070     */
00071     lightsource->setStateSetModes(*groupOfLights->getOrCreateStateSet(), osg::StateAttribute::ON);
00072  
00073     /*
00074     Add the lightsource to the osg::Group
00075     */
00076     groupOfLights->addChild(lightsource);
00077 
00078     /*
00079     Now we stick the address of the lightsource in the proper slot in
00080     the vector of lights, so future calls with this number will extract
00081     this light, and not allocate a new one.
00082     */
00083     lights[number] = lightsource;
00084         
00085     // Return a pointer to the osg::Light so the user may modify the properties.
00086     return light;
00087   }
00088   return NULL;
00089 }

Generated on Tue Jun 5 16:56:48 2007 for Anidam by  doxygen 1.5.1