1 /*---------------------------------------------------------------------------*\
5 * Copyright (C) 2000-2002 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"
51 #include "OSGDrawEnv.h"
53 #include "OSGTextureBaseChunk.h"
55 #include "OSGTexGenChunk.h"
56 #include "OSGCamera.h"
57 #include "OSGViewport.h"
62 // Documentation for this class is emited in the
63 // OSGTexGenChunkBase.cpp file.
64 // To modify it, please change the .fcd file (OSGTexGenChunk.fcd) and
65 // regenerate the base file.
67 /***************************************************************************\
69 \***************************************************************************/
71 StateChunkClass
TexGenChunk::_class("TexGen", osgMaxTexCoords
, 110);
73 /***************************************************************************\
75 \***************************************************************************/
77 void TexGenChunk::initMethod(InitPhase ePhase
)
79 Inherited::initMethod(ePhase
);
82 /***************************************************************************\
84 \***************************************************************************/
86 /*-------------------------------------------------------------------------*\
88 \*-------------------------------------------------------------------------*/
90 TexGenChunk::TexGenChunk(void) :
93 _sfEyeModelViewMatrix
.getValue().setIdentity();
96 TexGenChunk::TexGenChunk(const TexGenChunk
&source
) :
101 TexGenChunk::~TexGenChunk(void)
105 /*------------------------- Chunk Class Access ---------------------------*/
107 const StateChunkClass
*TexGenChunk::getClass(void) const
112 /*------------------------------- Sync -----------------------------------*/
114 void TexGenChunk::changed(ConstFieldMaskArg whichField
,
118 Inherited::changed(whichField
, origin
, details
);
121 /*------------------------------ Output ----------------------------------*/
123 void TexGenChunk::dump( UInt32
,
124 const BitVector
) const
126 SLOG
<< "Dump TexGenChunk NI" << std::endl
;
130 /*------------------------------ State ------------------------------------*/
132 static inline void setGenFunc( GLenum coord
,
141 #if !defined(OSG_OGL_COREONLY) || defined(OSG_CHECK_COREONLY)
145 beacon
->getToWorld(beaconMat
);
146 beaconMat
.multLeft(cameraMat
);
148 glLoadMatrixf(beaconMat
.getValues());
151 const_cast<GLfloat
*>(plane
.getValues()));
152 glTexGeni(coord
, GL_TEXTURE_GEN_MODE
, GL_EYE_LINEAR
);
156 else if(func
== GL_EYE_LINEAR
)
162 case TexGenChunk::EyeModelViewIdentity
:
166 case TexGenChunk::EyeModelViewStored
:
167 glLoadMatrixf(eyeMatrix
.getValues());
170 case TexGenChunk::EyeModelViewCamera
:
171 glLoadMatrixf(cameraMat
.getValues());
180 const_cast<GLfloat
*>(plane
.getValues()));
182 glTexGeni(coord
, GL_TEXTURE_GEN_MODE
, GL_EYE_LINEAR
);
188 else if(func
!= GL_NONE
)
190 glTexGeni(coord
, GL_TEXTURE_GEN_MODE
, func
);
192 if(func
== GL_OBJECT_LINEAR
)
196 const_cast<GLfloat
*>(plane
.getValues()));
204 void TexGenChunk::activate(DrawEnv
*pEnv
, UInt32 idx
)
206 glErr("TexGenChunk::activate precheck");
208 #if !defined(OSG_OGL_COREONLY) || defined(OSG_CHECK_COREONLY)
209 pEnv
->incNumChunkChanges();
211 Window
*win
= pEnv
->getWindow();
214 if((ntexcoords
= win
->getConstantValue(GL_MAX_TEXTURE_COORDS_ARB
)) ==
215 Window::unknownConstant
218 ntexcoords
= win
->getConstantValue(GL_MAX_TEXTURE_UNITS_ARB
);
219 // sgi doesn't support GL_MAX_TEXTURE_UNITS_ARB!
220 if(ntexcoords
== Window::unknownConstant
)
224 if(idx
>= static_cast<UInt32
>(ntexcoords
))
227 FWARNING(("TexGenChunk::activate: Trying to bind texcoord unit %d,"
228 " but Window %p only supports %lf!\n",
229 idx
, win
, ntexcoords
));
234 TextureBaseChunk::activateTexture(win
, idx
);
236 FDEBUG(("TexGenChunk::activate\n"));
238 Matrix cameraMat
= pEnv
->getCameraViewing();
241 Viewport
*vp
= pEnv
->getViewport();
244 pEnv
->getCamera()->getViewing(cameraMat
,
246 vp
->getPixelHeight());
251 setGenFunc(GL_S
, GL_TEXTURE_GEN_S
, getGenFuncS(), getGenFuncSPlane(),
252 getSBeacon(), cameraMat
, _sfEyeModelViewMode
.getValue(),
253 _sfEyeModelViewMatrix
.getValue());
254 glErr("TexGenChunk::activateS");
255 setGenFunc(GL_T
, GL_TEXTURE_GEN_T
, getGenFuncT(), getGenFuncTPlane(),
256 getTBeacon(), cameraMat
, _sfEyeModelViewMode
.getValue(),
257 _sfEyeModelViewMatrix
.getValue());
258 glErr("TexGenChunk::activateT");
259 setGenFunc(GL_R
, GL_TEXTURE_GEN_R
, getGenFuncR(), getGenFuncRPlane(),
260 getRBeacon(), cameraMat
, _sfEyeModelViewMode
.getValue(),
261 _sfEyeModelViewMatrix
.getValue());
262 glErr("TexGenChunk::activateR");
263 setGenFunc(GL_Q
, GL_TEXTURE_GEN_Q
, getGenFuncQ(), getGenFuncQPlane(),
264 getQBeacon(), cameraMat
, _sfEyeModelViewMode
.getValue(),
265 _sfEyeModelViewMatrix
.getValue());
269 glErr("TexGenChunk::activateQ");
273 static inline void changeGenFunc( GLenum oldfunc
,
284 #if !defined(OSG_OGL_COREONLY) || defined(OSG_CHECK_COREONLY)
288 beacon
->getToWorld(beaconMat
);
289 beaconMat
.multLeft(cameraMat
);
291 glLoadMatrixf(beaconMat
.getValues());
294 const_cast<GLfloat
*>(plane
.getValues()));
295 glTexGeni(coord
, GL_TEXTURE_GEN_MODE
, GL_EYE_LINEAR
);
297 if(oldfunc
== GL_NONE
&& oldbeacon
== NULL
)
300 else if(func
== GL_EYE_LINEAR
)
306 case TexGenChunk::EyeModelViewIdentity
:
310 case TexGenChunk::EyeModelViewStored
:
311 glLoadMatrixf(eyeMatrix
.getValues());
314 case TexGenChunk::EyeModelViewCamera
:
315 glLoadMatrixf(cameraMat
.getValues());
324 const_cast<GLfloat
*>(plane
.getValues()));
326 glTexGeni(coord
, GL_TEXTURE_GEN_MODE
, GL_EYE_LINEAR
);
330 if(oldfunc
== GL_NONE
&& oldbeacon
== NULL
)
333 else if(func
!= GL_NONE
)
335 glTexGeni(coord
, GL_TEXTURE_GEN_MODE
, func
);
337 if(func
== GL_OBJECT_LINEAR
)
341 const_cast<GLfloat
*>(plane
.getValues()));
344 if(oldfunc
== GL_NONE
&& oldbeacon
== NULL
)
347 else if(oldfunc
!= GL_NONE
|| oldbeacon
!= NULL
)
354 void TexGenChunk::changeFrom(DrawEnv
*pEnv
,
358 #if !defined(OSG_OGL_COREONLY) || defined(OSG_CHECK_COREONLY)
359 // change from me to me?
360 // this assumes I haven't changed in the meantime.
361 // is that a valid assumption?
362 // No, for TexGen it's not, as TexGen depends on the current
366 TexGenChunk
*oldp
= dynamic_cast<TexGenChunk
*>(old
);
368 // If the old one is not a texgen chunk, deactivate it and activate
372 old
->deactivate(pEnv
, idx
);
377 glErr("TexGenChunk::changeFrom precheck");
379 pEnv
->incNumChunkChanges();
381 Window
*win
= pEnv
->getWindow();
384 if((ntexcoords
= win
->getConstantValue(GL_MAX_TEXTURE_COORDS_ARB
)) ==
385 Window::unknownConstant
388 ntexcoords
= win
->getConstantValue(GL_MAX_TEXTURE_UNITS_ARB
);
389 // sgi doesn't support GL_MAX_TEXTURE_UNITS_ARB!
390 if(ntexcoords
== Window::unknownConstant
)
394 if(idx
>= static_cast<UInt32
>(ntexcoords
))
397 FWARNING(("TexGenChunk::changeFrom: Trying to bind texcoord unit "
398 "%d, but Window %p only supports %lf!\n",
399 idx
, win
, ntexcoords
));
404 Matrix cameraMat
= pEnv
->getCameraViewing();
407 Viewport
*vp
= pEnv
->getViewport();
410 pEnv
->getCamera()->getViewing(cameraMat
,
412 vp
->getPixelHeight());
416 TextureBaseChunk::activateTexture(win
, idx
);
418 changeGenFunc(oldp
->getGenFuncS(),
426 _sfEyeModelViewMode
.getValue(),
427 _sfEyeModelViewMatrix
.getValue());
429 changeGenFunc(oldp
->getGenFuncT(), oldp
->getTBeacon(), GL_T
,
431 getGenFuncT(), getGenFuncTPlane(), getTBeacon(), cameraMat
,
432 _sfEyeModelViewMode
.getValue(),
433 _sfEyeModelViewMatrix
.getValue());
435 changeGenFunc(oldp
->getGenFuncR(), oldp
->getRBeacon(), GL_R
,
437 getGenFuncR(), getGenFuncRPlane(), getRBeacon(), cameraMat
,
438 _sfEyeModelViewMode
.getValue(),
439 _sfEyeModelViewMatrix
.getValue());
441 changeGenFunc(oldp
->getGenFuncQ(), oldp
->getQBeacon(), GL_Q
,
443 getGenFuncQ(), getGenFuncQPlane(), getQBeacon(), cameraMat
,
444 _sfEyeModelViewMode
.getValue(),
445 _sfEyeModelViewMatrix
.getValue());
450 glErr("TexGenChunk::changeFrom");
453 void TexGenChunk::deactivate(DrawEnv
*pEnv
, UInt32 idx
)
455 glErr("TexGenChunk::deactivate precheck");
457 #if !defined(OSG_OGL_COREONLY) || defined(OSG_CHECK_COREONLY)
458 Window
*win
= pEnv
->getWindow();
461 if((ntexcoords
= win
->getConstantValue(GL_MAX_TEXTURE_COORDS_ARB
)) ==
462 Window::unknownConstant
465 ntexcoords
= win
->getConstantValue(GL_MAX_TEXTURE_UNITS_ARB
);
466 // sgi doesn't support GL_MAX_TEXTURE_UNITS_ARB!
467 if(ntexcoords
== Window::unknownConstant
)
471 if(idx
>= static_cast<UInt32
>(ntexcoords
))
474 FWARNING(("TexGenChunk::deactivate: Trying to bind texcoord unit %d,"
475 " but Window %p only supports %lf!\n",
476 idx
, win
, ntexcoords
));
481 TextureBaseChunk::activateTexture(win
, idx
);
483 if(getGenFuncS() != GL_NONE
|| getSBeacon() != NULL
)
484 glDisable(GL_TEXTURE_GEN_S
);
486 if(getGenFuncT() != GL_NONE
|| getTBeacon() != NULL
)
487 glDisable(GL_TEXTURE_GEN_T
);
489 if(getGenFuncR() != GL_NONE
|| getRBeacon() != NULL
)
490 glDisable(GL_TEXTURE_GEN_R
);
492 if(getGenFuncQ() != GL_NONE
|| getQBeacon() != NULL
)
493 glDisable(GL_TEXTURE_GEN_Q
);
498 glErr("TexGenChunk::deactivate");
501 /*-------------------------- Comparison -----------------------------------*/
503 Real32
TexGenChunk::switchCost(StateChunk
*OSG_CHECK_ARG(chunk
))
508 bool TexGenChunk::operator < (const StateChunk
&other
) const
510 return this < &other
;
513 bool TexGenChunk::operator == (const StateChunk
&other
) const
515 TexGenChunk
const *tother
= dynamic_cast<TexGenChunk
const*>(&other
);
523 return getGenFuncS() == tother
->getGenFuncS() &&
524 getGenFuncT() == tother
->getGenFuncT() &&
525 getGenFuncR() == tother
->getGenFuncR() &&
526 getGenFuncQ() == tother
->getGenFuncQ() &&
527 // not quite right. needs only to be tested for genfuncs using them
528 getGenFuncSPlane() == tother
->getGenFuncSPlane() &&
529 getGenFuncTPlane() == tother
->getGenFuncTPlane() &&
530 getGenFuncRPlane() == tother
->getGenFuncRPlane() &&
531 getGenFuncQPlane() == tother
->getGenFuncQPlane() ;
534 bool TexGenChunk::operator != (const StateChunk
&other
) const
536 return ! (*this == other
);