Lagrange
Camera.h
1/*
2 * Copyright 2019 Adobe. All rights reserved.
3 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License. You may obtain a copy
5 * of the License at http://www.apache.org/licenses/LICENSE-2.0
6 *
7 * Unless required by applicable law or agreed to in writing, software distributed under
8 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9 * OF ANY KIND, either express or implied. See the License for the specific language
10 * governing permissions and limitations under the License.
11 */
12#pragma once
13
14#include <lagrange/ui/types/Frustum.h>
15#include <lagrange/ui/api.h>
16
17namespace lagrange {
18namespace ui {
19
36
37class LA_UI_API Camera
38{
39public:
43 enum class Type { PERSPECTIVE, ORTHOGRAPHIC };
44
45 enum class RotationMode {
46 TUMBLE,
47 TURNTABLE,
48 ARCBALL
49 };
50
51 Camera(Type type = Type::PERSPECTIVE);
52 virtual ~Camera() = default;
53
62 static Camera default_camera(float width, float height, Type type = Type::PERSPECTIVE);
63
64 void set_type(Type type);
65 Type get_type() const { return m_type; }
66
67 void set_position(const Eigen::Vector3f& pos);
68 void set_lookat(const Eigen::Vector3f& dir);
69 void set_position_up(const Eigen::Vector3f& pos, const Eigen::Vector3f& up);
70 Eigen::Vector3f get_lookat() const { return m_lookat; }
71 void set_up(const Eigen::Vector3f& up);
72
73 Eigen::Vector3f get_position() const;
74 Eigen::Vector3f get_direction() const;
75 Eigen::Vector3f get_up() const;
76
77 float get_far_plane() const;
78 float get_near_plane() const;
79
86 void set_window_dimensions(float width, float height);
87 void set_aspect_ratio(float width, float height);
88
94 void set_fov(float fov);
95 float get_fov() const { return m_fov; }
96
97 void set_planes(float znear, float zfar);
98 float get_near() const { return m_znear; }
99 float get_far() const { return m_zfar; }
100
106 Eigen::Projective3f get_perspective() const;
107
113 Eigen::Matrix4f get_view() const;
114
120 Eigen::Matrix4f get_PV() const;
121
122 Eigen::Matrix4f get_view_inverse() const { return m_Vinv; }
123 Eigen::Projective3f get_perspective_inverse() const { return m_Pinv; }
124
125 float get_window_width() const;
126 float get_window_height() const;
127 Eigen::Vector2f get_window_size() const;
128
129
130 struct Ray
131 {
132 Eigen::Vector3f origin;
133 Eigen::Vector3f dir;
134 };
135
142 Ray cast_ray(const Eigen::Vector2f& coord) const;
143
150 Eigen::Vector2f project(const Eigen::Vector3f& pos) const;
151
158 Eigen::Vector3f project_with_depth(const Eigen::Vector3f& pos) const;
159
167 Eigen::Vector3f unproject(const Eigen::Vector2f& screen, float z = 0.0f) const;
168
178 bool get_ray_to_screen(
179 const Eigen::Vector3f& rayOrigin,
180 const Eigen::Vector3f& rayDir,
181 Eigen::Vector2f* beginOut,
182 Eigen::Vector2f* endOut) const;
183
184
185 void rotate_around_lookat(float angleRad);
186 void rotate_tumble(float yaw_delta, float pitch_delta);
187
188
199 void rotate_turntable(
200 float yaw_delta,
201 float pitch_delta,
202 Eigen::Vector3f primary_axis = Eigen::Vector3f::Zero());
203
204
205 void rotate_arcball(
206 const Eigen::Vector3f& camera_pos_start,
207 const Eigen::Vector3f& camera_up_start,
208 const Eigen::Vector2f& mouse_start,
209 const Eigen::Vector2f& mouse_current);
210
211 void zoom(float delta);
212 void dolly(float delta);
213
214
216 int get_retina_scale() const { return m_retina_scale; }
217
219 void set_retina_scale(int value) { m_retina_scale = value; }
220
221 void move_forward(float delta);
222 void move_right(float delta);
223 void move_up(float delta);
224
230 void set_ortho_viewport(Eigen::Vector4f viewport);
231
232 Eigen::Vector4f get_ortho_viewport() const;
233
234 void set_rotation_mode(RotationMode mode) { m_rotation_mode = mode; }
235 RotationMode get_rotation_mode() const { return m_rotation_mode; }
236
241 {
242 Eigen::Vector2f scale = Eigen::Vector2f::Ones();
243 Eigen::Vector2f translate = Eigen::Vector2f::Zero();
244
251 bool clip = false;
252 };
253
260 Camera transformed(const ViewportTransform& vt) const;
261
269 Eigen::Vector2f inverse_viewport_transform(const ViewportTransform& vt, Eigen::Vector2f& pixel)
270 const;
271
275 bool is_pixel_in(const Eigen::Vector2f& p) const;
276
280 bool intersects_region(const Eigen::Vector2f& begin, const Eigen::Vector2f& end) const;
281
282 Eigen::Vector2f get_window_origin() const { return {m_window_origin_x, m_window_origin_y}; }
283 void set_window_origin(float x, float y)
284 {
285 m_window_origin_x = x;
286 m_window_origin_y = y;
287 }
288
290 enum class Dir { TOP, BOTTOM, LEFT, RIGHT, FRONT, BACK };
291
295 bool is_orthogonal_direction(Dir dir) const;
296
300 void set_orthogonal_direction(Dir dir);
301
303 std::pair<Eigen::Vector3f, Eigen::Vector3f> get_orthogonal_direction(Dir dir) const;
304
310 Frustum get_frustum() const;
311
317 Frustum get_frustum(Eigen::Vector2f min, Eigen::Vector2f max) const;
318
319protected:
320 void update_view();
321 virtual void update_perspective();
322
323 void _changed();
324
325 Eigen::Projective3f m_P;
326 Eigen::Matrix4f m_V = Eigen::Matrix4f::Identity();
327 Eigen::Projective3f m_Pinv;
328 Eigen::Matrix4f m_Vinv = Eigen::Matrix4f::Identity();
329
330 Eigen::Vector3f m_pos = Eigen::Vector3f(1, 0, 0);
331 Eigen::Vector3f m_up = Eigen::Vector3f(0, 1, 0);
332 Eigen::Vector3f m_lookat = Eigen::Vector3f::Zero();
333
334 float m_aspectRatio = 1.0f;
335 float m_fov = 3.14f / 4.0f; // in radians
336 float m_znear = 0.0125f;
337 float m_zfar = 128.0f;
338
339 float m_windowWidth = 1.0f;
340 float m_windowHeight = 1.0f;
341 int m_retina_scale = 1;
342 Type m_type = Type::PERSPECTIVE;
343
344 float m_window_origin_x = 0.0f;
345 float m_window_origin_y = 0.0f;
346
347 Eigen::Vector4f m_ortho_viewport = Eigen::Vector4f(0.0f, 1.0f, 1.0f, 0.0f);
348
349 RotationMode m_rotation_mode = RotationMode::TUMBLE;
350};
351} // namespace ui
352} // namespace lagrange
Camera class.
Definition: Camera.h:38
Type
Camera Mode.
Definition: Camera.h:43
RotationMode
Definition: Camera.h:45
Dir
Orthogonal view directions, preserve distance from pos to lookat.
Definition: Camera.h:290
void set_retina_scale(int value)
Definition: Camera.h:219
int get_retina_scale() const
Definition: Camera.h:216
Frustum defined using 6 planes.
Definition: Frustum.h:43
Lagrange UI Viewer and mini 3D engine.
Definition: AcceleratedPicking.h:22
LA_UI_API const WindowSize & get_window_size(const Registry &r)
Returns global window size.
Definition: uipanel.cpp:151
Main namespace for Lagrange.
Definition: AABBIGL.h:30
Definition: Camera.h:131
Transform in the normalized coordinate space.
Definition: Camera.h:241