Compare commits

..

4 Commits

20 changed files with 294 additions and 199 deletions

View File

@ -3,27 +3,22 @@ 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/wavefront.cpp
src/renderer/core.cpp
src/renderer/renderer.cpp src/renderer/renderer.cpp
# src/main.cpp src/renderer/wavefront.cpp
)
#if (ENGINE_BUILD_SHARED) src/renderer/core.cpp
# add_library(${ENGINE_TARGET} SHARED ${SOURCES}) )
#else()
# add_library(${ENGINE_TARGET} STATIC ${SOURCES})
#endif()
if(ENGINE_BUILD_SHARED) if(ENGINE_BUILD_SHARED)
add_library(${ENGINE_TARGET} SHARED ${SOURCES}) add_library(${ENGINE_TARGET} SHARED ${SOURCES})

View File

@ -12,6 +12,7 @@
extern IApplication* CreateApplication(); extern IApplication* CreateApplication();
int main() { int main() {
Engine::Run(std::unique_ptr<IApplication>(CreateApplication())); auto engine = Engine::GetInstance();
engine->Run(std::unique_ptr<IApplication>(CreateApplication()));
return 0; return 0;
} }

View File

@ -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.hpp"
#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_

View 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"

View File

@ -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

View File

@ -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

View File

@ -5,18 +5,30 @@
#include <glm/glm.hpp> #include <glm/glm.hpp>
#include "engine/window/window.h" #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/app/app.h"
#include "engine/export.h" #include "engine/export.h"
class ENGINE_API Engine { class ENGINE_API Engine : public EventHandler {
public: public:
static void Run(std::unique_ptr<IApplication> app); static Engine* GetInstance();
void Run(std::unique_ptr<IApplication> app);
private: private:
static std::unique_ptr<IApplication> s_app; Engine() {}
static std::shared_ptr<Window> s_window; static Engine* s_instance;
static bool s_running;
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;
}; };

View File

@ -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;

View 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_

View File

@ -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_

View 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_

View File

@ -1,7 +0,0 @@
#ifndef WINDOW_EVENTS_H_
#define WINDOW_EVENTS_H_
struct WindowResized { int w, h; };
struct WindowCloseRequested {};
#endif // WINDOW_EVENTS_H_

View 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_

View File

@ -3,7 +3,7 @@
#include <SDL3/SDL.h> #include <SDL3/SDL.h>
#include <memory> #include <memory>
#include "engine/window/event.h" #include "engine/window/event.hpp"
#define ENGINE_GL_MAJOR_VERSION 4 #define ENGINE_GL_MAJOR_VERSION 4
#define ENGINE_GL_MINOR_VERSION 6 #define ENGINE_GL_MINOR_VERSION 6
@ -13,7 +13,7 @@
#define DEFAULT_WIDTH 1024 #define DEFAULT_WIDTH 1024
#define DEFAULT_HEIGHT 768 #define DEFAULT_HEIGHT 768
class Window : public EventDispatcher { class Window : public EventEmitter {
friend class Engine; friend class Engine;
private: private:
Window(); Window();

View File

@ -1,42 +1,68 @@
#include <memory> #include <memory>
#include "engine/renderer/core.h" #include "engine/renderer/core.h"
#include "engine/window/event.h"
#include "engine/window/event.hpp"
#include "engine/renderer/wavefront.h" #include "engine/renderer/wavefront.h"
std::unique_ptr<IApplication> Engine::s_app = nullptr; Engine* Engine::s_instance = nullptr;
std::shared_ptr<Window> Engine::s_window = nullptr;
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); m_scene = std::make_shared<Scene>();
s_window = Window::GetInstance(); m_renderer = std::make_unique<Renderer>(m_scene);
s_running = true; 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) { // m_window->Subscribe<WindowCloseEvent>([&](const WindowCloseEvent& e) {
Engine::s_running = false;
});
s_window->Subscribe<WindowResized>([](const WindowResized& e) { // m_app->OnEvent(e);
Engine::s_app->OnWindowResized(e); // });
});
while (s_running) { // m_window->Subscribe<WindowResizeEvent>([&](const WindowResizeEvent& e) {
s_window->ProcessEvents(); // 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(); m_window->Destroy();
s_app.reset(); 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;
} }

View File

@ -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

View File

@ -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"

View File

@ -0,0 +1,4 @@
#include "engine/scene/scene.h"
Scene::Scene() : m_registry() {
}

View File

@ -1,7 +1,7 @@
#include <SDL3/SDL.h> #include <SDL3/SDL.h>
#include "engine/window/window.h" #include "engine/window/window.h"
#include "engine/window/events/window.h" #include "engine/window/events/window_events.h"
#include <iostream> #include <iostream>
#include <GL/glew.h> #include <GL/glew.h>
@ -100,11 +100,13 @@ 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());
EmitEvent(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());
EmitEvent(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 +124,9 @@ void Window::ProcessEvents() {
0, 0,
width, width,
height); 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_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);

View File

@ -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"
@ -20,59 +19,61 @@
#include "engine/components/rotate.h" #include "engine/components/rotate.h"
#include "engine/components/batch.h" #include "engine/components/batch.h"
#include "engine/engine.h" #include "engine/api.h"
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;