1 /*---------------------------------------------------------------------------*\
5 * Copyright (C) 2000-2006 by the OpenSG Forum *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
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. *
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. *
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. *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
37 \*---------------------------------------------------------------------------*/
39 //---------------------------------------------------------------------------
41 //---------------------------------------------------------------------------
46 #include "OSGConfig.h"
48 #include "OSGDSLightChunk.h"
53 #include "OSGDrawEnv.h"
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 /***************************************************************************\
64 \***************************************************************************/
66 StateChunkClass
DSLightChunk::_class("DSLight", 1, 170);
68 /***************************************************************************\
70 \***************************************************************************/
72 void DSLightChunk::initMethod(InitPhase ePhase
)
74 Inherited::initMethod(ePhase
);
76 if(ePhase
== TypeObject::SystemPost
)
82 /***************************************************************************\
84 \***************************************************************************/
86 /*-------------------------------------------------------------------------*\
88 \*-------------------------------------------------------------------------*/
90 /*----------------------- constructors & destructors ----------------------*/
92 DSLightChunk::DSLightChunk(void) :
97 DSLightChunk::DSLightChunk(const DSLightChunk
&source
) :
102 DSLightChunk::~DSLightChunk(void)
106 /*----------------------------- class specific ----------------------------*/
108 const StateChunkClass
*DSLightChunk::getClass(void) const
113 void DSLightChunk::changed(ConstFieldMaskArg whichField
,
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");
133 calcModelView(drawEnv
, matMV
);
136 glLoadMatrixf(matMV
.getValues());
138 glLightfv( GL_LIGHT0
+ index
,
140 _sfDiffuse
.getValue().getValuesRGBA());
142 glLightfv( GL_LIGHT0
+ index
,
144 _sfAmbient
.getValue().getValuesRGBA());
146 glLightfv( GL_LIGHT0
+ index
,
148 _sfSpecular
.getValue().getValuesRGBA());
149 glLightfv( GL_LIGHT0
+ index
,
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
,
167 _sfCutoff
.getValue());
169 if(_sfCutoff
.getValue() < 180.f
)
171 glLightfv( GL_LIGHT0
+ index
,
173 _sfDirection
.getValue().getValues());
175 glLightf( GL_LIGHT0
+ index
,
177 _sfExponent
.getValue());
180 glEnable(GL_LIGHT0
+ index
);
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
201 calcModelView(drawEnv
, matMV
);
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
,
211 _sfDiffuse
.getValue().getValuesRGBA());
213 glLightfv( GL_LIGHT0
+ index
,
215 _sfAmbient
.getValue().getValuesRGBA());
217 glLightfv( GL_LIGHT0
+ index
,
219 _sfSpecular
.getValue().getValuesRGBA());
221 glLightfv( GL_LIGHT0
+ index
,
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
,
239 _sfCutoff
.getValue());
241 if(_sfCutoff
.getValue() < 180.f
)
243 glLightfv( GL_LIGHT0
+ index
,
245 _sfDirection
.getValue().getValues());
247 glLightf( GL_LIGHT0
+ index
,
249 _sfExponent
.getValue());
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
))
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
);
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 ()
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();
321 if(_sfBeacon
.getValue() != NULL
)
323 _sfBeacon
.getValue()->getToWorld(toWorld
);
327 toWorld
.setIdentity();
330 result
.mult(toWorld
);