HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Camera.h
Go to the documentation of this file.
1 //
2 // Copyright Contributors to the MaterialX Project
3 // SPDX-License-Identifier: Apache-2.0
4 //
5 
6 #ifndef MATERIALX_CAMERA_H
7 #define MATERIALX_CAMERA_H
8 
10 
12 
13 /// Shared pointer to a Camera
14 using CameraPtr = std::shared_ptr<class Camera>;
15 
16 /// @class Camera
17 /// A simple camera class, supporting transform matrices and arcball
18 /// functionality for object-viewing applications.
20 {
21  public:
22  Camera() :
23  _worldMatrix(Matrix44::IDENTITY),
24  _viewMatrix(Matrix44::IDENTITY),
25  _projectionMatrix(Matrix44::IDENTITY),
26  _arcballActive(false),
27  _arcballQuat(Quaternion::IDENTITY),
28  _arcballDelta(Quaternion::IDENTITY),
29  _arcballSpeed(2.0f)
30  {
31  }
32  ~Camera() { }
33 
34  /// Create a new camera.
35  static CameraPtr create() { return std::make_shared<Camera>(); }
36 
37  /// @name Transform Matrices
38  /// @{
39 
40  /// Set the world matrix.
41  void setWorldMatrix(const Matrix44& mat)
42  {
43  _worldMatrix = mat;
44  }
45 
46  /// Return the world matrix.
47  const Matrix44& getWorldMatrix() const
48  {
49  return _worldMatrix;
50  }
51 
52  /// Set the view matrix.
53  void setViewMatrix(const Matrix44& mat)
54  {
55  _viewMatrix = mat;
56  }
57 
58  /// Return the view matrix.
59  const Matrix44& getViewMatrix() const
60  {
61  return _viewMatrix;
62  }
63 
64  /// Set the projection matrix.
65  void setProjectionMatrix(const Matrix44& mat)
66  {
67  _projectionMatrix = mat;
68  }
69 
70  /// Return the projection matrix.
72  {
73  return _projectionMatrix;
74  }
75 
76  /// Compute our full model-view-projection matrix.
78  {
79  return _worldMatrix * _viewMatrix * _projectionMatrix;
80  }
81 
82  /// Derive viewer position from the view matrix.
84  {
85  Matrix44 invView = _viewMatrix.getInverse();
86  return Vector3(invView[3][0], invView[3][1], invView[3][2]);
87  }
88 
89  /// Derive viewer direction from the view matrix.
91  {
92  Matrix44 invView = _viewMatrix.getInverse();
93  return Vector3(invView[2][0], invView[2][1], invView[2][2]);
94  }
95 
96  /// @}
97  /// @name Viewport
98  /// @{
99 
100  /// Set the size of the viewport window.
102  {
103  _viewportSize = size;
104  }
105 
106  /// Return the size of the viewport window.
107  const Vector2& getViewportSize() const
108  {
109  return _viewportSize;
110  }
111 
112  /// Project a position from object to viewport space.
114  {
115  v = transformPointPerspective(getWorldViewProjMatrix(), v);
116  v = v * 0.5f + Vector3(0.5f);
117  v[0] *= _viewportSize[0];
118  v[1] *= _viewportSize[1];
119  return v;
120  }
121 
122  /// Unproject a position from viewport to object space.
124  {
125  v[0] /= _viewportSize[0];
126  v[1] /= _viewportSize[1];
127  v = v * 2.0f - Vector3(1.0f);
128  v = transformPointPerspective(getWorldViewProjMatrix().getInverse(), v);
129  return v;
130  }
131 
132  /// @}
133  /// @name Arcball
134  /// @{
135 
136  /// Indicates a button state change, with pos being the instantaneous location of the mouse.
137  void arcballButtonEvent(const Vector2& pos, bool pressed);
138 
139  /// Apply mouse motion to the arcball state.
140  bool applyArcballMotion(const Vector2& pos);
141 
142  /// Return the arcball matrix.
144  {
145  return (_arcballDelta * _arcballQuat).toMatrix();
146  }
147 
148  /// @}
149  /// @name Utilities
150  /// @{
151 
152  /// Create a view matrix given an eye position, a target position and an up vector.
153  static Matrix44 createViewMatrix(const Vector3& eye,
154  const Vector3& target,
155  const Vector3& up);
156 
157  /// Create a perpective projection matrix given a set of clip planes with [-1,1] projected Z.
158  static Matrix44 createPerspectiveMatrix(float left, float right,
159  float bottom, float top,
160  float nearP, float farP);
161 
162  /// Create an orthographic projection matrix given a set of clip planes with [-1,1] projected Z.
163  static Matrix44 createOrthographicMatrix(float left, float right,
164  float bottom, float top,
165  float nearP, float farP);
166 
167  /// Create a perpective projection matrix given a set of clip planes with [0,1] projected Z.
168  static Matrix44 createPerspectiveMatrixZP(float left, float right,
169  float bottom, float top,
170  float nearP, float farP);
171 
172  /// Create an orthographic projection matrix given a set of clip planes with [0,1] projected Z.
173  static Matrix44 createOrthographicMatrixZP(float left, float right,
174  float bottom, float top,
175  float nearP, float farP);
176 
177  /// Apply a perspective transform to the given 3D point, performing a
178  /// homogeneous divide on the transformed result.
180  {
181  Vector4 res = m.multiply(Vector4(v[0], v[1], v[2], 1.0f));
182  return Vector3(res[0], res[1], res[2]) / res[3];
183  }
184 
185  /// @}
186 
187  protected:
188  // Transform matrices
192 
193  // Viewport size
195 
196  // Arcball properties
202 };
203 
205 
206 #endif
const Matrix44 & getProjectionMatrix() const
Return the projection matrix.
Definition: Camera.h:71
std::shared_ptr< class Camera > CameraPtr
Shared pointer to a Camera.
Definition: Camera.h:14
float _arcballSpeed
Definition: Camera.h:201
static IMATH_HOSTDEVICE void multiply(const Matrix44 &a, const Matrix44 &b, Matrix44 &c) IMATH_NOEXCEPT
Matrix-matrix multiplication: compute c = a * b.
Definition: ImathMatrix.h:3650
#define MATERIALX_NAMESPACE_BEGIN
Definition: Generated.h:25
Quaternion _arcballDelta
Definition: Camera.h:200
GLint left
Definition: glcorearb.h:2005
const GLdouble * v
Definition: glcorearb.h:837
GLdouble right
Definition: glad.h:2817
Matrix44 _projectionMatrix
Definition: Camera.h:191
Definition: Camera.h:19
bool _arcballActive
Definition: Camera.h:197
const Matrix44 & getWorldMatrix() const
Return the world matrix.
Definition: Camera.h:47
void setProjectionMatrix(const Matrix44 &mat)
Set the projection matrix.
Definition: Camera.h:65
static Vector3 transformPointPerspective(const Matrix44 &m, const Vector3 &v)
Definition: Camera.h:179
GLfloat f
Definition: glcorearb.h:1926
Vector2 _arcballLastPos
Definition: Camera.h:198
Matrix44 getInverse() const
Return the inverse of the matrix.
Definition: Types.h:685
Camera()
Definition: Camera.h:22
Definition: Types.h:327
Matrix44 getWorldViewProjMatrix() const
Compute our full model-view-projection matrix.
Definition: Camera.h:77
GLenum target
Definition: glcorearb.h:1667
Definition: Types.h:285
void setViewportSize(const Vector2 &size)
Set the size of the viewport window.
Definition: Camera.h:101
Matrix44 arcballMatrix() const
Return the arcball matrix.
Definition: Camera.h:143
GLint GLint bottom
Definition: glcorearb.h:2005
GLsizeiptr size
Definition: glcorearb.h:664
#define MX_RENDER_API
Definition: Export.h:18
void setWorldMatrix(const Matrix44 &mat)
Set the world matrix.
Definition: Camera.h:41
Vector3 unprojectFromViewport(Vector3 v)
Unproject a position from viewport to object space.
Definition: Camera.h:123
GA_API const UT_StringHolder up
Definition: Types.h:305
Matrix44 _viewMatrix
Definition: Camera.h:190
~Camera()
Definition: Camera.h:32
Vector3 getViewPosition() const
Derive viewer position from the view matrix.
Definition: Camera.h:83
Quaternion _arcballQuat
Definition: Camera.h:199
Vector2 _viewportSize
Definition: Camera.h:194
Vector3 getViewDirection() const
Derive viewer direction from the view matrix.
Definition: Camera.h:90
GLdouble GLdouble GLdouble top
Definition: glad.h:2817
#define MATERIALX_NAMESPACE_END
Definition: Generated.h:26
void setViewMatrix(const Matrix44 &mat)
Set the view matrix.
Definition: Camera.h:53
const Vector2 & getViewportSize() const
Return the size of the viewport window.
Definition: Camera.h:107
Matrix44 _worldMatrix
Definition: Camera.h:189
const Matrix44 & getViewMatrix() const
Return the view matrix.
Definition: Camera.h:59
Vector3 projectToViewport(Vector3 v)
Project a position from object to viewport space.
Definition: Camera.h:113
static CameraPtr create()
Create a new camera.
Definition: Camera.h:35