MaterialX 1.39.1
Loading...
Searching...
No Matches
Camera.h
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
11MATERIALX_NAMESPACE_BEGIN
12
14using CameraPtr = std::shared_ptr<class Camera>;
15
19class MX_RENDER_API Camera
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
35 static CameraPtr create() { return std::make_shared<Camera>(); }
36
39
41 void setWorldMatrix(const Matrix44& mat)
42 {
43 _worldMatrix = mat;
44 }
45
47 const Matrix44& getWorldMatrix() const
48 {
49 return _worldMatrix;
50 }
51
53 void setViewMatrix(const Matrix44& mat)
54 {
55 _viewMatrix = mat;
56 }
57
59 const Matrix44& getViewMatrix() const
60 {
61 return _viewMatrix;
62 }
63
66 {
67 _projectionMatrix = mat;
68 }
69
72 {
73 return _projectionMatrix;
74 }
75
78 {
79 return _worldMatrix * _viewMatrix * _projectionMatrix;
80 }
81
84 {
85 Matrix44 invView = _viewMatrix.getInverse();
86 return Vector3(invView[3][0], invView[3][1], invView[3][2]);
87 }
88
91 {
92 Matrix44 invView = _viewMatrix.getInverse();
93 return Vector3(invView[2][0], invView[2][1], invView[2][2]);
94 }
95
99
101 void setViewportSize(const Vector2& size)
102 {
103 _viewportSize = size;
104 }
105
108 {
109 return _viewportSize;
110 }
111
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
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
135
137 void arcballButtonEvent(const Vector2& pos, bool pressed);
138
140 bool applyArcballMotion(const Vector2& pos);
141
144 {
145 return (_arcballDelta * _arcballQuat).toMatrix();
146 }
147
151
154 const Vector3& target,
155 const Vector3& up);
156
158 static Matrix44 createPerspectiveMatrix(float left, float right,
159 float bottom, float top,
160 float nearP, float farP);
161
163 static Matrix44 createOrthographicMatrix(float left, float right,
164 float bottom, float top,
165 float nearP, float farP);
166
168 static Matrix44 createPerspectiveMatrixZP(float left, float right,
169 float bottom, float top,
170 float nearP, float farP);
171
173 static Matrix44 createOrthographicMatrixZP(float left, float right,
174 float bottom, float top,
175 float nearP, float farP);
176
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
186
187 protected:
188 // Transform matrices
189 Matrix44 _worldMatrix;
190 Matrix44 _viewMatrix;
191 Matrix44 _projectionMatrix;
192
193 // Viewport size
194 Vector2 _viewportSize;
195
196 // Arcball properties
197 bool _arcballActive;
198 Vector2 _arcballLastPos;
199 Quaternion _arcballQuat;
200 Quaternion _arcballDelta;
201 float _arcballSpeed;
202};
203
204MATERIALX_NAMESPACE_END
205
206#endif
Data types for rendering functionality.
A simple camera class, supporting transform matrices and arcball functionality for object-viewing app...
Definition: Camera.h:20
void setProjectionMatrix(const Matrix44 &mat)
Set the projection matrix.
Definition: Camera.h:65
Vector3 getViewPosition() const
Derive viewer position from the view matrix.
Definition: Camera.h:83
const Matrix44 & getWorldMatrix() const
Return the world matrix.
Definition: Camera.h:47
void setViewMatrix(const Matrix44 &mat)
Set the view matrix.
Definition: Camera.h:53
static Matrix44 createOrthographicMatrixZP(float left, float right, float bottom, float top, float nearP, float farP)
Create an orthographic projection matrix given a set of clip planes with [0,1] projected Z.
Vector3 unprojectFromViewport(Vector3 v)
Unproject a position from viewport to object space.
Definition: Camera.h:123
static Matrix44 createPerspectiveMatrix(float left, float right, float bottom, float top, float nearP, float farP)
Create a perpective projection matrix given a set of clip planes with [-1,1] projected Z.
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 Matrix44 createPerspectiveMatrixZP(float left, float right, float bottom, float top, float nearP, float farP)
Create a perpective projection matrix given a set of clip planes with [0,1] projected Z.
static Vector3 transformPointPerspective(const Matrix44 &m, const Vector3 &v)
Apply a perspective transform to the given 3D point, performing a homogeneous divide on the transform...
Definition: Camera.h:179
Vector3 getViewDirection() const
Derive viewer direction from the view matrix.
Definition: Camera.h:90
void setViewportSize(const Vector2 &size)
Set the size of the viewport window.
Definition: Camera.h:101
void arcballButtonEvent(const Vector2 &pos, bool pressed)
Indicates a button state change, with pos being the instantaneous location of the mouse.
static Matrix44 createViewMatrix(const Vector3 &eye, const Vector3 &target, const Vector3 &up)
Create a view matrix given an eye position, a target position and an up vector.
bool applyArcballMotion(const Vector2 &pos)
Apply mouse motion to the arcball state.
Matrix44 getWorldViewProjMatrix() const
Compute our full model-view-projection matrix.
Definition: Camera.h:77
const Matrix44 & getProjectionMatrix() const
Return the projection matrix.
Definition: Camera.h:71
const Vector2 & getViewportSize() const
Return the size of the viewport window.
Definition: Camera.h:107
void setWorldMatrix(const Matrix44 &mat)
Set the world matrix.
Definition: Camera.h:41
static Matrix44 createOrthographicMatrix(float left, float right, float bottom, float top, float nearP, float farP)
Create an orthographic projection matrix given a set of clip planes with [-1,1] projected Z.
static CameraPtr create()
Create a new camera.
Definition: Camera.h:35
Matrix44 arcballMatrix() const
Return the arcball matrix.
Definition: Camera.h:143
A 4x4 matrix of floating-point values.
Definition: Types.h:656
Vector4 multiply(const Vector4 &v) const
Return the product of this matrix and a 4D vector.
Matrix44 getInverse() const
Return the inverse of the matrix.
Definition: Types.h:685
A quaternion vector.
Definition: Types.h:21
A vector of two floating-point values.
Definition: Types.h:286
A vector of three floating-point values.
Definition: Types.h:306
A vector of four floating-point values.
Definition: Types.h:328