1 /*---------------------------------------------------------------------------*\
5 * Copyright (C) 2000-2006 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"
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"
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 /***************************************************************************\
71 \***************************************************************************/
73 /***************************************************************************\
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 /***************************************************************************\
98 \***************************************************************************/
100 /*-------------------------------------------------------------------------*\
102 \*-------------------------------------------------------------------------*/
104 /*----------------------- constructors & destructors ----------------------*/
106 CubeMapGenerator::CubeMapGenerator(void) :
111 CubeMapGenerator::CubeMapGenerator(const CubeMapGenerator
&source
) :
116 CubeMapGenerator::~CubeMapGenerator(void)
120 /*----------------------------- class specific ----------------------------*/
122 void CubeMapGenerator::changed(ConstFieldMaskArg whichField
,
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
[] =
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
);
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();
196 pTarget
= pData
->getRenderTarget();
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
)
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,
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
);
265 m
[3][0] = oOrigin
[0];
266 m
[3][1] = oOrigin
[1];
267 m
[3][2] = oOrigin
[2];
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
);
284 pPart
->setBackground(this->getBackground());
287 if(this->getRoot() != NULL
)
289 this->recurse(a
, this->getRoot());
293 this->recurse(a
, a
->getTraversalRoot());
296 pPart
->setDrawBuffer(GL_COLOR_ATTACHMENT0_EXT
+ i
);
299 std::string
szMessage("CubeX\n");
300 pPart
->setDebugString(szMessage
);
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);
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
);
334 CubeMapGeneratorStageDataTransitPtr
CubeMapGenerator::setupStageData(
335 RenderAction
*pAction
)
337 CubeMapGeneratorStageDataTransitPtr returnValue
=
338 CubeMapGeneratorStageData::createLocal();
340 if(returnValue
== NULL
)
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
);
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
,
378 Image::OSG_UINT8_IMAGEDATA
,
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
);
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 (),
453 if(0x0000 != (_sfSetupMode
.getValue() & OverrideTex
))
455 returnValue
->addChunk(pCubeTex
,
459 if(0x0000 != (_sfSetupMode
.getValue() & SetupTexEnv
))
461 returnValue
->addChunk(pCubeTexEnv
,
465 if(0x0000 != (_sfSetupMode
.getValue() & SetupTexGen
))
467 returnValue
->addChunk(pCubeTexGen
,
469 returnValue
->addChunk(pCubeTexTrans
,
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
);
490 CubeMapGeneratorStageData
*CubeMapGenerator::initData(RenderAction
*pAction
)
492 CubeMapGeneratorStageDataUnrecPtr pData
=
493 pAction
->getData
<CubeMapGeneratorStageData
*>(_iDataSlotId
);
497 pData
= setupStageData(pAction
);
499 this->setData(pData
, _iDataSlotId
, pAction
);