fixed: gcc8 compile issues
[opensg.git] / Source / Contrib / ComplexSceneManager / OSGCSMViewport.cpp
bloba2a9bda24de16938bb26978b3d4b9c3fcd1e73ea
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"
57 #include "OSGNode.h"
58 #include "OSGBackground.h"
59 #include "OSGRenderOptions.h"
61 OSG_BEGIN_NAMESPACE
63 // Documentation for this class is emitted in the
64 // OSGCSMViewportBase.cpp file.
65 // To modify it, please change the .fcd file (OSGCSMViewport.fcd) and
66 // regenerate the base file.
68 /***************************************************************************\
69 * Class variables *
70 \***************************************************************************/
72 /***************************************************************************\
73 * Class methods *
74 \***************************************************************************/
76 void CSMViewport::initMethod(InitPhase ePhase)
78 Inherited::initMethod(ePhase);
80 if(ePhase == TypeObject::SystemPost)
86 /***************************************************************************\
87 * Instance methods *
88 \***************************************************************************/
90 /*-------------------------------------------------------------------------*\
91 - private -
92 \*-------------------------------------------------------------------------*/
94 /*----------------------- constructors & destructors ----------------------*/
96 CSMViewport::CSMViewport(void) :
97 Inherited (),
98 _vViewports()
102 CSMViewport::CSMViewport(const CSMViewport &source) :
103 Inherited (source ),
104 _vViewports(source._vViewports)
108 CSMViewport::~CSMViewport(void)
112 void CSMViewport::resolveLinks(void)
114 Inherited::resolveLinks();
116 ViewportStoreIt vIt = _vViewports.begin();
117 ViewportStoreConstIt vEnd = _vViewports.end ();
119 for(; vIt != vEnd; ++vIt)
121 (*vIt) = NULL;
124 _vViewports.clear();
127 /*----------------------------- class specific ----------------------------*/
129 void CSMViewport::changed(ConstFieldMaskArg whichField,
130 UInt32 origin,
131 BitVector details)
133 Inherited::changed(whichField, origin, details);
135 #if 0
136 if(0x0000 != (whichField & MTouchDataVCFieldMask))
138 fprintf(stderr, "vp touch %p : \n", this);
139 _sfMTouchDataVC.getValue().dump();
141 #endif
144 void CSMViewport::dump( UInt32 ,
145 const BitVector ) const
147 SLOG << "Dump CSMViewport NI" << std::endl;
150 bool CSMViewport::init(CSMWindow *pCSMWin)
152 bool returnValue = true;
154 bool bQuadBuff = _sfStereoMode.getValue() == "quadBuffer";
155 bool bSplitH = _sfStereoMode.getValue() == "splitHorizontal";
156 bool bSplitV = _sfStereoMode.getValue() == "splitVertical";
157 bool bSLeft = _sfStereoMode.getValue() == "stereoLeft";
158 bool bSRight = _sfStereoMode.getValue() == "stereoRight";
159 bool bAmberBlue = _sfStereoMode.getValue() == "amberBlue";
161 bool bStereo = (bQuadBuff ||
162 bSplitH ||
163 bSplitV ||
164 bAmberBlue ||
165 bSLeft ||
166 bSRight );
168 bool bFallback = false;
170 if(bStereo == true && _sfPassive.getValue() == true)
172 FWARNING(("CSMViewport: passive set with stereo config, ignoring\n"));
175 if(bQuadBuff == true)
177 StereoBufferViewportUnrecPtr pPortLeft =
178 StereoBufferViewport::create();
180 StereoBufferViewportUnrecPtr pPortRight =
181 StereoBufferViewport::create();
183 // Left
184 pPortLeft->setLeft (_sfLeftBottom.getValue()[0]);
185 pPortLeft->setBottom(_sfLeftBottom.getValue()[1]);
187 pPortLeft->setRight (_sfRightTop .getValue()[0]);
188 pPortLeft->setTop (_sfRightTop .getValue()[1]);
190 pPortLeft->setLeftBuffer (true );
191 pPortLeft->setRightBuffer(false);
193 pPortRight->setLeft (_sfLeftBottom.getValue()[0]);
194 pPortRight->setBottom(_sfLeftBottom.getValue()[1]);
196 pPortRight->setRight (_sfRightTop .getValue()[0]);
197 pPortRight->setTop (_sfRightTop .getValue()[1]);
199 pPortRight->setLeftBuffer (false);
200 pPortRight->setRightBuffer(true );
202 _vViewports.push_back(pPortLeft );
203 _vViewports.push_back(pPortRight);
205 else if(bAmberBlue == true)
207 ColorBufferViewportUnrecPtr pPortLeft =
208 ColorBufferViewport::create();
210 ColorBufferViewportUnrecPtr pPortRight =
211 ColorBufferViewport::create();
213 // Left
214 pPortLeft->setLeft (_sfLeftBottom.getValue()[0]);
215 pPortLeft->setBottom(_sfLeftBottom.getValue()[1]);
217 pPortLeft->setRight (_sfRightTop .getValue()[0]);
218 pPortLeft->setTop (_sfRightTop .getValue()[1]);
220 pPortLeft->setRed (GL_TRUE );
221 pPortLeft->setGreen(GL_TRUE );
222 pPortLeft->setBlue (GL_FALSE);
223 pPortLeft->setAlpha(GL_TRUE );
225 pPortRight->setLeft (_sfLeftBottom.getValue()[0]);
226 pPortRight->setBottom(_sfLeftBottom.getValue()[1]);
228 pPortRight->setRight (_sfRightTop .getValue()[0]);
229 pPortRight->setTop (_sfRightTop .getValue()[1]);
231 pPortRight->setRed (GL_FALSE);
232 pPortRight->setGreen(GL_FALSE);
233 pPortRight->setBlue (GL_TRUE );
234 pPortRight->setAlpha(GL_FALSE);
236 _vViewports.push_back(pPortLeft );
237 _vViewports.push_back(pPortRight);
239 else if(bSplitH == true)
241 ViewportUnrecPtr pPortLeft = Viewport::create();
242 ViewportUnrecPtr pPortRight = Viewport::create();
244 // Left
245 Real32 rCenter = _sfLeftBottom.getValue()[0] +
246 (_sfRightTop .getValue()[0] - _sfLeftBottom.getValue()[0]) * 0.5;
248 pPortLeft->setLeft (_sfLeftBottom.getValue()[0]);
249 pPortLeft->setBottom(_sfLeftBottom.getValue()[1]);
251 pPortLeft->setRight ( rCenter );
252 pPortLeft->setTop (_sfRightTop .getValue()[1]);
254 // Right
255 pPortRight->setLeft ( rCenter );
256 pPortRight->setBottom(_sfLeftBottom.getValue()[1]);
258 pPortRight->setRight (_sfRightTop .getValue()[0]);
259 pPortRight->setTop (_sfRightTop .getValue()[1]);
261 _vViewports.push_back(pPortLeft );
262 _vViewports.push_back(pPortRight);
264 else if(bSplitV == true)
266 ViewportUnrecPtr pPortLeft = Viewport::create();
267 ViewportUnrecPtr pPortRight = Viewport::create();
269 // Left
270 Real32 rCenter = _sfLeftBottom.getValue()[1] +
271 (_sfRightTop .getValue()[1] - _sfLeftBottom.getValue()[1]) * 0.5;
273 pPortLeft->setLeft (_sfLeftBottom.getValue()[0]);
274 pPortLeft->setBottom(_sfLeftBottom.getValue()[1]);
276 pPortLeft->setRight (_sfRightTop .getValue()[0]);
277 pPortLeft->setTop ( rCenter );
279 // Right
280 pPortRight->setLeft (_sfLeftBottom.getValue()[0]);
281 pPortRight->setBottom( rCenter );
283 pPortRight->setRight (_sfRightTop .getValue()[0]);
284 pPortRight->setTop (_sfRightTop .getValue()[1]);
286 _vViewports.push_back(pPortLeft );
287 _vViewports.push_back(pPortRight);
289 else
291 if(_sfPassive.getValue() == true)
293 ViewportUnrecPtr pPort = PassiveViewport::create();
295 _vViewports.push_back(pPort);
297 else
299 ViewportUnrecPtr pPort = Viewport::create();
301 // Left
302 pPort->setLeft (_sfLeftBottom.getValue()[0]);
303 pPort->setBottom(_sfLeftBottom.getValue()[1]);
305 pPort->setRight (_sfRightTop .getValue()[0]);
306 pPort->setTop (_sfRightTop .getValue()[1]);
308 _vViewports.push_back(pPort );
312 CSMPerspectiveCamera *pCSMCam =
313 dynamic_cast<CSMPerspectiveCamera *>(_sfCamera.getValue());
315 ProjectionCameraDecorator *pProjCam =
316 dynamic_cast<ProjectionCameraDecorator *>(_sfCamera.getValue());
318 if(pCSMCam == NULL)
320 if(pProjCam != NULL)
322 pCSMCam = dynamic_cast<CSMPerspectiveCamera *>(
323 pProjCam->getDecoratee());
327 if(pCSMCam == NULL)
328 bFallback = true;
330 if((bQuadBuff || bSplitH || bSplitV || bAmberBlue) && (bFallback == false))
332 // Left
333 StereoCameraDecoratorUnrecPtr pDecoLeft(NULL);
335 if(pProjCam != NULL)
337 if(pCSMCam->getHeadBeacon() != NULL)
339 pProjCam->setUser(pCSMCam->getHeadBeacon());
341 else
343 pProjCam->setUser(pCSMCam->getBeacon());
346 pDecoLeft = pProjCam;
348 else
350 ShearedStereoCameraDecoratorUnrecPtr pSheared =
351 ShearedStereoCameraDecorator::create();
354 pSheared->setZeroParallaxDistance(pCSMCam->getZeroParallax ());
355 pSheared->setDecoratee (pCSMCam );
357 pDecoLeft = pSheared;
359 addConnection(pCSMCam, "zeroParallax",
360 pDecoLeft, "zeroParallaxDistance");
363 pDecoLeft->setEyeSeparation (pCSMCam->getEyeSeparation());
364 pDecoLeft->setLeftEye (true );
366 _vViewports[0]->setCamera(pDecoLeft);
368 // Right
369 StereoCameraDecoratorUnrecPtr pDecoRight(NULL);
371 if(pProjCam != NULL)
373 ProjectionCameraDecoratorUnrecPtr pProject =
374 ProjectionCameraDecorator::create();
376 pProject->setUser (pProjCam->getUser());
377 pProject->setDecoratee(pCSMCam );
379 *(pProject->editMFSurface()) = *(pProjCam->getMFSurface());
381 pDecoRight = pProject;
383 else
385 ShearedStereoCameraDecoratorUnrecPtr pSheared =
386 ShearedStereoCameraDecorator::create();
388 pSheared->setZeroParallaxDistance(pCSMCam->getZeroParallax ());
389 pSheared->setDecoratee (pCSMCam );
391 pDecoRight = pSheared;
393 addConnection(pCSMCam, "zeroParallax",
394 pDecoRight, "zeroParallaxDistance");
397 pDecoRight->setEyeSeparation (pCSMCam->getEyeSeparation());
398 pDecoRight->setLeftEye (false );
400 _vViewports[1]->setCamera(pDecoRight);
402 addConnection(pCSMCam, "eyeSeparation",
403 pDecoRight, "eyeSeparation");
406 addConnection(pCSMCam, "eyeSeparation",
407 pDecoLeft, "eyeSeparation");
409 else if((bSLeft || bSRight) && (bFallback == false))
411 StereoCameraDecoratorUnrecPtr pDeco(NULL);
413 if(pProjCam != NULL)
415 if(pCSMCam->getHeadBeacon() != NULL)
417 pProjCam->setUser(pCSMCam->getHeadBeacon());
419 else
421 pProjCam->setUser(pCSMCam->getBeacon());
424 pDeco = pProjCam;
426 else
428 ShearedStereoCameraDecoratorUnrecPtr pSheared =
429 ShearedStereoCameraDecorator::create();
431 pSheared->setZeroParallaxDistance(pCSMCam->getZeroParallax ());
432 pSheared->setDecoratee (pCSMCam );
434 pDeco = pSheared;
436 addConnection(pCSMCam, "zeroParallax",
437 pDeco, "zeroParallaxDistance");
440 pDeco->setEyeSeparation (pCSMCam->getEyeSeparation());
441 pDeco->setLeftEye (bSLeft );
443 _vViewports[0]->setCamera(pDeco);
446 addConnection(pCSMCam, "eyeSeparation",
447 pDeco, "eyeSeparation");
449 else
451 _vViewports[0]->setCamera(_sfCamera.getValue());
453 if(bFallback == false)
455 if(pProjCam != NULL)
457 if(pCSMCam->getHeadBeacon() != NULL)
459 pProjCam->setUser(pCSMCam->getHeadBeacon());
461 else
463 pProjCam->setUser(pCSMCam->getBeacon());
469 ViewportStoreConstIt vIt = _vViewports.begin();
470 ViewportStoreConstIt vEnd = _vViewports.end ();
472 for(; vIt != vEnd; ++vIt)
474 (*vIt)->setRoot (_sfRoot .getValue());
475 (*vIt)->setBackground (_sfBackground .getValue());
476 (*vIt)->setRenderOptions(_sfRenderOptions.getValue());
477 (*vIt)->setTravMask (_sfTravMask .getValue());
479 MFUnrecForegroundPtr::const_iterator fIt = getMFForegrounds()->begin();
480 MFUnrecForegroundPtr::const_iterator fEnd = getMFForegrounds()->end ();
482 while(fIt != fEnd)
484 CSMStatisticsForeground *pCSMStatFG =
485 dynamic_cast<CSMStatisticsForeground *>(*fIt);
487 if(pCSMStatFG != NULL)
489 pCSMStatFG->init(pCSMWin);
491 StatisticsForeground *pOSGStatFG =
492 pCSMStatFG->getOSGForeground();
494 if(pOSGStatFG != NULL)
496 pCSMWin->_pStatFG = pOSGStatFG;
498 (*vIt)->addForeground(pOSGStatFG);
501 else
503 (*vIt)->addForeground(*fIt);
506 ++fIt;
510 return returnValue;
513 CSMViewport::ViewportStoreConstIt CSMViewport::beginViewports(void) const
515 return _vViewports.begin();
518 CSMViewport::ViewportStoreConstIt CSMViewport::endViewports(void) const
520 return _vViewports.end();
523 Viewport *CSMViewport::getViewport(UInt32 uiIndex) const
525 if(uiIndex < _vViewports.size())
526 return _vViewports[uiIndex];
528 return NULL;
531 bool CSMViewport::needsStereoVisual(void)
533 return _sfStereoMode.getValue() == "quadBuffer";
536 bool CSMViewport::pointInside(Real32 x, Real32 y) const
538 if(_vViewports.size() == 0)
539 return false;
541 Viewport *pPort = _vViewports[0];
543 if(pPort == NULL)
544 return false;
546 if(x >= pPort->calcPixelLeft () &&
547 x <= pPort->calcPixelRight () &&
548 y >= pPort->calcPixelBottom() &&
549 y <= pPort->calcPixelTop () )
551 return true;
554 return false;
557 Vec2f CSMViewport::translateWindowViewportAbs(Real32 rX,
558 Real32 rY) const
560 Viewport *pViewport = this->getViewport(0);
562 if(pViewport != NULL)
564 return Vec2f(rX - pViewport->calcPixelLeft (),
565 rY - pViewport->calcPixelBottom());
567 else
569 return Vec2f(rX, rY);
574 OSG_END_NAMESPACE