AGX Dynamics  2.37.2.0
FirstPersonManipulator.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2024. Algoryx Simulation AB.
3 
4 All AGX source code, intellectual property, documentation, sample code,
5 tutorials, scene files and technical white papers, are copyrighted, proprietary
6 and confidential material of Algoryx Simulation AB. You may not download, read,
7 store, distribute, publish, copy or otherwise disseminate, use or expose this
8 material unless having a written signed agreement with Algoryx Simulation AB, or having been
9 advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10 valid commercial license from Algoryx Simulation AB.
11 
12 Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13 from using this software, unless otherwise stated in written agreements with
14 Algoryx Simulation AB.
15 */
16 
17 /*
18 This source code has been taken and modified by Algoryx Simulation AB
19 from the source and under the license given below.
20 */
21 
22 /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2010 Robert Osfield
23  *
24  * This library is open source and may be redistributed and/or modified under
25  * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
26  * (at your option) any later version. The full license is in LICENSE file
27  * included with this distribution, and on the openscenegraph.org website.
28  *
29  * This library is distributed in the hope that it will be useful,
30  * but WITHOUT ANY WARRANTY; without even the implied warranty of
31  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32  * OpenSceneGraph Public License for more details.
33  *
34  * FirstPersonManipulator code Copyright (C) 2010 PCJohn (Jan Peciva)
35  * while some pieces of code were taken from OSG.
36  * Thanks to company Cadwork (www.cadwork.ch) and
37  * Brno University of Technology (www.fit.vutbr.cz) for open-sourcing this work.
38 */
39 
40 #ifndef AGXOSG_FIRST_PERSON_MANIPULATOR
41 #define AGXOSG_FIRST_PERSON_MANIPULATOR
42 
43 #include <osg/Version>
44 
45 #if defined(OSG_VERSION_GREATER_OR_EQUAL)
46 # if OSG_VERSION_GREATER_OR_EQUAL(2,9,11)
47 
48 
49 #include <agx/PushDisableWarnings.h> // Disabling warnings. Include agx/PopDisableWarnings.h below!
50 #include <osgGA/StandardManipulator>
51 #include <osgGA/TrackballManipulator>
52 #include <osgViewer/Viewer>
53 #include <agx/PopDisableWarnings.h> // End of disabled warnings.
54 
55 #include <agxOSG/export.h>
56 #include <agx/HashSet.h>
57 #include <agx/Timer.h>
58 #include <agx/Vec2.h>
59 
60 namespace agxOSG {
61 
62 
75 class AGXOSG_EXPORT FirstPersonManipulator : public osgGA::StandardManipulator
76 {
77  typedef StandardManipulator inherited;
78 
79  public:
80 
81  FirstPersonManipulator( osgViewer::GraphicsWindow* window, int flags = DEFAULT_SETTINGS );
82  FirstPersonManipulator( const FirstPersonManipulator& fpm,
83  const osg::CopyOp& copyOp = osg::CopyOp::SHALLOW_COPY );
84 
85  META_Object( osgGA, FirstPersonManipulator );
86 
87  virtual void setByMatrix( const osg::Matrixd& matrix );
88  virtual void setByInverseMatrix( const osg::Matrixd& matrix );
89  virtual osg::Matrixd getMatrix() const;
90  virtual osg::Matrixd getInverseMatrix() const;
91 
92  virtual void setTransformation( const osg::Vec3d& eye, const osg::Quat& rotation );
93  virtual void setTransformation( const osg::Vec3d& eye, const osg::Vec3d& center, const osg::Vec3d& up );
94  virtual void getTransformation( osg::Vec3d& eye, osg::Quat& rotation ) const;
95  virtual void getTransformation( osg::Vec3d& eye, osg::Vec3d& center, osg::Vec3d& up ) const;
96 
97  virtual void setVelocity( const double& velocity );
98  inline double getVelocity() const;
99  void setRotationSpeed( const double& rotationSpeed );
100  inline double getRotationSpeed() const;
101  virtual void setAcceleration( const double& acceleration, bool relativeToModelSize = false );
102  double getAcceleration( bool *relativeToModelSize = nullptr ) const;
103  virtual void setMaxVelocity( const double& maxVelocity, bool relativeToModelSize = false );
104  double getMaxVelocity( bool *relativeToModelSize = nullptr ) const;
105 
106  virtual void setMovement( const double& wheelMovement, bool relativeToModelSize = false );
107  double getMovement( bool *relativeToModelSize = nullptr ) const;
108 
109  virtual void home( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );
110  virtual void home( double );
111 
112  virtual void init( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );
113 
114  protected:
115  FirstPersonManipulator();
116 
117  virtual bool handleFrame( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );
118  virtual bool handleKeyDown( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );
119  virtual bool handleKeyUp( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );
120 
121  virtual bool handleMouseWheel( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );
122 
123  virtual bool handleMousePush( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );
124  virtual bool handleMouseRelease( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );
125 
126  //virtual bool performMovementLeftMouseButton( const double eventTimeDelta, const double dx, const double dy );
127  virtual bool performMouseDeltaMovement( const float dx, const float dy );
128  virtual void applyAnimationStep( const double currentProgress, const double prevProgress );
129  virtual bool startAnimationByMousePointerIntersection( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );
130 
131  void moveForward( const double distance );
132  void moveForward( const osg::Quat& rotation, const double distance );
133  void moveRight( const double distance );
134  void moveUp( const double distance );
135 
136  void handleContinuousEvents(agx::Real dt);
137 
138  osg::Vec3d m_eye;
139  osg::Quat m_rotation;
140  double m_velocity;
141  double m_rotationSpeed;
142  double m_acceleration;
143  static int m_accelerationFlagIndex;
144  double _maxVelocity;
145  static int m_maxVelocityFlagIndex;
146  double m_movement;
147  static int m_movementFlagIndex;
148  agx::Timer m_eventTimer;
149  agx::HashSet<int> m_continuousKeys;
150  bool m_mouseDragged;
151  bool m_mousedDraggedFirstFrame;
152  agx::Vec2f m_mouseStartPos;
153  agx::Vec2f m_mouseDelta;
154  osg::observer_ptr<osgViewer::GraphicsWindow> m_window;
155 
156 
157  class FirstPersonAnimationData : public AnimationData {
158  public:
159  osg::Quat m_startRot;
160  osg::Quat m_targetRot;
161  void start( const osg::Quat& startRotation, const osg::Quat& targetRotation, const double startTime );
162  };
163  virtual void allocAnimationData() { _animationData = new FirstPersonAnimationData(); }
164 };
165 
166 
167 //
168 // inline methods
169 //
170 
172 double FirstPersonManipulator::getVelocity() const { return m_velocity; }
173 
174 
176 double FirstPersonManipulator::getRotationSpeed() const { return m_rotationSpeed; }
177 
178 
179 }
180 
181 #endif // Version of OSG > 2.9.11
182 #endif // Version > 2.9.11
183 #endif /* OSGGA_FIRST_PERSON_MANIPULATOR */
184 
void AGXPHYSICS_EXPORT init()
Initialize AGX Dynamics API including thread resources and must be executed before using the AGX API...
Inheritance with partial specialization due to bug with ref_ptr containers.
Definition: HashSet.h:669
#define AGXOSG_EXPORT
Vec3T< Real64 > Vec3d
Definition: Vec3.h:40
The Timer class permits timing execution speed with the same refinement as the built in hardware cloc...
Definition: Timer.h:74
The agxOSG namespace provides functionality for visualizing AGX simulations with OpenSceneGraph.
QuatT< Real > Quat
Definition: Quat.h:24
double Real
Definition: Real.h:42