raytracer
Parallel Ray Tracer written from scratch (no graphics API).
Loading...
Searching...
No Matches
camera.h
Go to the documentation of this file.
1#pragma once
2#include <cstdint>
3#include <nlohmann/json.hpp>
4#include "common/util/vec3.h"
5
7{
8enum class ViewType : uint8_t
9{
12};
13
15 {{ViewType::ORTHOGRAPHIC, "orthographic"}, {ViewType::PERSPECTIVE, "perspective"}})
16
17struct Ray
18{
19 common::Vec3 origin;
20 common::Vec3 direction;
21};
22
23struct Pixel
24{
25 int x; // column [0, screen_width_pixels)
26 int y; // row [0, screen_height_pixels)
27};
28
29class CameraBuilder;
30
31class Camera
32{
33 friend CameraBuilder;
34
35 double screen_left_coord = -1.0;
36 double screen_right_coord = 1.0;
37 double screen_bottom_coord = -1.0;
38 double screen_top_coord = 1.0;
39 double focal_length = 1.0;
41 common::Vec3 direction{0, 0, -1};
42 common::Vec3 world_up{0, 1, 0};
43 int screen_width_pixels = 1920;
44 int screen_height_pixels = 1080;
46 int color_channels = 256;
47 int rays_per_pixel = 4;
48 int max_recursion_depth = 32;
49
50 public:
51 Camera() = default;
52 Camera(double _screen_left_coord, double _screen_right_coord, double _screen_bottom_coord, double _screen_top_coord,
53 double _focal_length, const common::Vec3 &_origin, const common::Vec3 &_direction,
54 const common::Vec3 &_world_up, int _screen_width_pixels, int _screen_height_pixels, ViewType _view_type,
55 int _color_channels, int _rays_per_pixel, int _max_recursion_depth);
62 [[nodiscard]] Ray compute_ray_for_pixel(Pixel p) const;
63
64 NLOHMANN_DEFINE_TYPE_INTRUSIVE(Camera, screen_left_coord, screen_right_coord, screen_bottom_coord, screen_top_coord,
65 focal_length, origin, direction, world_up, screen_width_pixels, screen_height_pixels,
66 view_type, color_channels, rays_per_pixel, max_recursion_depth)
67
68
73 [[nodiscard]] int get_screen_width_pixels() const
74 {
75 return screen_width_pixels;
76 }
77
83 [[nodiscard]] int get_screen_height_pixels() const
84 {
85 return screen_height_pixels;
86 }
87
93 [[nodiscard]] int get_color_channels() const
94 {
95 return color_channels;
96 }
97
103 [[nodiscard]] int get_rays_per_pixel() const
104 {
105 return rays_per_pixel;
106 }
107
113 [[nodiscard]] int get_max_recursion_depth() const
114 {
115 return max_recursion_depth;
116 }
117};
118
120{
121 Camera camera;
122
123 public:
125 CameraBuilder &with_screen_dimensions(int width, int height) &;
126 CameraBuilder &with_focal_length(double focal_length) &;
127 CameraBuilder &with_origin(const common::Vec3 &origin) &;
128 CameraBuilder &with_direction(const common::Vec3 &direction) &;
129 CameraBuilder &with_world_up(const common::Vec3 &direction) &;
130 CameraBuilder &with_virtual_screen_boundaries(double left, double top, double right, double bottom) &;
132 CameraBuilder &with_color_channels(int color_channels) &;
133 CameraBuilder &with_rays_per_pixel(int rays_per_pixel) &;
134 CameraBuilder &with_max_recursion_depth(int max_recursion_depth) &;
135 [[nodiscard]] Camera build() const;
136};
137} // namespace raytracer::raytracer
CameraBuilder & with_direction(const common::Vec3 &direction) &
Definition camera.cpp:78
CameraBuilder & with_screen_dimensions(int width, int height) &
Definition camera.cpp:59
CameraBuilder & with_virtual_screen_boundaries(double left, double top, double right, double bottom) &
Definition camera.cpp:90
CameraBuilder & with_color_channels(int color_channels) &
Definition camera.cpp:106
CameraBuilder & with_view_type(ViewType view_type) &
Definition camera.cpp:100
CameraBuilder & with_max_recursion_depth(int max_recursion_depth) &
Definition camera.cpp:118
CameraBuilder & with_origin(const common::Vec3 &origin) &
Definition camera.cpp:72
CameraBuilder & with_rays_per_pixel(int rays_per_pixel) &
Definition camera.cpp:112
Camera build() const
Definition camera.cpp:124
CameraBuilder & with_focal_length(double focal_length) &
Definition camera.cpp:66
CameraBuilder & with_world_up(const common::Vec3 &direction) &
Definition camera.cpp:84
Definition camera.h:32
int get_max_recursion_depth() const
Definition camera.h:113
int get_rays_per_pixel() const
Definition camera.h:103
NLOHMANN_DEFINE_TYPE_INTRUSIVE(Camera, screen_left_coord, screen_right_coord, screen_bottom_coord, screen_top_coord, focal_length, origin, direction, world_up, screen_width_pixels, screen_height_pixels, view_type, color_channels, rays_per_pixel, max_recursion_depth) int get_screen_width_pixels() const
Definition camera.h:64
Ray compute_ray_for_pixel(Pixel p) const
Definition camera.cpp:23
int get_screen_height_pixels() const
Definition camera.h:83
int get_color_channels() const
Definition camera.h:93
Definition raytracer.cpp:29
ViewType
Definition camera.h:9
@ ORTHOGRAPHIC
Definition camera.h:10
@ PERSPECTIVE
Definition camera.h:11
NLOHMANN_JSON_SERIALIZE_ENUM(ViewType, {{ViewType::ORTHOGRAPHIC, "orthographic"}, {ViewType::PERSPECTIVE, "perspective"}}) struct Ray
Definition camera.h:14
Definition vec3.h:11
static const Vec3 ZERO
Definition vec3.h:23
Definition camera.h:24
int x
Definition camera.h:25
int y
Definition camera.h:26