feat: engine as library
This commit is contained in:
15
engine/include/engine/IO/file_manager.h
Normal file
15
engine/include/engine/IO/file_manager.h
Normal file
@ -0,0 +1,15 @@
|
||||
#ifndef FILE_MANAGER_H
|
||||
#define FILE_MANAGER_H
|
||||
|
||||
#include <string>
|
||||
|
||||
class FileManager
|
||||
{
|
||||
public:
|
||||
FileManager();
|
||||
~FileManager();
|
||||
|
||||
static std::string read(const std::string &filename);
|
||||
};
|
||||
|
||||
#endif // FILE_MANAGER_H
|
20
engine/include/engine/IO/parser.h
Normal file
20
engine/include/engine/IO/parser.h
Normal file
@ -0,0 +1,20 @@
|
||||
#ifndef PARSER_H_
|
||||
#define PARSER_H_
|
||||
|
||||
// Very fast OBJ/MTL line parser
|
||||
class Parser {
|
||||
private:
|
||||
char* m_sv;
|
||||
public:
|
||||
Parser(char* sv) : m_sv(sv) {}
|
||||
public:
|
||||
void SkipSpaces();
|
||||
char* TakeWord();
|
||||
float TakeFloat();
|
||||
int TakeInt();
|
||||
bool TakeFaceIndices(int& vi, int& ti, int& ni);
|
||||
char* TakeUntil(char d);
|
||||
int TakeIndex(int baseCount);
|
||||
};
|
||||
|
||||
#endif // PARSER_H_
|
17
engine/include/engine/app/app.h
Normal file
17
engine/include/engine/app/app.h
Normal file
@ -0,0 +1,17 @@
|
||||
#ifndef APPLICATION_H_
|
||||
#define APPLICATION_H_
|
||||
|
||||
class IApplication {
|
||||
public:
|
||||
virtual ~IApplication() = default;
|
||||
|
||||
virtual void OnInit() {};
|
||||
virtual void OnUpdate() {};
|
||||
virtual void OnRender() {};
|
||||
virtual void OnShutdown() {};
|
||||
|
||||
virtual void OnEvent() {};
|
||||
virtual void OnWindowResized(const WindowResized& e) {};
|
||||
};
|
||||
|
||||
#endif // APPLICATION_H_
|
27
engine/include/engine/components/batch.h
Normal file
27
engine/include/engine/components/batch.h
Normal file
@ -0,0 +1,27 @@
|
||||
#ifndef COMPONENT_BATCH_H_
|
||||
#define COMPONENT_BATCH_H_
|
||||
|
||||
#include "engine/renderer/renderer.h"
|
||||
|
||||
// requires mesh component
|
||||
struct batch {
|
||||
friend class Renderer;
|
||||
public:
|
||||
// requires transform component
|
||||
struct item {
|
||||
unsigned int batchId;
|
||||
};
|
||||
|
||||
batch();
|
||||
|
||||
inline const unsigned int id() const { return m_id; }
|
||||
protected:
|
||||
static unsigned int LastID;
|
||||
private:
|
||||
unsigned int m_id;
|
||||
unsigned int m_instance_vbo { 0 };
|
||||
private:
|
||||
void prepare(glm::mat4 *instances, unsigned int count);
|
||||
};
|
||||
|
||||
#endif // COMPONENT_BATCH_H_
|
6
engine/include/engine/components/camera.h
Normal file
6
engine/include/engine/components/camera.h
Normal file
@ -0,0 +1,6 @@
|
||||
#ifndef COMPONENTS_PLAYER_H_
|
||||
#define COMPONENTS_PLAYER_H_
|
||||
|
||||
struct camera {};
|
||||
|
||||
#endif // COMPONENTS_PLAYER_H_
|
26
engine/include/engine/components/light.h
Normal file
26
engine/include/engine/components/light.h
Normal file
@ -0,0 +1,26 @@
|
||||
#ifndef COMPONENTS_LIGHT_H_
|
||||
#define COMPONENTS_LIGHT_H_
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
#include "engine/renderer/renderer.h"
|
||||
|
||||
struct light {
|
||||
friend class Renderer;
|
||||
public:
|
||||
enum LightType {
|
||||
DIRECTIONAL = 0,
|
||||
};
|
||||
LightType type;
|
||||
glm::vec3 color;
|
||||
float intensity;
|
||||
|
||||
light(LightType t, const glm::vec3& c, float i)
|
||||
: type(t), color(c), intensity(i),
|
||||
shadowMap(0), fbo(0), lightSpace(1.0f) {}
|
||||
private:
|
||||
unsigned int shadowMap;
|
||||
unsigned int fbo;
|
||||
glm::mat4 lightSpace;
|
||||
};
|
||||
|
||||
#endif // COMPONENTS_LIGHT_H_
|
11
engine/include/engine/components/mesh.h
Normal file
11
engine/include/engine/components/mesh.h
Normal file
@ -0,0 +1,11 @@
|
||||
#ifndef COMPONENTS_MESH_H_
|
||||
#define COMPONENTS_MESH_H_
|
||||
|
||||
#include <memory>
|
||||
#include "engine/renderer/wavefront.h"
|
||||
|
||||
struct mesh {
|
||||
std::shared_ptr<Object> object;
|
||||
};
|
||||
|
||||
#endif // COMPONENTS_MESH_H_
|
6
engine/include/engine/components/rotate.h
Normal file
6
engine/include/engine/components/rotate.h
Normal file
@ -0,0 +1,6 @@
|
||||
#ifndef COMPONENT_ROTATE_H_
|
||||
#define COMPONENT_ROTATE_H_
|
||||
|
||||
struct rotate {};
|
||||
|
||||
#endif // COMPONENT_ROTATE_H_
|
12
engine/include/engine/components/transform.h
Normal file
12
engine/include/engine/components/transform.h
Normal file
@ -0,0 +1,12 @@
|
||||
#ifndef COMPONENTS_TRANSFORM_H_
|
||||
#define COMPONENTS_TRANSFORM_H_
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
struct transform {
|
||||
glm::vec3 position;
|
||||
glm::vec3 rotation;
|
||||
glm::vec3 scale;
|
||||
};
|
||||
|
||||
#endif // COMPONENTS_TRANSFORM_H_
|
21
engine/include/engine/renderer/basics.h
Normal file
21
engine/include/engine/renderer/basics.h
Normal file
@ -0,0 +1,21 @@
|
||||
#ifndef RENDERER_BASICS_H
|
||||
#define RENDERER_BASICS_H
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
#include "engine/renderer/mesh.h"
|
||||
|
||||
class Vertex {
|
||||
friend class Mesh;
|
||||
private:
|
||||
glm::vec3 m_position;
|
||||
glm::vec3 m_normal;
|
||||
glm::vec2 m_texCoord;
|
||||
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
|
14
engine/include/engine/renderer/debug.h
Normal file
14
engine/include/engine/renderer/debug.h
Normal file
@ -0,0 +1,14 @@
|
||||
#ifndef RENDERER_DEBUG_
|
||||
#define RENDERER_DEBUG_
|
||||
|
||||
#include <GL/glew.h>
|
||||
|
||||
void MessageCallback(GLenum source,
|
||||
GLenum type,
|
||||
GLuint id,
|
||||
GLenum severity,
|
||||
GLsizei length,
|
||||
const GLchar* message,
|
||||
const void* userParam);
|
||||
|
||||
#endif // RENDERER_DEBUG_
|
22
engine/include/engine/renderer/engine.h
Normal file
22
engine/include/engine/renderer/engine.h
Normal file
@ -0,0 +1,22 @@
|
||||
#ifndef ENGINE_H_
|
||||
#define ENGINE_H_
|
||||
|
||||
#include <memory>
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
#include "engine/window/window.h"
|
||||
#include "engine/window/events/window.h"
|
||||
|
||||
#include "engine/app/app.h"
|
||||
|
||||
class Engine {
|
||||
public:
|
||||
static 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;
|
||||
};
|
||||
|
||||
|
||||
#endif // ENGINE_H_
|
42
engine/include/engine/renderer/material.h
Normal file
42
engine/include/engine/renderer/material.h
Normal file
@ -0,0 +1,42 @@
|
||||
#ifndef MATERIAL_H_
|
||||
#define MATERIAL_H_
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
#include <memory>
|
||||
|
||||
#include "engine/renderer/texture.h"
|
||||
|
||||
class Material {
|
||||
private:
|
||||
glm::vec3 m_ambient { 0.2f, 0.2f, 0.2f };
|
||||
glm::vec3 m_diffuse { 0.8f, 0.8f, 0.8f };
|
||||
glm::vec3 m_specular { 1.0f, 1.0f, 1.0f };
|
||||
float m_shininess { 32.0f };
|
||||
float m_opacity { 1.0f };
|
||||
int m_illum { 2 };
|
||||
|
||||
std::unique_ptr<Texture> m_diffuse_tex { nullptr };
|
||||
public:
|
||||
Material() = default;
|
||||
Material(const Material& other) = default; // copy constructor
|
||||
Material& operator=(const Material& other) = default;
|
||||
public:
|
||||
inline const glm::vec3 GetAmbientColor() const { return m_ambient; }
|
||||
inline const glm::vec3 GetDiffuseColor() const { return m_diffuse; }
|
||||
inline const glm::vec3 GetSpecularColor() const { return m_specular; }
|
||||
inline const float GetSpecularWeight() const { return m_shininess; }
|
||||
inline const bool HasDiffuseTexture() const { return m_diffuse_tex != nullptr; }
|
||||
inline const Texture* GetDiffuseTexture() const { return m_diffuse_tex.get(); }
|
||||
inline const float GetOpacity() const { return m_opacity; }
|
||||
inline const int GetIllumination() const { return m_illum; }
|
||||
public:
|
||||
inline void SetAmbientColor(glm::vec3 ambient) { m_ambient = ambient; }
|
||||
inline void SetDiffuseColor(glm::vec3 diffuse) { m_diffuse = diffuse; }
|
||||
inline void SetSpecularColor(glm::vec3 specular) { m_specular = specular; }
|
||||
inline void SetSpecularWeight(float weight) { m_shininess = weight; }
|
||||
inline void SetDiffuseTexture(std::unique_ptr<Texture>&& texture) { m_diffuse_tex = std::move(texture); }
|
||||
inline void SetOpacity(float opacity) { m_opacity = opacity; }
|
||||
inline void SetIllumination(float illum) { m_illum = illum; }
|
||||
};
|
||||
|
||||
#endif // MATERIAL_H_
|
27
engine/include/engine/renderer/mesh.h
Normal file
27
engine/include/engine/renderer/mesh.h
Normal file
@ -0,0 +1,27 @@
|
||||
#ifndef MESH_H_
|
||||
#define MESH_H_
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <GL/glew.h>
|
||||
|
||||
#include "engine/renderer/basics.h"
|
||||
|
||||
class Mesh {
|
||||
public: // TODO: abstract away
|
||||
unsigned int m_vao, m_vbo, m_ebo;
|
||||
std::vector<Vertex> m_vertexBuffer;
|
||||
std::vector<unsigned int> m_indexBuffer;
|
||||
public: // TODO: abstract away
|
||||
void Bind() const { glBindVertexArray(m_vao); }
|
||||
void Unbind() { glBindVertexArray(0); }
|
||||
void Upload() const;
|
||||
public:
|
||||
std::string materialName;
|
||||
public:
|
||||
Mesh();
|
||||
public:
|
||||
void Render(unsigned int count);
|
||||
};
|
||||
|
||||
#endif // MESH_H_
|
37
engine/include/engine/renderer/renderer.h
Normal file
37
engine/include/engine/renderer/renderer.h
Normal file
@ -0,0 +1,37 @@
|
||||
#ifndef RENDERER_H_
|
||||
#define RENDERER_H_
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
#include <entt/entity/registry.hpp>
|
||||
|
||||
#include "engine/renderer/shader.h"
|
||||
|
||||
// TODO: make static or singleton
|
||||
class Renderer {
|
||||
public:
|
||||
Renderer(entt::registry& registry);
|
||||
|
||||
void Render();
|
||||
void Init();
|
||||
void GenerateShadowMaps();
|
||||
|
||||
void OnWindowResized(int w, int h);
|
||||
private:
|
||||
void ApplyLights(Shader &shader);
|
||||
void UpdateView();
|
||||
void RenderScene(Shader &shader);
|
||||
private:
|
||||
Shader m_shader;
|
||||
Shader m_depthShader;
|
||||
|
||||
entt::registry& m_registry;
|
||||
|
||||
// unsigned int m_depth_fbo;
|
||||
// unsigned int m_depthMap;
|
||||
|
||||
glm::mat4 m_model;
|
||||
glm::mat4 m_proj;
|
||||
glm::mat4 m_view;
|
||||
};
|
||||
|
||||
#endif // RENDERER_H_
|
46
engine/include/engine/renderer/shader.h
Normal file
46
engine/include/engine/renderer/shader.h
Normal file
@ -0,0 +1,46 @@
|
||||
#ifndef SHADER_H
|
||||
#define SHADER_H
|
||||
|
||||
#include <string>
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
class Shader
|
||||
{
|
||||
public:
|
||||
Shader();
|
||||
~Shader();
|
||||
|
||||
unsigned int m_id;
|
||||
|
||||
void init(const std::string &vertexCode, const std::string &fragmentCode);
|
||||
void use();
|
||||
|
||||
void setBool(const std::string &name, bool value) const;
|
||||
void setInt(const std::string &name, int value) const;
|
||||
void setFloat(const std::string &name, float value) const;
|
||||
void setVec2(const std::string &name, const glm::vec2 &value) const;
|
||||
void setVec2(const std::string &name, float x, float y) const;
|
||||
void setVec3(const std::string &name, const glm::vec3 &value) const;
|
||||
void setVec3(const std::string &name, float x, float y, float z) const;
|
||||
void setVec4(const std::string &name, const glm::vec4 &value) const;
|
||||
void setVec4(const std::string &name, float x, float y, float z, float w) const;
|
||||
void setMat2(const std::string &name, const glm::mat2 &mat) const;
|
||||
void setMat3(const std::string &name, const glm::mat3 &mat) const;
|
||||
void setMat4(const std::string &name, const glm::mat4 &mat) const;
|
||||
|
||||
private:
|
||||
unsigned int m_vertexId;
|
||||
unsigned int m_fragmentId;
|
||||
|
||||
std::string m_vertexCode;
|
||||
std::string m_fragmentCode;
|
||||
|
||||
void compile();
|
||||
void link();
|
||||
|
||||
void checkCompileError(unsigned int shader, const std::string type);
|
||||
void checkLinkingError();
|
||||
};
|
||||
|
||||
#endif // SHADER_H
|
16
engine/include/engine/renderer/texture.h
Normal file
16
engine/include/engine/renderer/texture.h
Normal file
@ -0,0 +1,16 @@
|
||||
#ifndef TEXTURE_H_
|
||||
#define TEXTURE_H_
|
||||
#include <string>
|
||||
#include <memory>
|
||||
|
||||
class Texture {
|
||||
public:
|
||||
Texture() : m_id(0) {}
|
||||
static std::unique_ptr<Texture> LoadFile(const std::string& filename);
|
||||
public:
|
||||
[[nodiscard]] unsigned int GetID() const { return m_id; }
|
||||
private:
|
||||
unsigned int m_id;
|
||||
};
|
||||
|
||||
#endif // TEXTURE_H_
|
54
engine/include/engine/renderer/wavefront.h
Normal file
54
engine/include/engine/renderer/wavefront.h
Normal file
@ -0,0 +1,54 @@
|
||||
#ifndef MODEL_H_
|
||||
#define MODEL_H_
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <filesystem>
|
||||
#include <unordered_map>
|
||||
#include <glm/glm.hpp>
|
||||
#include <memory>
|
||||
|
||||
#include "engine/renderer/shader.h"
|
||||
#include "engine/renderer/renderer.h"
|
||||
#include "engine/renderer/material.h"
|
||||
#include "engine/renderer/mesh.h"
|
||||
|
||||
enum ObjElement { OHASH, MTLLIB, USEMTL, O, V, VN, VT, F, OUNKNOWN };
|
||||
enum MtlElement { MHASH, NEWMTL, NS, KA, KS, KD, NI, D, ILLUM, MAP_KD, MAP_KA, MUNKNOWN };
|
||||
|
||||
class Object {
|
||||
friend class Renderer;
|
||||
private:
|
||||
static inline int NormalizeIndex(int idx, int baseCount);
|
||||
|
||||
private:
|
||||
Object();
|
||||
public:
|
||||
~Object() = default;
|
||||
public:
|
||||
static Object* LoadFile(const std::string& filename);
|
||||
|
||||
private:
|
||||
void LoadMaterials(const std::filesystem::path& filename);
|
||||
private:
|
||||
void AddMaterial(std::string name, std::shared_ptr<Material> material);
|
||||
std::shared_ptr<Material> GetMaterial(std::string name);
|
||||
private:
|
||||
Mesh& GetLastMesh();
|
||||
void CreateNewMesh(const std::string& materialName);
|
||||
public:
|
||||
void Render(Shader& shader, unsigned int count);
|
||||
[[nodiscard]] inline const std::string Name() const { return m_name; }
|
||||
protected:
|
||||
void EnableBatch(unsigned int instanceVBO);
|
||||
private:
|
||||
std::string m_name;
|
||||
std::vector<glm::vec3> m_vertices;
|
||||
std::vector<glm::vec3> m_normals;
|
||||
std::vector<glm::vec2> m_texCoords;
|
||||
|
||||
std::vector<Mesh> m_meshes;
|
||||
|
||||
std::unordered_map<std::string, std::shared_ptr<Material>> m_materials;
|
||||
};
|
||||
|
||||
#endif // MODEL_H_
|
57
engine/include/engine/window/event.h
Normal file
57
engine/include/engine/window/event.h
Normal file
@ -0,0 +1,57 @@
|
||||
#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_
|
7
engine/include/engine/window/events/window.h
Normal file
7
engine/include/engine/window/events/window.h
Normal file
@ -0,0 +1,7 @@
|
||||
#ifndef WINDOW_EVENTS_H_
|
||||
#define WINDOW_EVENTS_H_
|
||||
|
||||
struct WindowResized { int w, h; };
|
||||
struct WindowCloseRequested {};
|
||||
|
||||
#endif // WINDOW_EVENTS_H_
|
51
engine/include/engine/window/window.h
Normal file
51
engine/include/engine/window/window.h
Normal file
@ -0,0 +1,51 @@
|
||||
#ifndef WINDOW_H_
|
||||
#define WINDOW_H_
|
||||
#include <SDL3/SDL.h>
|
||||
#include <memory>
|
||||
|
||||
#include "engine/window/event.h"
|
||||
|
||||
#define ENGINE_GL_MAJOR_VERSION 4
|
||||
#define ENGINE_GL_MINOR_VERSION 6
|
||||
#define ENGINE_GL_MULTISAMPLE_BUFFERS 1
|
||||
#define ENGINE_GL_MULTISAMPLE_SAMPLES 8
|
||||
|
||||
#define DEFAULT_WIDTH 1024
|
||||
#define DEFAULT_HEIGHT 768
|
||||
|
||||
class Window : public EventDispatcher {
|
||||
friend class Engine;
|
||||
private:
|
||||
Window();
|
||||
Window(const char* title, int width, int height);
|
||||
~Window();
|
||||
|
||||
struct WindowDeleter {
|
||||
void operator()(Window* w) const { delete w; };
|
||||
};
|
||||
public:
|
||||
static std::shared_ptr<Window> GetInstance();
|
||||
|
||||
Window(Window&& window) noexcept;
|
||||
Window& operator=(Window&& window) noexcept;
|
||||
|
||||
Window(const Window& window) noexcept = delete;
|
||||
Window& operator=(const Window& window) noexcept = delete;
|
||||
public:
|
||||
[[nodiscard]] static inline int GetWidth() { return Window::GetInstance()->m_width; }
|
||||
[[nodiscard]] static inline int GetHeight() { return Window::GetInstance()->m_height; }
|
||||
private:
|
||||
void ProcessEvents();
|
||||
void SwapBuffers() const;
|
||||
void Destroy() const;
|
||||
private:
|
||||
static std::shared_ptr<Window> s_instance;
|
||||
|
||||
SDL_Window *m_handle;
|
||||
SDL_GLContext m_context;
|
||||
|
||||
int m_width;
|
||||
int m_height;
|
||||
};
|
||||
|
||||
#endif //WINDOW_H_
|
Reference in New Issue
Block a user