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 "OSGCSMDrawer.h"
49 #include "OSGCSMWindow.h"
50 #include "OSGRenderAction.h"
51 #include "OSGCSMViewport.h"
52 #include "OSGCSMDrawManager.h"
54 #include "OSGOSGSceneFileType.h"
55 #include "OSGNameAttachment.h"
56 #include "OSGComplexSceneManager.h"
57 #include "OSGStatisticsForeground.h"
61 // Documentation for this class is emitted in the
62 // OSGCSMWindowBase.cpp file.
63 // To modify it, please change the .fcd file (OSGCSMWindow.fcd) and
64 // regenerate the base file.
66 /***************************************************************************\
68 \***************************************************************************/
70 /***************************************************************************\
72 \***************************************************************************/
74 void CSMWindow::initMethod(InitPhase ePhase
)
76 Inherited::initMethod(ePhase
);
78 if(ePhase
== TypeObject::SystemPost
)
84 /***************************************************************************\
86 \***************************************************************************/
88 /*-------------------------------------------------------------------------*\
90 \*-------------------------------------------------------------------------*/
92 /*----------------------- constructors & destructors ----------------------*/
94 CSMWindow::CSMWindow(void) :
105 CSMWindow::CSMWindow(const CSMWindow
&source
) :
109 _bFirstFrame (true ),
112 _bMouseActive(false )
116 CSMWindow::~CSMWindow(void)
120 void CSMWindow::resolveLinks(void)
122 Inherited::resolveLinks();
128 void CSMWindow::reshape(Int32 w
,
133 this->setSize(Vec2f(w
, h
));
135 _pWindow
->resize(w
, h
);
139 void CSMWindow::mouse(Int32 iButton
,
145 if(iState
== MouseData::ButtonDown
)
147 _bMouseActive
= true;
151 _bMouseActive
= false;
154 if(_sfMouseAsMTouch
.getValue() == true)
156 if(iState
== MouseData::ButtonDown
)
158 editMTouchData().addCursor(0, x
, y
, MTouchData::WindowAbs
);
162 editMTouchData().removeCursor(0);
164 if(_uiTouchMode
== 1)
166 editMTouchData().removeCursor(1);
172 _sfMTouchData
.getValue().clear();
178 editMouseData().setData( iButton
,
189 void CSMWindow::motion(Int32 x
,
193 if(_bMouseActive
== false)
196 if(_sfMouseAsMTouch
.getValue() == true)
198 if(_uiTouchMode
== 0)
200 if(0x0000 != (iModifier
& MouseData::CtrlActive
))
204 editMTouchData().updateCursor(0,
205 _oTouchBlob
._vPosition
[0],
206 _oTouchBlob
._vPosition
[1],
207 _oTouchBlob
._uiCoordSys
);
209 editMTouchData().updateCursor(1, x
, y
, MTouchData::WindowAbs
);
213 editMTouchData().updateCursor(0, x
, y
, MTouchData::WindowAbs
);
215 _oTouchBlob
._vPosition
.setValues(x
, y
, 0);
216 _oTouchBlob
._uiCoordSys
= MTouchData::WindowAbs
;
221 if(0x0000 != (iModifier
& MouseData::CtrlActive
))
223 editMTouchData().updateCursor(0,
224 _oTouchBlob
._vPosition
[0],
225 _oTouchBlob
._vPosition
[1],
226 _oTouchBlob
._uiCoordSys
);
228 editMTouchData().updateCursor(1, x
, y
, MTouchData::WindowAbs
);
234 editMTouchData().updateCursor(0, x
, y
, MTouchData::WindowAbs
);
235 editMTouchData().removeCursor(1);
237 _oTouchBlob
._vPosition
.setValues(x
, y
, 0);
238 _oTouchBlob
._uiCoordSys
= MTouchData::WindowAbs
;
244 _sfMTouchData
.getValue().clear();
248 editMouseData().updateData(x
, y
, _pWindow
);
253 void CSMWindow::addMTouchCursor(Int32 iCursorId
,
258 editMTouchData().addCursor(iCursorId
,
264 void CSMWindow::updateMTouchCursor(Int32 iCursorId
,
269 editMTouchData().updateCursor(iCursorId
,
275 void CSMWindow::removeMTouchCursor(Int32 iCursorId
)
277 editMTouchData().removeCursor(iCursorId
);
280 void CSMWindow::commitMTouchCursors(void)
284 _sfMTouchData
.getValue().clear();
287 CSMDrawer
*CSMWindow::getParent(void) const
289 CSMDrawer
*returnValue
= dynamic_cast<CSMDrawer
*>(_sfParent
.getValue());
291 if(returnValue
== NULL
)
293 CSMWindow
*pWin
= dynamic_cast<CSMWindow
*>(_sfParent
.getValue());
297 returnValue
= pWin
->getParent();
304 void CSMWindow::queueTask(DrawTask
*pTask
)
308 _pWindow
->queueTask(pTask
);
313 /*----------------------------- class specific ----------------------------*/
315 void CSMWindow::changed(ConstFieldMaskArg whichField
,
319 if (0x0000 != (whichField
& MTouchDataFieldMask
))
321 MFViewportsType::const_iterator vpIt
= _mfViewports
.begin();
322 MFViewportsType::const_iterator vpEnd
= _mfViewports
.end ();
324 for(; vpIt
!= vpEnd
; ++vpIt
)
329 (*vpIt
)->editMTouchDataVC().clear();
332 MTouchData::MTouchBlobStore
&vBlobs
=
333 _sfMTouchData
.getValue().getBlobStore();
335 MTouchData::MTouchBlobStoreIt bIt
= vBlobs
.begin();
336 MTouchData::MTouchBlobStoreIt bEnd
= vBlobs
.end ();
338 MTouchData::MTouchBlobStore
&vBlobsWC
=
339 editMTouchDataWC().getBlobStore();
342 fprintf(stderr
, "process\n=====================================\n");
343 fprintf(stderr
, "input mtouch\n");
344 _sfMTouchData
.getValue().dump();
346 fprintf(stderr
, "wc mtouch\n");
347 _sfMTouchDataWC
.getValue().dump();
355 MTouchData::MTouchBlobStoreIt bWCIt
= vBlobsWC
.begin();
357 for (; bIt
!= bEnd
; ++bIt
)
361 switch(bIt
->_uiCoordSys
)
363 case MTouchData::GlobalRel
:
365 vScreenC
= this->translateGlobalCoordinatesRel(
372 case MTouchData::GlobalAbs
:
373 vScreenC
= this->translateGlobalCoordinatesAbs(
379 case MTouchData::WindowRel
:
381 bIt
->_vPosition
[0] * Real32(this->getSize()[0]) + 0.5f
;
384 bIt
->_vPosition
[1] * Real32(this->getSize()[1]) + 0.5f
;
388 case MTouchData::WindowAbs
:
389 vScreenC
[0] = bIt
->_vPosition
[0];
390 vScreenC
[1] = bIt
->_vPosition
[1];
400 fprintf(stderr
, "process %d %d\n",
402 (bWCIt
!= vBlobsWC
.end()) ? bWCIt
->_iCursorId
: -1);
405 while(bWCIt
!= vBlobsWC
.end())
407 if(bWCIt
->_uiEvent
!= MTouchData::RemoveCursor
)
410 bWCIt
= vBlobsWC
.erase(bWCIt
);
413 if(bWCIt
!= vBlobsWC
.end() &&
414 bIt
->_iCursorId
== bWCIt
->_iCursorId
)
416 bWCIt
->_uiEvent
= bIt
->_uiEvent
;
417 bWCIt
->_vPosition
[0] = vScreenC
[0];
418 bWCIt
->_vPosition
[1] = vScreenC
[1];
419 bWCIt
->_uiCoordSys
= MTouchData::WindowAbs
;
422 fprintf(stderr
, "update %d (%d) | %p\n",
425 bIt
->_pActiveViewport
);
428 if(bWCIt
->_pActiveViewport
!= NULL
)
431 bWCIt
->_pActiveViewport
->translateWindowViewportAbs(
435 MTouchData::MTouchBlob
tmpBlobVC(
440 MTouchData::ViewportAbs
);
442 tmpBlobVC
._pWindow
= this->_pWindow
;
443 tmpBlobVC
._pViewport
=
444 bWCIt
->_pActiveViewport
->getViewport(0);
446 bWCIt
->_pActiveViewport
->editMTouchDataVC().
447 getBlobStore().push_back(tmpBlobVC
);
455 if(bWCIt
== vBlobsWC
.end() ||
456 bWCIt
->_iCursorId
> bIt
->_iCursorId
)
458 if(bIt
->_uiEvent
== MTouchData::AddCursor
)
460 MTouchData::MTouchBlob
tmpBlobWC(
465 MTouchData::WindowAbs
);
467 tmpBlobWC
._pWindow
= this->_pWindow
;
469 CSMViewport
*pViewport
= this->findViewport(
473 if(pViewport
!= NULL
)
476 pViewport
->translateWindowViewportAbs(
480 MTouchData::MTouchBlob
tmpBlobVC(
485 MTouchData::ViewportAbs
);
487 tmpBlobVC
._pWindow
= this->_pWindow
;
488 tmpBlobVC
._pViewport
=
489 pViewport
->getViewport(0);
491 tmpBlobWC
._pActiveViewport
= pViewport
;
493 pViewport
->editMTouchDataVC().getBlobStore().
494 push_back(tmpBlobVC
);
497 bWCIt
= vBlobsWC
.insert(bWCIt
, tmpBlobWC
);
501 fprintf(stderr
, "insert %d (%d)\n",
502 tmpBlobWC
._iCursorId
,
513 else if(bWCIt
->_iCursorId
< bIt
->_iCursorId
)
516 fprintf(stderr
, "remove %d (%d)\n",
521 bWCIt
= vBlobsWC
.erase(bWCIt
);
528 } while(bWCIt
!= vBlobsWC
.end());
531 if(bWCIt
!= vBlobsWC
.end())
533 vBlobsWC
.erase(bWCIt
, vBlobsWC
.end());
537 fprintf(stderr
, "process end\n=====================================\n");
538 fprintf(stderr
, "input mtouch\n");
539 _sfMTouchData
.getValue().dump();
541 fprintf(stderr
, "wc mtouch\n");
542 _sfMTouchDataWC
.getValue().dump();
546 if (0x0000 != (whichField
& MTouchDataWCFieldMask
))
549 fprintf(stderr
, "\n=============\nplop\n=====================\n");
553 if (0x0000 != (whichField
& GestureDataFieldMask
))
555 _sfGestureData
.getValue().setWindow(_pWindow
);
556 _sfGestureData
.getValue().setCSMWindow(this);
559 if (0x0000 != (whichField
& MouseDataFieldMask
))
561 MouseData
&oMouseData
= _sfMouseData
.getValue();
563 if(oMouseData
.getState() != -1)
565 if(oMouseData
.getState() == MouseData::ButtonDown
)
567 CSMViewport
*pViewport
= this->findViewport(
571 oMouseData
._pActivePort
= pViewport
;
573 if(oMouseData
._pActivePort
!= NULL
)
576 oMouseData
._pActivePort
->translateWindowViewportAbs(
580 MouseData
&oVPMouseData
=
581 oMouseData
._pActivePort
->editMouseDataVC();
583 oVPMouseData
.setData(oMouseData
.getButton (),
584 oMouseData
.getState (),
585 oMouseData
.getModifier(),
588 oMouseData
.getWindow (),
589 oMouseData
.getMode ());
592 oVPMouseData
.setViewport(
593 oMouseData
._pActivePort
->getViewport(0));
598 if(oMouseData
._pActivePort
!= NULL
)
601 oMouseData
._pActivePort
->translateWindowViewportAbs(
605 MouseData
&oVPMouseData
=
606 oMouseData
._pActivePort
->editMouseDataVC();
608 oVPMouseData
.setData(oMouseData
.getButton (),
609 oMouseData
.getState (),
610 oMouseData
.getModifier(),
613 oMouseData
.getWindow (),
614 oMouseData
.getMode ());
617 oVPMouseData
.setViewport(
618 oMouseData
._pActivePort
->getViewport(0));
621 oMouseData
._pActivePort
= NULL
;
626 if(oMouseData
._pActivePort
!= NULL
)
629 oMouseData
._pActivePort
->translateWindowViewportAbs(
633 MouseData
&oVPMouseData
=
634 oMouseData
._pActivePort
->editMouseDataVC();
636 oVPMouseData
.setData(oMouseData
.getButton (),
637 oMouseData
.getState (),
638 oMouseData
.getModifier(),
641 oMouseData
.getWindow (),
642 oMouseData
.getMode ());
645 oVPMouseData
.setViewport(
646 oMouseData
._pActivePort
->getViewport(0));
651 Inherited::changed(whichField
, origin
, details
);
654 CSMViewport
*CSMWindow::findViewport(Real32 x
, Real32 y
) const
656 MFViewportsType::const_iterator vpIt
= _mfViewports
.begin();
657 MFViewportsType::const_iterator vpEnd
= _mfViewports
.end ();
659 for(; vpIt
!= vpEnd
; ++vpIt
)
661 if((*vpIt
)->pointInside(x
, y
) == true)
668 void CSMWindow::dump( UInt32
,
669 const BitVector
) const
671 SLOG
<< "Dump CSMWindow NI" << std::endl
;
674 Vec2f
CSMWindow::translateScreenCoordinatesRel(Real32 rX
,
677 Vec2f
returnValue(0.f
, 0.f
);
682 Vec2i
CSMWindow::translateGlobalCoordinatesRel(Real32 rX
,
685 Vec2i
returnValue(0, 0);
690 Vec2i
CSMWindow::translateGlobalCoordinatesAbs(Int32 iX
,
693 Vec2i
returnValue(0, 0);
698 Vec2f
CSMWindow::translateToScreenCoordinatesAbs(Real32 rX
,
701 Vec2f
returnValue(0.f
, 0.f
);
706 bool CSMWindow::init(void)
708 bool returnValue
= true;
710 MFUnrecCSMViewportPtr::const_iterator vIt
= getMFViewports()->begin();
711 MFUnrecCSMViewportPtr::const_iterator vEnd
= getMFViewports()->end ();
715 returnValue
= (*vIt
)->init(this);
717 if(returnValue
== false)
725 if(_pWindow
!= NULL
&& returnValue
== true)
727 vIt
= getMFViewports()->begin();
728 vEnd
= getMFViewports()->end ();
730 for(; vIt
!= vEnd
; ++vIt
)
732 CSMViewport::ViewportStoreConstIt pIt
= (*vIt
)->beginViewports();
733 CSMViewport::ViewportStoreConstIt pEnd
= (*vIt
)->endViewports ();
735 for(; pIt
!= pEnd
; ++pIt
)
737 _pWindow
->addPort((*pIt
));
742 fprintf(stderr
, "foo %p %d\n",
743 ComplexSceneManager::the()->getDrawManager(),
744 UInt32(ComplexSceneManager::the()->
745 getDrawManager()->getParallel()));
749 UInt32 uiDrawMode
= this->getPartitionDrawMode();
751 UInt32 uiDrawMode
= Window::SequentialPartitionDraw
;
753 FWARNING(("Detected apple, only sequential draw mode available\n"));
756 if(ComplexSceneManager::the()->getDrawManager()->getParallel() == true)
758 uiDrawMode
|= Window::ParallelDrawer
;
762 uiDrawMode
|= Window::StdDrawer
;
765 _pWindow
->setRenderOptions (this->getRenderOptions());
766 _pWindow
->setPartitionDrawMode(uiDrawMode
);
767 _pWindow
->setDrawerType (uiDrawMode
);
769 _pWindow
->setIgnoreAllExtensions(this->getIgnoreAllExtensions());
771 MFString::const_iterator ieIt
= _mfIgnoreExtensions
.begin();
772 MFString::const_iterator ieEnd
= _mfIgnoreExtensions
.end ();
774 for(; ieIt
!= ieEnd
; ++ieIt
)
776 Window::ignoreExtensions(ieIt
->c_str());
780 // OSGSceneFileType::the().writeContainer(_pWindow, "/tmp/window.osg");
782 if(this->getDumpContainer() == true)
784 FieldContainerFactory::the()->dump();
790 void CSMWindow::render(RenderAction
*pAction
)
792 #ifdef OSG_MT_CPTR_ASPECT
793 Window
*pThreadLocalWin
=
794 Aspect::convertToCurrent
<Window
*>(_pWindow
.get());
795 StatisticsForeground
*pThreadLocalStatFG
=
796 Aspect::convertToCurrent
<StatisticsForeground
*>(_pStatFG
);
798 Window
*pThreadLocalWin
= _pWindow
;
799 StatisticsForeground
*pThreadLocalStatFG
= _pStatFG
;
802 if(pThreadLocalWin
== NULL
)
805 if(pThreadLocalStatFG
!= NULL
)
807 if(pThreadLocalStatFG
->getActive() == true)
809 pAction
->setStatCollector(pThreadLocalStatFG
->getCollector());
810 pAction
->setUseGLFinish (true);
814 pAction
->setStatCollector(NULL
);
815 pAction
->setUseGLFinish (false);
819 // fprintf(stderr, "csmwin::render %p\n", pThreadLocalStatFG);
822 if(_bFirstFrame
== true)
824 _bFirstFrame
= false;
826 pAction
->setFrustumCulling(false);
828 pThreadLocalWin
->render(pAction
);
830 pAction
->setFrustumCulling(true);
834 pThreadLocalWin
->render(pAction
);
837 if(_bFirstFrame
== true)
839 _bFirstFrame
= false;
841 pAction
->setFrustumCulling(false);
843 pThreadLocalWin
->renderNoFinish(pAction
);
845 pAction
->setFrustumCulling(true);
849 pThreadLocalWin
->renderNoFinish(pAction
);
852 pThreadLocalWin
->frameFinish();
856 void CSMWindow::frameRenderNoFinish(RenderAction
*pAction
)
858 #ifdef OSG_MT_CPTR_ASPECT
859 Window
*pThreadLocalWin
=
860 Aspect::convertToCurrent
<Window
*>(_pWindow
.get());
861 StatisticsForeground
*pThreadLocalStatFG
=
862 Aspect::convertToCurrent
<StatisticsForeground
*>(_pStatFG
);
864 Window
*pThreadLocalWin
= _pWindow
;
865 StatisticsForeground
*pThreadLocalStatFG
= _pStatFG
;
868 if(pThreadLocalWin
== NULL
)
871 if(pThreadLocalStatFG
!= NULL
)
873 if(pThreadLocalStatFG
->getActive() == true)
875 pAction
->setStatCollector(pThreadLocalStatFG
->getCollector());
879 pAction
->setStatCollector(NULL
);
883 if(_bFirstFrame
== true)
885 _bFirstFrame
= false;
887 pAction
->setFrustumCulling(false);
889 pThreadLocalWin
->renderNoFinish(pAction
);
891 pAction
->setFrustumCulling(true);
895 pThreadLocalWin
->renderNoFinish(pAction
);
899 void CSMWindow::frameFinish(void)
901 #ifdef OSG_MT_CPTR_ASPECT
902 Window
*pThreadLocalWin
=
903 Aspect::convertToCurrent
<Window
*>(_pWindow
.get());
905 Window
*pThreadLocalWin
= _pWindow
;
908 if(pThreadLocalWin
== NULL
)
911 pThreadLocalWin
->frameFinish();
914 void CSMWindow::frameExit(void)
916 #ifdef OSG_MT_CPTR_ASPECT
917 Window
*pThreadLocalWin
=
918 Aspect::convertToCurrent
<Window
*>(_pWindow
.get());
920 Window
*pThreadLocalWin
= _pWindow
;
923 if(pThreadLocalWin
== NULL
)
926 pThreadLocalWin
->runFrameExit();
929 void CSMWindow::shutdown(void)
931 #ifdef OSG_MT_CPTR_ASPECT
932 Window
*pThreadLocalWin
=
933 Aspect::convertToCurrent
<Window
*>(_pWindow
.get());
935 Window
*pThreadLocalWin
= _pWindow
;
938 Inherited::resolveLinks();
940 if(pThreadLocalWin
== NULL
)
943 pThreadLocalWin
->resolveLinks();
946 void CSMWindow::postSync(void)
950 _pWindow
->clearDrawTasks();
954 bool CSMWindow::requestStereoVisual(void)
956 bool returnValue
= false;
958 MFUnrecCSMViewportPtr::const_iterator vIt
= getMFViewports()->begin();
959 MFUnrecCSMViewportPtr::const_iterator vEnd
= getMFViewports()->end ();
961 for(; vIt
!= vEnd
; ++vIt
)
963 returnValue
= (*vIt
)->needsStereoVisual();
965 if(returnValue
== true)