VisuTwin Canvas
C++ 3D Engine — Metal Backend
Loading...
Searching...
No Matches
camera.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2025-2026 Arnis Lektauers
3//
4// Created by Arnis Lektauers on 05.09.2025.
5//
6#include "camera.h"
7
8#include "graphNode.h"
9
10namespace visutwin::canvas
11{
13 {
14 _node = std::unique_ptr<GraphNode>(value);
15 }
16
18 {
19 if (_projection != value) {
20 _projection = value;
21 _projMatDirty = true;
22 }
23 }
24
25 void Camera::setAspectRatio(float value)
26 {
27 if (_aspectRatio != value) {
28 _aspectRatio = value;
29 _projMatDirty = true;
30 }
31 }
32
34 {
35 if (_aspectRatioMode != value) {
36 _aspectRatioMode = value;
37 _projMatDirty = true;
38 }
39 }
40
41 void Camera::evaluateProjectionMatrix()
42 {
43 if (_projMatDirty) {
44 if (_projection == ProjectionType::Perspective) {
46 _projMatSkybox = _projMat;
47 } else {
48 const auto y = _orthoHeight;
49 const auto x = y * aspectRatio();
50 _projMat = Matrix4::ortho(-x, x, -y, y, nearClip(), farClip());
51 _projMatSkybox = Matrix4::perspective(fov(), aspectRatio(), nearClip(), farClip());
52 }
53
54 _projMatDirty = false;
55 }
56 }
57
58 void Camera::storeShaderMatrices(const Matrix4& viewProjection, const float jitterX, const float jitterY,
59 const int renderVersion)
60 {
61 if (_shaderMatricesVersion == renderVersion) {
62 return;
63 }
64
65 _shaderMatricesVersion = renderVersion;
66 _viewProjPrevious = _hasViewProjCurrent ? _viewProjCurrent : viewProjection;
67 _viewProjCurrent = viewProjection;
68 _hasViewProjCurrent = true;
69 _viewProjInverse = viewProjection.inverse();
70
71 _jitters[2] = _jitters[0];
72 _jitters[3] = _jitters[1];
73 _jitters[0] = jitterX;
74 _jitters[1] = jitterY;
75 }
76
77 void Camera::_enableRenderPassColorGrab(const std::shared_ptr<GraphicsDevice>& device, const bool enable)
78 {
79 if (enable) {
80 if (!_renderPassColorGrab) {
81 _renderPassColorGrab = std::make_shared<RenderPassColorGrab>(device);
82 }
83 } else {
84 _renderPassColorGrab.reset();
85 }
86 }
87
88 void Camera::_enableRenderPassDepthGrab(const std::shared_ptr<GraphicsDevice>& device, const bool enable)
89 {
90 if (enable) {
91 if (!_renderPassDepthGrab) {
92 _renderPassDepthGrab = std::make_shared<RenderPassDepthGrab>(device, this);
93 }
94 } else {
95 _renderPassDepthGrab.reset();
96 }
97 }
98}
float aspectRatio() const
Definition camera.h:48
void setProjection(ProjectionType value)
Definition camera.cpp:17
float farClip() const
Definition camera.h:57
void _enableRenderPassColorGrab(const std::shared_ptr< GraphicsDevice > &device, bool enable)
Definition camera.cpp:77
void _enableRenderPassDepthGrab(const std::shared_ptr< GraphicsDevice > &device, bool enable)
Definition camera.cpp:88
float nearClip() const
Definition camera.h:54
void setAspectRatio(float value)
Definition camera.cpp:25
void setNode(GraphNode *value)
Definition camera.cpp:12
bool horizontalFov() const
Definition camera.h:63
void setAspectRatioMode(AspectRatioMode value)
Definition camera.cpp:33
float fov() const
Definition camera.h:42
void storeShaderMatrices(const Matrix4 &viewProjection, float jitterX, float jitterY, int renderVersion)
Definition camera.cpp:58
Hierarchical scene graph node with local/world transforms and parent-child relationships.
Definition graphNode.h:28
4x4 column-major transformation matrix with SIMD acceleration.
Definition matrix4.h:31
Matrix4 inverse() const
static Matrix4 ortho(float left, float right, float bottom, float top, float near, float far)
static Matrix4 perspective(float fov, float aspect, float zNear, float zFar, bool fovIsHorizontal=false)