4#ifndef GREM_GRAPHICS_3D_CAMERA_3D_HPP
5#define GREM_GRAPHICS_3D_CAMERA_3D_HPP
209 : parameterBuffer(device) {
225 setProjectionWithoutFlush(newProjection);
241 setViewWithoutFlush(newView);
256 setOptionsWithoutFlush(newOptions);
274 setProjectionWithoutFlush(newProjection);
275 setViewWithoutFlush(newView);
292 setViewWithoutFlush(newView);
293 setOptionsWithoutFlush(newOptions);
313 setProjectionWithoutFlush(newProjection);
314 setViewWithoutFlush(newView);
315 setOptionsWithoutFlush(newOptions);
325 return parameterBuffer;
334 return projectionMatrix;
356 void setProjectionWithoutFlush(
const Projection3D& newProjection) {
359 projectionMatrix = newProjectionMatrix;
362 GREM_CASE(
const OrthographicProjection3D& newOrthographicProjection) {
363 if (newOrthographicProjection.nearZ < newOrthographicProjection.farZ) {
364 projectionMatrix =
ortho(newOrthographicProjection.offset.x, newOrthographicProjection.offset.x + newOrthographicProjection.size.x,
365 newOrthographicProjection.offset.y, newOrthographicProjection.offset.y + newOrthographicProjection.size.y, newOrthographicProjection.nearZ,
366 newOrthographicProjection.farZ);
368 projectionMatrix =
ortho(newOrthographicProjection.offset.x, newOrthographicProjection.offset.x + newOrthographicProjection.size.x,
369 newOrthographicProjection.offset.y, newOrthographicProjection.offset.y + newOrthographicProjection.size.y);
373 GREM_CASE(
const PerspectiveProjection3D& newPerspectiveProjection) {
374 if (newPerspectiveProjection.nearZ < newPerspectiveProjection.farZ) {
375 projectionMatrix =
perspective(newPerspectiveProjection.verticalFieldOfView, newPerspectiveProjection.aspectRatio, newPerspectiveProjection.nearZ,
376 newPerspectiveProjection.farZ);
378 projectionMatrix =
infinitePerspective(newPerspectiveProjection.verticalFieldOfView, newPerspectiveProjection.aspectRatio, newPerspectiveProjection.nearZ);
385 void setViewWithoutFlush(
const View3D& newView) {
388 viewMatrix = newViewMatrix;
391 GREM_CASE(
const WorldView3D& newWorldView) {
393 const vec3 inversePosition = inverseBasis * -newWorldView.position;
395 vec4{inverseBasis[0], 0.0f},
396 vec4{inverseBasis[1], 0.0f},
397 vec4{inverseBasis[2], 0.0f},
398 vec4{inversePosition, 1.0f},
405 void setOptionsWithoutFlush(
const Camera3DOptions& newOptions) {
406 options = newOptions;
411 const vec3 position = basis * -
vec3{viewMatrix[3]};
413 const float nearZ = projectionMatrix[3][2] / projectionMatrix[2][2];
414 const float farZ = (projectionMatrix[2][2] == -1.0f) ?
Limits<float>::MAX : projectionMatrix[3][2] / (projectionMatrix[2][2] + 1.0f);
417 .cameraProjectionMatrix = projectionMatrix,
418 .cameraViewMatrix = viewMatrix,
419 .cameraViewProjectionMatrix = projectionMatrix * viewMatrix,
420 .cameraNearAndFarPlaneDistances{nearZ, farZ},
421 .cameraPosition = position,
422 .cameraBasis = basis,
423 .cameraExposure = options.exposure,
428 mat4 projectionMatrix{1.0f};
429 mat4 viewMatrix{1.0f};
430 Camera3DOptions options{};
#define GREM_MATCH(...)
Definition Variant.hpp:1997
#define GREM_CASE(...)
Definition Variant.hpp:1998
friend class Variant
JSON value type using the default allocator.
Definition Variant.hpp:1402
const mat4 & getViewMatrix() const noexcept
Get the view matrix of the camera.
Definition Camera3D.hpp:342
void setOptions(const Camera3DOptions &newOptions)
Set the configuration options of the camera.
Definition Camera3D.hpp:255
void setProjectionAndViewAndOptions(const Projection3D &newProjection, const View3D &newView, const Camera3DOptions &newOptions)
Set both the projection and view of the camera, as well as its configuration options.
Definition Camera3D.hpp:312
void setProjectionAndView(const Projection3D &newProjection, const View3D &newView)
Set both the projection and view of the camera.
Definition Camera3D.hpp:273
Camera3DOptions getOptions() const noexcept
Get the configuration options of the camera.
Definition Camera3D.hpp:351
void setProjection(const Projection3D &newProjection)
Set the projection of the camera.
Definition Camera3D.hpp:224
const ParameterBuffer & getParameterBuffer() const noexcept
Get the parameter buffer of the camera.
Definition Camera3D.hpp:324
Camera3D(Device &device, const Projection3D &projection=mat4{1.0f}, const View3D &view=mat4{1.0f}, const Camera3DOptions &options={})
Construct a camera.
Definition Camera3D.hpp:208
const mat4 & getProjectionMatrix() const noexcept
Get the projection matrix of the camera.
Definition Camera3D.hpp:333
UniformBuffer< Parameters, "Camera3DParameters"> ParameterBuffer
Shader buffer for 3D camera parameters.
Definition Camera3D.hpp:191
void setViewAndOptions(const View3D &newView, const Camera3DOptions &newOptions)
Set both the view and configuration options of the camera.
Definition Camera3D.hpp:291
void setView(const View3D &newView)
Set the view of the camera.
Definition Camera3D.hpp:240
Persistent system managing the rendering context of a Window.
Definition Device.hpp:68
Definition buffer_layouts.hpp:20
qua< float > quat
Definition math.hpp:892
vec< 4, float > vec4
Definition math.hpp:440
vec< 2, float > vec2
Definition math.hpp:438
GREM_ALWAYS_INLINE constexpr mat< R, C, T > transpose(const mat< C, R, T > &a)
Definition math.hpp:2247
vec< 3, float > vec3
Definition math.hpp:439
mat< 3, 3, float > mat3
Definition math.hpp:805
GREM_ALWAYS_INLINE mat< 4, 4, T > GREM_VECTORCALL infinitePerspective(T verticalFieldOfView, T aspectRatio, T nearZ)
Definition math.hpp:3046
GREM_ALWAYS_INLINE mat< 3, 3, T > GREM_VECTORCALL rotateScale(T rotation, vec< 2, T > scale)
Create a combined rotation and scale matrix describing a 2D transformation.
Definition math.hpp:2765
GREM_ALWAYS_INLINE mat< 4, 4, T > GREM_VECTORCALL perspective(T verticalFieldOfView, T aspectRatio, T nearZ, T farZ)
Definition math.hpp:3035
GREM_ALWAYS_INLINE mat< 4, 4, T > GREM_VECTORCALL ortho(T left, T right, T bottom, T top)
Definition math.hpp:3005
mat< 4, 4, float > mat4
Definition math.hpp:806
static constexpr T MAX
Definition fundamentals.hpp:29
Configuration options for a Camera3D.
Definition Camera3D.hpp:154
float exposure
Exposure parameter of the camera.
Definition Camera3D.hpp:158
constexpr bool operator==(const Camera3DOptions &other) const noexcept=default
Compare these options to another set of options for equality.
Struct of shader parameters representing the state of a Camera3D.
Definition Camera3D.hpp:178
mat3 cameraBasis
Current right, up and backward direction vectors (local X, Y and Z axes) of the camera in world space...
Definition Camera3D.hpp:184
float cameraExposure
Current exposure parameter of the camera.
Definition Camera3D.hpp:185
mat4 cameraViewProjectionMatrix
Current combined view-projection matrix of the camera.
Definition Camera3D.hpp:181
vec2 cameraNearAndFarPlaneDistances
Current near (X) and far (Y) plane distances of the camera.
Definition Camera3D.hpp:182
vec3 cameraPosition
Current position of the camera in world space.
Definition Camera3D.hpp:183
mat4 cameraProjectionMatrix
Current projection matrix of the camera.
Definition Camera3D.hpp:179
mat4 cameraViewMatrix
Current view matrix of the camera.
Definition Camera3D.hpp:180
Configuration of an orthographic projection in 3D.
Definition Camera3D.hpp:22
float farZ
Distance to the far plane of the projection, in view coordinates.
Definition Camera3D.hpp:46
vec2 offset
Bottom left corner of the orthographic projection, in render target coordinates.
Definition Camera3D.hpp:27
vec2 size
Size of the orthographic projection, in render target coordinates.
Definition Camera3D.hpp:32
constexpr bool operator==(const OrthographicProjection3D &other) const =default
Compare these options to another set of options for equality.
float nearZ
Distance to the near plane of the projection, in view coordinates.
Definition Camera3D.hpp:39
Configuration of a perspective projection in 3D.
Definition Camera3D.hpp:61
constexpr bool operator==(const PerspectiveProjection3D &other) const =default
Compare these options to another set of options for equality.
float nearZ
Distance to the near plane of the projection, in view coordinates.
Definition Camera3D.hpp:83
float verticalFieldOfView
Vertical field of view of the projection, in radians.
Definition Camera3D.hpp:70
float farZ
Distance to the far plane of the projection, in view coordinates.
Definition Camera3D.hpp:90
float aspectRatio
Aspect ratio of the projection, X/Y.
Definition Camera3D.hpp:78
Projection matrix or configuration of a projection in 3D.
Definition Camera3D.hpp:105
constexpr Variant() noexcept(std::is_nothrow_default_constructible_v< variant_alternative_t< 0, Variant > >)
Default-construct a variant with the first variant alternative, if it is default-constructible.
Definition Variant.hpp:351
View matrix or configuration of a view in 3D.
Definition Camera3D.hpp:147
constexpr Variant() noexcept(std::is_nothrow_default_constructible_v< variant_alternative_t< 0, Variant > >)
Default-construct a variant with the first variant alternative, if it is default-constructible.
Definition Variant.hpp:351
Configuration of a world-space view in 3D.
Definition Camera3D.hpp:112
quat orientation
World-space orientation of the view around its origin.
Definition Camera3D.hpp:131
vec3 scale
World-space scale of the view.
Definition Camera3D.hpp:141
vec3 position
World-space position of the view origin.
Definition Camera3D.hpp:121