Vulkan-Hpp
CameraManipulator.hpp
Go to the documentation of this file.
1 // Copyright(c) 2019, NVIDIA CORPORATION. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 
16 #pragma once
17 
18 #if defined( _MSC_VER )
19 # pragma warning( push )
20 # pragma warning( disable : 4127 ) // conditional expression is constant (glm)
21 #elif defined( __clang__ )
22 // no need to ignore any warnings with clang
23 #elif defined( __GNUC__ )
24 # if ( 10 <= __GNUC__ ) && ( 201703L < __cplusplus )
25 # pragma GCC diagnostic push
26 # pragma GCC diagnostic ignored "-Wvolatile"
27 # endif
28 #endif
29 
30 #include <glm/glm.hpp>
31 
32 #if defined( _MSC_VER )
33 # pragma warning( pop )
34 #elif defined( __clang__ )
35 // no need to ignore any warnings with clang
36 #elif defined( __GNUC__ )
37 # if ( 10 <= __GNUC__ ) && ( 201703L < __cplusplus )
38 # pragma GCC diagnostic pop
39 # endif
40 #endif
41 
42 #include <vulkan/vulkan.hpp>
43 
44 namespace vk
45 {
46  namespace su
47  {
49  {
50  public:
51  enum class Action
52  {
53  None,
54  Orbit,
55  Dolly,
56  Pan,
58  };
59  enum class Mode
60  {
61  Examine,
62  Fly,
63  Walk,
64  Trackball
65  };
66  enum class MouseButton
67  {
68  None,
69  Left,
70  Middle,
71  Right
72  };
73  enum class ModifierFlagBits : uint32_t
74  {
75  Shift = 1,
76  Ctrl = 2,
77  Alt = 4
78  };
80 
81  public:
83 
84  glm::vec3 const & getCameraPosition() const;
85  glm::vec3 const & getCenterPosition() const;
86  glm::mat4 const & getMatrix() const;
87  Mode getMode() const;
88  glm::ivec2 const & getMousePosition() const;
89  float getRoll() const;
90  float getSpeed() const;
91  glm::vec3 const & getUpVector() const;
92  glm::u32vec2 const & getWindowSize() const;
93  Action mouseMove( glm::ivec2 const & position, MouseButton mouseButton, ModifierFlags & modifiers );
94  void setLookat( const glm::vec3 & cameraPosition, const glm::vec3 & centerPosition, const glm::vec3 & upVector );
95  void setMode( Mode mode );
96  void setMousePosition( glm::ivec2 const & position );
97  void setRoll( float roll ); // roll in radians
98  void setSpeed( float speed );
99  void setWindowSize( glm::ivec2 const & size );
100  void wheel( int value );
101 
102  private:
103  void dolly( glm::vec2 const & delta );
104  void motion( glm::ivec2 const & position, Action action = Action::None );
105  void orbit( glm::vec2 const & delta, bool invert = false );
106  void pan( glm::vec2 const & delta );
107  double projectOntoTBSphere( const glm::vec2 & p );
108  void trackball( glm::ivec2 const & position );
109  void update();
110 
111  private:
112  glm::vec3 m_cameraPosition = glm::vec3( 10, 10, 10 );
113  glm::vec3 m_centerPosition = glm::vec3( 0, 0, 0 );
114  glm::vec3 m_upVector = glm::vec3( 0, 1, 0 );
115  float m_roll = 0; // Rotation around the Z axis in RAD
116  glm::mat4 m_matrix = glm::mat4( 1 );
117 
118  glm::u32vec2 m_windowSize = glm::u32vec2( 1, 1 );
119 
120  float m_speed = 30.0f;
121  glm::ivec2 m_mousePosition = glm::ivec2( 0, 0 );
122 
123  Mode m_mode = Mode::Examine;
124  };
125  } // namespace su
126 } // namespace vk
Action mouseMove(glm::ivec2 const &position, MouseButton mouseButton, ModifierFlags &modifiers)
void setMousePosition(glm::ivec2 const &position)
glm::mat4 const & getMatrix() const
glm::vec3 const & getCameraPosition() const
void setLookat(const glm::vec3 &cameraPosition, const glm::vec3 &centerPosition, const glm::vec3 &upVector)
glm::vec3 const & getUpVector() const
void setWindowSize(glm::ivec2 const &size)
glm::u32vec2 const & getWindowSize() const
glm::ivec2 const & getMousePosition() const
glm::vec3 const & getCenterPosition() const
Definition: vulkan.cppm:23