Visual Computing Library
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:
35 {
36 enum Enum { ORTHO, PERSPECTIVE };
37 };
38
39private:
42
43 /* Extrinsics */
44
46 PointType mCenter = PointType(0.0f, 0.0f, 0.0f);
47
49 PointType mEye = PointType(0.0f, 0.0f, 1.0f);
50
52 PointType mUp = PointType(0.0f, 1.0f, 0.0f);
53
54 /* Intrinsics */
55
57 Scalar mFovDeg = 54.0;
58
60 ProjectionMode::Enum mProjectionMode = ProjectionMode::PERSPECTIVE;
61
65 Scalar mVerticalHeight = 2.0;
66
68 Scalar mAspect = 1.0;
69
71 Scalar mNear = 0.1;
72
74 Scalar mFar = 500.0;
75
76public:
77 Camera() { setFieldOfViewAdaptingEyeDistance(mFovDeg); };
78
79 void reset() { *this = {}; }
80
81 PointType& center() { return mCenter; }
82
83 const PointType& center() const { return mCenter; }
84
85 PointType& eye() { return mEye; }
86
87 const PointType& eye() const { return mEye; }
88
89 PointType& up() { return mUp; }
90
91 const PointType& up() const { return mUp; }
92
93 Scalar& fieldOfView() { return mFovDeg; }
94
95 const Scalar& fieldOfView() const { return mFovDeg; }
96
97 void setFieldOfViewAdaptingEyeDistance(const Scalar& fov)
98 {
99 mFovDeg = fov;
100 PointType targetToEye = (mEye - mCenter).normalized();
101 mEye = mCenter + targetToEye * ((mVerticalHeight / 2.0) /
102 std::tan((fov / 2.0) / 180.0 * M_PI));
103 }
104
105 ProjectionMode::Enum& projectionMode() { return mProjectionMode; }
106
107 ProjectionMode::Enum projectionMode() const { return mProjectionMode; }
108
109 Scalar& verticalHeight() { return mVerticalHeight; }
110
111 const Scalar& verticalHeight() const { return mVerticalHeight; }
112
113 Scalar& aspectRatio() { return mAspect; }
114
115 const Scalar& aspectRatio() const { return mAspect; }
116
117 Scalar& nearPlane() { return mNear; }
118
119 const Scalar& nearPlane() const { return mNear; }
120
121 Scalar& farPlane() { return mFar; }
122
123 const Scalar& farPlane() const { return mFar; }
124
125 MatrixType viewMatrix() const
126 {
127 return lookAtMatrix<MatrixType>(mEye, mCenter, mUp);
128 }
129
130 MatrixType projMatrix() const
131 {
132 switch (mProjectionMode) {
133 case ProjectionMode::ORTHO: {
134 const Scalar h = mVerticalHeight / 2.0;
135 const Scalar w = h * mAspect;
136 return orthoProjectionMatrix<MatrixType>(
137 -w, w, h, -h, mNear, mFar, false);
138 }
139 case ProjectionMode::PERSPECTIVE: {
140 return projectionMatrix<MatrixType>(
141 mFovDeg, mAspect, mNear, mFar, false);
142 }
143 default: assert(false); return MatrixType::Identity();
144 }
145 }
146};
147
148} // namespace vcl
149
150#endif // VCL_RENDER_VIEWER_CAMERA_H
Definition camera.h:32
Scalar mNear
Z position of the mNear plane.
Definition camera.h:71
Scalar mVerticalHeight
Height of the target in world space (used for ortho projection, and adapting the eye distance for per...
Definition camera.h:65
Scalar mFovDeg
Field of view in degrees.
Definition camera.h:57
ProjectionMode::Enum mProjectionMode
Projection mode.
Definition camera.h:60
PointType mCenter
Position where the camera is looking at.
Definition camera.h:46
Scalar mFar
Z position of the mFar plane.
Definition camera.h:74
PointType mUp
Up direction of the camera.
Definition camera.h:52
Scalar mAspect
Aspect ratio.
Definition camera.h:68
PointType mEye
Position of (eye of) the camera.
Definition camera.h:49
A class representing a line segment in n-dimensional space. The class is parameterized by a PointConc...
Definition segment.h:43
Segment()
Default constructor. Creates a segment with endpoints at the origin.
Definition segment.h:68
Definition camera.h:35