Compare commits
4 Commits
ecba8247bf
...
71f1b2c6d2
| Author | SHA1 | Date | |
|---|---|---|---|
| 71f1b2c6d2 | |||
| 31fbecbb47 | |||
| 66e2531eb7 | |||
| ce0904ebec |
@ -3,27 +3,22 @@ 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/wavefront.cpp
|
||||
src/renderer/core.cpp
|
||||
src/renderer/renderer.cpp
|
||||
|
||||
# src/main.cpp
|
||||
)
|
||||
src/renderer/wavefront.cpp
|
||||
|
||||
#if (ENGINE_BUILD_SHARED)
|
||||
# add_library(${ENGINE_TARGET} SHARED ${SOURCES})
|
||||
#else()
|
||||
# add_library(${ENGINE_TARGET} STATIC ${SOURCES})
|
||||
#endif()
|
||||
src/renderer/core.cpp
|
||||
)
|
||||
|
||||
if(ENGINE_BUILD_SHARED)
|
||||
add_library(${ENGINE_TARGET} SHARED ${SOURCES})
|
||||
|
||||
@ -12,6 +12,7 @@
|
||||
extern IApplication* CreateApplication();
|
||||
|
||||
int main() {
|
||||
Engine::Run(std::unique_ptr<IApplication>(CreateApplication()));
|
||||
auto engine = Engine::GetInstance();
|
||||
engine->Run(std::unique_ptr<IApplication>(CreateApplication()));
|
||||
return 0;
|
||||
}
|
||||
@ -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.hpp"
|
||||
#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_
|
||||
8
engine/include/engine/components.h
Normal file
8
engine/include/engine/components.h
Normal file
@ -0,0 +1,8 @@
|
||||
#pragma once
|
||||
|
||||
#include "components/batch.h"
|
||||
#include "components/transform.h"
|
||||
#include "components/camera.h"
|
||||
#include "components/mesh.h"
|
||||
#include "components/rotate.h"
|
||||
#include "components/light.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
|
||||
@ -5,18 +5,30 @@
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
#include "engine/window/window.h"
|
||||
#include "engine/window/events/window.h"
|
||||
#include "engine/window/events/window_events.h"
|
||||
|
||||
#include "engine/renderer/renderer.h"
|
||||
#include "engine/scene/scene.h"
|
||||
|
||||
#include "engine/app/app.h"
|
||||
#include "engine/export.h"
|
||||
|
||||
class ENGINE_API Engine {
|
||||
class ENGINE_API Engine : public EventHandler {
|
||||
public:
|
||||
static void Run(std::unique_ptr<IApplication> app);
|
||||
static Engine* GetInstance();
|
||||
|
||||
void Run(std::unique_ptr<IApplication> app);
|
||||
private:
|
||||
static std::unique_ptr<IApplication> s_app;
|
||||
static std::shared_ptr<Window> s_window;
|
||||
static bool s_running;
|
||||
Engine() {}
|
||||
static Engine* s_instance;
|
||||
|
||||
void OnEvent(const Event& event) override;
|
||||
private:
|
||||
std::unique_ptr<IApplication> m_app;
|
||||
std::shared_ptr<Window> m_window;
|
||||
std::unique_ptr<Renderer> m_renderer;
|
||||
std::shared_ptr<Scene> m_scene;
|
||||
bool m_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_
|
||||
@ -1,57 +0,0 @@
|
||||
#ifndef EVENT_H_
|
||||
#define EVENT_H_
|
||||
|
||||
#include <functional>
|
||||
#include <algorithm>
|
||||
#include <typeindex>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
class EventDispatcher {
|
||||
using Type = std::type_index;
|
||||
using RawFn = std::function<void(const void*)>;
|
||||
|
||||
struct Slot { std::size_t id; RawFn fn; };
|
||||
|
||||
std::unordered_map<Type, std::vector<Slot>> subs_;
|
||||
std::size_t next_id_ = 1;
|
||||
|
||||
public:
|
||||
struct Handle {
|
||||
std::type_index type{typeid(void)};
|
||||
std::size_t id{0};
|
||||
explicit operator bool() const { return id != 0; }
|
||||
};
|
||||
|
||||
template<class E, class F>
|
||||
Handle Subscribe(F&& f) {
|
||||
auto& vec = subs_[Type(typeid(E))];
|
||||
Handle h{ Type(typeid(E)), next_id_++ };
|
||||
// Wrap strongly typed callback into type-erased RawFn
|
||||
RawFn wrapper = [fn = std::function<void(const E&)>(std::forward<F>(f))](const void* p){
|
||||
fn(*static_cast<const E*>(p));
|
||||
};
|
||||
vec.push_back(Slot{h.id, std::move(wrapper)});
|
||||
return h;
|
||||
}
|
||||
|
||||
// Unsubscribe with handle
|
||||
void Unsubscribe(const Handle& h) {
|
||||
auto it = subs_.find(h.type);
|
||||
if (it == subs_.end()) return;
|
||||
auto& vec = it->second;
|
||||
vec.erase(std::remove_if(vec.begin(), vec.end(),
|
||||
[&](const Slot& s){ return s.id == h.id; }),
|
||||
vec.end());
|
||||
}
|
||||
|
||||
// Publish immediately
|
||||
template<class E>
|
||||
void Dispatch(const E& e) const {
|
||||
auto it = subs_.find(Type(typeid(E)));
|
||||
if (it == subs_.end()) return;
|
||||
for (auto& slot : it->second) slot.fn(&e);
|
||||
}
|
||||
};
|
||||
|
||||
#endif // EVENT_H_
|
||||
74
engine/include/engine/window/event.hpp
Normal file
74
engine/include/engine/window/event.hpp
Normal file
@ -0,0 +1,74 @@
|
||||
#ifndef EVENT_H_
|
||||
#define EVENT_H_
|
||||
|
||||
#include <functional>
|
||||
#include <algorithm>
|
||||
#include <typeindex>
|
||||
#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 EventHandler {
|
||||
public:
|
||||
EventHandler() = default;
|
||||
|
||||
virtual void OnEvent(const Event& event) = 0;
|
||||
};
|
||||
|
||||
class EventEmitter {
|
||||
public:
|
||||
struct Handle {
|
||||
std::size_t id{0};
|
||||
explicit operator bool() const { return id != 0; }
|
||||
};
|
||||
|
||||
EventEmitter() = default;
|
||||
|
||||
Handle Subscribe2(EventHandler* handler) {
|
||||
auto slot = Slot{ m_next_id++, handler };
|
||||
m_subs.push_back(slot);
|
||||
return Handle{ slot.id };
|
||||
}
|
||||
|
||||
void Unsubscribe2(const Handle& h) {
|
||||
m_subs.erase(std::remove_if(m_subs.begin(), m_subs.end(),
|
||||
[&](const Slot& s){ return s.id == h.id; }),
|
||||
m_subs.end());
|
||||
}
|
||||
protected:
|
||||
void EmitEvent(const Event& event) {
|
||||
for (auto &sub : m_subs) {
|
||||
sub.handler->OnEvent(event);
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
struct Slot { std::size_t id; EventHandler* handler; };
|
||||
std::vector<Slot> m_subs;
|
||||
std::size_t m_next_id = 1;
|
||||
};
|
||||
|
||||
#endif // EVENT_H_
|
||||
@ -1,7 +0,0 @@
|
||||
#ifndef WINDOW_EVENTS_H_
|
||||
#define WINDOW_EVENTS_H_
|
||||
|
||||
struct WindowResized { int w, h; };
|
||||
struct WindowCloseRequested {};
|
||||
|
||||
#endif // WINDOW_EVENTS_H_
|
||||
30
engine/include/engine/window/events/window_events.h
Normal file
30
engine/include/engine/window/events/window_events.h
Normal file
@ -0,0 +1,30 @@
|
||||
#ifndef WINDOW_EVENTS_H_
|
||||
#define WINDOW_EVENTS_H_
|
||||
|
||||
#include "engine/window/event.hpp"
|
||||
|
||||
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_
|
||||
@ -3,7 +3,7 @@
|
||||
#include <SDL3/SDL.h>
|
||||
#include <memory>
|
||||
|
||||
#include "engine/window/event.h"
|
||||
#include "engine/window/event.hpp"
|
||||
|
||||
#define ENGINE_GL_MAJOR_VERSION 4
|
||||
#define ENGINE_GL_MINOR_VERSION 6
|
||||
@ -13,7 +13,7 @@
|
||||
#define DEFAULT_WIDTH 1024
|
||||
#define DEFAULT_HEIGHT 768
|
||||
|
||||
class Window : public EventDispatcher {
|
||||
class Window : public EventEmitter {
|
||||
friend class Engine;
|
||||
private:
|
||||
Window();
|
||||
|
||||
@ -1,42 +1,68 @@
|
||||
#include <memory>
|
||||
|
||||
#include "engine/renderer/core.h"
|
||||
#include "engine/window/event.h"
|
||||
|
||||
#include "engine/window/event.hpp"
|
||||
#include "engine/renderer/wavefront.h"
|
||||
|
||||
std::unique_ptr<IApplication> Engine::s_app = nullptr;
|
||||
std::shared_ptr<Window> Engine::s_window = nullptr;
|
||||
bool Engine::s_running = false;
|
||||
Engine* Engine::s_instance = nullptr;
|
||||
|
||||
void Engine::Run(std::unique_ptr<IApplication> app) {
|
||||
s_app = std::move(app);
|
||||
s_window = Window::GetInstance();
|
||||
s_running = true;
|
||||
m_scene = std::make_shared<Scene>();
|
||||
m_renderer = std::make_unique<Renderer>(m_scene);
|
||||
m_window = Window::GetInstance();
|
||||
m_app = std::move(app);
|
||||
m_running = true;
|
||||
|
||||
s_app->OnInit();
|
||||
m_app->OnInit(m_scene);
|
||||
m_renderer->Init();
|
||||
|
||||
s_window->Subscribe<WindowCloseRequested>([](const WindowCloseRequested& e) {
|
||||
Engine::s_running = false;
|
||||
});
|
||||
// m_window->Subscribe<WindowCloseEvent>([&](const WindowCloseEvent& e) {
|
||||
|
||||
s_window->Subscribe<WindowResized>([](const WindowResized& e) {
|
||||
Engine::s_app->OnWindowResized(e);
|
||||
});
|
||||
// m_app->OnEvent(e);
|
||||
// });
|
||||
|
||||
while (s_running) {
|
||||
s_window->ProcessEvents();
|
||||
// m_window->Subscribe<WindowResizeEvent>([&](const WindowResizeEvent& e) {
|
||||
// m_renderer->OnWindowResized(e.GetWidth(), e.GetHeight());
|
||||
// m_app->OnEvent(e);
|
||||
// });
|
||||
|
||||
s_app->OnUpdate();
|
||||
m_window->Subscribe2(this);
|
||||
|
||||
s_app->OnRender();
|
||||
while (m_running) {
|
||||
m_window->ProcessEvents();
|
||||
|
||||
s_window->SwapBuffers();
|
||||
m_app->OnUpdate();
|
||||
|
||||
m_renderer->Render();
|
||||
|
||||
m_window->SwapBuffers();
|
||||
}
|
||||
|
||||
s_app->OnShutdown();
|
||||
m_app->OnShutdown();
|
||||
|
||||
s_window->Destroy();
|
||||
s_app.reset();
|
||||
m_window->Destroy();
|
||||
m_app.reset();
|
||||
}
|
||||
|
||||
void Engine::OnEvent(const Event& event) {
|
||||
m_app->OnEvent(event);
|
||||
if (event.GetCategory() == Event::EventCategory::WINDOW) {
|
||||
if (event.GetType() == EventType::WINDOW_RESIZE) {
|
||||
auto e = static_cast<const WindowResizeEvent&>(event);
|
||||
m_renderer->OnWindowResized(e.GetWidth(), e.GetHeight());
|
||||
}
|
||||
if (event.GetType() == EventType::WINDOW_CLOSE) {
|
||||
m_running = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Engine* Engine::GetInstance() {
|
||||
if (!s_instance) {
|
||||
s_instance = new Engine();
|
||||
}
|
||||
|
||||
return s_instance;
|
||||
}
|
||||
|
||||
|
||||
@ -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() {
|
||||
}
|
||||
@ -1,7 +1,7 @@
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include "engine/window/window.h"
|
||||
#include "engine/window/events/window.h"
|
||||
#include "engine/window/events/window_events.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <GL/glew.h>
|
||||
@ -100,11 +100,13 @@ void Window::ProcessEvents() {
|
||||
switch (event.type) {
|
||||
case SDL_EVENT_WINDOW_CLOSE_REQUESTED:
|
||||
case SDL_EVENT_QUIT:
|
||||
Dispatch(WindowCloseRequested());
|
||||
Dispatch(WindowCloseEvent());
|
||||
EmitEvent(WindowCloseEvent{});
|
||||
break;
|
||||
case SDL_EVENT_KEY_DOWN:
|
||||
if (event.key.scancode == SDL_SCANCODE_ESCAPE) {
|
||||
Dispatch(WindowCloseRequested());
|
||||
Dispatch(WindowCloseEvent());
|
||||
EmitEvent(WindowCloseEvent{});
|
||||
}
|
||||
if (event.key.scancode == SDL_SCANCODE_F11) {
|
||||
bool isFullscreen = SDL_GetWindowFlags(m_handle) & SDL_WINDOW_FULLSCREEN;
|
||||
@ -122,7 +124,9 @@ void Window::ProcessEvents() {
|
||||
0,
|
||||
width,
|
||||
height);
|
||||
Dispatch(WindowResized{ m_width, m_height });
|
||||
auto event = WindowResizeEvent(static_cast<unsigned int>(m_width), static_cast<unsigned int>(m_height));
|
||||
Dispatch(event);
|
||||
EmitEvent(event);
|
||||
SDL_SetWindowRelativeMouseMode(m_handle, true);
|
||||
SDL_Rect boundaries = {0, 0, m_width, m_height};
|
||||
SDL_SetWindowMouseRect(m_handle, &boundaries);
|
||||
|
||||
@ -9,7 +9,6 @@
|
||||
#endif
|
||||
|
||||
#include "engine/renderer/wavefront.h"
|
||||
#include "engine/renderer/renderer.h"
|
||||
|
||||
#include "engine/app/app.h"
|
||||
|
||||
@ -20,59 +19,61 @@
|
||||
#include "engine/components/rotate.h"
|
||||
#include "engine/components/batch.h"
|
||||
|
||||
#include "engine/engine.h"
|
||||
#include "engine/api.h"
|
||||
|
||||
class Game : public IApplication {
|
||||
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");
|
||||
const auto lght = m_registry.create();
|
||||
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);
|
||||
m_registry.emplace<mesh>(lght, std::shared_ptr<Object>(lightObj));
|
||||
const auto lght = scene->m_registry.create();
|
||||
scene->m_registry.emplace<transform>(lght, glm::vec3(5.f, 5.f, 5.f), glm::vec3(0.f));
|
||||
scene->m_registry.emplace<light>(lght, light::LightType::DIRECTIONAL, glm::vec3(1.f, 1.f, 1.f), 1.5f);
|
||||
scene->m_registry.emplace<mesh>(lght, std::shared_ptr<Object>(lightObj));
|
||||
|
||||
const auto cameraEntity = m_registry.create();
|
||||
m_registry.emplace<transform>(cameraEntity, glm::vec3(0.f, 2.f, 2.f));
|
||||
m_registry.emplace<camera>(cameraEntity);
|
||||
const auto cameraEntity = scene->m_registry.create();
|
||||
scene->m_registry.emplace<transform>(cameraEntity, glm::vec3(0.f, 2.f, 2.f));
|
||||
scene->m_registry.emplace<camera>(cameraEntity);
|
||||
|
||||
Object* targetObj = Object::LoadFile("./assets/car/car.obj");
|
||||
const auto targetEntity = m_registry.create();
|
||||
m_registry.emplace<transform>(targetEntity, glm::vec3(0.f, 0.0f, 0.f));
|
||||
m_registry.emplace<mesh>(targetEntity, std::shared_ptr<Object>(targetObj));
|
||||
m_registry.emplace<rotate>(targetEntity);
|
||||
Object* targetObj = Object::LoadFile("./assets/wizard/wizard.obj");
|
||||
const auto targetEntity = scene->m_registry.create();
|
||||
scene->m_registry.emplace<transform>(targetEntity, glm::vec3(0.f, 0.0f, 0.f));
|
||||
scene->m_registry.emplace<mesh>(targetEntity, std::shared_ptr<Object>(targetObj));
|
||||
scene->m_registry.emplace<rotate>(targetEntity);
|
||||
|
||||
Object* grass = Object::LoadFile("./assets/common/cube/cube.obj");
|
||||
const auto cubeEntity = m_registry.create();
|
||||
m_registry.emplace<transform>(cubeEntity, glm::vec3(-1.5f, 0.4f, 0.f));
|
||||
m_registry.emplace<mesh>(cubeEntity, std::shared_ptr<Object>(grass));
|
||||
const auto cubeEntity = scene->m_registry.create();
|
||||
scene->m_registry.emplace<transform>(cubeEntity, glm::vec3(-1.5f, 0.4f, 0.f));
|
||||
scene->m_registry.emplace<mesh>(cubeEntity, std::shared_ptr<Object>(grass));
|
||||
|
||||
// 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"));
|
||||
const auto batchEntt = m_registry.create();
|
||||
m_registry.emplace<batch>(batchEntt);
|
||||
m_registry.emplace<mesh>(batchEntt, cubeObj);
|
||||
auto cubeBatch = m_registry.get<batch>(batchEntt);
|
||||
const auto batchEntt = scene->m_registry.create();
|
||||
scene->m_registry.emplace<batch>(batchEntt);
|
||||
scene->m_registry.emplace<mesh>(batchEntt, cubeObj);
|
||||
auto cubeBatch = scene->m_registry.get<batch>(batchEntt);
|
||||
// Generate 1000 random cubes
|
||||
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 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]
|
||||
|
||||
m_registry.emplace<transform>(cubeEntity, glm::vec3(x, y, z));
|
||||
m_registry.emplace<rotate>(cubeEntity);
|
||||
m_registry.emplace<batch::item>(cubeEntity, cubeBatch.id());
|
||||
scene->m_registry.emplace<transform>(cubeEntity, glm::vec3(x, y, z));
|
||||
scene->m_registry.emplace<rotate>(cubeEntity);
|
||||
scene->m_registry.emplace<batch::item>(cubeEntity, cubeBatch.id());
|
||||
}
|
||||
|
||||
Object* floorObj = Object::LoadFile("./assets/common/plane/plane.obj");
|
||||
const auto floorEntt = m_registry.create();
|
||||
m_registry.emplace<transform>(floorEntt, glm::vec3(0.f));
|
||||
m_registry.emplace<mesh>(floorEntt, std::shared_ptr<Object>(floorObj));
|
||||
}
|
||||
~Game() override {}
|
||||
const auto floorEntt = scene->m_registry.create();
|
||||
scene->m_registry.emplace<transform>(floorEntt, glm::vec3(0.f));
|
||||
scene->m_registry.emplace<mesh>(floorEntt, std::shared_ptr<Object>(floorObj));
|
||||
|
||||
void OnInit() override {
|
||||
std::cout << "Game initialized" << std::endl;
|
||||
|
||||
m_angle = 3.45f;
|
||||
@ -86,13 +87,6 @@ public:
|
||||
// FPS tracking
|
||||
m_startTicks = SDL_GetTicks();
|
||||
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 {
|
||||
@ -136,7 +130,7 @@ public:
|
||||
if (state[SDL_SCANCODE_SPACE]) 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()) {
|
||||
camTransform.position += velocity * deltaTime * 2.5f; // speed is e.g. 2.5f
|
||||
camTransform.rotation = cameraViewDirection;
|
||||
@ -173,7 +167,7 @@ public:
|
||||
glm::vec3 sunColor = glm::mix(dayColor, sunsetColor, sunsetFactor);
|
||||
|
||||
// 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()) {
|
||||
if (l.type == light::LightType::DIRECTIONAL) {
|
||||
// "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()) {
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void OnRender() override {
|
||||
m_renderer.Render();
|
||||
|
||||
m_frameCount++;
|
||||
m_currentTicks = SDL_GetTicks();
|
||||
@ -207,9 +197,18 @@ public:
|
||||
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:
|
||||
Renderer m_renderer;
|
||||
entt::registry m_registry;
|
||||
std::shared_ptr<Scene> m_scene;
|
||||
|
||||
float m_angle;
|
||||
Uint64 m_lastTicks;
|
||||
|
||||
Reference in New Issue
Block a user