sculpto
camera.cpp
Go to the documentation of this file.
1/*!****************************************************************//*!*
2 * \file camera.cpp
3 * \brief Renderer virtual camer class implementation module.
4 *
5 * \author Sabitov Kirill
6 * \date 12 July 2022
7 *********************************************************************/
8
9#include "sclpch.h"
10#include "camera.h"
12
14 this->ProjectionType = ProjectionType;
15 InvalidateProjection();
16}
17
18void scl::camera::SetFieldOfView(float FieldOfView) {
19 this->FieldOfView = FieldOfView;
20 InvalidateProjection();
21}
22
23void scl::camera::SetProjectionDistance(float ProjectionDistance) {
24 this->ProjectionDistance = ProjectionDistance;
25 InvalidateProjection();
26}
27
28void scl::camera::SetFarClip(float FarClip) {
29 this->FarClip = FarClip;
30 InvalidateProjection();
31}
32
33void scl::camera::SetViewportWidth(int ViewportWidth) {
34 this->ViewportWidth = ViewportWidth;
35 InvalidateProjection();
36}
37
38void scl::camera::SetViewportHeight(int ViewportHeight) {
39 this->ViewportHeight = ViewportHeight;
40 InvalidateProjection();
41}
42
43void scl::camera::SetUpDirection(const vec3 &UpDirection) {
44 this->UpDirection = UpDirection;
45 RightDirection = LookDirection.Cross(UpDirection).Normalized();
46 InvalidateView();
47}
48
49void scl::camera::SetDirection(const vec3 &Direction) {
50 this->LookDirection = Direction;
51 RightDirection = LookDirection.Cross(UpDirection).Normalized();
52 InvalidateView();
53}
54
55void scl::camera::SetPosition(const vec3 &Position) {
56 this->Position = Position;
57 LookDirection = (Focus - Position).Normalized();
58 RightDirection = LookDirection.Cross(UpDirection).Normalized();
59 InvalidateView();
60}
61
62void scl::camera::SetFocus(const vec3 &Focus) {
63 this->Focus = Focus;
64 LookDirection = (Focus - Position).Normalized();
65 RightDirection = LookDirection.Cross(UpDirection).Normalized();
66 InvalidateView();
67}
68
69void scl::camera::SetView(const vec3 &Position, const vec3 &Focus, const vec3 &UpDirection)
70{
71 this->Position = Position;
72 this->UpDirection = UpDirection;
73 this->Focus = Focus;
74 LookDirection = (Focus - Position).Normalized();
75 RightDirection = LookDirection.Cross(UpDirection).Normalized();
76 InvalidateView();
77}
78
79void scl::camera::SetRenderToSwapChain(bool IsSwapChainTarget)
80{
81 frame_buffer_props current_props = MainFrameBuffer->GetFrameBufferProps();
82 if (current_props.IsSwapChainTarget != IsSwapChainTarget)
83 {
84 current_props.IsSwapChainTarget = IsSwapChainTarget;
85 MainFrameBuffer->SetFrameBufferProps(current_props);
86 }
87}
88
89void scl::camera::InvalidateViewProjection()
90{
91 ViewProjection = View * Projection;
92}
93
94void scl::camera::InvalidateProjection()
95{
96 float ratio_x = FieldOfView / 2, ratio_y = FieldOfView / 2;
97
98 if (ViewportWidth >= ViewportHeight) ratio_x *= (float)ViewportWidth / ViewportHeight;
99 else ratio_y *= (float)ViewportHeight / ViewportWidth;
100 ViewportProjectionWidth = ratio_x * 2;
101 ViewportProjectionHeight = ratio_y * 2;
102
103 if (ProjectionType == camera_projection_type::ORTHOGRAPHIC)
104 Projection = matr4::Ortho(-ratio_x, ratio_x, -ratio_y, ratio_y, ProjectionDistance, FarClip);
105 if (ProjectionType == camera_projection_type::PERSPECTIVE)
106 Projection = matr4::Frustum(-ratio_x, ratio_x, -ratio_y, ratio_y, ProjectionDistance, FarClip);
107 InvalidateViewProjection();
108}
109
110void scl::camera::InvalidateView()
111{
112 View = matr4::View(Position, Focus, UpDirection);
113 InvalidateViewProjection();
114}
115
116void scl::camera::InvalidateBuffers()
117{
118 ResizeMainFrameBuffer();
119 ResizeGBuffer();
120 ResizeHDRFrameBuffer();
121 ResizeBlurFrameBuffers();
122}
123
124void scl::camera::ResizeMainFrameBuffer()
125{
126 frame_buffer_props current_props = MainFrameBuffer->GetFrameBufferProps();
127 if (current_props.Width != ViewportWidth || current_props.Height != ViewportHeight)
128 {
129 current_props.Width = (int)ViewportWidth;
130 current_props.Height = (int)ViewportHeight;
131 MainFrameBuffer->SetFrameBufferProps(current_props);
132 }
133}
134
135void scl::camera::ResizeGBuffer()
136{
137 frame_buffer_props current_props = GBuffer->GetFrameBufferProps();
138 if (current_props.Width != ViewportWidth || current_props.Height != ViewportHeight)
139 {
140 current_props.Width = (int)ViewportWidth;
141 current_props.Height = (int)ViewportHeight;
142 GBuffer->SetFrameBufferProps(current_props);
143 }
144}
145
146void scl::camera::ResizeHDRFrameBuffer()
147{
148 frame_buffer_props current_props = HDRFrameBuffer->GetFrameBufferProps();
149 if (current_props.Width != ViewportWidth || current_props.Height != ViewportHeight)
150 {
151 current_props.Width = (int)ViewportWidth;
152 current_props.Height = (int)ViewportHeight;
153 HDRFrameBuffer->SetFrameBufferProps(current_props);
154 }
155}
156
157void scl::camera::ResizeBlurFrameBuffers()
158{
159 for (int i = 0; i < 2; i++)
160 {
161 frame_buffer_props current_props = BlurFrameBuffers[i]->GetFrameBufferProps();
162 if (current_props.Width != ViewportWidth || current_props.Height != ViewportHeight)
163 {
164 current_props.Width = (int)ViewportWidth;
165 current_props.Height = (int)ViewportHeight;
166 BlurFrameBuffers[i]->SetFrameBufferProps(current_props);
167 }
168 }
169}
170
172 ProjectionType(ProjectionType), Effects(Effects)
173{
174 MainFrameBuffer = frame_buffer::Create(frame_buffer_props { 16, 16, 1, false, 1, 0, true });
175 GBuffer = frame_buffer::Create(frame_buffer_props { 16, 16, 1, false, 6, 1, true });
176 HDRFrameBuffer = frame_buffer::Create(frame_buffer_props { 16, 16, 1, false, 2, 0, true });
177 for (int i = 0; i < 2; i++) BlurFrameBuffers[i] = frame_buffer::Create(frame_buffer_props { 16, 16, 1, false, 1, 0, true });
178
179 Resize(16, 16);
180 SetView({ 0, 3, 10 }, vec3 { 0 }, { 0, 1, 0 });
181}
182
183scl::camera &scl::camera::Resize(int ViewportWidth, int ViewportHeight)
184{
185 this->ViewportWidth = ViewportWidth;
186 this->ViewportHeight = ViewportHeight;
187
188 InvalidateProjection();
189 InvalidateBuffers();
190 return *this;
191}
192
194{
195 matr4 transform =
196 matr4::Translate(-Position) *
197 matr4::Rotate(Axis, Angle) *
198 matr4::Translate(Position);
199
200 Focus = transform.TransformPoint(Focus);
201 Position = transform.TransformPoint(Position);
202 UpDirection = transform.TransformVector(UpDirection);
203 LookDirection = (Focus - Position).Normalized();
204 RightDirection = LookDirection.Cross(UpDirection).Normalized();
205
206 InvalidateView();
207 return *this;
208}
209
211{
212 Position += MoveVector;
213 Focus += MoveVector;
214
215 InvalidateView();
216 return *this;
217}
Renderer virtual camer class definiton module.
camera & Move(const vec3 &MoveVector)
Definition: camera.cpp:210
void SetProjectionDistance(float ProjectionDistance)
Definition: camera.cpp:23
void SetView(const vec3 &Position, const vec3 &Focus, const vec3 &UpDirection)
Definition: camera.cpp:69
void SetProjectionType(camera_projection_type ProjectionType)
Definition: camera.cpp:13
camera & Resize(int ViewportWidth, int ViewportHeight)
Definition: camera.cpp:183
void SetFieldOfView(float FieldOfView)
Definition: camera.cpp:18
void SetDirection(const vec3 &Direction)
Definition: camera.cpp:49
camera(camera_projection_type ProjectionType, camera_effects Effects={})
Definition: camera.cpp:171
void SetFarClip(float FarClip)
Definition: camera.cpp:28
void SetViewportHeight(int ViewportHeight)
Definition: camera.cpp:38
camera & Rotate(const vec3 &Axis, degrees Angle)
Definition: camera.cpp:193
void SetPosition(const vec3 &Position)
Definition: camera.cpp:55
void SetUpDirection(const vec3 &UpDirection)
Definition: camera.cpp:43
void SetRenderToSwapChain(bool IsSwapChainTarget)
Definition: camera.cpp:79
void SetViewportWidth(int ViewportWidth)
Definition: camera.cpp:33
void SetFocus(const vec3 &Focus)
Definition: camera.cpp:62
static shared< frame_buffer > Create(const frame_buffer_props &Props)
static matr4 Rotate(vec3< float > Axis, degrees< float > Angle)
Definition: matr4.h:288
static matr4 Frustum(float Left, float Right, float Bottom, float Top, float Near, float Far)
Definition: matr4.h:359
static matr4 View(vec3< float > Location, vec3< float > At, vec3< float > Up)
Definition: matr4.h:391
static matr4 Ortho(float Left, float Right, float Bottom, float Top, float Near, float Far)
Definition: matr4.h:375
vec3< T > TransformVector(const vec3< T > &V) const
Definition: matr4.h:513
vec3< T > TransformPoint(const vec3< T > &V) const
Definition: matr4.h:500
static matr4 Translate(const vec3< float > &Transform)
Definition: matr4.h:257
vec3 Normalized() const
Definition: vec3.h:190
vec3 Cross(const vec3 &Other) const
Definition: vec3.h:224
Frame buffer interface implementation module.
camera_projection_type
Definition: camera.h:20
Sculpto library prehompiled header. Defines common definitions, includes commonly used modules.