Blobalizer
Loading...
Searching...
No Matches
ArcBall.h
Go to the documentation of this file.
1#ifndef Magnum_Examples_ArcBall_h
2#define Magnum_Examples_ArcBall_h
3/*
4 This file is part of Magnum.
5
6 Original authors — credit is appreciated but not required:
7
8 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019,
9 2020, 2021, 2022 — Vladimír Vondruš <mosra@centrum.cz>
10 2020 — Nghia Truong <nghiatruong.vn@gmail.com>
11
12 This is free and unencumbered software released into the public domain.
13
14 Anyone is free to copy, modify, publish, use, compile, sell, or distribute
15 this software, either in source code form or as a compiled binary, for any
16 purpose, commercial or non-commercial, and by any means.
17
18 In jurisdictions that recognize copyright laws, the author or authors of
19 this software dedicate any and all copyright interest in the software to
20 the public domain. We make this dedication for the benefit of the public
21 at large and to the detriment of our heirs and successors. We intend this
22 dedication to be an overt act of relinquishment in perpetuity of all
23 present and future rights to this software under copyright law.
24
25 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
28 THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
29 IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 */
32
33#include <Magnum/Magnum.h>
34#include <Magnum/Math/Functions.h>
35#include <Magnum/Math/DualQuaternion.h>
36#include <Magnum/Math/Vector2.h>
37#include <Magnum/Math/Vector3.h>
38
39using namespace Magnum;
40
43class ArcBall {
44 public:
51 ArcBall(const Vector3& cameraPosition, const Vector3& viewCenter,
52 const Vector3& upDir, Deg fov, const Vector2i& windowSize);
53
58 void setViewParameters(const Vector3 &eye, const Vector3 &viewCenter,
59 const Vector3 &upDir);
60
62 void reset();
63
66 void reshape(const Vector2i& windowSize) { _windowSize = windowSize; }
67
71
74 Float lagging() const { return _lagging; }
75
77 void setLagging(Float lagging);
78
81 void initTransformation(const Vector2i &mousePos);
82
85 void rotate(const Vector2i &mousePos);
86
89 void translate(const Vector2i &mousePos);
90
93 void translateDelta(const Vector2 &translationNDC);
94
97 void zoom(Float delta);
98
101 Deg fov() const { return _fov; }
102
105 const DualQuaternion& view() const { return _view; }
106
109 Matrix4 viewMatrix() const { return _view.toMatrix(); }
110
113 Matrix4 inverseViewMatrix() const {
114 return _inverseView.toMatrix();
115 }
116
119 const DualQuaternion& transformation() const { return _inverseView; }
120
123 Matrix4 transformationMatrix() const { return _inverseView.toMatrix(); }
124
127 Float viewDistance() const { return Math::abs(_targetZooming); }
128
129 protected:
132
137 Vector2 screenCoordToNDC(const Vector2i &mousePos) const;
138
139 Deg _fov;
140 Vector2i _windowSize;
141
143 Float _lagging{};
144
148 DualQuaternion _view, _inverseView;
149};
150
151#endif
Implementation of Ken Shoemake's arcball camera with smooth navigation.
Definition ArcBall.h:43
Quaternion _qRotationT0
Definition ArcBall.h:146
Vector3 _positionT0
Definition ArcBall.h:145
void reset()
Reset the camera to its initial position, view center, and up dir.
Definition ArcBall.cpp:85
Vector2 _prevMousePosNDC
Definition ArcBall.h:142
Quaternion _currentQRotation
Definition ArcBall.h:146
Float _zoomingT0
Definition ArcBall.h:147
Quaternion _targetQRotation
Definition ArcBall.h:146
Matrix4 viewMatrix() const
Get the camera's view transformation as a matrix.
Definition ArcBall.h:109
Vector3 _targetPosition
Definition ArcBall.h:145
Matrix4 transformationMatrix() const
Get the camera's transformation matrix.
Definition ArcBall.h:123
Float _lagging
Definition ArcBall.h:143
void updateInternalTransformations()
Update the camera transformations-.
Definition ArcBall.cpp:171
Float _targetZooming
Definition ArcBall.h:147
void rotate(const Vector2i &mousePos)
Rotate the camera from the previous (screen) mouse position to the current(screen) position.
Definition ArcBall.cpp:100
void setLagging(Float lagging)
Set the amount of lagging such that the camera will (slowly) smoothly navigate. Lagging must be in [0...
Definition ArcBall.cpp:91
Vector2 screenCoordToNDC(const Vector2i &mousePos) const
Transform from screen coordinate to NDC - normalized device coordinate. The top - left of the screen ...
Definition ArcBall.cpp:178
void zoom(Float delta)
Zoom the camera.
Definition ArcBall.cpp:126
Vector3 _currentPosition
Definition ArcBall.h:145
Float viewDistance() const
Return the distance from the camera position to the center view.
Definition ArcBall.h:127
Float lagging() const
Get the amount of lagging such that the camera will (slowly) smoothly navigate.
Definition ArcBall.h:74
Matrix4 inverseViewMatrix() const
Get the camera's inverse view matrix (which also produces transformation of the camera)
Definition ArcBall.h:113
const DualQuaternion & transformation() const
Get the camera's transformation as a dual quaternion.
Definition ArcBall.h:119
void initTransformation(const Vector2i &mousePos)
Initialize the first (screen) mouse position for camera transformation. This should be called from th...
Definition ArcBall.cpp:96
void reshape(const Vector2i &windowSize)
Update screen size after the window has been resized.
Definition ArcBall.h:66
DualQuaternion _inverseView
Definition ArcBall.h:148
Deg _fov
Definition ArcBall.h:139
void translateDelta(const Vector2 &translationNDC)
Translate the camera by the delta amount of (NDC) mouse position. Note that NDC position must be in[-...
Definition ArcBall.cpp:116
const DualQuaternion & view() const
Get the camera's view transformation as a dual quaternion.
Definition ArcBall.h:105
void translate(const Vector2i &mousePos)
Translate the camera from the previous (screen) mouse position to the current(screen) mouse position.
Definition ArcBall.cpp:109
Vector2i _windowSize
Definition ArcBall.h:140
Deg fov() const
Field of view.
Definition ArcBall.h:101
void setViewParameters(const Vector3 &eye, const Vector3 &viewCenter, const Vector3 &upDir)
Set the camera view parameters: eye position, view center, up direction.
Definition ArcBall.cpp:64
Float _currentZooming
Definition ArcBall.h:147
DualQuaternion _view
Definition ArcBall.h:148
bool updateTransformation()
Update any unfinished transformation due to lagging.
Definition ArcBall.cpp:133