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
|
||||
src/IO/parser.cpp
|
||||
src/IO/file_manager.cpp
|
||||
src/renderer/debug.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/shader.cpp
|
||||
src/renderer/texture.cpp
|
||||
src/renderer/core.cpp
|
||||
src/renderer/renderer.cpp
|
||||
|
||||
src/renderer/wavefront.cpp
|
||||
|
||||
src/renderer/core.cpp
|
||||
)
|
||||
|
||||
if(ENGINE_BUILD_SHARED)
|
||||
|
||||
@ -1,20 +1,19 @@
|
||||
#ifndef 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"
|
||||
|
||||
class ENGINE_API IApplication {
|
||||
public:
|
||||
virtual ~IApplication() = default;
|
||||
|
||||
virtual void OnInit() {};
|
||||
virtual void OnInit(std::shared_ptr<Scene> scene) {};
|
||||
virtual void OnUpdate() {};
|
||||
virtual void OnRender() {};
|
||||
virtual void OnShutdown() {};
|
||||
|
||||
virtual void OnEvent() {};
|
||||
virtual void OnWindowResized(const WindowResized& e) {};
|
||||
virtual void OnEvent(const Event& event) {};
|
||||
};
|
||||
|
||||
#endif // APPLICATION_H_
|
||||
@ -1,7 +1,7 @@
|
||||
#ifndef COMPONENT_BATCH_H_
|
||||
#define COMPONENT_BATCH_H_
|
||||
|
||||
#include "engine/renderer/renderer.h"
|
||||
#include <glm/mat4x4.hpp>
|
||||
#include "engine/export.h"
|
||||
|
||||
// requires mesh component
|
||||
|
||||
@ -3,8 +3,6 @@
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
#include "engine/renderer/mesh.h"
|
||||
|
||||
class Vertex {
|
||||
friend class Mesh;
|
||||
private:
|
||||
@ -14,8 +12,6 @@ private:
|
||||
public:
|
||||
Vertex(glm::vec3 position, glm::vec3 normal, glm::vec2 texCoord)
|
||||
: m_position(position), m_normal(normal), m_texCoord(texCoord) {}
|
||||
public:
|
||||
static void DefineAttrib();
|
||||
};
|
||||
|
||||
#endif // RENDERER_BASICS_H
|
||||
@ -7,6 +7,9 @@
|
||||
#include "engine/window/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/export.h"
|
||||
|
||||
@ -16,6 +19,8 @@ public:
|
||||
private:
|
||||
static std::unique_ptr<IApplication> s_app;
|
||||
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;
|
||||
};
|
||||
|
||||
|
||||
@ -2,8 +2,8 @@
|
||||
#define RENDERER_H_
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
#include <entt/entity/registry.hpp>
|
||||
|
||||
#include "engine/scene/scene.h"
|
||||
#include "engine/renderer/shader.h"
|
||||
#include "engine/export.h"
|
||||
#include "engine/components/light.h"
|
||||
@ -11,23 +11,23 @@
|
||||
// TODO: make static or singleton
|
||||
class ENGINE_API Renderer {
|
||||
public:
|
||||
Renderer(entt::registry& registry);
|
||||
Renderer(std::shared_ptr<Scene> scene);
|
||||
|
||||
void Render();
|
||||
void Init();
|
||||
void GenerateShadowMaps();
|
||||
|
||||
void OnWindowResized(int w, int h);
|
||||
private:
|
||||
void ApplyLights(Shader &shader);
|
||||
void UpdateView();
|
||||
void RenderScene(Shader &shader);
|
||||
void GenerateShadowMaps();
|
||||
void EnsureShadowResources(light& l);
|
||||
private:
|
||||
Shader m_shader;
|
||||
Shader m_depthShader;
|
||||
|
||||
entt::registry& m_registry;
|
||||
std::shared_ptr<Scene> m_scene;
|
||||
|
||||
// unsigned int m_depth_fbo;
|
||||
// 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 <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 {
|
||||
using Type = std::type_index;
|
||||
using RawFn = std::function<void(const void*)>;
|
||||
|
||||
@ -1,7 +1,30 @@
|
||||
#ifndef WINDOW_EVENTS_H_
|
||||
#define WINDOW_EVENTS_H_
|
||||
|
||||
struct WindowResized { int w, h; };
|
||||
struct WindowCloseRequested {};
|
||||
#include "engine/window/event.h"
|
||||
|
||||
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_
|
||||
@ -1,27 +1,34 @@
|
||||
#include <memory>
|
||||
|
||||
#include "engine/renderer/core.h"
|
||||
#include "engine/window/event.h"
|
||||
|
||||
#include "engine/window/event.h"
|
||||
#include "engine/renderer/wavefront.h"
|
||||
|
||||
std::unique_ptr<IApplication> Engine::s_app = 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;
|
||||
|
||||
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_app = std::move(app);
|
||||
s_running = true;
|
||||
|
||||
s_app->OnInit();
|
||||
s_app->OnInit(s_scene);
|
||||
s_renderer->Init();
|
||||
|
||||
s_window->Subscribe<WindowCloseRequested>([](const WindowCloseRequested& e) {
|
||||
Engine::s_running = false;
|
||||
s_window->Subscribe<WindowCloseEvent>([&](const WindowCloseEvent& e) {
|
||||
s_running = false;
|
||||
s_app->OnEvent(e);
|
||||
});
|
||||
|
||||
s_window->Subscribe<WindowResized>([](const WindowResized& e) {
|
||||
Engine::s_app->OnWindowResized(e);
|
||||
s_window->Subscribe<WindowResizeEvent>([&](const WindowResizeEvent& e) {
|
||||
s_renderer->OnWindowResized(e.GetWidth(), e.GetHeight());
|
||||
s_app->OnEvent(e);
|
||||
});
|
||||
|
||||
while (s_running) {
|
||||
@ -29,7 +36,7 @@ void Engine::Run(std::unique_ptr<IApplication> app) {
|
||||
|
||||
s_app->OnUpdate();
|
||||
|
||||
s_app->OnRender();
|
||||
s_renderer->Render();
|
||||
|
||||
s_window->SwapBuffers();
|
||||
}
|
||||
|
||||
@ -18,7 +18,7 @@
|
||||
#include "engine/components/mesh.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(
|
||||
static_cast<float>(M_PI_2),
|
||||
@ -44,15 +44,7 @@ Renderer::Renderer(entt::registry& registry) : m_registry(registry)
|
||||
}
|
||||
|
||||
void Renderer::Init() {
|
||||
// auto view = m_registry.view<batch, mesh>();
|
||||
// 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);
|
||||
// }
|
||||
GenerateShadowMaps();
|
||||
}
|
||||
|
||||
void Renderer::OnWindowResized(int w, int h) {
|
||||
@ -65,13 +57,13 @@ void Renderer::OnWindowResized(int w, int h) {
|
||||
}
|
||||
|
||||
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
|
||||
shader.setInt("lightsCount", static_cast<int>(lights.size()));
|
||||
size_t lightIndex = 0;
|
||||
for (auto entity : lights) {
|
||||
auto &l = m_registry.get<light>(entity);
|
||||
auto &transf = m_registry.get<transform>(entity);
|
||||
auto &l = m_scene->m_registry.get<light>(entity);
|
||||
auto &transf = m_scene->m_registry.get<transform>(entity);
|
||||
|
||||
shader.setInt("lights[" + std::to_string(lightIndex) + "].type", static_cast<int>(l.type));
|
||||
shader.setVec3("lights[" + std::to_string(lightIndex) + "].position", transf.position);
|
||||
@ -113,24 +105,27 @@ void Renderer::EnsureShadowResources(light& l) {
|
||||
}
|
||||
|
||||
void Renderer::UpdateView() {
|
||||
auto cam = m_registry.view<transform, camera>().back();
|
||||
auto camTransform = m_registry.get<transform>(cam);
|
||||
auto camView = m_scene->m_registry.view<camera>();
|
||||
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(
|
||||
camTransform.position,
|
||||
camTransform.position + camTransform.rotation,
|
||||
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.setMat4("u_view", m_view);
|
||||
m_shader.setMat4("u_projection", m_proj);
|
||||
}
|
||||
|
||||
void Renderer::RenderScene(Shader &shader) {
|
||||
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())
|
||||
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("isLight", false);
|
||||
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
|
||||
if (batches.find(b.id()) == batches.end()) continue;
|
||||
|
||||
@ -150,7 +145,7 @@ void Renderer::RenderScene(Shader &shader) {
|
||||
models.reserve(batchItems.size());
|
||||
|
||||
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);
|
||||
auto itemModel = glm::translate(glm::mat4(1.f), t.position) * rotation;
|
||||
models.push_back(itemModel);
|
||||
@ -166,14 +161,14 @@ void Renderer::RenderScene(Shader &shader) {
|
||||
}
|
||||
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) {
|
||||
std::cerr << "WARN: Entity doesn't have a mesh to render" << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
if (m_registry.all_of<light>(entity)) {
|
||||
auto &l = m_registry.get<light>(entity);
|
||||
if (m_scene->m_registry.all_of<light>(entity)) {
|
||||
auto &l = m_scene->m_registry.get<light>(entity);
|
||||
shader.setBool("isLight", true);
|
||||
shader.setVec3("currentLightColor", l.color);
|
||||
} else {
|
||||
@ -193,7 +188,7 @@ void Renderer::RenderScene(Shader &shader) {
|
||||
void Renderer::GenerateShadowMaps() {
|
||||
m_depthShader.use();
|
||||
|
||||
auto lights = m_registry.view<light>();
|
||||
auto lights = m_scene->m_registry.view<light>();
|
||||
|
||||
for (auto [_, l] : lights.each()) {
|
||||
// TODO: support other light types when ready
|
||||
@ -207,7 +202,7 @@ void Renderer::Render() {
|
||||
|
||||
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()) {
|
||||
// TODO: support other light types when ready
|
||||
|
||||
@ -5,9 +5,10 @@
|
||||
#include <filesystem>
|
||||
#include <GL/glew.h>
|
||||
|
||||
#include "engine/renderer/wavefront.h"
|
||||
|
||||
#include "engine/IO/parser.h"
|
||||
#include "engine/renderer/mesh.h"
|
||||
#include "engine/renderer/wavefront.h"
|
||||
|
||||
#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) {
|
||||
case SDL_EVENT_WINDOW_CLOSE_REQUESTED:
|
||||
case SDL_EVENT_QUIT:
|
||||
Dispatch(WindowCloseRequested());
|
||||
Dispatch(WindowCloseEvent());
|
||||
break;
|
||||
case SDL_EVENT_KEY_DOWN:
|
||||
if (event.key.scancode == SDL_SCANCODE_ESCAPE) {
|
||||
Dispatch(WindowCloseRequested());
|
||||
Dispatch(WindowCloseEvent());
|
||||
}
|
||||
if (event.key.scancode == SDL_SCANCODE_F11) {
|
||||
bool isFullscreen = SDL_GetWindowFlags(m_handle) & SDL_WINDOW_FULLSCREEN;
|
||||
@ -122,7 +122,7 @@ void Window::ProcessEvents() {
|
||||
0,
|
||||
width,
|
||||
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_Rect boundaries = {0, 0, m_width, m_height};
|
||||
SDL_SetWindowMouseRect(m_handle, &boundaries);
|
||||
|
||||
Reference in New Issue
Block a user