fixed: stay with old cmake qt link setup (policy CMP0020)
[opensg.git] / Source / System / State / OpenGL / OSGTexGenChunk.cpp
blobd2b4fbc348f250dc8fa85745afd4ba0fdc6a35b3
1 /*---------------------------------------------------------------------------*\
2 * OpenSG *
3 * *
4 * *
5 * Copyright (C) 2000-2002 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 "OSGGL.h"
49 #include "OSGGLU.h"
51 #include "OSGDrawEnv.h"
53 #include "OSGTextureBaseChunk.h"
55 #include "OSGTexGenChunk.h"
56 #include "OSGCamera.h"
57 #include "OSGViewport.h"
58 #include "OSGNode.h"
60 OSG_USING_NAMESPACE
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 /***************************************************************************\
68 * Class variables *
69 \***************************************************************************/
71 StateChunkClass TexGenChunk::_class("TexGen", osgMaxTexCoords, 110);
73 /***************************************************************************\
74 * Class methods *
75 \***************************************************************************/
77 void TexGenChunk::initMethod(InitPhase ePhase)
79 Inherited::initMethod(ePhase);
82 /***************************************************************************\
83 * Instance methods *
84 \***************************************************************************/
86 /*-------------------------------------------------------------------------*\
87 - private -
88 \*-------------------------------------------------------------------------*/
90 TexGenChunk::TexGenChunk(void) :
91 Inherited()
93 _sfEyeModelViewMatrix.getValue().setIdentity();
96 TexGenChunk::TexGenChunk(const TexGenChunk &source) :
97 Inherited(source)
101 TexGenChunk::~TexGenChunk(void)
105 /*------------------------- Chunk Class Access ---------------------------*/
107 const StateChunkClass *TexGenChunk::getClass(void) const
109 return &_class;
112 /*------------------------------- Sync -----------------------------------*/
114 void TexGenChunk::changed(ConstFieldMaskArg whichField,
115 UInt32 origin,
116 BitVector details)
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,
133 GLenum gen,
134 GLenum func,
135 const Vec4f &plane,
136 Node *beacon,
137 Matrix &cameraMat,
138 UInt32 eyeMode,
139 Matrix &eyeMatrix)
141 #if !defined(OSG_OGL_COREONLY) || defined(OSG_CHECK_COREONLY)
142 if(beacon != NULL)
144 Matrix beaconMat;
145 beacon->getToWorld(beaconMat);
146 beaconMat.multLeft(cameraMat);
147 glPushMatrix();
148 glLoadMatrixf(beaconMat.getValues());
149 glTexGenfv(coord,
150 GL_EYE_PLANE,
151 const_cast<GLfloat *>(plane.getValues()));
152 glTexGeni(coord, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
153 glPopMatrix();
154 glEnable(gen);
156 else if(func == GL_EYE_LINEAR)
158 glPushMatrix();
160 switch(eyeMode)
162 case TexGenChunk::EyeModelViewIdentity:
163 glLoadIdentity();
164 break;
166 case TexGenChunk::EyeModelViewStored:
167 glLoadMatrixf(eyeMatrix.getValues());
168 break;
170 case TexGenChunk::EyeModelViewCamera:
171 glLoadMatrixf(cameraMat.getValues());
172 break;
174 default:
175 break;
178 glTexGenfv(coord,
179 GL_EYE_PLANE,
180 const_cast<GLfloat *>(plane.getValues()));
182 glTexGeni(coord, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
184 glPopMatrix();
186 glEnable(gen);
188 else if(func != GL_NONE)
190 glTexGeni(coord, GL_TEXTURE_GEN_MODE, func);
192 if(func == GL_OBJECT_LINEAR)
194 glTexGenfv(coord,
195 GL_OBJECT_PLANE,
196 const_cast<GLfloat *>(plane.getValues()));
199 glEnable(gen);
201 #endif
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();
213 Real32 ntexcoords;
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)
221 ntexcoords = 1.0f;
224 if(idx >= static_cast<UInt32>(ntexcoords))
226 #ifdef OSG_DEBUG
227 FWARNING(("TexGenChunk::activate: Trying to bind texcoord unit %d,"
228 " but Window %p only supports %lf!\n",
229 idx, win, ntexcoords));
230 #endif
231 return;
234 TextureBaseChunk::activateTexture(win, idx);
236 FDEBUG(("TexGenChunk::activate\n"));
238 Matrix cameraMat = pEnv->getCameraViewing();
240 #if OLD_DA
241 Viewport *vp = pEnv->getViewport();
242 if(vp != NULL)
244 pEnv->getCamera()->getViewing(cameraMat,
245 vp->getPixelWidth(),
246 vp->getPixelHeight());
248 #endif
250 // genfuncs
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());
266 #else
267 OSG_ASSERT(false);
268 #endif
269 glErr("TexGenChunk::activateQ");
273 static inline void changeGenFunc( GLenum oldfunc,
274 Node *oldbeacon,
275 GLenum coord,
276 GLenum gen,
277 GLenum func,
278 const Vec4f &plane,
279 Node *beacon,
280 Matrix &cameraMat,
281 UInt32 eyeMode,
282 Matrix &eyeMatrix)
284 #if !defined(OSG_OGL_COREONLY) || defined(OSG_CHECK_COREONLY)
285 if(beacon != NULL)
287 Matrix beaconMat;
288 beacon->getToWorld(beaconMat);
289 beaconMat.multLeft(cameraMat);
290 glPushMatrix();
291 glLoadMatrixf(beaconMat.getValues());
292 glTexGenfv(coord,
293 GL_EYE_PLANE,
294 const_cast<GLfloat *>(plane.getValues()));
295 glTexGeni(coord, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
296 glPopMatrix();
297 if(oldfunc == GL_NONE && oldbeacon == NULL)
298 glEnable(gen);
300 else if(func == GL_EYE_LINEAR)
302 glPushMatrix();
304 switch(eyeMode)
306 case TexGenChunk::EyeModelViewIdentity:
307 glLoadIdentity();
308 break;
310 case TexGenChunk::EyeModelViewStored:
311 glLoadMatrixf(eyeMatrix.getValues());
312 break;
314 case TexGenChunk::EyeModelViewCamera:
315 glLoadMatrixf(cameraMat.getValues());
316 break;
318 default:
319 break;
322 glTexGenfv(coord,
323 GL_EYE_PLANE,
324 const_cast<GLfloat *>(plane.getValues()));
326 glTexGeni(coord, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
328 glPopMatrix();
330 if(oldfunc == GL_NONE && oldbeacon == NULL)
331 glEnable(gen);
333 else if(func != GL_NONE)
335 glTexGeni(coord, GL_TEXTURE_GEN_MODE, func);
337 if(func == GL_OBJECT_LINEAR)
339 glTexGenfv(coord,
340 GL_OBJECT_PLANE,
341 const_cast<GLfloat *>(plane.getValues()));
344 if(oldfunc == GL_NONE && oldbeacon == NULL)
345 glEnable(gen);
347 else if(oldfunc != GL_NONE || oldbeacon != NULL)
349 glDisable(gen);
351 #endif
354 void TexGenChunk::changeFrom(DrawEnv *pEnv,
355 StateChunk *old ,
356 UInt32 idx)
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
363 // toWorld matrix!!!
364 // if(old == this)
365 // return;
366 TexGenChunk *oldp = dynamic_cast<TexGenChunk *>(old);
368 // If the old one is not a texgen chunk, deactivate it and activate
369 // ourselves
370 if(!oldp)
372 old->deactivate(pEnv, idx);
373 activate(pEnv, idx);
374 return;
377 glErr("TexGenChunk::changeFrom precheck");
379 pEnv->incNumChunkChanges();
381 Window *win = pEnv->getWindow();
383 Real32 ntexcoords;
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)
391 ntexcoords = 1.0f;
394 if(idx >= static_cast<UInt32>(ntexcoords))
396 #ifdef OSG_DEBUG
397 FWARNING(("TexGenChunk::changeFrom: Trying to bind texcoord unit "
398 "%d, but Window %p only supports %lf!\n",
399 idx, win, ntexcoords));
400 #endif
401 return;
404 Matrix cameraMat = pEnv->getCameraViewing();
406 #ifdef OLD_DA
407 Viewport *vp = pEnv->getViewport();
408 if(vp != NULL)
410 pEnv->getCamera()->getViewing(cameraMat,
411 vp->getPixelWidth(),
412 vp->getPixelHeight());
414 #endif
416 TextureBaseChunk::activateTexture(win, idx);
418 changeGenFunc(oldp->getGenFuncS(),
419 oldp->getSBeacon(),
420 GL_S,
421 GL_TEXTURE_GEN_S,
422 getGenFuncS(),
423 getGenFuncSPlane(),
424 getSBeacon(),
425 cameraMat,
426 _sfEyeModelViewMode.getValue(),
427 _sfEyeModelViewMatrix.getValue());
429 changeGenFunc(oldp->getGenFuncT(), oldp->getTBeacon(), GL_T,
430 GL_TEXTURE_GEN_T,
431 getGenFuncT(), getGenFuncTPlane(), getTBeacon(), cameraMat,
432 _sfEyeModelViewMode.getValue(),
433 _sfEyeModelViewMatrix.getValue());
435 changeGenFunc(oldp->getGenFuncR(), oldp->getRBeacon(), GL_R,
436 GL_TEXTURE_GEN_R,
437 getGenFuncR(), getGenFuncRPlane(), getRBeacon(), cameraMat,
438 _sfEyeModelViewMode.getValue(),
439 _sfEyeModelViewMatrix.getValue());
441 changeGenFunc(oldp->getGenFuncQ(), oldp->getQBeacon(), GL_Q,
442 GL_TEXTURE_GEN_Q,
443 getGenFuncQ(), getGenFuncQPlane(), getQBeacon(), cameraMat,
444 _sfEyeModelViewMode.getValue(),
445 _sfEyeModelViewMatrix.getValue());
446 #else
447 OSG_ASSERT(false);
448 #endif
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();
460 Real32 ntexcoords;
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)
468 ntexcoords = 1.0f;
471 if(idx >= static_cast<UInt32>(ntexcoords))
473 #ifdef OSG_DEBUG
474 FWARNING(("TexGenChunk::deactivate: Trying to bind texcoord unit %d,"
475 " but Window %p only supports %lf!\n",
476 idx, win, ntexcoords));
477 #endif
478 return;
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);
494 #else
495 OSG_ASSERT(false);
496 #endif
498 glErr("TexGenChunk::deactivate");
501 /*-------------------------- Comparison -----------------------------------*/
503 Real32 TexGenChunk::switchCost(StateChunk *OSG_CHECK_ARG(chunk))
505 return 0;
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);
517 if(!tother)
518 return false;
520 if(tother == this)
521 return true;
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);