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 #define OSG_COMPILEMANIPULATORSLIB
48 #include "OSGConfig.h"
49 #include "OSGRenderAction.h"
50 #include "OSGIntersectAction.h"
51 #include "OSGCamera.h"
53 #include "OSGMoveManipulator.h"
57 /***************************************************************************\
59 \***************************************************************************/
61 /*! \class OSG::MoveManipulator
62 * The MoveHandle is used for moving objects. It consist of three axis which
63 * can be picked and translated and one center box to translate freely in 3D.
66 /***************************************************************************\
68 \***************************************************************************/
70 /***************************************************************************\
72 \***************************************************************************/
74 void MoveManipulator::initMethod(InitPhase ePhase
)
76 Inherited::initMethod(ePhase
);
78 if(ePhase
== TypeObject::SystemPost
)
80 IntersectAction::registerEnterDefault(
82 reinterpret_cast<Action::Callback
>(
83 &MoveManipulator::intersectEnter
));
85 IntersectAction::registerLeaveDefault(
87 reinterpret_cast<Action::Callback
>(
88 &MoveManipulator::intersectLeave
));
90 RenderAction::registerEnterDefault(
92 reinterpret_cast<Action::Callback
>(&MoveManipulator::renderEnter
));
94 RenderAction::registerLeaveDefault(
96 reinterpret_cast<Action::Callback
>(&MoveManipulator::renderLeave
));
101 /***************************************************************************\
103 \***************************************************************************/
105 /*-------------------------------------------------------------------------*\
107 \*-------------------------------------------------------------------------*/
109 /*----------------------- constructors & destructors ----------------------*/
111 MoveManipulator::MoveManipulator(void) :
116 MoveManipulator::MoveManipulator(const MoveManipulator
&source
) :
121 MoveManipulator::~MoveManipulator(void)
125 /*----------------------------- class specific ----------------------------*/
127 void MoveManipulator::changed(ConstFieldMaskArg whichField
,
131 Inherited::changed(whichField
, origin
, details
);
134 void MoveManipulator::dump( UInt32 uiIndent
,
135 const BitVector bvFlags
) const
137 Inherited::dump(uiIndent
, bvFlags
);
140 NodeTransitPtr
MoveManipulator::makeHandleGeo()
142 return makeCone(0.75f
, 0.1f
, 12, true, true);
145 /*! The mouseMove is called by the viewer when the mouse is moved in the
146 viewer and this handle is the active one.
148 \param x the x-pos of the mouse (pixel)
149 \param y the y-pos of the mouse (pixel)
151 void MoveManipulator::mouseMove(const Int16 x
,
154 //SLOG << "==============================" << endLog;
155 //SLOG << "Manipulator::mouseMove() enter x=" << x << " y=" << y << endLog;
157 // get the beacon's core (must be ComponentTransform) and it's center
158 if( getTarget() != NULL
)
160 // get transformation of beacon
161 Transform
*t
= dynamic_cast<Transform
*>(getTarget()->getCore());
165 Vec3f translation
; // for matrix decomposition
168 Quaternion scaleOrientation
;
170 t
->getMatrix().getTransform(translation
, rotation
, scaleFactor
,
174 getViewport()->getCamera()->calcViewRay(viewray
, x
, y
, *getViewport());
176 //SLOG << "Manipulator::mouseMove(): viewray: " << viewray << endLog;
178 Line
axis(getAxisBase(), getAxisDirection()); // HACK: Should add a Line Fieldtype
180 //SLOG << "Manipulator::mouseMove(): axis: " << axis << endLog;
182 Real32 axist
, viewrayt
;
184 axis
.getClosestPoints(viewray
, axist
, viewrayt
);
186 // Only accept cases where the closest point is not behind the viewer
189 axist
/= getAxisDirection().length();
191 //SLOG << "Manipulator::mouseMove(): axist=" << axist << " viewrayt=" << viewrayt <<endLog;
195 rotation
.multVec(getActiveAxis(), rot_axis
);
196 Vec3f trans
= getBaseTranslation() + rot_axis
* axist
;
200 m
.setTransform(trans
, rotation
, scaleFactor
, scaleOrientation
);
207 SWARNING
<< "handled object has no parent transform!\n";
209 callExternalUpdateHandler();
213 SWARNING
<< "Handle has no target.\n";
216 setLastMousePos(Pnt2f(Real32(x
), Real32(y
)));
217 updateHandleTransform();
219 //SLOG << "Manipulator::mouseMove() leave\n" << std::flush;
222 /*! The mouseButtonPress is called by the viewer when the mouse is
223 pressed in the viewer above a subhandle of this handle.
225 \param button the button pressed
226 \param x the x-pos of the mouse (pixel)
227 \param y the y-pos of the mouse (pixel)
230 void MoveManipulator::mouseButtonPress(const UInt16 button
,
234 Transform
*t
= dynamic_cast<Transform
*>(getTarget()->getCore());
238 SWARNING
<< "Manipulator::mouseButtonPress() target is not a Transform!" << endLog
;
242 //SLOG << "Manipulator::mouseButtonPress() button=" << button << " x=" << x << " y=" << y << std::endl << endLog;
245 getViewport()->getCamera()->calcViewRay(viewray
, x
, y
, *getViewport());
247 OSG::Node
*scene
= getTarget();
248 while (scene
->getParent() != 0)
250 scene
= scene
->getParent();
253 OSG::IntersectActionRefPtr act
= OSG::IntersectAction::create();
254 act
->setLine( viewray
);
257 //SLOG << "Manipulator::mouseButtonPress() viewray=" << viewray << " scene=" << scene << endLog;
261 //SLOG << "Manipulator::mouseButtonPress() hit! at " << act->getHitPoint() << endLog;
263 // Get manipulator axis into world space
264 OSG::Matrix m
= getTarget()->getToWorld();
266 Pnt3f
origin(0,0,0), base
, dummy
;
269 m
.multFull(origin
, base
);
270 m
.multFull(getActiveAxis(), dir
);
272 Line
axis(base
, dir
);
274 //SLOG << "Manipulator::mouseButtonPress() world axis=" << axis << endLog;
277 axis
.getClosestPoints(viewray
, apoint
, dummy
);
279 //SLOG << "Manipulator::mouseButtonPress() apoint " << apoint << endLog;
282 setAxisDirection(dir
);
284 Vec3f translation
; // for matrix decomposition
287 Quaternion scaleOrientation
;
289 t
->getMatrix().getTransform(translation
, rotation
, scaleFactor
,
292 setBaseTranslation(translation
);
300 void MoveManipulator::mouseButtonRelease(const UInt16 button
,
304 //SLOG << "Manipulator::mouseButtonRelease() button=" << button << " x=" << x << " y=" << y << std::endl << endLog;