Blobalizer
Loading...
Searching...
No Matches
ArcBallCamera.h
Go to the documentation of this file.
1#ifndef Magnum_Examples_ArcBallCamera_h
2#define Magnum_Examples_ArcBallCamera_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/SceneGraph/AbstractTranslationRotation3D.h>
34#include <Magnum/SceneGraph/Camera.h>
35#include <Magnum/SceneGraph/Object.h>
36#include <Magnum/SceneGraph/Scene.h>
37
38#include "ArcBall.h"
39
41class ArcBallCamera : public ArcBall {
42public:
52 template <class Transformation>
54 SceneGraph::Scene<Transformation> &scene,
55 const Vector3 &cameraPosition, const Vector3 &viewCenter,
56 const Vector3 &upDir, Deg fov, const Vector2i &windowSize,
57 const Vector2i &viewportSize)
58 : ArcBall{cameraPosition, viewCenter, upDir, fov, windowSize} {
59 /* Create a camera object of a concrete type */
60 auto *cameraObject = new SceneGraph::Object<Transformation>{&scene};
61 (*(_camera = new SceneGraph::Camera3D{*cameraObject}))
62 .setAspectRatioPolicy(SceneGraph::AspectRatioPolicy::Extend)
63 .setProjectionMatrix(Matrix4::perspectiveProjection(
64 fov, Vector2{windowSize}.aspectRatio(), 0.01f, 1000.0f))
65 .setViewport(viewportSize);
66
67 /* Save the abstract transformation interface and initialize the
68 camera position through that */
69 (*(_cameraObject = cameraObject))
70 .rotate(transformation().rotation())
71 .translate(transformation().translation());
72 }
73
77 void reshape(const Vector2i &windowSize, const Vector2i &viewportSize) {
78 _windowSize = windowSize;
79 _camera->setViewport(viewportSize);
80 }
81
84 bool update() {
85 /* call the internal update */
87 return false;
88
89 (*_cameraObject)
90 .resetTransformation()
91 .rotate(transformation().rotation())
92 .translate(transformation().translation());
93 return true;
94 }
95
98 void draw(SceneGraph::DrawableGroup3D &drawables) {
99 _camera->draw(drawables);
100 }
101
104 SceneGraph::Camera3D &camera() const {
105 return *_camera;
106 }
107
108private:
109 SceneGraph::AbstractTranslationRotation3D *_cameraObject{};
110 SceneGraph::Camera3D *_camera{};
111};
112
113#endif
Arcball camera implementation integrated into the SceneGraph.
Definition ArcBallCamera.h:41
void draw(SceneGraph::DrawableGroup3D &drawables)
Draw objects using the internal scene graph camera.
Definition ArcBallCamera.h:98
void reshape(const Vector2i &windowSize, const Vector2i &viewportSize)
Update screen and viewport size after the window has been resized.
Definition ArcBallCamera.h:77
ArcBallCamera(SceneGraph::Scene< Transformation > &scene, const Vector3 &cameraPosition, const Vector3 &viewCenter, const Vector3 &upDir, Deg fov, const Vector2i &windowSize, const Vector2i &viewportSize)
Creates a new arc ball camera.
Definition ArcBallCamera.h:53
bool update()
Update the SceneGraph camera if arc ball has been changed.
Definition ArcBallCamera.h:84
SceneGraph::Camera3D & camera() const
Accessor to the raw camera object.
Definition ArcBallCamera.h:104
SceneGraph::AbstractTranslationRotation3D * _cameraObject
Definition ArcBallCamera.h:109
SceneGraph::Camera3D * _camera
Definition ArcBallCamera.h:110
Implementation of Ken Shoemake's arcball camera with smooth navigation.
Definition ArcBall.h:43
const DualQuaternion & transformation() const
Get the camera's transformation as a dual quaternion.
Definition ArcBall.h:119
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
bool updateTransformation()
Update any unfinished transformation due to lagging.
Definition ArcBall.cpp:133