fixed: auto_ptr -> unique_ptr
[opensg.git] / Source / System / NodeCores / Groups / Effects / DeferredShading / OSGDSLightChunk.cpp
blob451130a182dfaaecc474f62573b7c59f62f098dc
1 /*---------------------------------------------------------------------------*\
2 * OpenSG *
3 * *
4 * *
5 * Copyright (C) 2000-2006 by the OpenSG Forum *
6 * *
7 * www.opensg.org *
8 * *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
10 * *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
13 * License *
14 * *
15 * This library is free software; you can redistribute it and/or modify it *
16 * under the terms of the GNU Library General Public License as published *
17 * by the Free Software Foundation, version 2. *
18 * *
19 * This library is distributed in the hope that it will be useful, but *
20 * WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
22 * Library General Public License for more details. *
23 * *
24 * You should have received a copy of the GNU Library General Public *
25 * License along with this library; if not, write to the Free Software *
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
27 * *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
30 * Changes *
31 * *
32 * *
33 * *
34 * *
35 * *
36 * *
37 \*---------------------------------------------------------------------------*/
39 //---------------------------------------------------------------------------
40 // Includes
41 //---------------------------------------------------------------------------
43 #include <cstdlib>
44 #include <cstdio>
46 #include "OSGConfig.h"
48 #include "OSGDSLightChunk.h"
50 #include "OSGGL.h"
51 #include "OSGGLU.h"
53 #include "OSGDrawEnv.h"
55 OSG_BEGIN_NAMESPACE
57 // Documentation for this class is emitted in the
58 // OSGDSLightChunkBase.cpp file.
59 // To modify it, please change the .fcd file (OSGDSLightChunk.fcd) and
60 // regenerate the base file.
62 /***************************************************************************\
63 * Class variables *
64 \***************************************************************************/
66 StateChunkClass DSLightChunk::_class("DSLight", 1, 170);
68 /***************************************************************************\
69 * Class methods *
70 \***************************************************************************/
72 void DSLightChunk::initMethod(InitPhase ePhase)
74 Inherited::initMethod(ePhase);
76 if(ePhase == TypeObject::SystemPost)
82 /***************************************************************************\
83 * Instance methods *
84 \***************************************************************************/
86 /*-------------------------------------------------------------------------*\
87 - private -
88 \*-------------------------------------------------------------------------*/
90 /*----------------------- constructors & destructors ----------------------*/
92 DSLightChunk::DSLightChunk(void) :
93 Inherited()
97 DSLightChunk::DSLightChunk(const DSLightChunk &source) :
98 Inherited(source)
102 DSLightChunk::~DSLightChunk(void)
106 /*----------------------------- class specific ----------------------------*/
108 const StateChunkClass *DSLightChunk::getClass(void) const
110 return &_class;
113 void DSLightChunk::changed(ConstFieldMaskArg whichField,
114 UInt32 origin,
115 BitVector details)
117 Inherited::changed(whichField, origin, details);
120 void DSLightChunk::dump( UInt32 ,
121 const BitVector ) const
123 SLOG << "Dump DSLightChunk NI" << std::endl;
126 /*--------------------------------- State --------------------------------*/
128 void DSLightChunk::activate(DrawEnv *drawEnv, UInt32 index)
130 glErr("dslight:activate:precheck");
132 Matrix matMV;
133 calcModelView(drawEnv, matMV);
135 glPushMatrix();
136 glLoadMatrixf(matMV.getValues());
138 glLightfv( GL_LIGHT0 + index,
139 GL_DIFFUSE,
140 _sfDiffuse.getValue().getValuesRGBA());
142 glLightfv( GL_LIGHT0 + index,
143 GL_AMBIENT,
144 _sfAmbient.getValue().getValuesRGBA());
146 glLightfv( GL_LIGHT0 + index,
147 GL_SPECULAR,
148 _sfSpecular.getValue().getValuesRGBA());
149 glLightfv( GL_LIGHT0 + index,
150 GL_POSITION,
151 _sfPosition.getValue().getValues());
153 glLightf ( GL_LIGHT0 + index,
154 GL_CONSTANT_ATTENUATION,
155 _sfConstantAttenuation.getValue());
157 glLightf ( GL_LIGHT0 + index,
158 GL_LINEAR_ATTENUATION,
159 _sfLinearAttenuation.getValue());
161 glLightf ( GL_LIGHT0 + index,
162 GL_QUADRATIC_ATTENUATION,
163 _sfQuadraticAttenuation.getValue());
165 glLightf( GL_LIGHT0 + index,
166 GL_SPOT_CUTOFF,
167 _sfCutoff.getValue());
169 if(_sfCutoff.getValue() < 180.f)
171 glLightfv( GL_LIGHT0 + index,
172 GL_SPOT_DIRECTION,
173 _sfDirection.getValue().getValues());
175 glLightf( GL_LIGHT0 + index,
176 GL_SPOT_EXPONENT,
177 _sfExponent.getValue());
180 glEnable(GL_LIGHT0 + index);
182 glPopMatrix();
184 glErr("dslight:activate:postcheck");
187 void DSLightChunk::changeFrom(DrawEnv *drawEnv, StateChunk *old, UInt32 index)
189 glErr("dslight:change:precheck");
191 const DSLightChunk *oldChunk = dynamic_cast<const DSLightChunk *>(old);
193 // change from me to me?
194 // this assumes I haven't changed in the meantime. is that a valid
195 // assumption?
197 if(oldChunk == this)
198 return;
200 Matrix matMV;
201 calcModelView(drawEnv, matMV);
203 glPushMatrix();
204 glLoadMatrixf(matMV.getValues());
206 // it could theoretically be more efficient to turn the light off before
207 // changing its parameters, have to try that sometime
209 glLightfv( GL_LIGHT0 + index,
210 GL_DIFFUSE,
211 _sfDiffuse.getValue().getValuesRGBA());
213 glLightfv( GL_LIGHT0 + index,
214 GL_AMBIENT,
215 _sfAmbient.getValue().getValuesRGBA());
217 glLightfv( GL_LIGHT0 + index,
218 GL_SPECULAR,
219 _sfSpecular.getValue().getValuesRGBA());
221 glLightfv( GL_LIGHT0 + index,
222 GL_POSITION,
223 _sfPosition.getValue().getValues());
225 glLightf ( GL_LIGHT0 + index,
226 GL_CONSTANT_ATTENUATION,
227 _sfConstantAttenuation.getValue());
229 glLightf ( GL_LIGHT0 + index,
230 GL_LINEAR_ATTENUATION,
231 _sfLinearAttenuation.getValue());
233 glLightf ( GL_LIGHT0 + index,
234 GL_QUADRATIC_ATTENUATION,
235 _sfQuadraticAttenuation.getValue());
237 glLightf( GL_LIGHT0 + index,
238 GL_SPOT_CUTOFF,
239 _sfCutoff.getValue());
241 if(_sfCutoff.getValue() < 180.f)
243 glLightfv( GL_LIGHT0 + index,
244 GL_SPOT_DIRECTION,
245 _sfDirection.getValue().getValues());
247 glLightf( GL_LIGHT0 + index,
248 GL_SPOT_EXPONENT,
249 _sfExponent.getValue());
252 glPopMatrix();
254 glErr("dslight:change:postcheck");
257 void DSLightChunk::deactivate(DrawEnv *drawEnv, UInt32 index)
259 glDisable(GL_LIGHT0 + index);
262 /*-------------------------- Comparison -----------------------------------*/
264 Real32 DSLightChunk::switchCost(StateChunk *OSG_CHECK_ARG(chunk))
266 return 0;
269 bool DSLightChunk::operator < (const StateChunk &other) const
271 return this < &other;
274 bool DSLightChunk::operator == (const StateChunk &other) const
276 const DSLightChunk *tother = dynamic_cast<const DSLightChunk *>(&other);
278 if(!tother)
279 return false;
281 if(tother == this)
282 return true;
284 if(!getAmbient ().equals(tother->getAmbient (),
285 TypeTraits<Real32>::getDefaultEps()) ||
286 !getDiffuse ().equals(tother->getDiffuse (),
287 TypeTraits<Real32>::getDefaultEps()) ||
288 !getSpecular ().equals(tother->getSpecular (),
289 TypeTraits<Real32>::getDefaultEps()) ||
290 !getPosition ().equals(tother->getPosition (),
291 TypeTraits<Real32>::getDefaultEps()) ||
292 !getDirection().equals(tother->getDirection(),
293 TypeTraits<Real32>::getDefaultEps()) ||
295 getConstantAttenuation () != tother->getConstantAttenuation () ||
296 getLinearAttenuation () != tother->getLinearAttenuation () ||
297 getQuadraticAttenuation() != tother->getQuadraticAttenuation() ||
298 getCutoff () != tother->getCutoff () ||
299 getExponent () != tother->getExponent ()
302 return false;
305 return true;
308 bool DSLightChunk::operator != (const StateChunk &other) const
310 return ! (*this == other);
313 /*-------------------------------- Helper --------------------------------*/
315 void DSLightChunk::calcModelView(DrawEnv *drawEnv, Matrix &result)
317 result = drawEnv->getVPCameraViewing();
319 Matrix toWorld;
321 if(_sfBeacon.getValue() != NULL)
323 _sfBeacon.getValue()->getToWorld(toWorld);
325 else
327 toWorld.setIdentity();
330 result.mult(toWorld);
333 OSG_END_NAMESPACE