OpenVDB  9.0.1
Camera.h
Go to the documentation of this file.
1 // Copyright Contributors to the OpenVDB Project
2 // SPDX-License-Identifier: MPL-2.0
3 
4 /// @file Camera.h
5 ///
6 /// @author Ken Museth
7 ///
8 /// @brief A simple camera class.
9 
10 #ifndef NANOVDB_CAMERA_H_HAS_BEEN_INCLUDED
11 #define NANOVDB_CAMERA_H_HAS_BEEN_INCLUDED
12 
13 #include <nanovdb/NanoVDB.h> // for Vec3
14 #include <nanovdb/util/Ray.h>
15 
16 namespace nanovdb {
17 
18 /// @brief A minimal perspective camera for ray generation
19 template<typename RealT = float, typename Vec3T = Vec3<RealT>, typename RayT = Ray<RealT>>
20 class Camera
21 {
22  Vec3T mEye, mW, mU, mV;
23 
24  __hostdev__ void init(RealT vfov, RealT aspect)
25  {
26  const RealT halfHeight = RealT(tan(vfov * 3.14159265358979323846 / 360));
27  const RealT halfWidth = aspect * halfHeight;
28  mW = halfWidth * mU + halfHeight * mV + mW; // remove eye here and in getRay
29  mU *= 2 * halfWidth;
30  mV *= 2 * halfHeight;
31  }
32 
33 public:
34  /// @brief default Ctor.
35  Camera() = default;
36 
37  /// @brief Ctor. // vfov is top to bottom in degrees
38  /// @note up is assumed to be a unit-vector
39  __hostdev__ Camera(const Vec3T& eye, const Vec3T& lookat, const Vec3T& up, RealT vfov, RealT aspect)
40  : mEye(eye)
41  , mW((eye - lookat).normalize())
42  , mU(up.cross(mW))
43  , mV(up)
44  {
45  this->init(vfov, aspect);
46  }
47  __hostdev__ void update(const Vec3T& eye, const Vec3T& lookat, const Vec3T& up, RealT vfov, RealT aspect)
48  {
49  mEye = eye;
50  mV = up;
51  mW = mEye - lookat;
52  mW.normalize();
53  mU = mV.cross(mW);
54  this->init(vfov, aspect);
55  }
56  /// @brief {u,v} are are assumed to be [0,1]
57  __hostdev__ RayT getRay(RealT u, RealT v) const {
58  auto dir = u * mU + v * mV - mW;
59  dir.normalize();
60  return RayT(mEye, dir);
61  }
62 
63  __hostdev__ const Vec3T& P() const { return mEye; }
64  __hostdev__ const Vec3T& U() const { return mU; }
65  __hostdev__ const Vec3T& V() const { return mV; }
66  __hostdev__ const Vec3T& W() const { return mW; }
67 
68 }; // Camera
69 
70 } // namespace nanovdb
71 
72 #endif // NANOVDB_CAMERA_HAS_BEEN_INCLUDED
__hostdev__ const Vec3T & U() const
Definition: Camera.h:64
GridType::Ptr normalize(const GridType &grid, bool threaded, InterruptT *interrupt)
Normalize the vectors of the given vector-valued grid.
Definition: GridOperators.h:1068
__hostdev__ Camera(const Vec3T &eye, const Vec3T &lookat, const Vec3T &up, RealT vfov, RealT aspect)
Ctor. // vfov is top to bottom in degrees.
Definition: Camera.h:39
Implements a light-weight self-contained VDB data-structure in a single file! In other words...
Definition: NanoVDB.h:184
__hostdev__ void update(const Vec3T &eye, const Vec3T &lookat, const Vec3T &up, RealT vfov, RealT aspect)
Definition: Camera.h:47
__hostdev__ const Vec3T & P() const
Definition: Camera.h:63
__hostdev__ const Vec3T & V() const
Definition: Camera.h:65
__hostdev__ const Vec3T & W() const
Definition: Camera.h:66
__hostdev__ RayT getRay(RealT u, RealT v) const
{u,v} are are assumed to be [0,1]
Definition: Camera.h:57
#define __hostdev__
Definition: NanoVDB.h:168
A minimal perspective camera for ray generation.
Definition: Camera.h:20
Camera()=default
default Ctor.