feat: reorganize source code + cleanup

This commit is contained in:
2025-10-01 16:15:19 +02:00
parent fec93b098b
commit 2b0494a23d
21 changed files with 183 additions and 610 deletions

View File

@ -1,16 +0,0 @@
#ifndef BLOCK_H_
#define BLOCK_H_
#include <glm/glm.hpp>
class Block {
private:
glm::vec3 m_position;
glm::vec4 m_color;
public:
Block(glm::vec3 position, glm::vec4 color);
public:
inline glm::vec3 Position() const { return m_position; }
inline glm::vec4 Color() const { return m_color; }
};
#endif

View File

@ -1,141 +0,0 @@
#ifndef MODEL_H_
#define MODEL_H_
#include <vector>
#include <string>
#include <filesystem>
#include <unordered_map>
#include <glm/glm.hpp>
#include <memory>
#include "shader.h"
#include "texture.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 Vertex {
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();
};
class FaceItem {
private:
unsigned int m_vIndex;
unsigned int m_nIndex;
unsigned int m_tIndex;
public:
FaceItem()
: m_vIndex(0), m_nIndex(0), m_tIndex(0) {}
FaceItem(unsigned int vI, unsigned int nI, unsigned int tI)
: m_vIndex(vI), m_nIndex(nI), m_tIndex(tI) {}
public:
inline const unsigned int GetVertex() const { return m_vIndex; }
inline const unsigned int GetNormal() const { return m_nIndex; }
inline const unsigned int GetTex() const { return m_tIndex; }
public:
inline void SetVertex(unsigned int vIndex) { m_vIndex = vIndex; }
inline void SetNorm(unsigned int nIndex) { m_nIndex = nIndex; }
inline void SetTex(unsigned int tIndex) { m_tIndex = tIndex; }
};
class Face {
private:
std::vector<FaceItem> m_items;
public:
Face()
: m_items(std::vector<FaceItem>()) {}
public:
void PushItem(const FaceItem& item);
public:
inline const unsigned int GetSize() const { return m_items.size(); }
inline const FaceItem& GetItem(unsigned int index) const { return m_items[index]; }
};
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; }
};
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() { glBindVertexArray(m_vao); }
void Unbind() { glBindVertexArray(0); }
void Upload();
public:
std::string materialName;
public:
Mesh();
public:
void Render();
};
class Object {
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;
private:
static inline int NormalizeIndex(const std::string &s, int baseCount);
private:
Object();
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);
};
#endif // MODEL_H_

View File

@ -1,43 +0,0 @@
#ifndef PRELUDE_H_
#define PRELUDE_H_
#ifndef WIN32
#define GLEW_STATIC
#endif
#include <GL/glew.h>
#include "SDL3/SDL.h"
struct RenderContext {
Uint64 time;
Uint64 prev_time;
bool program_failed = false;
GLuint program = 0;
GLint resolution_location = 0;
GLint time_location = 0;
bool pause = false;
};
bool compile_shader_source(const GLchar *source, GLenum shader_type, GLuint *shader);
bool compile_shader_file(const char *file_path, GLenum shader_type, GLuint *shader);
bool link_program(GLuint vert_shader, GLuint frag_shader, GLuint *program);
void reload_shaders(RenderContext*context);
// void key_callback(SDL_Window* window, int key, int scancode, int action, int mods);
void window_size_callback(SDL_Window* window, int width, int height);
void MessageCallback(GLenum source,
GLenum type,
GLuint id,
GLenum severity,
GLsizei length,
const GLchar* message,
const void* userParam);
void process_prelude(RenderContext *context);
#endif // PRELUDE_H_

18
include/renderer/basics.h Normal file
View File

@ -0,0 +1,18 @@
#ifndef RENDERER_BASICS_H
#define RENDERER_BASICS_H
#include <glm/glm.hpp>
class Vertex {
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

16
include/renderer/debug.h Normal file
View File

@ -0,0 +1,16 @@
#ifndef RENDERER_DEBUG_
#define RENDERER_DEBUG_
#include <iostream>
#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,42 @@
#ifndef MATERIAL_H_
#define MATERIAL_H_
#include <glm/glm.hpp>
#include <memory>
#include "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,65 @@
#ifndef MODEL_H_
#define MODEL_H_
#include <vector>
#include <string>
#include <filesystem>
#include <unordered_map>
#include <glm/glm.hpp>
#include <memory>
#include "shader.h"
#include "texture.h"
#include "renderer/material.h"
#include "renderer/basics.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 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() { glBindVertexArray(m_vao); }
void Unbind() { glBindVertexArray(0); }
void Upload();
public:
std::string materialName;
public:
Mesh();
public:
void Render();
};
class Object {
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;
private:
static inline int NormalizeIndex(const std::string &s, int baseCount);
private:
Object();
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);
};
#endif // MODEL_H_

View File

@ -1,37 +0,0 @@
#ifndef VERTEX_H_
#define VERTEX_H_
#include <vector>
#include <glm/glm.hpp>
#include "block.h"
class Point {
private:
glm::vec3 m_position;
glm::vec3 m_normal;
glm::vec4 m_color;
public:
Point(glm::vec3 position, glm::vec3 normal, glm::vec4 color);
};
class Vertices {
private:
std::vector<Point> m_items;
std::vector<unsigned int> m_indices;
unsigned int m_vao;
unsigned int m_vbo;
unsigned int m_ebo;
public:
Vertices();
public: // GPU
void Bind();
void Unbind();
void Draw();
void Upload();
public:
void PushVertex(const Point& point);
void PushIndex(unsigned int index);
};
#endif // VERTEX_H_