added: travMask to csm viewport
[opensg.git] / Source / Contrib / ComplexSceneManager / OSGCSMViewport.cpp
blob9bd2c1b33f2bcaf750fe5a3a563eb3dc109368e8
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 "OSGCSMViewport.h"
49 #include "OSGStereoBufferViewport.h"
50 #include "OSGShearedStereoCameraDecorator.h"
51 #include "OSGProjectionCameraDecorator.h"
52 #include "OSGCSMPerspectiveCamera.h"
53 #include "OSGColorBufferViewport.h"
54 #include "OSGPassiveViewport.h"
55 #include "OSGCSMStatisticsForeground.h"
56 #include "OSGCSMWindow.h"
58 OSG_BEGIN_NAMESPACE
60 // Documentation for this class is emitted in the
61 // OSGCSMViewportBase.cpp file.
62 // To modify it, please change the .fcd file (OSGCSMViewport.fcd) and
63 // regenerate the base file.
65 /***************************************************************************\
66 * Class variables *
67 \***************************************************************************/
69 /***************************************************************************\
70 * Class methods *
71 \***************************************************************************/
73 void CSMViewport::initMethod(InitPhase ePhase)
75 Inherited::initMethod(ePhase);
77 if(ePhase == TypeObject::SystemPost)
83 /***************************************************************************\
84 * Instance methods *
85 \***************************************************************************/
87 /*-------------------------------------------------------------------------*\
88 - private -
89 \*-------------------------------------------------------------------------*/
91 /*----------------------- constructors & destructors ----------------------*/
93 CSMViewport::CSMViewport(void) :
94 Inherited()
98 CSMViewport::CSMViewport(const CSMViewport &source) :
99 Inherited(source)
103 CSMViewport::~CSMViewport(void)
107 void CSMViewport::resolveLinks(void)
109 Inherited::resolveLinks();
111 ViewportStoreIt vIt = _vViewports.begin();
112 ViewportStoreConstIt vEnd = _vViewports.end ();
114 for(; vIt != vEnd; ++vIt)
116 (*vIt) = NULL;
119 _vViewports.clear();
122 /*----------------------------- class specific ----------------------------*/
124 void CSMViewport::changed(ConstFieldMaskArg whichField,
125 UInt32 origin,
126 BitVector details)
128 Inherited::changed(whichField, origin, details);
131 void CSMViewport::dump( UInt32 ,
132 const BitVector ) const
134 SLOG << "Dump CSMViewport NI" << std::endl;
137 bool CSMViewport::init(CSMWindow *pCSMWin)
139 bool returnValue = true;
141 bool bQuadBuff = _sfStereoMode.getValue() == "quadBuffer";
142 bool bSplitH = _sfStereoMode.getValue() == "splitHorizontal";
143 bool bSplitV = _sfStereoMode.getValue() == "splitVertical";
144 bool bSLeft = _sfStereoMode.getValue() == "stereoLeft";
145 bool bSRight = _sfStereoMode.getValue() == "stereoRight";
146 bool bAmberBlue = _sfStereoMode.getValue() == "amberBlue";
148 bool bStereo = (bQuadBuff ||
149 bSplitH ||
150 bSplitV ||
151 bAmberBlue ||
152 bSLeft ||
153 bSRight );
155 bool bFallback = false;
157 if(bStereo == true && _sfPassive.getValue() == true)
159 FWARNING(("CSMViewport: passive set with stereo config, ignoring\n"));
162 if(bQuadBuff == true)
164 StereoBufferViewportUnrecPtr pPortLeft =
165 StereoBufferViewport::create();
167 StereoBufferViewportUnrecPtr pPortRight =
168 StereoBufferViewport::create();
170 // Left
171 pPortLeft->setLeft (_sfLeftBottom.getValue()[0]);
172 pPortLeft->setBottom(_sfLeftBottom.getValue()[1]);
174 pPortLeft->setRight (_sfRightTop .getValue()[0]);
175 pPortLeft->setTop (_sfRightTop .getValue()[1]);
177 pPortLeft->setLeftBuffer (true );
178 pPortLeft->setRightBuffer(false);
180 pPortRight->setLeft (_sfLeftBottom.getValue()[0]);
181 pPortRight->setBottom(_sfLeftBottom.getValue()[1]);
183 pPortRight->setRight (_sfRightTop .getValue()[0]);
184 pPortRight->setTop (_sfRightTop .getValue()[1]);
186 pPortRight->setLeftBuffer (false);
187 pPortRight->setRightBuffer(true );
189 _vViewports.push_back(pPortLeft );
190 _vViewports.push_back(pPortRight);
192 else if(bAmberBlue == true)
194 ColorBufferViewportUnrecPtr pPortLeft =
195 ColorBufferViewport::create();
197 ColorBufferViewportUnrecPtr pPortRight =
198 ColorBufferViewport::create();
200 // Left
201 pPortLeft->setLeft (_sfLeftBottom.getValue()[0]);
202 pPortLeft->setBottom(_sfLeftBottom.getValue()[1]);
204 pPortLeft->setRight (_sfRightTop .getValue()[0]);
205 pPortLeft->setTop (_sfRightTop .getValue()[1]);
207 pPortLeft->setRed (GL_TRUE );
208 pPortLeft->setGreen(GL_TRUE );
209 pPortLeft->setBlue (GL_FALSE);
210 pPortLeft->setAlpha(GL_TRUE );
212 pPortRight->setLeft (_sfLeftBottom.getValue()[0]);
213 pPortRight->setBottom(_sfLeftBottom.getValue()[1]);
215 pPortRight->setRight (_sfRightTop .getValue()[0]);
216 pPortRight->setTop (_sfRightTop .getValue()[1]);
218 pPortRight->setRed (GL_FALSE);
219 pPortRight->setGreen(GL_FALSE);
220 pPortRight->setBlue (GL_TRUE );
221 pPortRight->setAlpha(GL_FALSE);
223 _vViewports.push_back(pPortLeft );
224 _vViewports.push_back(pPortRight);
226 else if(bSplitH == true)
228 ViewportUnrecPtr pPortLeft = Viewport::create();
229 ViewportUnrecPtr pPortRight = Viewport::create();
231 // Left
232 Real32 rCenter = _sfLeftBottom.getValue()[0] +
233 (_sfRightTop .getValue()[0] - _sfLeftBottom.getValue()[0]) * 0.5;
235 pPortLeft->setLeft (_sfLeftBottom.getValue()[0]);
236 pPortLeft->setBottom(_sfLeftBottom.getValue()[1]);
238 pPortLeft->setRight ( rCenter );
239 pPortLeft->setTop (_sfRightTop .getValue()[1]);
241 // Right
242 pPortRight->setLeft ( rCenter );
243 pPortRight->setBottom(_sfLeftBottom.getValue()[1]);
245 pPortRight->setRight (_sfRightTop .getValue()[0]);
246 pPortRight->setTop (_sfRightTop .getValue()[1]);
248 _vViewports.push_back(pPortLeft );
249 _vViewports.push_back(pPortRight);
251 else if(bSplitV == true)
253 ViewportUnrecPtr pPortLeft = Viewport::create();
254 ViewportUnrecPtr pPortRight = Viewport::create();
256 // Left
257 Real32 rCenter = _sfLeftBottom.getValue()[1] +
258 (_sfRightTop .getValue()[1] - _sfLeftBottom.getValue()[1]) * 0.5;
260 pPortLeft->setLeft (_sfLeftBottom.getValue()[0]);
261 pPortLeft->setBottom(_sfLeftBottom.getValue()[1]);
263 pPortLeft->setRight (_sfRightTop .getValue()[0]);
264 pPortLeft->setTop ( rCenter );
266 // Right
267 pPortRight->setLeft (_sfLeftBottom.getValue()[0]);
268 pPortRight->setBottom( rCenter );
270 pPortRight->setRight (_sfRightTop .getValue()[0]);
271 pPortRight->setTop (_sfRightTop .getValue()[1]);
273 _vViewports.push_back(pPortLeft );
274 _vViewports.push_back(pPortRight);
276 else
278 if(_sfPassive.getValue() == true)
280 ViewportUnrecPtr pPort = PassiveViewport::create();
282 _vViewports.push_back(pPort);
284 else
286 ViewportUnrecPtr pPort = Viewport::create();
288 // Left
289 pPort->setLeft (_sfLeftBottom.getValue()[0]);
290 pPort->setBottom(_sfLeftBottom.getValue()[1]);
292 pPort->setRight (_sfRightTop .getValue()[0]);
293 pPort->setTop (_sfRightTop .getValue()[1]);
295 _vViewports.push_back(pPort );
299 CSMPerspectiveCamera *pCSMCam =
300 dynamic_cast<CSMPerspectiveCamera *>(_sfCamera.getValue());
302 ProjectionCameraDecorator *pProjCam =
303 dynamic_cast<ProjectionCameraDecorator *>(_sfCamera.getValue());
305 if(pCSMCam == NULL)
307 if(pProjCam != NULL)
309 pCSMCam = dynamic_cast<CSMPerspectiveCamera *>(
310 pProjCam->getDecoratee());
314 if(pCSMCam == NULL)
315 bFallback = true;
317 if((bQuadBuff || bSplitH || bSplitV || bAmberBlue) && (bFallback == false))
319 // Left
320 StereoCameraDecoratorUnrecPtr pDecoLeft(NULL);
322 if(pProjCam != NULL)
324 if(pCSMCam->getHeadBeacon() != NULL)
326 pProjCam->setUser(pCSMCam->getHeadBeacon());
328 else
330 pProjCam->setUser(pCSMCam->getBeacon());
333 pDecoLeft = pProjCam;
335 else
337 ShearedStereoCameraDecoratorUnrecPtr pSheared =
338 ShearedStereoCameraDecorator::create();
341 pSheared->setZeroParallaxDistance(pCSMCam->getZeroParallax ());
342 pSheared->setDecoratee (pCSMCam );
344 pDecoLeft = pSheared;
346 addConnection(pCSMCam, "zeroParallax",
347 pDecoLeft, "zeroParallaxDistance");
350 pDecoLeft->setEyeSeparation (pCSMCam->getEyeSeparation());
351 pDecoLeft->setLeftEye (true );
353 _vViewports[0]->setCamera(pDecoLeft);
355 // Right
356 StereoCameraDecoratorUnrecPtr pDecoRight(NULL);
358 if(pProjCam != NULL)
360 ProjectionCameraDecoratorUnrecPtr pProject =
361 ProjectionCameraDecorator::create();
363 pProject->setUser (pProjCam->getUser());
364 pProject->setDecoratee(pCSMCam );
366 *(pProject->editMFSurface()) = *(pProjCam->getMFSurface());
368 pDecoRight = pProject;
370 else
372 ShearedStereoCameraDecoratorUnrecPtr pSheared =
373 ShearedStereoCameraDecorator::create();
375 pSheared->setZeroParallaxDistance(pCSMCam->getZeroParallax ());
376 pSheared->setDecoratee (pCSMCam );
378 pDecoRight = pSheared;
380 addConnection(pCSMCam, "zeroParallax",
381 pDecoRight, "zeroParallaxDistance");
384 pDecoRight->setEyeSeparation (pCSMCam->getEyeSeparation());
385 pDecoRight->setLeftEye (false );
387 _vViewports[1]->setCamera(pDecoRight);
389 addConnection(pCSMCam, "eyeSeparation",
390 pDecoRight, "eyeSeparation");
393 addConnection(pCSMCam, "eyeSeparation",
394 pDecoLeft, "eyeSeparation");
396 else if((bSLeft || bSRight) && (bFallback == false))
398 StereoCameraDecoratorUnrecPtr pDeco(NULL);
400 if(pProjCam != NULL)
402 if(pCSMCam->getHeadBeacon() != NULL)
404 pProjCam->setUser(pCSMCam->getHeadBeacon());
406 else
408 pProjCam->setUser(pCSMCam->getBeacon());
411 pDeco = pProjCam;
413 else
415 ShearedStereoCameraDecoratorUnrecPtr pSheared =
416 ShearedStereoCameraDecorator::create();
418 pSheared->setZeroParallaxDistance(pCSMCam->getZeroParallax ());
419 pSheared->setDecoratee (pCSMCam );
421 pDeco = pSheared;
423 addConnection(pCSMCam, "zeroParallax",
424 pDeco, "zeroParallaxDistance");
427 pDeco->setEyeSeparation (pCSMCam->getEyeSeparation());
428 pDeco->setLeftEye (bSLeft );
430 _vViewports[0]->setCamera(pDeco);
433 addConnection(pCSMCam, "eyeSeparation",
434 pDeco, "eyeSeparation");
436 else
438 _vViewports[0]->setCamera(_sfCamera.getValue());
440 if(bFallback == false)
442 if(pProjCam != NULL)
444 if(pCSMCam->getHeadBeacon() != NULL)
446 pProjCam->setUser(pCSMCam->getHeadBeacon());
448 else
450 pProjCam->setUser(pCSMCam->getBeacon());
456 ViewportStoreConstIt vIt = _vViewports.begin();
457 ViewportStoreConstIt vEnd = _vViewports.end ();
459 for(; vIt != vEnd; ++vIt)
461 (*vIt)->setRoot (_sfRoot .getValue());
462 (*vIt)->setBackground (_sfBackground .getValue());
463 (*vIt)->setRenderOptions(_sfRenderOptions.getValue());
464 (*vIt)->setTravMask (_sfTravMask .getValue());
466 MFUnrecForegroundPtr::const_iterator fIt = getMFForegrounds()->begin();
467 MFUnrecForegroundPtr::const_iterator fEnd = getMFForegrounds()->end ();
469 while(fIt != fEnd)
471 CSMStatisticsForeground *pCSMStatFG =
472 dynamic_cast<CSMStatisticsForeground *>(*fIt);
474 if(pCSMStatFG != NULL)
476 pCSMStatFG->init(pCSMWin);
478 StatisticsForeground *pOSGStatFG =
479 pCSMStatFG->getOSGForeground();
481 if(pOSGStatFG != NULL)
483 pCSMWin->_pStatFG = pOSGStatFG;
485 (*vIt)->addForeground(pOSGStatFG);
488 else
490 (*vIt)->addForeground(*fIt);
493 ++fIt;
497 return returnValue;
500 CSMViewport::ViewportStoreConstIt CSMViewport::beginViewports(void) const
502 return _vViewports.begin();
505 CSMViewport::ViewportStoreConstIt CSMViewport::endViewports(void) const
507 return _vViewports.end();
510 bool CSMViewport::needsStereoVisual(void)
512 return _sfStereoMode.getValue() == "quadBuffer";
515 OSG_END_NAMESPACE