feat: refactor and move out the renderer with entt registry + better event system
This commit is contained in:
@ -3,18 +3,21 @@ option(ENGINE_BUILD_SHARED "Build the Engine library as a shared library" ON)
|
|||||||
set(SOURCES
|
set(SOURCES
|
||||||
src/IO/parser.cpp
|
src/IO/parser.cpp
|
||||||
src/IO/file_manager.cpp
|
src/IO/file_manager.cpp
|
||||||
|
src/renderer/debug.cpp
|
||||||
|
|
||||||
src/window/window.cpp
|
src/window/window.cpp
|
||||||
|
|
||||||
src/components/batch.cpp
|
src/scene/scene.cpp
|
||||||
|
|
||||||
src/renderer/debug.cpp
|
src/components/batch.cpp
|
||||||
src/renderer/mesh.cpp
|
src/renderer/mesh.cpp
|
||||||
src/renderer/shader.cpp
|
src/renderer/shader.cpp
|
||||||
src/renderer/texture.cpp
|
src/renderer/texture.cpp
|
||||||
src/renderer/core.cpp
|
|
||||||
src/renderer/renderer.cpp
|
src/renderer/renderer.cpp
|
||||||
|
|
||||||
src/renderer/wavefront.cpp
|
src/renderer/wavefront.cpp
|
||||||
|
|
||||||
|
src/renderer/core.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
if(ENGINE_BUILD_SHARED)
|
if(ENGINE_BUILD_SHARED)
|
||||||
|
|||||||
@ -1,20 +1,19 @@
|
|||||||
#ifndef APPLICATION_H_
|
#ifndef APPLICATION_H_
|
||||||
#define APPLICATION_H_
|
#define APPLICATION_H_
|
||||||
|
|
||||||
#include "engine/window/events/window.h"
|
#include "engine/scene/scene.h"
|
||||||
|
#include "engine/window/event.h"
|
||||||
#include "engine/export.h"
|
#include "engine/export.h"
|
||||||
|
|
||||||
class ENGINE_API IApplication {
|
class ENGINE_API IApplication {
|
||||||
public:
|
public:
|
||||||
virtual ~IApplication() = default;
|
virtual ~IApplication() = default;
|
||||||
|
|
||||||
virtual void OnInit() {};
|
virtual void OnInit(std::shared_ptr<Scene> scene) {};
|
||||||
virtual void OnUpdate() {};
|
virtual void OnUpdate() {};
|
||||||
virtual void OnRender() {};
|
|
||||||
virtual void OnShutdown() {};
|
virtual void OnShutdown() {};
|
||||||
|
|
||||||
virtual void OnEvent() {};
|
virtual void OnEvent(const Event& event) {};
|
||||||
virtual void OnWindowResized(const WindowResized& e) {};
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // APPLICATION_H_
|
#endif // APPLICATION_H_
|
||||||
@ -1,7 +1,7 @@
|
|||||||
#ifndef COMPONENT_BATCH_H_
|
#ifndef COMPONENT_BATCH_H_
|
||||||
#define COMPONENT_BATCH_H_
|
#define COMPONENT_BATCH_H_
|
||||||
|
|
||||||
#include "engine/renderer/renderer.h"
|
#include <glm/mat4x4.hpp>
|
||||||
#include "engine/export.h"
|
#include "engine/export.h"
|
||||||
|
|
||||||
// requires mesh component
|
// requires mesh component
|
||||||
|
|||||||
@ -3,8 +3,6 @@
|
|||||||
|
|
||||||
#include <glm/glm.hpp>
|
#include <glm/glm.hpp>
|
||||||
|
|
||||||
#include "engine/renderer/mesh.h"
|
|
||||||
|
|
||||||
class Vertex {
|
class Vertex {
|
||||||
friend class Mesh;
|
friend class Mesh;
|
||||||
private:
|
private:
|
||||||
@ -14,8 +12,6 @@ private:
|
|||||||
public:
|
public:
|
||||||
Vertex(glm::vec3 position, glm::vec3 normal, glm::vec2 texCoord)
|
Vertex(glm::vec3 position, glm::vec3 normal, glm::vec2 texCoord)
|
||||||
: m_position(position), m_normal(normal), m_texCoord(texCoord) {}
|
: m_position(position), m_normal(normal), m_texCoord(texCoord) {}
|
||||||
public:
|
|
||||||
static void DefineAttrib();
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // RENDERER_BASICS_H
|
#endif // RENDERER_BASICS_H
|
||||||
@ -7,6 +7,9 @@
|
|||||||
#include "engine/window/window.h"
|
#include "engine/window/window.h"
|
||||||
#include "engine/window/events/window.h"
|
#include "engine/window/events/window.h"
|
||||||
|
|
||||||
|
#include "engine/renderer/renderer.h"
|
||||||
|
#include "engine/scene/scene.h"
|
||||||
|
|
||||||
#include "engine/app/app.h"
|
#include "engine/app/app.h"
|
||||||
#include "engine/export.h"
|
#include "engine/export.h"
|
||||||
|
|
||||||
@ -16,6 +19,8 @@ public:
|
|||||||
private:
|
private:
|
||||||
static std::unique_ptr<IApplication> s_app;
|
static std::unique_ptr<IApplication> s_app;
|
||||||
static std::shared_ptr<Window> s_window;
|
static std::shared_ptr<Window> s_window;
|
||||||
|
static std::unique_ptr<Renderer> s_renderer;
|
||||||
|
static std::shared_ptr<Scene> s_scene;
|
||||||
static bool s_running;
|
static bool s_running;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -2,8 +2,8 @@
|
|||||||
#define RENDERER_H_
|
#define RENDERER_H_
|
||||||
|
|
||||||
#include <glm/glm.hpp>
|
#include <glm/glm.hpp>
|
||||||
#include <entt/entity/registry.hpp>
|
|
||||||
|
|
||||||
|
#include "engine/scene/scene.h"
|
||||||
#include "engine/renderer/shader.h"
|
#include "engine/renderer/shader.h"
|
||||||
#include "engine/export.h"
|
#include "engine/export.h"
|
||||||
#include "engine/components/light.h"
|
#include "engine/components/light.h"
|
||||||
@ -11,23 +11,23 @@
|
|||||||
// TODO: make static or singleton
|
// TODO: make static or singleton
|
||||||
class ENGINE_API Renderer {
|
class ENGINE_API Renderer {
|
||||||
public:
|
public:
|
||||||
Renderer(entt::registry& registry);
|
Renderer(std::shared_ptr<Scene> scene);
|
||||||
|
|
||||||
void Render();
|
void Render();
|
||||||
void Init();
|
void Init();
|
||||||
void GenerateShadowMaps();
|
|
||||||
|
|
||||||
void OnWindowResized(int w, int h);
|
void OnWindowResized(int w, int h);
|
||||||
private:
|
private:
|
||||||
void ApplyLights(Shader &shader);
|
void ApplyLights(Shader &shader);
|
||||||
void UpdateView();
|
void UpdateView();
|
||||||
void RenderScene(Shader &shader);
|
void RenderScene(Shader &shader);
|
||||||
|
void GenerateShadowMaps();
|
||||||
void EnsureShadowResources(light& l);
|
void EnsureShadowResources(light& l);
|
||||||
private:
|
private:
|
||||||
Shader m_shader;
|
Shader m_shader;
|
||||||
Shader m_depthShader;
|
Shader m_depthShader;
|
||||||
|
|
||||||
entt::registry& m_registry;
|
std::shared_ptr<Scene> m_scene;
|
||||||
|
|
||||||
// unsigned int m_depth_fbo;
|
// unsigned int m_depth_fbo;
|
||||||
// unsigned int m_depthMap;
|
// unsigned int m_depthMap;
|
||||||
|
|||||||
15
engine/include/engine/scene/scene.h
Normal file
15
engine/include/engine/scene/scene.h
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
#ifndef ENGINE_SCENE_H_
|
||||||
|
#define ENGINE_SCENE_H_
|
||||||
|
|
||||||
|
#include <entt/entt.hpp>
|
||||||
|
|
||||||
|
class Scene {
|
||||||
|
public:
|
||||||
|
Scene();
|
||||||
|
private:
|
||||||
|
entt::registry m_registry;
|
||||||
|
friend class Renderer;
|
||||||
|
friend class Game;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // ENGINE_SCENE_H_
|
||||||
@ -7,6 +7,30 @@
|
|||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
enum class EventType {
|
||||||
|
WINDOW_RESIZE,
|
||||||
|
WINDOW_CLOSE,
|
||||||
|
};
|
||||||
|
|
||||||
|
class Event {
|
||||||
|
public:
|
||||||
|
enum EventCategory {
|
||||||
|
WINDOW,
|
||||||
|
// KEYBOARD ...
|
||||||
|
};
|
||||||
|
|
||||||
|
Event(EventCategory category) : m_category(category) {}
|
||||||
|
virtual ~Event() {}
|
||||||
|
|
||||||
|
Event(const Event& event) = default;
|
||||||
|
|
||||||
|
inline const EventCategory GetCategory() const { return m_category; }
|
||||||
|
|
||||||
|
inline const virtual EventType GetType() const = 0;
|
||||||
|
private:
|
||||||
|
EventCategory m_category;
|
||||||
|
};
|
||||||
|
|
||||||
class EventDispatcher {
|
class EventDispatcher {
|
||||||
using Type = std::type_index;
|
using Type = std::type_index;
|
||||||
using RawFn = std::function<void(const void*)>;
|
using RawFn = std::function<void(const void*)>;
|
||||||
|
|||||||
@ -1,7 +1,30 @@
|
|||||||
#ifndef WINDOW_EVENTS_H_
|
#ifndef WINDOW_EVENTS_H_
|
||||||
#define WINDOW_EVENTS_H_
|
#define WINDOW_EVENTS_H_
|
||||||
|
|
||||||
struct WindowResized { int w, h; };
|
#include "engine/window/event.h"
|
||||||
struct WindowCloseRequested {};
|
|
||||||
|
class WindowEvent : public Event {
|
||||||
|
public:
|
||||||
|
WindowEvent() : Event(Event::EventCategory::WINDOW) {}
|
||||||
|
|
||||||
|
inline const EventType GetType() const override { return EventType::WINDOW_CLOSE; }
|
||||||
|
};
|
||||||
|
|
||||||
|
class WindowResizeEvent : public WindowEvent {
|
||||||
|
public:
|
||||||
|
WindowResizeEvent(unsigned int w, unsigned int h) : m_width(w), m_height(h) {}
|
||||||
|
|
||||||
|
inline const EventType GetType() const override { return EventType::WINDOW_RESIZE; }
|
||||||
|
|
||||||
|
inline const unsigned int GetWidth() const { return m_width; }
|
||||||
|
inline const unsigned int GetHeight() const { return m_height; }
|
||||||
|
private:
|
||||||
|
unsigned int m_width, m_height;
|
||||||
|
};
|
||||||
|
|
||||||
|
class WindowCloseEvent : public WindowEvent {
|
||||||
|
public:
|
||||||
|
WindowCloseEvent() {}
|
||||||
|
};
|
||||||
|
|
||||||
#endif // WINDOW_EVENTS_H_
|
#endif // WINDOW_EVENTS_H_
|
||||||
@ -1,27 +1,34 @@
|
|||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
#include "engine/renderer/core.h"
|
#include "engine/renderer/core.h"
|
||||||
#include "engine/window/event.h"
|
|
||||||
|
|
||||||
|
#include "engine/window/event.h"
|
||||||
#include "engine/renderer/wavefront.h"
|
#include "engine/renderer/wavefront.h"
|
||||||
|
|
||||||
std::unique_ptr<IApplication> Engine::s_app = nullptr;
|
std::unique_ptr<IApplication> Engine::s_app = nullptr;
|
||||||
std::shared_ptr<Window> Engine::s_window = nullptr;
|
std::shared_ptr<Window> Engine::s_window = nullptr;
|
||||||
|
std::unique_ptr<Renderer> Engine::s_renderer = nullptr;
|
||||||
|
std::shared_ptr<Scene> Engine::s_scene = nullptr;
|
||||||
bool Engine::s_running = false;
|
bool Engine::s_running = false;
|
||||||
|
|
||||||
void Engine::Run(std::unique_ptr<IApplication> app) {
|
void Engine::Run(std::unique_ptr<IApplication> app) {
|
||||||
s_app = std::move(app);
|
s_scene = std::make_shared<Scene>();
|
||||||
|
s_renderer = std::make_unique<Renderer>(s_scene);
|
||||||
s_window = Window::GetInstance();
|
s_window = Window::GetInstance();
|
||||||
|
s_app = std::move(app);
|
||||||
s_running = true;
|
s_running = true;
|
||||||
|
|
||||||
s_app->OnInit();
|
s_app->OnInit(s_scene);
|
||||||
|
s_renderer->Init();
|
||||||
|
|
||||||
s_window->Subscribe<WindowCloseRequested>([](const WindowCloseRequested& e) {
|
s_window->Subscribe<WindowCloseEvent>([&](const WindowCloseEvent& e) {
|
||||||
Engine::s_running = false;
|
s_running = false;
|
||||||
|
s_app->OnEvent(e);
|
||||||
});
|
});
|
||||||
|
|
||||||
s_window->Subscribe<WindowResized>([](const WindowResized& e) {
|
s_window->Subscribe<WindowResizeEvent>([&](const WindowResizeEvent& e) {
|
||||||
Engine::s_app->OnWindowResized(e);
|
s_renderer->OnWindowResized(e.GetWidth(), e.GetHeight());
|
||||||
|
s_app->OnEvent(e);
|
||||||
});
|
});
|
||||||
|
|
||||||
while (s_running) {
|
while (s_running) {
|
||||||
@ -29,7 +36,7 @@ void Engine::Run(std::unique_ptr<IApplication> app) {
|
|||||||
|
|
||||||
s_app->OnUpdate();
|
s_app->OnUpdate();
|
||||||
|
|
||||||
s_app->OnRender();
|
s_renderer->Render();
|
||||||
|
|
||||||
s_window->SwapBuffers();
|
s_window->SwapBuffers();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -18,7 +18,7 @@
|
|||||||
#include "engine/components/mesh.h"
|
#include "engine/components/mesh.h"
|
||||||
#include "engine/components/batch.h"
|
#include "engine/components/batch.h"
|
||||||
|
|
||||||
Renderer::Renderer(entt::registry& registry) : m_registry(registry)
|
Renderer::Renderer(std::shared_ptr<Scene> scene) : m_scene(scene)
|
||||||
{
|
{
|
||||||
m_proj = glm::perspective(
|
m_proj = glm::perspective(
|
||||||
static_cast<float>(M_PI_2),
|
static_cast<float>(M_PI_2),
|
||||||
@ -44,15 +44,7 @@ Renderer::Renderer(entt::registry& registry) : m_registry(registry)
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Renderer::Init() {
|
void Renderer::Init() {
|
||||||
// auto view = m_registry.view<batch, mesh>();
|
GenerateShadowMaps();
|
||||||
// for (auto [_, b, m] : m_registry.view<batch, mesh>().each()) {
|
|
||||||
// unsigned int items = 0;
|
|
||||||
// for (auto [entt, item] : m_registry.view<batch::item>().each()) {
|
|
||||||
// if (item.batchId == b.id()) ++items;
|
|
||||||
// }
|
|
||||||
// b.prepare()
|
|
||||||
// m.object->EnableBatch(b.m_instance_vbo);
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Renderer::OnWindowResized(int w, int h) {
|
void Renderer::OnWindowResized(int w, int h) {
|
||||||
@ -65,13 +57,13 @@ void Renderer::OnWindowResized(int w, int h) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Renderer::ApplyLights(Shader &shader) {
|
void Renderer::ApplyLights(Shader &shader) {
|
||||||
auto lights = m_registry.view<light>();
|
auto lights = m_scene->m_registry.view<light>();
|
||||||
// TODO: Pass Lights Data to depth shader as well
|
// TODO: Pass Lights Data to depth shader as well
|
||||||
shader.setInt("lightsCount", static_cast<int>(lights.size()));
|
shader.setInt("lightsCount", static_cast<int>(lights.size()));
|
||||||
size_t lightIndex = 0;
|
size_t lightIndex = 0;
|
||||||
for (auto entity : lights) {
|
for (auto entity : lights) {
|
||||||
auto &l = m_registry.get<light>(entity);
|
auto &l = m_scene->m_registry.get<light>(entity);
|
||||||
auto &transf = m_registry.get<transform>(entity);
|
auto &transf = m_scene->m_registry.get<transform>(entity);
|
||||||
|
|
||||||
shader.setInt("lights[" + std::to_string(lightIndex) + "].type", static_cast<int>(l.type));
|
shader.setInt("lights[" + std::to_string(lightIndex) + "].type", static_cast<int>(l.type));
|
||||||
shader.setVec3("lights[" + std::to_string(lightIndex) + "].position", transf.position);
|
shader.setVec3("lights[" + std::to_string(lightIndex) + "].position", transf.position);
|
||||||
@ -113,24 +105,27 @@ void Renderer::EnsureShadowResources(light& l) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Renderer::UpdateView() {
|
void Renderer::UpdateView() {
|
||||||
auto cam = m_registry.view<transform, camera>().back();
|
auto camView = m_scene->m_registry.view<camera>();
|
||||||
auto camTransform = m_registry.get<transform>(cam);
|
auto camTransform = camView.size() > 0 ?
|
||||||
|
m_scene->m_registry.get<transform>(camView.back()) :
|
||||||
|
transform {glm::vec3(0.f, 0.f, 0.f), glm::vec3(0.f, 0.f, 0.f), glm::vec3(0.f, 0.f, 0.f)};
|
||||||
|
|
||||||
m_view = glm::lookAt(
|
m_view = glm::lookAt(
|
||||||
camTransform.position,
|
camTransform.position,
|
||||||
camTransform.position + camTransform.rotation,
|
camTransform.position + camTransform.rotation,
|
||||||
glm::vec3(0.f, 1.f, 0.f)
|
glm::vec3(0.f, 1.f, 0.f)
|
||||||
);
|
);
|
||||||
m_shader.setMat4("u_view", m_view);
|
|
||||||
m_shader.setMat4("u_projection", m_proj);
|
|
||||||
|
|
||||||
m_shader.setVec3("viewPos", camTransform.position);
|
m_shader.setVec3("viewPos", camTransform.position);
|
||||||
|
|
||||||
|
m_shader.setMat4("u_view", m_view);
|
||||||
|
m_shader.setMat4("u_projection", m_proj);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Renderer::RenderScene(Shader &shader) {
|
void Renderer::RenderScene(Shader &shader) {
|
||||||
std::unordered_map<unsigned int, std::vector<entt::entity>> batches;
|
std::unordered_map<unsigned int, std::vector<entt::entity>> batches;
|
||||||
|
|
||||||
for (auto [entt, item] : m_registry.view<batch::item>().each()) {
|
for (auto [entt, item] : m_scene->m_registry.view<batch::item>().each()) {
|
||||||
if (batches.find(item.batchId) == batches.end())
|
if (batches.find(item.batchId) == batches.end())
|
||||||
batches.insert(std::make_pair(item.batchId, std::vector<entt::entity>()));
|
batches.insert(std::make_pair(item.batchId, std::vector<entt::entity>()));
|
||||||
|
|
||||||
@ -140,7 +135,7 @@ void Renderer::RenderScene(Shader &shader) {
|
|||||||
shader.setBool("u_isInstanced", true);
|
shader.setBool("u_isInstanced", true);
|
||||||
shader.setBool("isLight", false);
|
shader.setBool("isLight", false);
|
||||||
shader.setVec3("currentLightColor", glm::vec3(0.f));
|
shader.setVec3("currentLightColor", glm::vec3(0.f));
|
||||||
for (auto [entt, b, m] : m_registry.view<batch, mesh>().each()) {
|
for (auto [entt, b, m] : m_scene->m_registry.view<batch, mesh>().each()) {
|
||||||
// check if have items for batch render
|
// check if have items for batch render
|
||||||
if (batches.find(b.id()) == batches.end()) continue;
|
if (batches.find(b.id()) == batches.end()) continue;
|
||||||
|
|
||||||
@ -150,7 +145,7 @@ void Renderer::RenderScene(Shader &shader) {
|
|||||||
models.reserve(batchItems.size());
|
models.reserve(batchItems.size());
|
||||||
|
|
||||||
for (auto item : batchItems) {
|
for (auto item : batchItems) {
|
||||||
auto &t = m_registry.get<transform>(item);
|
auto &t = m_scene->m_registry.get<transform>(item);
|
||||||
glm::mat4 rotation = glm::yawPitchRoll(t.rotation.y, t.rotation.x, t.rotation.z);
|
glm::mat4 rotation = glm::yawPitchRoll(t.rotation.y, t.rotation.x, t.rotation.z);
|
||||||
auto itemModel = glm::translate(glm::mat4(1.f), t.position) * rotation;
|
auto itemModel = glm::translate(glm::mat4(1.f), t.position) * rotation;
|
||||||
models.push_back(itemModel);
|
models.push_back(itemModel);
|
||||||
@ -166,14 +161,14 @@ void Renderer::RenderScene(Shader &shader) {
|
|||||||
}
|
}
|
||||||
shader.setBool("u_isInstanced", false);
|
shader.setBool("u_isInstanced", false);
|
||||||
|
|
||||||
for (auto [entity, transf, mesh] : m_registry.view<transform, mesh>(entt::exclude<batch, batch::item>).each()) {
|
for (auto [entity, transf, mesh] : m_scene->m_registry.view<transform, mesh>(entt::exclude<batch, batch::item>).each()) {
|
||||||
if (mesh.object == nullptr) {
|
if (mesh.object == nullptr) {
|
||||||
std::cerr << "WARN: Entity doesn't have a mesh to render" << std::endl;
|
std::cerr << "WARN: Entity doesn't have a mesh to render" << std::endl;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (m_registry.all_of<light>(entity)) {
|
if (m_scene->m_registry.all_of<light>(entity)) {
|
||||||
auto &l = m_registry.get<light>(entity);
|
auto &l = m_scene->m_registry.get<light>(entity);
|
||||||
shader.setBool("isLight", true);
|
shader.setBool("isLight", true);
|
||||||
shader.setVec3("currentLightColor", l.color);
|
shader.setVec3("currentLightColor", l.color);
|
||||||
} else {
|
} else {
|
||||||
@ -193,7 +188,7 @@ void Renderer::RenderScene(Shader &shader) {
|
|||||||
void Renderer::GenerateShadowMaps() {
|
void Renderer::GenerateShadowMaps() {
|
||||||
m_depthShader.use();
|
m_depthShader.use();
|
||||||
|
|
||||||
auto lights = m_registry.view<light>();
|
auto lights = m_scene->m_registry.view<light>();
|
||||||
|
|
||||||
for (auto [_, l] : lights.each()) {
|
for (auto [_, l] : lights.each()) {
|
||||||
// TODO: support other light types when ready
|
// TODO: support other light types when ready
|
||||||
@ -207,7 +202,7 @@ void Renderer::Render() {
|
|||||||
|
|
||||||
glCullFace(GL_FRONT);
|
glCullFace(GL_FRONT);
|
||||||
|
|
||||||
const auto lights = m_registry.view<light, transform>();
|
const auto lights = m_scene->m_registry.view<light, transform>();
|
||||||
|
|
||||||
for (auto [_, l, t] : lights.each()) {
|
for (auto [_, l, t] : lights.each()) {
|
||||||
// TODO: support other light types when ready
|
// TODO: support other light types when ready
|
||||||
|
|||||||
@ -5,9 +5,10 @@
|
|||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
#include <GL/glew.h>
|
#include <GL/glew.h>
|
||||||
|
|
||||||
|
#include "engine/renderer/wavefront.h"
|
||||||
|
|
||||||
#include "engine/IO/parser.h"
|
#include "engine/IO/parser.h"
|
||||||
#include "engine/renderer/mesh.h"
|
#include "engine/renderer/mesh.h"
|
||||||
#include "engine/renderer/wavefront.h"
|
|
||||||
|
|
||||||
#define DEFAULT_MATERIAL_NAME "default"
|
#define DEFAULT_MATERIAL_NAME "default"
|
||||||
|
|
||||||
|
|||||||
4
engine/src/scene/scene.cpp
Normal file
4
engine/src/scene/scene.cpp
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
#include "engine/scene/scene.h"
|
||||||
|
|
||||||
|
Scene::Scene() : m_registry() {
|
||||||
|
}
|
||||||
@ -100,11 +100,11 @@ void Window::ProcessEvents() {
|
|||||||
switch (event.type) {
|
switch (event.type) {
|
||||||
case SDL_EVENT_WINDOW_CLOSE_REQUESTED:
|
case SDL_EVENT_WINDOW_CLOSE_REQUESTED:
|
||||||
case SDL_EVENT_QUIT:
|
case SDL_EVENT_QUIT:
|
||||||
Dispatch(WindowCloseRequested());
|
Dispatch(WindowCloseEvent());
|
||||||
break;
|
break;
|
||||||
case SDL_EVENT_KEY_DOWN:
|
case SDL_EVENT_KEY_DOWN:
|
||||||
if (event.key.scancode == SDL_SCANCODE_ESCAPE) {
|
if (event.key.scancode == SDL_SCANCODE_ESCAPE) {
|
||||||
Dispatch(WindowCloseRequested());
|
Dispatch(WindowCloseEvent());
|
||||||
}
|
}
|
||||||
if (event.key.scancode == SDL_SCANCODE_F11) {
|
if (event.key.scancode == SDL_SCANCODE_F11) {
|
||||||
bool isFullscreen = SDL_GetWindowFlags(m_handle) & SDL_WINDOW_FULLSCREEN;
|
bool isFullscreen = SDL_GetWindowFlags(m_handle) & SDL_WINDOW_FULLSCREEN;
|
||||||
@ -122,7 +122,7 @@ void Window::ProcessEvents() {
|
|||||||
0,
|
0,
|
||||||
width,
|
width,
|
||||||
height);
|
height);
|
||||||
Dispatch(WindowResized{ m_width, m_height });
|
Dispatch(WindowResizeEvent(static_cast<unsigned int>(m_width), static_cast<unsigned int>(m_height)));
|
||||||
SDL_SetWindowRelativeMouseMode(m_handle, true);
|
SDL_SetWindowRelativeMouseMode(m_handle, true);
|
||||||
SDL_Rect boundaries = {0, 0, m_width, m_height};
|
SDL_Rect boundaries = {0, 0, m_width, m_height};
|
||||||
SDL_SetWindowMouseRect(m_handle, &boundaries);
|
SDL_SetWindowMouseRect(m_handle, &boundaries);
|
||||||
|
|||||||
@ -9,7 +9,6 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "engine/renderer/wavefront.h"
|
#include "engine/renderer/wavefront.h"
|
||||||
#include "engine/renderer/renderer.h"
|
|
||||||
|
|
||||||
#include "engine/app/app.h"
|
#include "engine/app/app.h"
|
||||||
|
|
||||||
@ -24,55 +23,57 @@
|
|||||||
|
|
||||||
class Game : public IApplication {
|
class Game : public IApplication {
|
||||||
public:
|
public:
|
||||||
Game() : m_renderer(m_registry) {
|
Game() {}
|
||||||
|
~Game() override {}
|
||||||
|
|
||||||
|
void OnInit(std::shared_ptr<Scene> scene) override {
|
||||||
|
m_scene = scene;
|
||||||
|
|
||||||
Object* lightObj = Object::LoadFile("./assets/common/sphere/sphere.obj");
|
Object* lightObj = Object::LoadFile("./assets/common/sphere/sphere.obj");
|
||||||
const auto lght = m_registry.create();
|
const auto lght = scene->m_registry.create();
|
||||||
m_registry.emplace<transform>(lght, glm::vec3(5.f, 5.f, 5.f), glm::vec3(0.f));
|
scene->m_registry.emplace<transform>(lght, glm::vec3(5.f, 5.f, 5.f), glm::vec3(0.f));
|
||||||
m_registry.emplace<light>(lght, light::LightType::DIRECTIONAL, glm::vec3(1.f, 1.f, 1.f), 1.5f);
|
scene->m_registry.emplace<light>(lght, light::LightType::DIRECTIONAL, glm::vec3(1.f, 1.f, 1.f), 1.5f);
|
||||||
m_registry.emplace<mesh>(lght, std::shared_ptr<Object>(lightObj));
|
scene->m_registry.emplace<mesh>(lght, std::shared_ptr<Object>(lightObj));
|
||||||
|
|
||||||
const auto cameraEntity = m_registry.create();
|
const auto cameraEntity = scene->m_registry.create();
|
||||||
m_registry.emplace<transform>(cameraEntity, glm::vec3(0.f, 2.f, 2.f));
|
scene->m_registry.emplace<transform>(cameraEntity, glm::vec3(0.f, 2.f, 2.f));
|
||||||
m_registry.emplace<camera>(cameraEntity);
|
scene->m_registry.emplace<camera>(cameraEntity);
|
||||||
|
|
||||||
Object* targetObj = Object::LoadFile("./assets/car/car.obj");
|
Object* targetObj = Object::LoadFile("./assets/wizard/wizard.obj");
|
||||||
const auto targetEntity = m_registry.create();
|
const auto targetEntity = scene->m_registry.create();
|
||||||
m_registry.emplace<transform>(targetEntity, glm::vec3(0.f, 0.0f, 0.f));
|
scene->m_registry.emplace<transform>(targetEntity, glm::vec3(0.f, 0.0f, 0.f));
|
||||||
m_registry.emplace<mesh>(targetEntity, std::shared_ptr<Object>(targetObj));
|
scene->m_registry.emplace<mesh>(targetEntity, std::shared_ptr<Object>(targetObj));
|
||||||
m_registry.emplace<rotate>(targetEntity);
|
scene->m_registry.emplace<rotate>(targetEntity);
|
||||||
|
|
||||||
Object* grass = Object::LoadFile("./assets/common/cube/cube.obj");
|
Object* grass = Object::LoadFile("./assets/common/cube/cube.obj");
|
||||||
const auto cubeEntity = m_registry.create();
|
const auto cubeEntity = scene->m_registry.create();
|
||||||
m_registry.emplace<transform>(cubeEntity, glm::vec3(-1.5f, 0.4f, 0.f));
|
scene->m_registry.emplace<transform>(cubeEntity, glm::vec3(-1.5f, 0.4f, 0.f));
|
||||||
m_registry.emplace<mesh>(cubeEntity, std::shared_ptr<Object>(grass));
|
scene->m_registry.emplace<mesh>(cubeEntity, std::shared_ptr<Object>(grass));
|
||||||
|
|
||||||
// Cube template (use shared object to avoid reloading 1000 times)
|
// Cube template (use shared object to avoid reloading 1000 times)
|
||||||
std::shared_ptr<Object> cubeObj = std::shared_ptr<Object>(Object::LoadFile("./assets/grass_block/grass_block.obj"));
|
std::shared_ptr<Object> cubeObj = std::shared_ptr<Object>(Object::LoadFile("./assets/grass_block/grass_block.obj"));
|
||||||
const auto batchEntt = m_registry.create();
|
const auto batchEntt = scene->m_registry.create();
|
||||||
m_registry.emplace<batch>(batchEntt);
|
scene->m_registry.emplace<batch>(batchEntt);
|
||||||
m_registry.emplace<mesh>(batchEntt, cubeObj);
|
scene->m_registry.emplace<mesh>(batchEntt, cubeObj);
|
||||||
auto cubeBatch = m_registry.get<batch>(batchEntt);
|
auto cubeBatch = scene->m_registry.get<batch>(batchEntt);
|
||||||
// Generate 1000 random cubes
|
// Generate 1000 random cubes
|
||||||
for (int i = 0; i < 1000; ++i) {
|
for (int i = 0; i < 1000; ++i) {
|
||||||
const auto cubeEntity = m_registry.create();
|
const auto cubeEntity = scene->m_registry.create();
|
||||||
|
|
||||||
float x = static_cast<float>(rand()) / RAND_MAX * 200.f - 100.f; // range [-100, 100]
|
float x = static_cast<float>(rand()) / RAND_MAX * 200.f - 100.f; // range [-100, 100]
|
||||||
float y = static_cast<float>(rand()) / RAND_MAX * 10.f; // range [0, 10]
|
float y = static_cast<float>(rand()) / RAND_MAX * 10.f; // range [0, 10]
|
||||||
float z = static_cast<float>(rand()) / RAND_MAX * 200.f - 100.f; // range [-100, 100]
|
float z = static_cast<float>(rand()) / RAND_MAX * 200.f - 100.f; // range [-100, 100]
|
||||||
|
|
||||||
m_registry.emplace<transform>(cubeEntity, glm::vec3(x, y, z));
|
scene->m_registry.emplace<transform>(cubeEntity, glm::vec3(x, y, z));
|
||||||
m_registry.emplace<rotate>(cubeEntity);
|
scene->m_registry.emplace<rotate>(cubeEntity);
|
||||||
m_registry.emplace<batch::item>(cubeEntity, cubeBatch.id());
|
scene->m_registry.emplace<batch::item>(cubeEntity, cubeBatch.id());
|
||||||
}
|
}
|
||||||
|
|
||||||
Object* floorObj = Object::LoadFile("./assets/common/plane/plane.obj");
|
Object* floorObj = Object::LoadFile("./assets/common/plane/plane.obj");
|
||||||
const auto floorEntt = m_registry.create();
|
const auto floorEntt = scene->m_registry.create();
|
||||||
m_registry.emplace<transform>(floorEntt, glm::vec3(0.f));
|
scene->m_registry.emplace<transform>(floorEntt, glm::vec3(0.f));
|
||||||
m_registry.emplace<mesh>(floorEntt, std::shared_ptr<Object>(floorObj));
|
scene->m_registry.emplace<mesh>(floorEntt, std::shared_ptr<Object>(floorObj));
|
||||||
}
|
|
||||||
~Game() override {}
|
|
||||||
|
|
||||||
void OnInit() override {
|
|
||||||
std::cout << "Game initialized" << std::endl;
|
std::cout << "Game initialized" << std::endl;
|
||||||
|
|
||||||
m_angle = 3.45f;
|
m_angle = 3.45f;
|
||||||
@ -86,13 +87,6 @@ public:
|
|||||||
// FPS tracking
|
// FPS tracking
|
||||||
m_startTicks = SDL_GetTicks();
|
m_startTicks = SDL_GetTicks();
|
||||||
m_frameCount = 0;
|
m_frameCount = 0;
|
||||||
|
|
||||||
m_renderer.Init();
|
|
||||||
m_renderer.GenerateShadowMaps();
|
|
||||||
}
|
|
||||||
|
|
||||||
void OnWindowResized(const WindowResized& event) override {
|
|
||||||
m_renderer.OnWindowResized(event.w, event.h);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void OnUpdate() override {
|
void OnUpdate() override {
|
||||||
@ -136,7 +130,7 @@ public:
|
|||||||
if (state[SDL_SCANCODE_SPACE]) velocity.y += 1.f;
|
if (state[SDL_SCANCODE_SPACE]) velocity.y += 1.f;
|
||||||
if (state[SDL_SCANCODE_LSHIFT]) velocity.y -= 1.f;
|
if (state[SDL_SCANCODE_LSHIFT]) velocity.y -= 1.f;
|
||||||
|
|
||||||
auto view = m_registry.view<camera, transform>();
|
auto view = m_scene->m_registry.view<camera, transform>();
|
||||||
for (auto [cam, camTransform] : view.each()) {
|
for (auto [cam, camTransform] : view.each()) {
|
||||||
camTransform.position += velocity * deltaTime * 2.5f; // speed is e.g. 2.5f
|
camTransform.position += velocity * deltaTime * 2.5f; // speed is e.g. 2.5f
|
||||||
camTransform.rotation = cameraViewDirection;
|
camTransform.rotation = cameraViewDirection;
|
||||||
@ -173,7 +167,7 @@ public:
|
|||||||
glm::vec3 sunColor = glm::mix(dayColor, sunsetColor, sunsetFactor);
|
glm::vec3 sunColor = glm::mix(dayColor, sunsetColor, sunsetFactor);
|
||||||
|
|
||||||
// Update the directional light in the registry
|
// Update the directional light in the registry
|
||||||
auto lightsView = m_registry.view<light, transform>();
|
auto lightsView = m_scene->m_registry.view<light, transform>();
|
||||||
for (auto [entity, l, t] : lightsView.each()) {
|
for (auto [entity, l, t] : lightsView.each()) {
|
||||||
if (l.type == light::LightType::DIRECTIONAL) {
|
if (l.type == light::LightType::DIRECTIONAL) {
|
||||||
// "position" for directional light often stores direction vector
|
// "position" for directional light often stores direction vector
|
||||||
@ -184,17 +178,13 @@ public:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
auto rotateEntts = m_registry.view<transform, rotate>();
|
auto rotateEntts = m_scene->m_registry.view<transform, rotate>();
|
||||||
for (auto [entity, t] : rotateEntts.each()) {
|
for (auto [entity, t] : rotateEntts.each()) {
|
||||||
// auto targetTransform = rotateEntts.get<transform>(entity);
|
// auto targetTransform = rotateEntts.get<transform>(entity);
|
||||||
if (!m_registry.all_of<light>(entity)) {
|
if (!m_scene->m_registry.all_of<light>(entity)) {
|
||||||
t.rotation.y = m_angle;
|
t.rotation.y = m_angle;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
void OnRender() override {
|
|
||||||
m_renderer.Render();
|
|
||||||
|
|
||||||
m_frameCount++;
|
m_frameCount++;
|
||||||
m_currentTicks = SDL_GetTicks();
|
m_currentTicks = SDL_GetTicks();
|
||||||
@ -207,9 +197,18 @@ public:
|
|||||||
m_startTicks = m_currentTicks;
|
m_startTicks = m_currentTicks;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void OnEvent(const Event& event) override {
|
||||||
|
if (event.GetType() == EventType::WINDOW_RESIZE) {
|
||||||
|
auto resizeEvent = static_cast<const WindowResizeEvent&>(event);
|
||||||
|
std::cout << "[DEBUG] <EVENT> Window resized to " << resizeEvent.GetWidth() << "x" << resizeEvent.GetHeight() << std::endl;
|
||||||
|
}
|
||||||
|
else if (event.GetType() == EventType::WINDOW_CLOSE) {
|
||||||
|
std::cout << "[DEBUG] <EVENT> Window closing" << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
private:
|
private:
|
||||||
Renderer m_renderer;
|
std::shared_ptr<Scene> m_scene;
|
||||||
entt::registry m_registry;
|
|
||||||
|
|
||||||
float m_angle;
|
float m_angle;
|
||||||
Uint64 m_lastTicks;
|
Uint64 m_lastTicks;
|
||||||
|
|||||||
Reference in New Issue
Block a user