fixed: auto_ptr -> unique_ptr
[opensg.git] / Source / System / State / OpenGL / OSGCubeTextureObjChunk.cpp
blob5c0ae59d2619d578f8460011936d2f91302a079e
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>
45 #include <boost/bind.hpp>
47 #include "OSGConfig.h"
49 #include "OSGGL.h"
50 #include "OSGGLU.h"
51 #include "OSGGLEXT.h"
52 #include "OSGDrawEnv.h"
54 #include "OSGCubeTextureObjChunk.h"
55 #include "OSGImage.h"
57 OSG_USING_NAMESPACE
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 /***************************************************************************\
71 * Instance methods *
72 \***************************************************************************/
74 /*-------------------------------------------------------------------------*\
75 - public -
76 \*-------------------------------------------------------------------------*/
78 /*------------- constructors & destructors --------------------------------*/
80 CubeTextureObjChunk::CubeTextureObjChunk(void) :
81 Inherited()
85 CubeTextureObjChunk::CubeTextureObjChunk(const CubeTextureObjChunk &source) :
86 Inherited(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)
105 return true;
110 void CubeTextureObjChunk::changed(ConstFieldMaskArg whichField,
111 UInt32 origin,
112 BitVector details)
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)
125 return;
127 setGLId(
128 Window::registerGLObject(
129 boost::bind(&CubeTextureObjChunk::handleGL,
130 CubeTextureObjChunkMTUncountedPtr(this),
131 _1, _2, _3, _4),
132 &CubeTextureObjChunk::handleDestroyGL));
135 void CubeTextureObjChunk::onDestroy(UInt32 uiContainerId)
137 if(getGLId() > 0)
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,
156 UInt32 osgid,
157 Window::GLObjectStatusE mode,
158 UInt64 uiOptions)
160 GLuint id;
162 Window *win = pEnv->getWindow();
164 // does the window support cubemaps?
165 if(win->hasExtOrVersion(_arbCubeTex, 0x0103, 0x0200) == false)
166 return 0;
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,
183 mode, getImage());
184 handleTexture(win, id,
185 GL_TEXTURE_CUBE_MAP_ARB,
186 GL_NONE,
187 GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB,
188 Window::initialize, getPosZImage());
189 handleTexture(win, id,
190 GL_TEXTURE_CUBE_MAP_ARB,
191 GL_NONE,
192 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB,
193 Window::initialize, getNegYImage());
194 handleTexture(win, id,
195 GL_TEXTURE_CUBE_MAP_ARB,
196 GL_NONE,
197 GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,
198 Window::initialize, getPosYImage());
199 handleTexture(win, id,
200 GL_TEXTURE_CUBE_MAP_ARB,
201 GL_NONE,
202 GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB,
203 Window::initialize, getNegXImage());
204 handleTexture(win, id,
205 GL_TEXTURE_CUBE_MAP_ARB,
206 GL_NONE,
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,
216 mode, getImage());
217 handleTexture(win, id,
218 GL_TEXTURE_CUBE_MAP_ARB,
219 GL_NONE,
220 GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB,
221 mode, getPosZImage());
222 handleTexture(win, id,
223 GL_TEXTURE_CUBE_MAP_ARB,
224 GL_NONE,
225 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB,
226 mode, getNegYImage());
227 handleTexture(win, id,
228 GL_TEXTURE_CUBE_MAP_ARB,
229 GL_NONE,
230 GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,
231 mode, getPosYImage());
232 handleTexture(win, id,
233 GL_TEXTURE_CUBE_MAP_ARB,
234 GL_NONE,
235 GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB,
236 mode, getNegXImage());
237 handleTexture(win, id,
238 GL_TEXTURE_CUBE_MAP_ARB,
239 GL_NONE,
240 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,
241 mode, getPosXImage());
243 else
245 SWARNING << "Win:" << win << "TextureObjChunk(" << this
246 << "::handleGL: Illegal mode: "
247 << mode << " for id " << id << std::endl;
250 return 0;
254 void CubeTextureObjChunk::handleDestroyGL(DrawEnv *pEnv,
255 UInt32 osgid,
256 Window::GLObjectStatusE mode)
258 GLuint id;
260 Window *win = pEnv->getWindow();
262 // does the window support cubemaps?
263 if(win->hasExtOrVersion(_arbCubeTex, 0x0103, 0x0200) == false)
264 return;
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;
277 else
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)
292 return;
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)
307 nteximages = 1.0f;
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)
317 ntexcoords = 1.0f;
320 if(idx >= static_cast<UInt32>(nteximages))
322 #ifdef OSG_DEBUG
323 FWARNING(("CubeTextureObjChunk::activate: Trying to bind image unit "
324 "%d, but Window %p only supports %lf!\n",
325 idx,
326 static_cast<void *>(win),
327 nteximages));
328 #endif
329 return;
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();
346 if(NULL != pColl)
348 pColl->getElem(TextureObjChunk::statNTextures)->inc(getGLId());
350 if(getImage() != NULL)
352 pColl->getElem(TextureObjChunk::statNTexBytes)->add(
353 getGLId(),
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,
367 StateChunk *old ,
368 UInt32 idx )
370 // does the window support cubemaps?
371 if(pEnv->getWindow()->hasExtOrVersion(_arbCubeTex, 0x0103, 0x0200) == false)
372 return;
374 // change from me to me?
375 // this assumes I haven't changed in the meantime.
376 // is that a valid assumption?
377 if(old == this)
378 return;
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);
387 activate(pEnv, idx);
388 return;
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)
404 nteximages = 1.0f;
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)
414 ntexcoords = 1.0f;
417 if(idx >= static_cast<UInt32>(nteximages))
419 #ifdef OSG_DEBUG
420 FWARNING(("TextureObjChunk::activate: Trying to bind image unit %d,"
421 " but Window %p only supports %lf!\n",
422 idx,
423 static_cast<void *>(win),
424 nteximages));
425 #endif
426 return;
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();
439 if(NULL != pColl)
441 pColl->getElem(TextureObjChunk::statNTextures)->inc(getGLId());
443 if(getImage() != NULL)
445 pColl->getElem(TextureObjChunk::statNTexBytes)->add(
446 getGLId(),
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)
463 return;
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)
475 nteximages = 1.0f;
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)
485 ntexcoords = 1.0f;
488 if(idx >= static_cast<UInt32>(nteximages))
490 #ifdef OSG_DEBUG
491 FWARNING(("CubeTextureObjChunk::deactivate: Trying to bind image unit"
492 " %d, but Window %p only supports %lf!\n",
493 idx,
494 static_cast<void *>(win),
495 nteximages));
496 #endif
497 return;
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))
513 return 0;
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);
526 if(!tother)
527 return false;
529 if(tother == this)
530 return true;
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);