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 //---------------------------------------------------------------------------
45 #include <boost/bind.hpp>
47 #include "OSGConfig.h"
52 #include "OSGDrawEnv.h"
54 #include "OSGCubeTextureObjChunk.h"
59 // Documentation for this class is emited in the
60 // OSGCubeTextureObjChunkBase.cpp file.
61 // To modify it, please change the .fcd file (OSGCubeTextureObjChunk.fcd) and
62 // regenerate the base file.
64 void CubeTextureObjChunk::initMethod(InitPhase ePhase
)
66 Inherited::initMethod(ePhase
);
70 /***************************************************************************\
72 \***************************************************************************/
74 /*-------------------------------------------------------------------------*\
76 \*-------------------------------------------------------------------------*/
78 /*------------- constructors & destructors --------------------------------*/
80 CubeTextureObjChunk::CubeTextureObjChunk(void) :
85 CubeTextureObjChunk::CubeTextureObjChunk(const CubeTextureObjChunk
&source
) :
90 CubeTextureObjChunk::~CubeTextureObjChunk(void)
94 /*------------------------- Chunk Class Access ---------------------------*/
96 const StateChunkClass
*CubeTextureObjChunk::getClass(void) const
98 return TextureBaseChunk::getClass();
101 /*------------------------------- Sync -----------------------------------*/
103 bool CubeTextureObjChunk::isCubeTexture(void)
110 void CubeTextureObjChunk::changed(ConstFieldMaskArg whichField
,
114 Inherited::changed(whichField
, origin
, details
);
117 /*----------------------------- onCreate --------------------------------*/
119 void CubeTextureObjChunk::onCreate(const CubeTextureObjChunk
*source
)
121 // skip TextureObjChunk::onCreate
122 TextureBaseChunk::onCreate(source
);
124 if(GlobalSystemState
== Startup
)
128 Window::registerGLObject(
129 boost::bind(&CubeTextureObjChunk::handleGL
,
130 CubeTextureObjChunkMTUncountedPtr(this),
132 &CubeTextureObjChunk::handleDestroyGL
));
135 void CubeTextureObjChunk::onDestroy(UInt32 uiContainerId
)
138 Window::destroyGLObject(getGLId(), 1);
140 // skip TextureObjChunk::onDestroy
141 TextureBaseChunk::onDestroy(uiContainerId
);
144 /*------------------------------ Output ----------------------------------*/
146 void CubeTextureObjChunk::dump( UInt32
,
147 const BitVector
) const
149 SLOG
<< "Dump CubeTextureObjChunk NI" << std::endl
;
153 /*-------------------------------- GL ------------------------------------*/
155 UInt32
CubeTextureObjChunk::handleGL(DrawEnv
*pEnv
,
157 Window::GLObjectStatusE mode
,
162 Window
*win
= pEnv
->getWindow();
164 // does the window support cubemaps?
165 if(win
->hasExtOrVersion(_arbCubeTex
, 0x0103, 0x0200) == false)
168 id
= win
->getGLObjectId(osgid
);
170 if(mode
== Window::initialize
|| mode
== Window::reinitialize
)
172 if(mode
== Window::initialize
)
174 glGenTextures(1, &id
);
176 win
->setGLObjectId(osgid
, id
);
179 handleTexture(win
, id
,
180 GL_TEXTURE_CUBE_MAP_ARB
,
181 GL_TEXTURE_CUBE_MAP_ARB
,
182 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
,
184 handleTexture(win
, id
,
185 GL_TEXTURE_CUBE_MAP_ARB
,
187 GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
,
188 Window::initialize
, getPosZImage());
189 handleTexture(win
, id
,
190 GL_TEXTURE_CUBE_MAP_ARB
,
192 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
,
193 Window::initialize
, getNegYImage());
194 handleTexture(win
, id
,
195 GL_TEXTURE_CUBE_MAP_ARB
,
197 GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
,
198 Window::initialize
, getPosYImage());
199 handleTexture(win
, id
,
200 GL_TEXTURE_CUBE_MAP_ARB
,
202 GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
,
203 Window::initialize
, getNegXImage());
204 handleTexture(win
, id
,
205 GL_TEXTURE_CUBE_MAP_ARB
,
207 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
,
208 Window::initialize
, getPosXImage());
210 else if(mode
== Window::needrefresh
)
212 handleTexture(win
, id
,
213 GL_TEXTURE_CUBE_MAP_ARB
,
214 GL_TEXTURE_CUBE_MAP_ARB
,
215 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
,
217 handleTexture(win
, id
,
218 GL_TEXTURE_CUBE_MAP_ARB
,
220 GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
,
221 mode
, getPosZImage());
222 handleTexture(win
, id
,
223 GL_TEXTURE_CUBE_MAP_ARB
,
225 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
,
226 mode
, getNegYImage());
227 handleTexture(win
, id
,
228 GL_TEXTURE_CUBE_MAP_ARB
,
230 GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
,
231 mode
, getPosYImage());
232 handleTexture(win
, id
,
233 GL_TEXTURE_CUBE_MAP_ARB
,
235 GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
,
236 mode
, getNegXImage());
237 handleTexture(win
, id
,
238 GL_TEXTURE_CUBE_MAP_ARB
,
240 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
,
241 mode
, getPosXImage());
245 SWARNING
<< "Win:" << win
<< "TextureObjChunk(" << this
246 << "::handleGL: Illegal mode: "
247 << mode
<< " for id " << id
<< std::endl
;
254 void CubeTextureObjChunk::handleDestroyGL(DrawEnv
*pEnv
,
256 Window::GLObjectStatusE mode
)
260 Window
*win
= pEnv
->getWindow();
262 // does the window support cubemaps?
263 if(win
->hasExtOrVersion(_arbCubeTex
, 0x0103, 0x0200) == false)
266 id
= win
->getGLObjectId(osgid
);
268 if(mode
== Window::destroy
)
270 glDeleteTextures(1, &id
);
271 win
->setGLObjectId(osgid
, 0);
273 else if(mode
== Window::finaldestroy
)
275 //SWARNING << "Last texture user destroyed" << std::endl;
279 SWARNING
<< "Win:" << win
<< "TextureObjChunk"
280 << "::handleDestroyGL: Illegal mode: "
281 << mode
<< " for id " << id
<< std::endl
;
286 /*------------------------------ State ------------------------------------*/
288 void CubeTextureObjChunk::activate(DrawEnv
*pEnv
, UInt32 idx
)
290 // does the window support cubemaps?
291 if(pEnv
->getWindow()->hasExtOrVersion(_arbCubeTex
, 0x0103, 0x0200) == false)
294 pEnv
->incNumChunkChanges();
296 Window
*win
= pEnv
->getWindow();
298 Real32 nteximages
, ntexcoords
;
300 if((nteximages
= win
->getConstantValue(GL_MAX_TEXTURE_IMAGE_UNITS_ARB
)) ==
301 Window::unknownConstant
)
303 nteximages
= win
->getConstantValue(GL_MAX_TEXTURE_UNITS_ARB
);
305 // sgi doesn't support GL_MAX_TEXTURE_UNITS_ARB!
306 if(nteximages
== Window::unknownConstant
)
310 if((ntexcoords
= win
->getConstantValue(GL_MAX_TEXTURE_COORDS_ARB
)) ==
311 Window::unknownConstant
)
313 ntexcoords
= win
->getConstantValue(GL_MAX_TEXTURE_UNITS_ARB
);
315 // sgi doesn't support GL_MAX_TEXTURE_UNITS_ARB!
316 if(ntexcoords
== Window::unknownConstant
)
320 if(idx
>= static_cast<UInt32
>(nteximages
))
323 FWARNING(("CubeTextureObjChunk::activate: Trying to bind image unit "
324 "%d, but Window %p only supports %lf!\n",
326 static_cast<void *>(win
),
332 if(TextureBaseChunk::activateTexture(win
, idx
))
333 return; // trying to use too many textures
335 glErr("CubeTextureObjChunk::activate precheck");
337 win
->validateGLObject(getGLId(), pEnv
);
340 FDEBUG(("CubeTextureObjChunk::activate - %d\n", getGLId()));
343 // Update the texture statistics
344 StatCollectorP pColl
= pEnv
->getStatCollector();
348 pColl
->getElem(TextureObjChunk::statNTextures
)->inc(getGLId());
350 if(getImage() != NULL
)
352 pColl
->getElem(TextureObjChunk::statNTexBytes
)->add(
354 getImage()->getSize(true,true,true) * 6);
358 glBindTexture(GL_TEXTURE_CUBE_MAP_ARB
, win
->getGLObjectId(getGLId()));
360 pEnv
->setActiveTexTarget(idx
, GL_TEXTURE_CUBE_MAP_ARB
);
362 glEnable(GL_TEXTURE_CUBE_MAP_ARB
);
363 glErr("CubeTextureObjChunk::activate");
366 void CubeTextureObjChunk::changeFrom(DrawEnv
*pEnv
,
370 // does the window support cubemaps?
371 if(pEnv
->getWindow()->hasExtOrVersion(_arbCubeTex
, 0x0103, 0x0200) == false)
374 // change from me to me?
375 // this assumes I haven't changed in the meantime.
376 // is that a valid assumption?
380 pEnv
->incNumChunkChanges();
382 // If the old one is not a cube texture chunk, deactivate it and
383 // activate ourselves
384 if(getTypeId() != old
->getTypeId())
386 old
->deactivate(pEnv
, idx
);
391 Window
*win
= pEnv
->getWindow();
393 Real32 nteximages
, ntexcoords
;
395 glErr("CubeTextureObjChunk::changeFrom precheck");
397 if((nteximages
= win
->getConstantValue(GL_MAX_TEXTURE_IMAGE_UNITS_ARB
)) ==
398 Window::unknownConstant
)
400 nteximages
= win
->getConstantValue(GL_MAX_TEXTURE_UNITS_ARB
);
402 // sgi doesn't support GL_MAX_TEXTURE_UNITS_ARB!
403 if(nteximages
== Window::unknownConstant
)
407 if((ntexcoords
= win
->getConstantValue(GL_MAX_TEXTURE_COORDS_ARB
)) ==
408 Window::unknownConstant
)
410 ntexcoords
= win
->getConstantValue(GL_MAX_TEXTURE_UNITS_ARB
);
412 // sgi doesn't support GL_MAX_TEXTURE_UNITS_ARB!
413 if(ntexcoords
== Window::unknownConstant
)
417 if(idx
>= static_cast<UInt32
>(nteximages
))
420 FWARNING(("TextureObjChunk::activate: Trying to bind image unit %d,"
421 " but Window %p only supports %lf!\n",
423 static_cast<void *>(win
),
429 if(TextureBaseChunk::activateTexture(win
, idx
))
430 return; // trying to use too many textures
432 win
->validateGLObject(getGLId(), pEnv
);
434 FDEBUG(("CubeTextureObjChunk::activate - %d\n", getGLId()));
436 // Update the texture statistics
437 StatCollectorP pColl
= pEnv
->getStatCollector();
441 pColl
->getElem(TextureObjChunk::statNTextures
)->inc(getGLId());
443 if(getImage() != NULL
)
445 pColl
->getElem(TextureObjChunk::statNTexBytes
)->add(
447 getImage()->getSize(true,true,true) * 6);
451 glBindTexture(GL_TEXTURE_CUBE_MAP_ARB
, win
->getGLObjectId(getGLId()));
453 pEnv
->setActiveTexTarget(idx
, GL_TEXTURE_CUBE_MAP_ARB
);
456 glErr("CubeTextureObjChunk::changeFrom");
459 void CubeTextureObjChunk::deactivate(DrawEnv
*pEnv
, UInt32 idx
)
461 // does the window support cubemaps?
462 if(pEnv
->getWindow()->hasExtOrVersion(_arbCubeTex
, 0x0103, 0x0200) == false)
465 Window
*win
= pEnv
->getWindow();
467 Real32 nteximages
, ntexcoords
;
468 if((nteximages
= win
->getConstantValue(GL_MAX_TEXTURE_IMAGE_UNITS_ARB
)) ==
469 Window::unknownConstant
)
471 nteximages
= win
->getConstantValue(GL_MAX_TEXTURE_UNITS_ARB
);
473 // sgi doesn't support GL_MAX_TEXTURE_UNITS_ARB!
474 if(nteximages
== Window::unknownConstant
)
478 if((ntexcoords
= win
->getConstantValue(GL_MAX_TEXTURE_COORDS_ARB
)) ==
479 Window::unknownConstant
)
481 ntexcoords
= win
->getConstantValue(GL_MAX_TEXTURE_UNITS_ARB
);
483 // sgi doesn't support GL_MAX_TEXTURE_UNITS_ARB!
484 if(ntexcoords
== Window::unknownConstant
)
488 if(idx
>= static_cast<UInt32
>(nteximages
))
491 FWARNING(("CubeTextureObjChunk::deactivate: Trying to bind image unit"
492 " %d, but Window %p only supports %lf!\n",
494 static_cast<void *>(win
),
500 TextureBaseChunk::activateTexture(win
, idx
);
502 glDisable(GL_TEXTURE_CUBE_MAP_ARB
);
504 pEnv
->setActiveTexTarget(idx
, GL_NONE
);
506 glErr("CubeTextureObjChunk::deactivate");
509 /*-------------------------- Comparison -----------------------------------*/
511 Real32
CubeTextureObjChunk::switchCost(StateChunk
*OSG_CHECK_ARG(chunk
))
516 bool CubeTextureObjChunk::operator < (const StateChunk
&other
) const
518 return this < &other
;
521 bool CubeTextureObjChunk::operator == (const StateChunk
&other
) const
523 CubeTextureObjChunk
const *tother
=
524 dynamic_cast<CubeTextureObjChunk
const*>(&other
);
532 return TextureBaseChunk::operator==(other
) &&
533 getPosZImage() == tother
->getPosZImage() &&
534 getPosYImage() == tother
->getPosYImage() &&
535 getNegYImage() == tother
->getNegYImage() &&
536 getPosXImage() == tother
->getPosXImage() &&
537 getNegXImage() == tother
->getNegXImage();
540 bool CubeTextureObjChunk::operator != (const StateChunk
&other
) const
542 return ! (*this == other
);