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
33template<typename Scalar>
34class Camera
35{
36public:
37 using ScalarType = Scalar;
40
41 enum class ProjectionMode { ORTHO, PERSPECTIVE };
42
43private:
44 /* Extrinsics */
45
47 PointType mCenter = PointType(0.0f, 0.0f, 0.0f);
48
50 PointType mEye = PointType(0.0f, 0.0f, 1.0f);
51
53 PointType mUp = PointType(0.0f, 1.0f, 0.0f);
54
55 /* Intrinsics */
56
58 Scalar mFovDeg = 54.0;
59
61 ProjectionMode mProjectionMode = ProjectionMode::PERSPECTIVE;
62
66 // |\___
67 // | \___
68 // h/2 | \___
69 // | \__
70 // | \__
71 //--- target ---------------X- eye --
72 // | __/
73 // | __/
74 // h/2 | __/
75 // | __/
76 // | __/
77 // | /
78 Scalar mVerticalHeight = 2.0;
79
81 Scalar mAspect = 1.0;
82
84 Scalar mNear = 0.1;
85
87 Scalar mFar = 500.0;
88
89public:
90 Camera() { setFieldOfViewAdaptingEyeDistance(mFovDeg); };
91
92 void reset() { *this = {}; }
93
94 PointType& center() { return mCenter; }
95
96 const PointType& center() const { return mCenter; }
97
98 PointType& eye() { return mEye; }
99
100 const PointType& eye() const { return mEye; }
101
102 PointType& up() { return mUp; }
103
104 const PointType& up() const { return mUp; }
105
106 Scalar& fieldOfView() { return mFovDeg; }
107
108 const Scalar& fieldOfView() const { return mFovDeg; }
109
110 void setFieldOfViewAdaptingEyeDistance(const Scalar& fovDeg)
111 {
112 mFovDeg = fovDeg;
113 PointType targetToEye = (mEye - mCenter).normalized();
114 mEye =
115 mCenter + targetToEye * ((mVerticalHeight / 2.0) /
116 std::tan((fovDeg / 2.0) / 180.0 * M_PI));
117 }
118
119 ProjectionMode& projectionMode() { return mProjectionMode; }
120
121 ProjectionMode projectionMode() const { return mProjectionMode; }
122
123 Scalar& verticalHeight() { return mVerticalHeight; }
124
125 const Scalar& verticalHeight() const { return mVerticalHeight; }
126
127 Scalar& aspectRatio() { return mAspect; }
128
129 const Scalar& aspectRatio() const { return mAspect; }
130
131 Scalar& nearPlane() { return mNear; }
132
133 const Scalar& nearPlane() const { return mNear; }
134
135 Scalar& farPlane() { return mFar; }
136
137 const Scalar& farPlane() const { return mFar; }
138
139 MatrixType viewMatrix() const
140 {
141 return lookAtMatrix<MatrixType>(mEye, mCenter, mUp);
142 }
143
144 MatrixType projectionMatrix() const
145 {
146 switch (mProjectionMode) {
147 case ProjectionMode::ORTHO: {
148 const Scalar h = mVerticalHeight / 2.0;
149 const Scalar w = h * mAspect;
150 return orthoProjectionMatrix<MatrixType>(
151 -w, w, h, -h, mNear, mFar, false);
152 }
153 case ProjectionMode::PERSPECTIVE: {
155 mFovDeg, mAspect, mNear, mFar, false);
156 }
157 default: assert(false); return MatrixType::Identity();
158 }
159 }
160};
161
162} // namespace vcl
163
164#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
A Pinhole camera model.
Definition camera.h:35
Scalar mNear
Z position of the mNear plane.
Definition camera.h:84
Scalar mVerticalHeight
Height of the target in world space (used for ortho projection, and adapting the eye distance for per...
Definition camera.h:78
Scalar mFovDeg
Field of view in degrees.
Definition camera.h:58
ProjectionMode mProjectionMode
Projection mode.
Definition camera.h:61
PointType mCenter
Position where the camera is looking at (i.e. target point)
Definition camera.h:47
Scalar mFar
Z position of the mFar plane.
Definition camera.h:87
PointType mUp
Up direction of the camera.
Definition camera.h:53
Scalar mAspect
Aspect ratio.
Definition camera.h:81
PointType mEye
Position of (eye of) the camera.
Definition camera.h:50