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 // TM & (c) 2021 Lucasfilm Entertainment Company Ltd. and Lucasfilm Ltd.
3 // All rights reserved. See LICENSE.txt for license.
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.
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.
163  static Matrix44 createOrthographicMatrix(float left, float right,
164  float bottom, float top,
165  float nearP, float farP);
166 
167  /// Apply a perspective transform to the given 3D point, performing a
168  /// homogeneous divide on the transformed result.
170  {
171  Vector4 res = m.multiply(Vector4(v[0], v[1], v[2], 1.0f));
172  return Vector3(res[0], res[1], res[2]) / res[3];
173  }
174 
175  /// @}
176 
177  protected:
178  // Transform matrices
182 
183  // Viewport size
185 
186  // Arcball properties
192 };
193 
195 
196 #endif
GLfloat GLfloat GLfloat top
Definition: glew.h:15525
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:191
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:23
Quaternion _arcballDelta
Definition: Camera.h:190
GLint left
Definition: glcorearb.h:2005
Matrix44 _projectionMatrix
Definition: Camera.h:181
Definition: Camera.h:19
bool _arcballActive
Definition: Camera.h:187
GLfloat right
Definition: glew.h:15525
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
GLenum target
Definition: glcorearb.h:1667
GLsizeiptr size
Definition: glcorearb.h:664
M getInverse() const
Return the inverse of the matrix.
Definition: Types.h:543
static Vector3 transformPointPerspective(const Matrix44 &m, const Vector3 &v)
Definition: Camera.h:169
Vector2 _arcballLastPos
Definition: Camera.h:188
const GLdouble * v
Definition: glcorearb.h:837
GLint GLint bottom
Definition: glcorearb.h:2005
Camera()
Definition: Camera.h:22
Definition: Types.h:324
Matrix44 getWorldViewProjMatrix() const
Compute our full model-view-projection matrix.
Definition: Camera.h:77
Definition: Types.h:282
void setViewportSize(const Vector2 &size)
Set the size of the viewport window.
Definition: Camera.h:101
GLuint res
Definition: glew.h:11549
Matrix44 arcballMatrix() const
Return the arcball matrix.
Definition: Camera.h:143
#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:302
Matrix44 _viewMatrix
Definition: Camera.h:180
~Camera()
Definition: Camera.h:32
Vector3 getViewPosition() const
Derive viewer position from the view matrix.
Definition: Camera.h:83
const GLdouble * m
Definition: glew.h:9166
Quaternion _arcballQuat
Definition: Camera.h:189
GLfloat f
Definition: glcorearb.h:1926
Vector2 _viewportSize
Definition: Camera.h:184
Vector3 getViewDirection() const
Derive viewer direction from the view matrix.
Definition: Camera.h:90
#define MATERIALX_NAMESPACE_END
Definition: Generated.h:24
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:179
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