Easy3D 2.6.1
Loading...
Searching...
No Matches
camera.h
1/********************************************************************
2 * Copyright (C) 2015 Liangliang Nan <liangliang.nan@gmail.com>
3 * https://3d.bk.tudelft.nl/liangliang/
4 *
5 * This file is part of Easy3D. If it is useful in your research/work,
6 * I would be grateful if you show your appreciation by citing it:
7 * ------------------------------------------------------------------
8 * Liangliang Nan.
9 * Easy3D: a lightweight, easy-to-use, and efficient C++ library
10 * for processing and rendering 3D data.
11 * Journal of Open Source Software, 6(64), 3255, 2021.
12 * ------------------------------------------------------------------
13 *
14 * Easy3D is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License Version 3
16 * as published by the Free Software Foundation.
17 *
18 * Easy3D is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 ********************************************************************/
26
38
39#ifndef EASY3D_RENDERER_CAMERA_H
40#define EASY3D_RENDERER_CAMERA_H
41
42
43#include <easy3d/core/types.h>
44#include "easy3d/util/signal.h"
45
46
47namespace easy3d {
48
49
50 class Frame;
53
113 class Camera {
114 public:
122 virtual ~Camera();
123
128 Camera(const Camera &camera);
129
135 Camera &operator=(const Camera &camera);
136
147
150 public:
155 vec3 position() const;
160 vec3 upVector() const;
176
177 // -----------------------------------------------------------------
178
186 void set_from_model_view_matrix(const mat4 &mv) const;
209 void set_from_calibration(float fx, float fy, float skew, float cx, float cy,
210 const mat3 &R, const vec3 &t, bool convert = true);
211
230 void set_from_calibration(const mat34 &proj);
231
232 // -----------------------------------------------------------------
233
242 void set_projection_matrix(const mat4 &proj);
243
252 void set_modelview_matrix(const mat4 &mv);
253
254 // -----------------------------------------------------------------
255
256 public:
261 void setPosition(const vec3 &pos) const;
266 void setOrientation(const quat &q) const;
272 void setOrientation(float theta, float phi) const;
278 void setUpVector(const vec3 &up, bool noMove = true) const;
283 void setViewDirection(const vec3 &direction) const;
285
287 public:
292 void lookAt(const vec3 &target) const;
296 void showEntireScene() const;
302 void fitSphere(const vec3 &center, float radius) const;
308 void fitBoundingBox(const vec3 &min, const vec3 &max) const;
321 void fitScreenRegion(int xmin, int ymin, int xmax, int ymax) const;
322
329 void centerScene() const;
330
331 public:
336 KeyFrameInterpolator *keyframe_interpolator() const { return interpolationKfi_; }
337
342 void interpolateToLookAt(const vec3 &point);
343
348
354 void interpolateTo(const Frame* frame, float duration);
355
356 public:
365 Type type() const { return type_; }
366
376 float fieldOfView() const { return fieldOfView_; }
377
386 float horizontalFieldOfView() const {
387 return 2.0f * std::atan(std::tan(fieldOfView() / 2.0f) * aspectRatio());
388 }
389
396 float aspectRatio() const {
397 return static_cast<float>(screenWidth_) / static_cast<float>(screenHeight_);
398 }
399
406 int screenWidth() const { return screenWidth_; }
407
414 int screenHeight() const { return screenHeight_; }
415
421 float pixelGLRatio(const vec3 &position) const;
422
434 float zNearCoefficient() const { return zNearCoef_; }
435
452 float zClippingCoefficient() const { return zClippingCoef_; }
453
458 virtual float zNear() const;
463 virtual float zFar() const;
469 virtual void getOrthoWidthHeight(float &halfWidth, float &halfHeight) const;
477 void getFrustumPlanesCoefficients(float coef[6][4]) const;
478
486 void getFrustumPlanesCoefficients2(float coef[6][4]) const;
487
488 public:
498 void setFieldOfView(float fov);
499
507 void setHorizontalFieldOfView(float hfov) {
508 setFieldOfView(2.0f * std::atan(std::tan(hfov / 2.0f) / aspectRatio()));
509 }
510
514
526 void setAspectRatio(float aspect) {
527 setScreenWidthAndHeight(int(100.0f * aspect), 100);
528 }
529
534 void setScreenWidthAndHeight(int width, int height);
535
540 void setZNearCoefficient(float coef) {
541 zNearCoef_ = coef;
542 projectionMatrixIsUpToDate_ = false;
543 }
544
549 void setZClippingCoefficient(float coef) {
550 zClippingCoef_ = coef;
551 projectionMatrixIsUpToDate_ = false;
552 }
553
554
557 public:
565 float sceneRadius() const { return sceneRadius_; }
566
576 vec3 sceneCenter() const { return sceneCenter_; }
582
583 public:
588 void setSceneRadius(float radius);
593 void setSceneCenter(const vec3 &center);
594
601 void setSceneBoundingBox(const vec3 &min, const vec3 &max);
603
604 public:
609 void setPivotPoint(const vec3 &point);
615
616 public:
623 ManipulatedCameraFrame *frame() const { return frame_; }
630
631 public:
644 const mat4 &projectionMatrix() const;
649 const mat4 &modelViewMatrix() const;
655
658 public:
664 vec3 cameraCoordinatesOf(const vec3 &src) const;
670 vec3 worldCoordinatesOf(const vec3 &src) const;
672
675 public:
683 vec3 projectedCoordinatesOf(const vec3 &src, const Frame *frame = nullptr) const;
692 vec3 unprojectedCoordinatesOf(const vec3 &src, const Frame *frame = nullptr) const;
693
704 void convertClickToLine(int x, int y, vec3 &orig, vec3 &dir) const;
706
707 private:
708 void modified();
709
710 private:
711 // Frame
713
714 // Camera parameters
715 int screenWidth_, screenHeight_; // size of the window, in pixels
716 float fieldOfView_; // in radians
717 vec3 sceneCenter_;
718 float sceneRadius_; // OpenGL units
719 float zNearCoef_;
720 float zClippingCoef_;
721 float orthoCoef_;
722 Type type_; // PERSPECTIVE or ORTHOGRAPHIC
723 mat4 modelViewMatrix_; // Buffered model view matrix.
724 bool modelViewMatrixIsUpToDate_;
725 mat4 projectionMatrix_; // Buffered projection matrix.
726 bool projectionMatrixIsUpToDate_;
727
728 // Key frame interpolation
729 KeyFrameInterpolator *interpolationKfi_;
730
731 public:
734 };
735
736}
737
738#endif // EASY3D_RENDERER_CAMERA_H
void setHorizontalFieldOfView(float hfov)
Sets the horizontalFieldOfView() of the Camera (in radians).
Definition camera.h:507
void setSceneCenter(const vec3 &center)
Set the center of the scene.
void showEntireScene() const
Show the entire scene.
void setPivotPoint(const vec3 &point)
Set the pivot point of the camera. After this method is called, the Camera rotates around this point.
float zClippingCoefficient() const
Returns the coefficient used to position the near and far clipping planes.
Definition camera.h:452
Camera & operator=(const Camera &camera)
Assignment operator.
KeyFrameInterpolator * keyframe_interpolator() const
Get the keyframe interpolator.
Definition camera.h:336
void setOrientation(const quat &q) const
Set the orientation of the camera.
int screenWidth() const
Returns the width (in pixels) of the Camera screen.
Definition camera.h:406
mat4 modelViewProjectionMatrix() const
Get the model view projection matrix.
int screenHeight() const
Returns the height (in pixels) of the Camera screen.
Definition camera.h:414
Type
Enumerates the two possible types of Camera.
Definition camera.h:143
@ PERSPECTIVE
Perspective camera.
Definition camera.h:144
@ ORTHOGRAPHIC
Orthographic camera.
Definition camera.h:145
void setZNearCoefficient(float coef)
Set the zNear coefficient.
Definition camera.h:540
void setFOVToFitScene()
Set the field of view to fit the scene.
void setSceneRadius(float radius)
Set the radius of the scene.
void fitSphere(const vec3 &center, float radius) const
Move the camera so that entire sphere fits the screen.
quat orientation() const
Get the orientation of the camera.
float sceneRadius() const
Returns the radius of the scene observed by the Camera.
Definition camera.h:565
Camera(const Camera &camera)
Copy constructor.
void interpolateToLookAt(const vec3 &point)
Perform keyframe interpolation to look at a point.
vec3 projectedCoordinatesOf(const vec3 &src, const Frame *frame=nullptr) const
Returns the screen projected coordinates of a 3D point defined in the frame coordinate system....
vec3 cameraCoordinatesOf(const vec3 &src) const
Convert world coordinates to camera coordinates.
void set_modelview_matrix(const mat4 &mv)
Temporally change the modelview matrix.
float aspectRatio() const
Returns the Camera aspect ratio defined by screenWidth() / screenHeight().
Definition camera.h:396
const mat4 & projectionMatrix() const
Get the projection matrix.
void setScreenWidthAndHeight(int width, int height)
Set the screen width and height.
vec3 pivotPoint() const
Get the pivot point of the camera.
void convertClickToLine(int x, int y, vec3 &orig, vec3 &dir) const
Returns the coefficients of a 3D half-line passing through the Camera eye and pixel (x,...
void setFieldOfView(float fov)
Set the field of view of the camera.
void set_projection_matrix(const mat4 &proj)
Temporally change the projection matrix.
virtual float zFar() const
Get the far clipping plane distance.
void setAspectRatio(float aspect)
Defines the Camera aspectRatio().
Definition camera.h:526
void setOrientation(float theta, float phi) const
Set the orientation of the camera.
void setType(Type type)
Set the type of the camera.
void lookAt(const vec3 &target) const
Make the camera look at a target.
void setZClippingCoefficient(float coef)
Set the zClipping coefficient.
Definition camera.h:549
void set_from_calibration(float fx, float fy, float skew, float cx, float cy, const mat3 &R, const vec3 &t, bool convert=true)
Defines the position(), orientation() and fieldOfView() of the camera from calibrated camera intrinsi...
float zNearCoefficient() const
Returns the coefficient which is used to set zNear() when the Camera is inside the sphere defined by ...
Definition camera.h:434
void computeModelViewMatrix()
Compute the model view matrix.
void setUpVector(const vec3 &up, bool noMove=true) const
Set the up vector of the camera.
vec3 rightVector() const
Get the right vector of the camera.
float fieldOfView() const
Returns the vertical field of view of the camera (in radians).
Definition camera.h:376
void fitBoundingBox(const vec3 &min, const vec3 &max) const
Move the camera so that entire box fits the screen.
vec3 unprojectedCoordinatesOf(const vec3 &src, const Frame *frame=nullptr) const
Returns the world unprojected coordinates of a point defined in the screen coordinate system....
vec3 upVector() const
Get the up vector of the camera.
Signal frame_modified
A signal indicating the frame has been modified.
Definition camera.h:733
void setFrame(ManipulatedCameraFrame *const mcf)
Set the manipulated frame of the camera.
void interpolateToFitScene()
Perform keyframe interpolation to fit the scene.
void centerScene() const
Moves the Camera so that its sceneCenter() is projected in the center of the screen.
void fitScreenRegion(int xmin, int ymin, int xmax, int ymax) const
Moves the Camera so that the rectangular screen region fits the screen.
virtual ~Camera()
Destructor.
float pixelGLRatio(const vec3 &position) const
Get the pixel GL ratio at a position.
void setViewDirection(const vec3 &direction) const
Set the view direction of the camera.
virtual float zNear() const
Get the near clipping plane distance.
Camera()
Default constructor.
void computeProjectionMatrix()
Compute the projection matrix.
const mat4 & modelViewMatrix() const
Get the model view matrix.
void getFrustumPlanesCoefficients(float coef[6][4]) const
Get the frustum planes coefficients.
vec3 position() const
Get the position of the camera.
void set_from_model_view_matrix(const mat4 &mv) const
Sets the Camera's position() and orientation() from an OpenGL ModelView matrix.
vec3 viewDirection() const
Get the view direction of the camera.
vec3 worldCoordinatesOf(const vec3 &src) const
Convert camera coordinates to world coordinates.
void setPosition(const vec3 &pos) const
Set the position of the camera.
vec3 sceneCenter() const
Returns the position of the scene center, defined in the world coordinate system.
Definition camera.h:576
void interpolateTo(const Frame *frame, float duration)
Perform keyframe interpolation to a frame.
Type type() const
Get the type of the camera.
Definition camera.h:365
float horizontalFieldOfView() const
Returns the horizontal field of view of the camera (in radians).
Definition camera.h:386
void set_from_calibration(const mat34 &proj)
Defines the position(), orientation() and fieldOfView() of the camera from calibrated camera intrinsi...
virtual void getOrthoWidthHeight(float &halfWidth, float &halfHeight) const
Get the orthographic width and height.
float distanceToSceneCenter() const
Get the distance to the scene center.
void getFrustumPlanesCoefficients2(float coef[6][4]) const
Get the frustum planes coefficients (alternative version).
ManipulatedCameraFrame * frame() const
Returns the ManipulatedFrame attached to the Camera.
Definition camera.h:623
void setSceneBoundingBox(const vec3 &min, const vec3 &max)
Set the bounding box of the scene.
The Frame class represents a coordinate system, defined by a position and an orientation.
Definition frame.h:78
A keyframe interpolator for animation generation.
Definition key_frame_interpolator.h:104
A manipulated frame with camera-specific mouse bindings.
Definition manipulated_camera_frame.h:61
A light-weight implementation of the simple signal-slot mechanism.
Definition signal.h:54
Definition collider.cpp:182
Vec< 3, float > vec3
A 3D point/vector of float type.
Definition types.h:44
FT min()
Function returning minimum representable value for a given type.
Quat< float > quat
A quaternion of float type.
Definition types.h:85
FT max()
Function returning maximum representable value for a given type.
Mat4< float > mat4
A 4 by 4 matrix of float type.
Definition types.h:67
Mat3< float > mat3
A 3 by 3 matrix of float type.
Definition types.h:65
Mat< 3, 4, float > mat34
A 3 by 4 matrix of float type.
Definition types.h:69