opynsim
Unofficial C++ Documentation
Loading...
Searching...
No Matches
camera.h
Go to the documentation of this file.
1#pragma once
2
10#include <liboscar/maths/rect.h>
13
14#include <iosfwd>
15#include <optional>
16
17namespace osc { class RenderTexture; }
18namespace osc { class RenderTarget; }
19namespace osc { class SharedDepthStencilRenderBuffer; }
20
21namespace osc
22{
23 // camera
24 //
25 // represents a camera in world space that can rasterize drawcalls issued
26 // via `graphics::draw` to a 2D render target.
27 class Camera {
28 public:
30
31 // resets the camera to default parameters
32 void reset();
33
34 // get/set the background color that the camera will clear the output with before
35 // performing a draw call (assuming `CameraClearFlags::SolidColor` is set)
38
39 // get/set the kind of projection that the camera should use when projecting view space
40 // vertices into clip space (ignored if `set_projection_matrix_override` is used)
43
44 // get/set the height of the orthographic projection plane that the camera will use
45 //
46 // undefined behavior if `projection() != CameraProjection::Orthographic`, or the
47 // projection matrix has been overridden with `set_projection_matrix_override`, the
48 // width of the orthographic plane is calculated from the aspect ratio of the render
49 // target at runtime.
50 float orthographic_size() const;
52
53 // get/set the vertical field-of-view angle of the viewer's projection camera
54 //
55 // undefined behavior if `projection() != CameraProjection::Perspective` or the projection matrix
56 // has been overridden with `set_projection_matrix_override`.
59
60 // returns the horizontal field-of-view angle of the viewer's projection camera, assuming
61 // it's rendering to a render target with the given `aspect_ratio`.
62 //
63 // undefined behavior if `projection() != CameraProjection::Perspective` or the projection matrix
64 // has been overridden with `set_projection_matrix_override`.
66
67 // get/set the distance, in world space units, between both the camera and the nearest
68 // clipping plane, and the camera and the farthest clipping plane
71
72 // get/set the distance, in world space units, between the camera and the nearest
73 // clipping plane
74 float near_clipping_plane() const;
76
77 // get/set the distance, in world space units, between the camera and the farthest
78 // clipping plane
79 float far_clipping_plane() const;
81
82 // get/set the camera's clear flags, which affect how/if the camera clears the output
83 // during a call to `graphics::draw`
86
87 // get/set where on the output that this `Camera` should rasterize its pixels
88 // during a call to `graphics::draw`
89 //
90 // the rectangle is defined in screen space, which:
91 //
92 // - is measured in device-independent pixels
93 // - starts in the bottom-left corner
94 // - ends in the top-right corner
95 //
96 // `std::nullopt` implies that the camera should render to the full extents
97 // of the render target
98 std::optional<Rect> pixel_rect() const;
99 void set_pixel_rect(std::optional<Rect>);
100
101 // get/set the scissor rectangle, which tells the renderer to only clear and/or
102 // render fragments (pixels) that occur within the given rectangle
103 //
104 // the rectangle is defined in screen space, which:
105 //
106 // - is measured in device-independent pixels
107 // - starts in the bottom-left corner
108 // - ends in the top-right corner
109 //
110 // `std::nullopt` implies that the camera should clear (if applicable) the entire
111 // output, followed by writing output fragments to the output pixel rectangle
112 // with no scissoring
113 //
114 // the usefulness of scissor testing is that it can be used to:
115 //
116 // - limit running an expensive fragment shader to a smaller region
117 // - only draw sub-parts of a scene without having to recompute transforms or render it differently etc.
118 // - only clear + draw to a smaller region of the output (e.g. writing to subsections of a larger UI)
119 std::optional<Rect> scissor_rect() const;
120 void set_scissor_rect(std::optional<Rect>);
121
122 // get/set the world space position of this `Camera`
124 void set_position(const Vector3&);
125
126 // get/set the orientation of this `Camera`
127 //
128 // the default/identity orientation of the camera has it pointing along `-Z`, with
129 // `+Y` pointing "up"
132
133 // get/set the direction in which this `Camera` is pointing
134 //
135 // care: This is a convenience method. `Camera` actually stores a rotation, not this
136 // direction vector. The implementation assumes that the direction is along `-Z`
137 // and that `+Y` is "up", followed by figuring out what rotation is necessary to
138 // point it along directions get/set via these methods.
139 //
140 // Therefore, if you want to "roll" the camera (i.e. where `+Y` isn't "up"), you
141 // should directly manipulate the rotation of this camera, rather than trying to
142 // play with this method.
144 void set_direction(const Vector3&);
145
146 // returns the "up" direction of this camera
148
149 // returns the matrix that this camera uses to transform world space points into
150 // view space
151 //
152 // world space and view space operate with the same units-of-measure, handedness, etc.
153 // but view space places the camera at `(0, 0, 0)`
155
156 // returns the equivalent of `inverse(view_matrix())`, i.e. a matrix that transforms
157 // view space points into world space points.
159
160 // get/set matrices that override the default view matrix that this `Camera` uses
161 //
162 // by default, `Camera` computes its view matrix from its position and rotation, but
163 // it's sometimes necessary/handy to override this default behavior.
164 std::optional<Matrix4x4> view_matrix_override() const;
165 void set_view_matrix_override(std::optional<Matrix4x4>);
166
167 // returns the matrix that this camera uses to transform view space points into
168 // clip space.
169 //
170 // clip space is defined such that there exists a unit cube in it that eventually
171 // projects onto screen space in the following way:
172 //
173 // - transformed points (affine, homogeneous) are divided by their `w` component (perspective
174 // divide) to yield their normalized device coordinates (NDC).
175 // - Anything outside of [{-1,-1,-1},{+1,+1,+1}] in NDC is discarded (clipping)
176 // - NDC `( 0, 0, 0)` maps to the midpoint of screen space (i.e. 0.5 * {w, h})
177 // - NDC `(-1, -1, -1)` maps to the bottom-left of screen space (z = -1 means 'closest')
178 // - NDC `(+1, +1, +1)` maps to the top-right of screen space (z = +1 means 'farthest')
179 // - Therefore, NDC is left-handed
180 //
181 // The XY component of fragments that land within clip space are transformed into screen
182 // space and drawn to the output pixel rectangle (assuming they also pass the scissor test).
183 // The Z component of things that land within the NDC cube are written to the depth buffer
184 // if the `Material` that's being drawn enables this behavior (and there's a depth buffer
185 // attached to the render target).
187 std::optional<Matrix4x4> projection_matrix_override() const;
188 void set_projection_matrix_override(std::optional<Matrix4x4>);
189
190 // returns the equivalent of `projection_matrix(aspect_ratio) * view_matrix()`
192
193 // returns the equivalent of `inverse(view_projection_matrix(aspect_ratio))`
195
196 // flushes and renders any queued drawcalls from `graphics::draw(...)` to the
197 // main application window.
199
200 // flushes and renders any queued drawcalls from `graphics::draw(...)` to `render_texture`.
202
203 // flushes and renders any queued drawcalls from `graphics::draw(...)` to `render_target`.
205
206 // flushes and renders any queued drawcalls from `graphics::draw(...)` to `shared_depth_stencil_buffer`.
207 //
208 // the resulting render pass is a depth-only render
210
211 private:
212 friend bool operator==(const Camera&, const Camera&);
213 friend std::ostream& operator<<(std::ostream&, const Camera&);
214 friend class GraphicsBackend;
215
216 class Impl;
218 };
219
220 bool operator==(const Camera&, const Camera&);
221 std::ostream& operator<<(std::ostream&, const Camera&);
222}
Definition angle.h:25
Definition camera.h:27
void set_vertical_field_of_view(Radians)
Vector3 upwards_direction() const
void set_far_clipping_plane(float)
void render_to(RenderTexture &render_texture)
float near_clipping_plane() const
void set_clipping_planes(CameraClippingPlanes)
void render_to(SharedDepthStencilRenderBuffer &shared_depth_stencil_buffer)
friend class GraphicsBackend
Definition camera.h:214
void set_position(const Vector3 &)
std::optional< Rect > pixel_rect() const
Vector3 position() const
friend std::ostream & operator<<(std::ostream &, const Camera &)
Matrix4x4 inverse_view_projection_matrix(float aspect_ratio) const
Matrix4x4 inverse_view_matrix() const
void set_scissor_rect(std::optional< Rect >)
friend bool operator==(const Camera &, const Camera &)
CameraProjection projection() const
float far_clipping_plane() const
Radians horizontal_field_of_view(float aspect_ratio) const
void render_to_main_window()
std::optional< Matrix4x4 > view_matrix_override() const
std::optional< Matrix4x4 > projection_matrix_override() const
Matrix4x4 view_matrix() const
CameraClippingPlanes clipping_planes() const
void set_projection_matrix_override(std::optional< Matrix4x4 >)
void set_direction(const Vector3 &)
void reset()
void set_projection(CameraProjection)
void set_orthographic_size(float)
Radians vertical_field_of_view() const
Vector3 direction() const
void set_clear_flags(CameraClearFlags)
Matrix4x4 view_projection_matrix(float aspect_ratio) const
Color background_color() const
void set_rotation(const Quaternion &)
void set_view_matrix_override(std::optional< Matrix4x4 >)
float orthographic_size() const
CameraClearFlags clear_flags() const
void set_pixel_rect(std::optional< Rect >)
Matrix4x4 projection_matrix(float aspect_ratio) const
void set_background_color(const Color &)
void render_to(const RenderTarget &render_target)
Quaternion rotation() const
void set_near_clipping_plane(float)
std::optional< Rect > scissor_rect() const
Definition copy_on_upd_shared_value.h:16
Definition flags.h:20
Definition render_target.h:14
Definition render_texture.h:18
Definition shared_depth_stencil_render_buffer.h:49
Definition custom_decoration_generator.h:5
std::ostream & operator<<(std::ostream &, const Object &)
CameraProjection
Definition camera_projection.h:8
bool operator==(const Class &, const Class &)
constexpr U to(T &&value)
Definition conversion.h:56
Definition camera_clipping_planes.h:7