1 /*---------------------------------------------------------------------------*\
5 * Copyright (C) 2000-2002 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 "OSGScreenGroup.h"
49 #include "OSGIntersectAction.h"
50 #include "OSGRenderAction.h"
51 #include "OSGViewport.h"
52 #include "OSGCamera.h"
56 /***************************************************************************\
58 \***************************************************************************/
60 /*! \class OSG::ScreenGroup
63 /***************************************************************************\
65 \***************************************************************************/
67 /***************************************************************************\
69 \***************************************************************************/
71 void ScreenGroup::initMethod(InitPhase ePhase
)
73 Inherited::initMethod(ePhase
);
75 if(ePhase
== TypeObject::SystemPost
)
77 IntersectAction::registerEnterDefault(
78 ScreenGroup::getClassType(),
79 reinterpret_cast<Action::Callback
>(&ScreenGroup::intersectEnter
));
81 IntersectAction::registerLeaveDefault(
82 ScreenGroup::getClassType(),
83 reinterpret_cast<Action::Callback
>(&ScreenGroup::intersectLeave
));
85 RenderAction::registerEnterDefault(
86 ScreenGroup::getClassType(),
87 reinterpret_cast<Action::Callback
>(&ScreenGroup::renderEnter
));
89 RenderAction::registerLeaveDefault(
90 ScreenGroup::getClassType(),
91 reinterpret_cast<Action::Callback
>(&ScreenGroup::renderLeave
));
96 /***************************************************************************\
98 \***************************************************************************/
100 /*-------------------------------------------------------------------------*\
102 \*-------------------------------------------------------------------------*/
104 /*----------------------- constructors & destructors ----------------------*/
106 ScreenGroup::ScreenGroup(void) :
112 ScreenGroup::ScreenGroup(const ScreenGroup
&source
) :
114 _camTransform(source
._camTransform
)
118 ScreenGroup::~ScreenGroup(void)
122 /*----------------------------- class specific ----------------------------*/
124 void ScreenGroup::changed(ConstFieldMaskArg whichField
,
128 Inherited::changed(whichField
, origin
, details
);
131 void ScreenGroup::dump( UInt32
,
132 const BitVector
) const
134 SLOG
<< "Dump ScreenGroup NI" << std::endl
;
138 /*------------------------- volume update -------------------------------*/
140 void ScreenGroup::adjustVolume(Volume
&volume
)
142 volume
.transform(_camTransform
);
145 void ScreenGroup::accumulateMatrix(Matrix
&result
)
147 result
.mult(_camTransform
);
150 void ScreenGroup::calcMatrix( RenderAction
*pAction
,
151 const Matrix
&mToWorld
,
154 const DrawEnv
&oEnv
= pAction
->getActivePartition()->getDrawEnv();
156 Matrix mToScreen
= oEnv
.getVPWorldToScreen();
159 Viewport
*viewport
= pAction
->getViewport();
160 pAction
->getCamera()->getWorldToScreen(mToScreen
, *viewport
);
163 mToScreen
.mult(mToWorld
);
165 Pnt3f
origin(0.f
, 0.f
, 0.f
);
167 mToScreen
.multFull(origin
, origin
);
169 Pnt3f
xAxis(1.f
, 0.f
, 0.f
);
171 mToScreen
.multFull(xAxis
, xAxis
);
174 2.f
/ oEnv
.getPixelWidth() / (xAxis
- origin
).length();
176 Pnt3f
yAxis(0.f
, 1.f
, 0.f
);
178 mToScreen
.multFull(yAxis
, yAxis
);
181 2.f
/ oEnv
.getPixelHeight() / (yAxis
- origin
).length();
183 mResult
.setScale (scaleX
, scaleY
, 1.f
);
184 mResult
.setTranslate(0.375 * scaleX
, 0.375 * scaleY
, 0.f
);
186 bool equal
= mResult
.equals(_camTransform
, 0.00001f
);
188 _camTransform
= mResult
;
191 this->invalidateVolume();
194 /*-------------------------------------------------------------------------*/
197 Action::ResultE
ScreenGroup::intersectEnter(Action
*action
)
199 IntersectAction
*ia
= dynamic_cast<IntersectAction
*>(action
);
200 Matrix
m(_camTransform
);
207 m
.multFull(ia
->getLine().getPosition (), pos
);
208 m
.mult (ia
->getLine().getDirection(), dir
);
210 ia
->setLine(Line(pos
, dir
), ia
->getMaxDist());
211 ia
->scale(dir
.length());
213 return Action::Continue
;
216 Action::ResultE
ScreenGroup::intersectLeave(Action
*action
)
218 IntersectAction
*ia
= dynamic_cast<IntersectAction
*>(action
);
219 Matrix
m(_camTransform
);
224 m
.multFull(ia
->getLine().getPosition (), pos
);
225 m
.mult (ia
->getLine().getDirection(), dir
);
227 ia
->setLine(Line(pos
, dir
), ia
->getMaxDist());
228 ia
->scale(dir
.length());
230 return Action::Continue
;
233 /*-------------------------------------------------------------------------*/
236 Action::ResultE
ScreenGroup::renderEnter(Action
*action
)
238 RenderAction
*pAction
= dynamic_cast<RenderAction
*>(action
);
243 if(!pAction
->getEffectsPass())
244 calcMatrix(pAction
, pAction
->top_matrix(), mMat
);
246 mMat
= _camTransform
;
248 calcMatrix(pAction
, pAction
->topMatrix(), mMat
);
251 pAction
->pushMatrix(mMat
);
253 // !!! can't use visibles, as ToWorld gives garbage leading to wrong culling
254 // pAction->selectVisibles();
256 return Action::Continue
;
259 Action::ResultE
ScreenGroup::renderLeave(Action
*action
)
261 RenderAction
*pAction
= dynamic_cast<RenderAction
*>(action
);
263 pAction
->popMatrix();
265 return Action::Continue
;