feat: engine as library

This commit is contained in:
2025-10-16 19:43:51 +02:00
parent 165073c36d
commit aa7aafe944
42 changed files with 160 additions and 147 deletions

View 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

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

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

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

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

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

View 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

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

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