Visual Computing Library  devel
Loading...
Searching...
No Matches
camera.h
1/*****************************************************************************
2 * VCLib *
3 * Visual Computing Library *
4 * *
5 * Copyright(C) 2021-2025 *
6 * Visual Computing Lab *
7 * ISTI - Italian National Research Council *
8 * *
9 * All rights reserved. *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the Mozilla Public License Version 2.0 as published *
13 * by the Mozilla Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * Mozilla Public License Version 2.0 *
20 * (https://www.mozilla.org/en-US/MPL/2.0/) for more details. *
21 ****************************************************************************/
22
23#ifndef VCL_RENDER_VIEWER_CAMERA_H
24#define VCL_RENDER_VIEWER_CAMERA_H
25
26#include "matrix.h"
27
28namespace vcl {
29
30template<typename Scalar>
31class Camera
32{
33public:
34 using ScalarType = Scalar;
37
38 enum class ProjectionMode { ORTHO, PERSPECTIVE };
39
40private:
41 /* Extrinsics */
42
44 PointType mCenter = PointType(0.0f, 0.0f, 0.0f);
45
47 PointType mEye = PointType(0.0f, 0.0f, 1.0f);
48
50 PointType mUp = PointType(0.0f, 1.0f, 0.0f);
51
52 /* Intrinsics */
53
55 Scalar mFovDeg = 54.0;
56
58 ProjectionMode mProjectionMode = ProjectionMode::PERSPECTIVE;
59
63 Scalar mVerticalHeight = 2.0;
64
66 Scalar mAspect = 1.0;
67
69 Scalar mNear = 0.1;
70
72 Scalar mFar = 500.0;
73
74public:
75 Camera() { setFieldOfViewAdaptingEyeDistance(mFovDeg); };
76
77 void reset() { *this = {}; }
78
79 PointType& center() { return mCenter; }
80
81 const PointType& center() const { return mCenter; }
82
83 PointType& eye() { return mEye; }
84
85 const PointType& eye() const { return mEye; }
86
87 PointType& up() { return mUp; }
88
89 const PointType& up() const { return mUp; }
90
91 Scalar& fieldOfView() { return mFovDeg; }
92
93 const Scalar& fieldOfView() const { return mFovDeg; }
94
95 void setFieldOfViewAdaptingEyeDistance(const Scalar& fov)
96 {
97 mFovDeg = fov;
98 PointType targetToEye = (mEye - mCenter).normalized();
99 mEye = mCenter + targetToEye * ((mVerticalHeight / 2.0) /
100 std::tan((fov / 2.0) / 180.0 * M_PI));
101 }
102
103 ProjectionMode& projectionMode() { return mProjectionMode; }
104
105 ProjectionMode projectionMode() const { return mProjectionMode; }
106
107 Scalar& verticalHeight() { return mVerticalHeight; }
108
109 const Scalar& verticalHeight() const { return mVerticalHeight; }
110
111 Scalar& aspectRatio() { return mAspect; }
112
113 const Scalar& aspectRatio() const { return mAspect; }
114
115 Scalar& nearPlane() { return mNear; }
116
117 const Scalar& nearPlane() const { return mNear; }
118
119 Scalar& farPlane() { return mFar; }
120
121 const Scalar& farPlane() const { return mFar; }
122
123 MatrixType viewMatrix() const
124 {
125 return lookAtMatrix<MatrixType>(mEye, mCenter, mUp);
126 }
127
128 MatrixType projectionMatrix() const
129 {
130 switch (mProjectionMode) {
131 case ProjectionMode::ORTHO: {
132 const Scalar h = mVerticalHeight / 2.0;
133 const Scalar w = h * mAspect;
134 return orthoProjectionMatrix<MatrixType>(
135 -w, w, h, -h, mNear, mFar, false);
136 }
137 case ProjectionMode::PERSPECTIVE: {
139 mFovDeg, mAspect, mNear, mFar, false);
140 }
141 default: assert(false); return MatrixType::Identity();
142 }
143 }
144};
145
146} // namespace vcl
147
148#endif // VCL_RENDER_VIEWER_CAMERA_H
A class representing a box in N-dimensional space.
Definition box.h:46
Box()
The Empty constructor of a box, initializes a null box.
Definition box.h:65
Definition camera.h:32
Scalar mNear
Z position of the mNear plane.
Definition camera.h:69
Scalar mVerticalHeight
Height of the target in world space (used for ortho projection, and adapting the eye distance for per...
Definition camera.h:63
Scalar mFovDeg
Field of view in degrees.
Definition camera.h:55
ProjectionMode mProjectionMode
Projection mode.
Definition camera.h:58
PointType mCenter
Position where the camera is looking at.
Definition camera.h:44
Scalar mFar
Z position of the mFar plane.
Definition camera.h:72
PointType mUp
Up direction of the camera.
Definition camera.h:50
Scalar mAspect
Aspect ratio.
Definition camera.h:66
PointType mEye
Position of (eye of) the camera.
Definition camera.h:47