-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCamera.cpp
116 lines (98 loc) · 4.21 KB
/
Camera.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include "gfx/Include/Context.h"
#include "Include/Camera.h"
#include "Include/Context.h"
#include "Include/CameraController.h"
#include "Include/Bindings.h"
#include <glm/ext.hpp>
namespace hlgfx
{
camera::camera(f32 FOV, f32 AspectRatio, f32 NearClip, f32 FarClip, b8 ShadowCam) : object3D("Camera"), ShadowCam(ShadowCam)
{
this->Ortho=false;
this->Controls = std::make_shared<orbitCameraController>(this);
gfx::context *LowLevelContext = gfx::context::Get();
context *HighLevelContext = context::Get();
//Create uniform buffer and uniform group
this->UniformBuffer = LowLevelContext->CreateBuffer(sizeof(cameraUniformData), gfx::bufferUsage::UniformBuffer, gfx::memoryUsage::CpuToGpu);
this->Uniforms = std::make_shared<gfx::uniformGroup>();
this->Uniforms->Reset();
this->Uniforms->AddUniformBuffer(CameraBinding, this->UniformBuffer);
this->Data.FOV = FOV;
this->Data.AspectRatio = AspectRatio;
this->Data.NearClip = NearClip;
this->Data.FarClip = FarClip;
RecalculateMatrices();
//Bind the uniform group to the context pipelines
for(auto &Pipeline : HighLevelContext->Pipelines)
{
//This allocates the descriptor sets based on the DS layouts of each pipeline
LowLevelContext->BindUniformsToPipeline(this->Uniforms, Pipeline.second, CameraDescriptorSetBinding);
}
this->Uniforms->Update();
}
camera::camera(f32 Left, f32 Right, f32 Bottom, f32 Top, f32 Back, f32 Front, b8 ShadowCam) : object3D("OrthoCamera"), ShadowCam(ShadowCam)
{
this->Ortho=true;
this->Controls = std::make_shared<orbitCameraController>(this);
gfx::context *LowLevelContext = gfx::context::Get();
context *HighLevelContext = context::Get();
//Create uniform buffer and uniform group
this->UniformBuffer = LowLevelContext->CreateBuffer(sizeof(cameraUniformData), gfx::bufferUsage::UniformBuffer, gfx::memoryUsage::CpuToGpu);
this->Uniforms = std::make_shared<gfx::uniformGroup>();
this->Uniforms->Reset();
this->Uniforms->AddUniformBuffer(CameraBinding, this->UniformBuffer);
this->SetOrtho(Left, Right, Bottom, Top, Back, Front);
RecalculateMatrices();
//Bind the uniform group to the context pipelines
for(auto &Pipeline : HighLevelContext->Pipelines)
{
//This allocates the descriptor sets based on the DS layouts of each pipeline
LowLevelContext->BindUniformsToPipeline(this->Uniforms, Pipeline.second, CameraDescriptorSetBinding);
}
this->Uniforms->Update();
}
void camera::SetOrtho(f32 Left, f32 Right, f32 Bottom, f32 Top, f32 Back, f32 Front)
{
this->Ortho = true;
this->Data.LeftRightBottomTop.x = Left;
this->Data.LeftRightBottomTop.y = Right;
this->Data.LeftRightBottomTop.z = Bottom;
this->Data.LeftRightBottomTop.w = Top;
this->Data.BackFront.x = Back;
this->Data.BackFront.y = Front;
RecalculateMatrices();
}
void camera::SetLocalPosition(v3f LocalPosition)
{
this->Transform.SetLocalPosition(LocalPosition);
RecalculateMatrices();
this->Controls->Recalculate();
}
void camera::SetLocalRotation(v3f LocalRotation)
{
this->Transform.SetLocalRotation(LocalRotation);
RecalculateMatrices();
this->Controls->Recalculate();
}
void camera::SetLocalScale(v3f LocalScale)
{
this->Transform.SetLocalScale(LocalScale);
RecalculateMatrices();
this->Controls->Recalculate();
}
void camera::RecalculateMatrices()
{
if(this->Ortho)
this->Data.ProjectionMatrix = glm::ortho(this->Data.LeftRightBottomTop.x, this->Data.LeftRightBottomTop.y, this->Data.LeftRightBottomTop.z, this->Data.LeftRightBottomTop.w, this->Data.BackFront.x, this->Data.BackFront.y);
else
this->Data.ProjectionMatrix = glm::perspective(glm::radians(this->Data.FOV), this->Data.AspectRatio, this->Data.NearClip, this->Data.FarClip);
this->Data.ViewMatrix = this->Transform.Matrices.WorldToLocal;
this->Data.ViewProjectionMatrix = this->Data.ProjectionMatrix * this->Data.ViewMatrix;
this->Data.CameraPosition = v4f(this->Transform.GetWorldPosition(), 1);
gfx::context::Get()->CopyDataToBuffer(this->UniformBuffer, &this->Data, sizeof(cameraUniformData), 0);
}
camera::~camera()
{
gfx::context::Get()->QueueDestroyBuffer(this->UniformBuffer);
}
}