-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path3DSceneExample.cpp
325 lines (289 loc) · 9.59 KB
/
3DSceneExample.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#include <Enlivengine/Engine.hpp>
#include <Enlivengine/Window/EventSystem.hpp>
#include <Enlivengine/Window/Controller.hpp>
#include <Enlivengine/Core/World.hpp>
#include <Enlivengine/Core/TransformComponent.hpp>
#include <Enlivengine/Core/CameraComponent.hpp>
#include <Enlivengine/Core/Components.hpp>
#include <Enlivengine/Graphics/Camera.hpp>
#include <Enlivengine/Graphics/BgfxWrapper.hpp>
#include <Enlivengine/Tools/ImGuiEditor.hpp>
#include <Enlivengine/Audio/AudioManager.hpp>
#include <Enlivengine/Audio/AudioSourceWav.hpp>
#include <Enlivengine/Audio/AudioSourceSpeech.hpp>
using namespace en;
class RenderSystem : public RenderSystemBase
{
public:
const char* GetName() const override;
bool Serialize(Serializer& serializer, const char* name) override;
bool Edit(ObjectEditor& objectEditor, const char* name) override;
void Render() override
{
#ifdef ENLIVE_DEBUG
mWorld->GetDebugDraw().DrawCross(Vector3f(0.0f));
mWorld->GetDebugDraw().DrawGrid(Vector3f::Zero(), ENLIVE_DEFAULT_UP, -16, 16, 1, Colors::White);
#endif // ENLIVE_DEBUG
#ifdef ENLIVE_TOOL
Framebuffer& framebuffer = ImGuiEditor::GetFramebuffer();
Camera* camera = &ImGuiEditor::GetCamera();
#else
Framebuffer& framebuffer = Framebuffer::GetDefaultFramebuffer();
Camera* camera = Camera::GetMainCamera();
#endif // ENLIVE_TOOL
if (framebuffer.IsValid() && camera != nullptr)
{
const bgfx::ViewId mainViewID = 2;
BgfxWrapper::SetCurrentView(mainViewID);
const Vector2u& framebufferSize = framebuffer.GetSize();
const F32 aspectRatio = static_cast<F32>(framebufferSize.x) / static_cast<F32>(framebufferSize.y);
const Vector2f vpMin = camera->GetViewport().GetMin();
const Vector2f vpSize = camera->GetViewport().GetSize();
if (camera->GetAspect() != aspectRatio)
{
camera->SetAspect(aspectRatio);
}
bgfx::setViewClear(mainViewID, BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH, camera->GetClearColor().ToRGBA(), 1.0f, 0);
bgfx::setViewTransform(mainViewID, camera->GetViewMatrix().GetData(), camera->GetProjectionMatrix().GetData());
bgfx::setViewRect(mainViewID, static_cast<U16>(vpMin.x * framebufferSize.x), static_cast<U16>(vpMin.y * framebufferSize.y), static_cast<U16>(vpSize.x * framebufferSize.x), static_cast<U16>(vpSize.y * framebufferSize.y));
bgfx::setViewFrameBuffer(mainViewID, framebuffer.GetHandle());
bgfx::touch(mainViewID);
RenderWorld();
#ifdef ENLIVE_DEBUG
#ifdef ENLIVE_TOOL
if (ImGuiEditor::IsShowingDebug())
#else
if (mWorld->IsDebugRendering())
#endif // ENLIVE_TOOL
{
mWorld->GetDebugDraw().Render();
}
mWorld->GetDebugDraw().Clear();
#endif // ENLIVE_DEBUG
}
}
void RenderWorld()
{
auto& entityManager = mWorld->GetEntityManager();
auto view = entityManager.View<TransformComponent>();
for (auto entt : view)
{
Entity entity(entityManager, entt);
if (entity.IsValid())
{
bool render = false;
const Matrix4f matrix = (entity.Has<TransformComponent>()) ? entity.Get<TransformComponent>().GetGlobalMatrix() : Matrix4f::Identity();
if (entity.Has<SpriteComponent>())
{
Sprite& sprite = entity.Get<SpriteComponent>().sprite;
if (sprite.CanRender())
{
bgfx::setTransform(matrix.GetData());
sprite.Render();
render = true;
}
}
if (entity.Has<TilemapComponent>())
{
Tilemap& tilemap = entity.Get<TilemapComponent>().tilemap;
if (tilemap.CanRender())
{
bgfx::setTransform(matrix.GetData());
entity.Get<TilemapComponent>().tilemap.Render();
render = true;
}
}
if (!render)
{
bgfx::setTransform(Matrix4f::Identity().GetData());
}
}
}
}
};
ENLIVE_META_CLASS_BEGIN(RenderSystem, en::Type_ClassSerialization, en::Type_ClassEditor)
ENLIVE_META_CLASS_END()
const char* RenderSystem::GetName() const
{
return TypeInfo<RenderSystem>::GetName();
}
bool RenderSystem::Serialize(Serializer& serializer, const char* name)
{
return GenericSerialization(serializer, name, *this);
}
bool RenderSystem::Edit(ObjectEditor& objectEditor, const char* name)
{
return GenericEdit(objectEditor, name, *this);
}
class DebugSystem : public System
{
public:
DebugSystem() : System()
{
#ifdef ENLIVE_TOOL
mUpdateOnTool = true;
#endif // ENLIVE_TOOL
}
const char* GetName() const override;
bool Serialize(Serializer& serializer, const char* name) override;
bool Edit(ObjectEditor& objectEditor, const char* name) override;
void Update() override
{
#ifdef ENLIVE_DEBUG
mWorld->GetDebugDraw().DrawBox({ 1.0f, 0.5f, 1.0f }, { 2.0f, 1.5f, 2.0f }, Colors::Red);
mWorld->GetDebugDraw().DrawSphere({ -1.0f, 0.5f, -3.0f }, 0.5f, Colors::Red);
auto& entityManager = mWorld->GetEntityManager();
auto view = entityManager.View<en::CameraComponent>();
for (auto entt : view)
{
Entity entity(entityManager, entt);
if (entity.IsValid() && mWorld->IsSelected(entity))
{
auto& cam = entity.Get<en::CameraComponent>();
mWorld->GetDebugDraw().DrawFrustum(cam.CreateFrustum());
}
}
#endif // ENLIVE_DEBUG
}
};
ENLIVE_META_CLASS_BEGIN(DebugSystem, en::Type_ClassSerialization, en::Type_ClassEditor)
ENLIVE_META_CLASS_END()
const char* DebugSystem::GetName() const
{
return TypeInfo<DebugSystem>::GetName();
}
bool DebugSystem::Serialize(Serializer& serializer, const char* name)
{
return GenericSerialization(serializer, name, *this);
}
bool DebugSystem::Edit(ObjectEditor& objectEditor, const char* name)
{
return GenericEdit(objectEditor, name, *this);
}
struct StupidShipComponent {};
ENLIVE_META_CLASS_BEGIN(StupidShipComponent, en::Type_ClassSerialization, en::Type_ClassEditor)
ENLIVE_META_CLASS_END()
class StupidShipSystem : public System
{
public:
const char* GetName() const override;
bool Serialize(Serializer& serializer, const char* name) override;
bool Edit(ObjectEditor& objectEditor, const char* name) override;
void Update() override
{
auto& entityManager = mWorld->GetEntityManager();
auto view = entityManager.View<TransformComponent, StupidShipComponent>();
for (auto entt : view)
{
Entity entity(entityManager, entt);
if (entity.IsValid())
{
entity.Get<TransformComponent>().Rotate(Matrix3f::RotationY(180.0f * Time::GetDeltaTime().AsSeconds()));
}
}
static bool played = false;
if (!played)
{
speech.SetText("1, 2, 3");
AudioManager::GetInstance().PlaySource(speech);
AudioManager::GetInstance().PlaySource(ResourceManager::GetInstance().Get<AudioSourceWav>("theme").Get());
played = true;
}
}
AudioSourceSpeech speech;
};
ENLIVE_META_CLASS_BEGIN(StupidShipSystem, en::Type_ClassSerialization, en::Type_ClassEditor)
ENLIVE_META_CLASS_END()
const char* StupidShipSystem::GetName() const
{
return TypeInfo<StupidShipSystem>::GetName();
}
bool StupidShipSystem::Serialize(Serializer& serializer, const char* name)
{
return GenericSerialization(serializer, name, *this);
}
bool StupidShipSystem::Edit(ObjectEditor& objectEditor, const char* name)
{
return GenericEdit(objectEditor, name, *this);
}
struct PlayerComponent {};
ENLIVE_META_CLASS_BEGIN(PlayerComponent, en::Type_ClassSerialization, en::Type_ClassEditor)
ENLIVE_META_CLASS_END()
class PlayerSystem : public System
{
public:
const char* GetName() const override;
bool Serialize(Serializer& serializer, const char* name) override;
bool Edit(ObjectEditor& objectEditor, const char* name) override;
void Update() override
{
if (EventSystem::IsButtonActive("action") || EventSystem::IsButtonActive("jactionP1"))
{
printf("Action!\n");
Controller::Rumble(0, 0.25f, 100);
}
auto& entityManager = mWorld->GetEntityManager();
auto view = entityManager.View<TransformComponent, PlayerComponent>();
for (auto entt : view)
{
Entity entity(entityManager, entt);
if (entity.IsValid())
{
const F32 forwardMvt = -Controller::GetAxis(0, 1);
const F32 leftMvt = -Controller::GetAxis(0, 0);
const F32 deltaYaw = Controller::GetAxis(0, 3);
const F32 deltaPitch = Controller::GetAxis(0, 4);
const F32 dtSeconds = Time::GetDeltaTime().AsSeconds();
TransformComponent& playerTransform = entity.Get<TransformComponent>();
Vector3f direction = playerTransform.GetRotation().GetForward();
// Movement
if (forwardMvt != 0.0f || leftMvt != 0.0f)
{
Vector3f mvtUnit = direction;
mvtUnit.y = 0.0f;
mvtUnit.Normalize();
Vector3f movement;
movement += 3.0f * forwardMvt * mvtUnit * dtSeconds;
movement -= 3.0f * leftMvt * mvtUnit.CrossProduct(ENLIVE_DEFAULT_UP) * dtSeconds;
playerTransform.Move(movement);
}
// Rotation
if (deltaYaw != 0.0f || deltaPitch != 0.0f)
{
if (!Math::Equals(deltaYaw, 0.0f))
{
playerTransform.SetRotation(playerTransform.GetRotation() * Matrix3f::RotationY(100.0f * dtSeconds * deltaYaw));
}
if (!Math::Equals(deltaPitch, 0.0f))
{
//playerTransform.Rotate(Quaternionf(100.0f * dtSeconds * deltaPitch, direction.CrossProduct(ENLIVE_DEFAULT_UP)));
}
}
}
}
}
};
ENLIVE_META_CLASS_BEGIN(PlayerSystem, en::Type_ClassSerialization, en::Type_ClassEditor)
ENLIVE_META_CLASS_END()
const char* PlayerSystem::GetName() const
{
return TypeInfo<PlayerSystem>::GetName();
}
bool PlayerSystem::Serialize(Serializer& serializer, const char* name)
{
return GenericSerialization(serializer, name, *this);
}
bool PlayerSystem::Edit(ObjectEditor& objectEditor, const char* name)
{
return GenericEdit(objectEditor, name, *this);
}
int main(int argc, char** argv)
{
Engine::RegisterComponent<StupidShipComponent>();
Engine::RegisterComponent<PlayerComponent>();
Engine::RegisterSystem<RenderSystem>();
Engine::RegisterSystem<DebugSystem>();
Engine::RegisterSystem<StupidShipSystem>();
Engine::RegisterSystem<PlayerSystem>();
return Engine::Main(argc, argv);
}