fixed: auto_ptr -> unique_ptr
[opensg.git] / Source / System / NodeCores / Groups / DynamicStateGenerators / OSGCubeMapGenerator.cpp
blob200459d544ffe8e7d233bdc0ca586ab5cd88b409
1 /*---------------------------------------------------------------------------*\
2 * OpenSG *
3 * *
4 * *
5 * Copyright (C) 2000-2006 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 "OSGCubeMapGenerator.h"
49 #include "OSGCubeMapGeneratorStageData.h"
51 #include "OSGFrameBufferObject.h"
52 #include "OSGFrameBufferAttachment.h"
53 #include "OSGRenderBuffer.h"
54 #include "OSGTextureBuffer.h"
55 #include "OSGTextureEnvChunk.h"
56 #include "OSGTextureObjChunk.h"
57 #include "OSGTexGenChunk.h"
58 #include "OSGTextureTransformChunk.h"
59 #include "OSGRenderAction.h"
60 #include "OSGPerspectiveCamera.h"
62 OSG_BEGIN_NAMESPACE
64 // Documentation for this class is emitted in the
65 // OSGCubeMapGeneratorBase.cpp file.
66 // To modify it, please change the .fcd file (OSGCubeMapGenerator.fcd) and
67 // regenerate the base file.
69 /***************************************************************************\
70 * Class variables *
71 \***************************************************************************/
73 /***************************************************************************\
74 * Class methods *
75 \***************************************************************************/
77 void CubeMapGenerator::initMethod(InitPhase ePhase)
79 Inherited::initMethod(ePhase);
81 if(ePhase == TypeObject::SystemPost)
83 RenderAction::registerEnterDefault(
84 CubeMapGenerator::getClassType(),
85 reinterpret_cast<Action::Callback>(
86 &CubeMapGenerator::renderEnter));
88 RenderAction::registerLeaveDefault(
89 CubeMapGenerator::getClassType(),
90 reinterpret_cast<Action::Callback>(
91 &CubeMapGenerator::renderLeave));
96 /***************************************************************************\
97 * Instance methods *
98 \***************************************************************************/
100 /*-------------------------------------------------------------------------*\
101 - private -
102 \*-------------------------------------------------------------------------*/
104 /*----------------------- constructors & destructors ----------------------*/
106 CubeMapGenerator::CubeMapGenerator(void) :
107 Inherited()
111 CubeMapGenerator::CubeMapGenerator(const CubeMapGenerator &source) :
112 Inherited(source)
116 CubeMapGenerator::~CubeMapGenerator(void)
120 /*----------------------------- class specific ----------------------------*/
122 void CubeMapGenerator::changed(ConstFieldMaskArg whichField,
123 UInt32 origin,
124 BitVector details)
126 Inherited::changed(whichField, origin, details);
129 void CubeMapGenerator::dump( UInt32 ,
130 const BitVector ) const
132 SLOG << "Dump CubeMapGenerator NI" << std::endl;
135 Action::ResultE CubeMapGenerator::renderEnter(Action *action)
137 static Matrix transforms[] =
139 Matrix( 1, 0, 0, 0,
140 0, -1, 0, 0,
141 0, 0, -1, 0,
142 0, 0, 0, 1),
144 Matrix(-1, 0, 0, 0,
145 0, -1, 0, 0,
146 0, 0, 1, 0,
147 0, 0, 0, 1),
149 Matrix( 1, 0, 0, 0,
150 0, 0, -1, 0,
151 0, 1, 0, 0,
152 0, 0, 0, 1),
154 Matrix( 1, 0, 0, 0,
155 0, 0, 1, 0,
156 0, -1, 0, 0,
157 0, 0, 0, 1),
159 Matrix( 0, 0, -1, 0,
160 0, -1, 0, 0,
161 -1, 0, 0, 0,
162 0, 0, 0, 1),
164 Matrix( 0, 0, 1, 0,
165 0, -1, 0, 0,
166 1, 0, 0, 0,
167 0, 0, 0, 1)
170 RenderAction *a = dynamic_cast<RenderAction *>(action);
172 Action::ResultE returnValue = Action::Continue;
174 Background *pBack = a->getBackground();
176 Node *pActNode = a->getActNode();
178 CubeMapGeneratorStageData *pData =
179 a->getData<CubeMapGeneratorStageData *>(_iDataSlotId);
181 if(pData == NULL)
183 pData = this->initData(a);
186 TraversalValidator::ValidationStatus eStatus = this->validateOnEnter(a);
188 if(eStatus == TraversalValidator::Run)
190 this->beginPartitionGroup(a);
192 FrameBufferObject *pTarget = this->getRenderTarget();
194 if(pTarget == NULL)
196 pTarget = pData->getRenderTarget();
199 Pnt3f oOrigin;
201 if(this->getOriginMode() == CubeMapGenerator::UseStoredValue)
203 oOrigin = this->getOrigin();
205 else if(this->getOriginMode() == CubeMapGenerator::UseBeacon)
207 fprintf(stderr, "CubemapGen::UseBeacon NYI\n");
209 else if(this->getOriginMode() ==
210 CubeMapGenerator::UseCurrentVolumeCenter)
212 BoxVolume oWorldVol;
214 commitChanges();
216 pActNode->updateVolume();
218 pActNode->getWorldVolume(oWorldVol);
220 oWorldVol.getCenter(oOrigin);
222 else if(this->getOriginMode() ==
223 CubeMapGenerator::UseParentsVolumeCenter)
225 fprintf(stderr, "CubemapGen::UseParentsCenter NYI\n");
228 Camera *pCam = pData->getCamera();
230 pActNode->setTravMask(0);
232 for(UInt32 i = 0; i < 6; ++i)
234 this->pushPartition(a);
236 RenderPartition *pPart = a->getActivePartition();
238 pPart->setVolumeDrawing(false);
240 pPart->setRenderTarget(pTarget );
241 pPart->setWindow (a->getWindow());
243 pPart->calcViewportDimension(0,
247 this->getWidth (),
248 this->getHeight());
250 Matrix m, t;
252 // set the projection
253 pCam->getProjection (m,
254 pPart->getViewportWidth (),
255 pPart->getViewportHeight());
257 pCam->getProjectionTranslation(t,
258 pPart->getViewportWidth (),
259 pPart->getViewportHeight());
261 pPart->setupProjection(m, t);
263 m = transforms[i];
265 m[3][0] = oOrigin[0];
266 m[3][1] = oOrigin[1];
267 m[3][2] = oOrigin[2];
269 m.invert();
271 pPart->setupViewing(m);
273 pPart->setNear (pCam->getNear());
274 pPart->setFar (pCam->getFar ());
276 pPart->calcFrustum();
278 if(this->getBackground() == NULL)
280 pPart->setBackground(pBack);
282 else
284 pPart->setBackground(this->getBackground());
287 if(this->getRoot() != NULL)
289 this->recurse(a, this->getRoot());
291 else
293 this->recurse(a, a->getTraversalRoot());
296 pPart->setDrawBuffer(GL_COLOR_ATTACHMENT0_EXT + i);
298 #ifdef OSG_DEBUGX
299 std::string szMessage("CubeX\n");
300 pPart->setDebugString(szMessage );
301 #endif
303 this->popPartition(a);
306 pActNode->setTravMask(~0);
308 this->endPartitionGroup(a);
311 OSG_ASSERT(pActNode == a->getActNode());
313 returnValue = Inherited::renderEnter(action);
315 action->useNodeList(false);
317 return returnValue;
320 Action::ResultE CubeMapGenerator::renderLeave(Action *action)
322 Action::ResultE returnValue = Action::Continue;
324 returnValue = Inherited::renderLeave(action);
326 RenderAction *a = dynamic_cast<RenderAction *>(action);
328 this->validateOnLeave(a);
330 return returnValue;
334 CubeMapGeneratorStageDataTransitPtr CubeMapGenerator::setupStageData(
335 RenderAction *pAction)
337 CubeMapGeneratorStageDataTransitPtr returnValue =
338 CubeMapGeneratorStageData::createLocal();
340 if(returnValue == NULL)
341 return returnValue;
343 FrameBufferObjectUnrecPtr pCubeTarget = NULL;
344 RenderBufferUnrecPtr pDepthBuffer = NULL;
346 if(this->getRenderTarget() == NULL)
348 pCubeTarget = FrameBufferObject::createLocal();
349 pDepthBuffer = RenderBuffer ::createLocal();
351 pDepthBuffer->setInternalFormat (GL_DEPTH_COMPONENT24);
353 pCubeTarget ->setDepthAttachment(pDepthBuffer );
355 returnValue ->setRenderTarget (pCubeTarget );
357 else
359 pCubeTarget = this->getRenderTarget();
362 TextureObjChunkUnrecPtr pCubeTex = NULL;
364 if(0x0000 != (_sfSetupMode.getValue() & SetupTexture))
366 pCubeTex = TextureObjChunk::createLocal();
368 ImageUnrecPtr pImg = Image::createLocal();
370 pImg->set(Image::OSG_RGB_PF,
371 getWidth (),
372 getHeight(),
376 0.0,
378 Image::OSG_UINT8_IMAGEDATA,
379 false,
382 pCubeTex ->setImage (pImg );
383 pCubeTex ->setMinFilter (GL_LINEAR );
384 pCubeTex ->setMagFilter (GL_LINEAR );
385 pCubeTex ->setWrapS (GL_CLAMP_TO_EDGE );
386 pCubeTex ->setWrapT (GL_CLAMP_TO_EDGE );
387 pCubeTex ->setWrapR (GL_CLAMP_TO_EDGE );
389 GLenum eTexTarget = this->getTextureFormat();
391 if(eTexTarget == GL_NONE)
393 eTexTarget = pAction->getActivePartition()->
394 getDrawEnv().getTargetBufferFormat();
397 pCubeTex->setInternalFormat(eTexTarget);
399 else
401 pCubeTex = _sfTexture.getValue();
404 TextureEnvChunkUnrecPtr pCubeTexEnv = NULL;
406 if(0x0000 != (_sfSetupMode.getValue() & SetupTexEnv))
408 pCubeTexEnv = TextureEnvChunk::createLocal();
410 pCubeTexEnv->setEnvMode (GL_REPLACE );
413 TexGenChunkUnrecPtr pCubeTexGen = NULL;
414 TextureTransformChunkUnrecPtr pCubeTexTrans = NULL;
416 if(0x0000 != (_sfSetupMode.getValue() & SetupTexGen))
418 pCubeTexGen = TexGenChunk::createLocal();
420 pCubeTexGen->setGenFuncS(GL_REFLECTION_MAP);
421 pCubeTexGen->setGenFuncT(GL_REFLECTION_MAP);
422 pCubeTexGen->setGenFuncR(GL_REFLECTION_MAP);
424 pCubeTexTrans = TextureTransformChunk::createLocal();
426 pCubeTexTrans->setUseCameraBeacon(true);
430 static GLenum targets[6] =
432 GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB,
433 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB,
434 GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,
435 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB,
436 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,
437 GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
440 for(UInt32 i = 0; i < 6; ++i)
442 TextureBufferUnrecPtr pCubeTexBuffer = TextureBuffer::createLocal();
444 pCubeTexBuffer->setTexture (pCubeTex );
445 pCubeTexBuffer->setTexTarget(targets[i]);
447 pCubeTarget->setColorAttachment(pCubeTexBuffer, i);
450 pCubeTarget->setSize(getWidth (),
451 getHeight());
453 if(0x0000 != (_sfSetupMode.getValue() & OverrideTex))
455 returnValue->addChunk(pCubeTex,
456 getTexUnit());
459 if(0x0000 != (_sfSetupMode.getValue() & SetupTexEnv))
461 returnValue->addChunk(pCubeTexEnv,
462 getTexUnit());
465 if(0x0000 != (_sfSetupMode.getValue() & SetupTexGen))
467 returnValue->addChunk(pCubeTexGen,
468 getTexUnit());
469 returnValue->addChunk(pCubeTexTrans,
470 getTexUnit());
472 returnValue->setTexTransform(pCubeTexTrans);
475 if(this->getCamera() == NULL)
477 PerspectiveCameraUnrecPtr pCam = PerspectiveCamera::createLocal();
479 pCam->setNear(pAction->getCamera()->getNear());
480 pCam->setFar (pAction->getCamera()->getFar ());
482 pCam->setFov (osgDegree2Rad(90.f));
484 returnValue->setCamera(pCam);
487 return returnValue;
490 CubeMapGeneratorStageData *CubeMapGenerator::initData(RenderAction *pAction)
492 CubeMapGeneratorStageDataUnrecPtr pData =
493 pAction->getData<CubeMapGeneratorStageData *>(_iDataSlotId);
495 if(pData == NULL)
497 pData = setupStageData(pAction);
499 this->setData(pData, _iDataSlotId, pAction);
502 return pData;
506 OSG_END_NAMESPACE