48 using Tscal = shambase::VecComponent<Tvec>;
64 Camera3d(Tvec pos, Tvec dir, Tvec up,
u32 nx,
u32 ny, Tscal fovy, Tscal znear, Tscal zfar)
65 :
Camera3d(pos, dir, up, nx, ny, Tscal(nx) / Tscal(ny), fovy, znear, zfar) {}
86 sham::format(
"Camera3d: aspect must be > 0, got aspect = {}", aspect));
88 u32 ny =
static_cast<u32>(sycl::max(Tscal(1), std::round(Tscal(nx) / aspect)));
89 return Camera3d(pos, dir, up, nx, ny, aspect, fovy, znear, zfar);
101 Tvec pos, Tvec target, Tvec up,
u32 nx,
u32 ny, Tscal fovy, Tscal znear, Tscal zfar) {
102 if (sham::equals(pos, target)) {
104 "Camera3d: target must differ from pos, got pos = target = {}", pos));
106 return Camera3d(pos, target - pos, up, nx, ny, fovy, znear, zfar);
109 inline Tvec get_pos()
const {
return pos; }
113 inline Tvec
get_up()
const {
return up; }
116 inline u32 get_nx()
const {
return nx; }
117 inline u32 get_ny()
const {
return ny; }
118 inline Tscal get_aspect()
const {
return aspect; }
119 inline Tscal get_fovy()
const {
return fovy; }
120 inline Tscal get_znear()
const {
return znear; }
121 inline Tscal get_zfar()
const {
return zfar; }
128 Tscal tan_half = sycl::tan(fovy / 2);
129 Tscal ndc_x = Tscal(2) * (Tscal(ix) + Tscal(0.5)) / Tscal(nx) - Tscal(1);
130 Tscal ndc_y = Tscal(2) * (Tscal(iy) + Tscal(0.5)) / Tscal(ny) - Tscal(1);
133 Tvec d = dir + right * (ndc_x * tan_half * aspect) + up * (ndc_y * tan_half);
141 std::vector<Ray<Tvec>> rays;
142 rays.reserve(
size_t(nx) *
size_t(ny));
143 for (
u32 iy = 0; iy < ny; iy++) {
144 for (
u32 ix = 0; ix < nx; ix++) {
155 view(0, 0) = right.x();
156 view(0, 1) = right.y();
157 view(0, 2) = right.z();
158 view(0, 3) = -sycl::dot(right, pos);
163 view(1, 3) = -sycl::dot(up, pos);
165 view(2, 0) = -dir.x();
166 view(2, 1) = -dir.y();
167 view(2, 2) = -dir.z();
168 view(2, 3) = sycl::dot(dir, pos);
179 view_inv(0, 0) = right.x();
180 view_inv(1, 0) = right.y();
181 view_inv(2, 0) = right.z();
183 view_inv(0, 1) = up.x();
184 view_inv(1, 1) = up.y();
185 view_inv(2, 1) = up.z();
187 view_inv(0, 2) = -dir.x();
188 view_inv(1, 2) = -dir.y();
189 view_inv(2, 2) = -dir.z();
191 view_inv(0, 3) = pos.x();
192 view_inv(1, 3) = pos.y();
193 view_inv(2, 3) = pos.z();
204 Tscal f = Tscal(1) / sycl::tan(fovy / 2);
206 proj(0, 0) = f / aspect;
208 proj(2, 2) = (zfar + znear) / (znear - zfar);
209 proj(2, 3) = Tscal(2) * zfar * znear / (znear - zfar);
220 proj_inv(0, 0) = Tscal(1) / proj(0, 0);
221 proj_inv(1, 1) = Tscal(1) / proj(1, 1);
223 proj_inv(3, 2) = Tscal(1) / proj(2, 3);
224 proj_inv(3, 3) = proj(2, 2) / proj(2, 3);
271 : pos(pos), nx(nx), ny(ny), aspect(aspect), fovy(fovy), znear(znear), zfar(zfar) {
273 auto throw_invalid = [&](
const std::string &reason) {
298 if (nx == 0 || ny == 0) {
299 throw_invalid(
"nx and ny must be > 0");
302 throw_invalid(
"aspect must be > 0");
304 if (!(fovy > 0 && fovy < shambase::constants::pi<Tscal>) ) {
305 throw_invalid(
"fovy must be in (0, pi)");
307 if (!(znear > 0 && zfar > znear)) {
308 throw_invalid(
"znear and zfar must satisfy 0 < znear < zfar");
311 Tscal len_dir = sycl::length(dir);
312 if (!(len_dir > 0)) {
313 throw_invalid(
"dir must be non zero");
315 this->dir = dir / len_dir;
317 Tvec r = sycl::cross(this->dir, up);
318 Tscal len_r = sycl::length(r);
320 throw_invalid(
"up must be non zero and not colinear with dir");
322 this->right = r / len_r;
323 this->up = sycl::cross(this->right, this->dir);
std::uint32_t u32
32 bit unsigned integer
Tvec get_dir() const
Normalized viewing direction.
mat< Tscal, 4, 4 > get_camera_transform() const
Full camera transform (world -> clip space), projection * view.
Tvec get_up() const
Normalized up direction, orthogonal to dir.
Ray< Tvec > get_pixel_ray(u32 ix, u32 iy) const
Ray going through the center of pixel (ix, iy).
static Camera3d from_aspect(Tvec pos, Tvec dir, Tvec up, u32 nx, Tscal aspect, Tscal fovy, Tscal znear, Tscal zfar)
Construct a camera from the horizontal resolution and the aspect ratio.
mat< Tscal, 4, 4 > get_camera_transform_inv() const
Inverse of the full camera transform (clip -> world space).
mat< Tscal, 4, 4 > get_view_matrix_inv() const
Inverse of the view matrix (camera -> world space).
Tvec get_right() const
Normalized right direction dir x up.
std::vector< Ray< Tvec > > get_rays() const
Rays of all the pixels, pixel (ix, iy) being at index iy * nx + ix.
mat< Tscal, 4, 4 > get_projection_matrix() const
Projection matrix (camera -> clip space), equivalent to gluPerspective.
static Camera3d look_at(Tvec pos, Tvec target, Tvec up, u32 nx, u32 ny, Tscal fovy, Tscal znear, Tscal zfar)
Construct a camera looking at a target point (gluLookAt convention).
mat< Tscal, 4, 4 > get_projection_matrix_inv() const
Inverse of the projection matrix (clip -> camera space).
Camera3d(Tvec pos, Tvec dir, Tvec up, u32 nx, u32 ny, Tscal fovy, Tscal znear, Tscal zfar)
Construct a camera from the image size in pixels.
mat< Tscal, 4, 4 > get_view_matrix() const
View matrix (world -> camera space), equivalent to gluLookAt.
Matrix class based on std::array storage and mdspan.
constexpr auto get_mdspan()
Get the matrix data as a mdspan.
Class holding the value of numerous constants generated from the following source.
This header file contains utility functions related to exception handling in the code.
Define the fmt formatters for sycl::vec.
ExcptTypes make_except_with_loc(std::string message, SourceLocation loc=SourceLocation{})
Create an exception with a message and a location.
namespace for math utility
void mat_prod(const std::mdspan< Ta, Extents1, Layout1, Accessor1 > &input1, const std::mdspan< Ta, Extents2, Layout2, Accessor2 > &input2, const std::mdspan< Tb, Extents3, Layout3, Accessor3 > &output)
Compute the product of two matrices.
Ray representation for intersection testing.