-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathviewport.cpp
More file actions
134 lines (111 loc) · 3.85 KB
/
viewport.cpp
File metadata and controls
134 lines (111 loc) · 3.85 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include "./viewport.hpp"
#include "managers/render_manager.hpp"
#include "things/thing_data.hpp"
#include "theatre/theatre.hpp"
#include "things/thing_factory.hpp"
#define LOCK LockGuard<RMutex> _lock{mFramebufferMutex};
using namespace TheatreFile;
void Viewport::Ready()
{
Super::Ready();
if(mCurrentCamera2D.invalid() or mCurrentCamera3D.invalid())
{ UpdateCurrentCameras(); }
mFramebuffer = FrameBuffer::Create();
SetSize(mSize); // Update FrameBuffer
}
void Viewport::SetVariables(Farg<ThingData> data)
{
Super::SetVariables(data);
data.get_variable(mCurrentCamera3D, "CurrentCamera3D");
data.get_variable(mCurrentCamera2D, "CurrentCamera2D");
if(glm::vec2 size{}; data.get_variable(size, "ViewportSize", "ContentSize") == OK)
{ SetSize(size); }
}
Shared<ThingData> Viewport::GetVariables() const
{
auto data{Super::GetVariables()};
data->set_variable(mCurrentCamera3D, "CurrentCamera3D");
data->set_variable(mCurrentCamera2D, "CurrentCamera2D");
data->set_variable(mSize.glm(), "ViewportSize");
return data;
}
void Viewport::Attach() const
{
LOCK
mFramebuffer->Bind();
g_pRenderManager->GetAPI()->SetClearColor(Settings::Graphics::ClearColor);
g_pRenderManager->GetAPI()->Clear();
g_pRenderManager->GetAPI()->SetViewport({0, 0}, Size());
}
void Viewport::Detach() const
{
LOCK
mFramebuffer->Unbind();
}
Shared<Image> Viewport::GetImage() const
{ return mTexturebuffer->GetImage(); }
Shared<TextureBuffer> Viewport::GetTextureBuffer()
{ return mTexturebuffer; }
uint Viewport::GetTextureBufferID() const
{ return mTexturebuffer->ID(); }
ID Viewport::CurrentCamera3D()
{ return mCurrentCamera3D; }
ID Viewport::CurrentCamera2D()
{ return mCurrentCamera2D; }
void Viewport::SetCurrentCamera3D(ID inUID)
{
mCurrentCamera3D = inUID;
if(inUID.invalid())
{ UpdateCurrentCameras(); }
}
void Viewport::SetCurrentCamera2D(ID inUID)
{
mCurrentCamera2D = inUID;
if(inUID.invalid())
{ UpdateCurrentCameras(); }
}
void Viewport::UpdateCurrentCameras()
{
auto descendants{Theatre::Current()->GetAllChildren(uid())};
for(FAUTO descendant : descendants)
{
if(mCurrentCamera3D.invalid() and Theatre::Current()->DerivedFrom(descendant, ThingType::Camera3D))
{ mCurrentCamera3D = descendant; }
else if(mCurrentCamera2D.invalid() and Theatre::Current()->DerivedFrom(descendant, ThingType::Camera2D))
{ mCurrentCamera2D = descendant; }
}
}
Farg<Size2D> Viewport::Size() const
{ return mSize; }
void Viewport::SetSize(Farg<Size2D> inSize)
{
if(inSize[0] == 0 or inSize[1] == 0)
{ print_warning("Cannot set Viewport width/height to 0"); }
mSize[0] = (inSize[0]) ? inSize[0] : mSize[0];
mSize[1] = (inSize[1]) ? inSize[1] : mSize[1];
LOCK
mTexturebuffer = TextureBuffer::Create();
mTexturebuffer->Load(nullptr, {mSize.w(), mSize.h(), DATA_FORMAT_SRGB});
mTexturebuffer->SetSamplerState({SAMPLER_FILTER_NEAREST, SAMPLER_FILTER_NONE, SAMPLER_FILTER_NEAREST});
mRenderbuffer->SetStorage(inSize);
mFramebuffer->AttachRenderBuffer(mRenderbuffer);
mFramebuffer->AttachTextureBuffer(mTexturebuffer);
}
void Viewport::OnDescendantRemoved(Relative inRelative)
{
if(inRelative.uid == mCurrentCamera3D)
{ mCurrentCamera3D = {}; }
else if(inRelative.uid == mCurrentCamera2D)
{ mCurrentCamera2D = {}; }
if(mCurrentCamera2D.invalid() or mCurrentCamera3D.invalid())
{ UpdateCurrentCameras(); }
}
void Viewport::OnDescendantAdded(Relative inRelative)
{
if(mCurrentCamera3D.invalid()
and ThingFactory::IsDerivedFrom(inRelative.type, ThingType::Camera3D))
{ mCurrentCamera3D = inRelative.uid; }
else if(mCurrentCamera2D.invalid()
and ThingFactory::IsDerivedFrom(inRelative.type, ThingType::Camera2D))
{ mCurrentCamera2D = inRelative.uid; }
}