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

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_

48
include/renderer/shader.h Normal file
View File

@ -0,0 +1,48 @@
#ifndef SHADER_H
#define SHADER_H
#include <iostream>
#include <string>
#include <GL/glew.h>
#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,17 @@
#ifndef TEXTURE_H_
#define TEXTURE_H_
#include <string>
#include <memory>
class Texture {
private:
unsigned int m_id;
private:
public:
Texture() {}
static std::unique_ptr<Texture> LoadFile(const std::string& filename);
public:
inline const unsigned int GetID() const { return m_id; }
};
#endif // TEXTURE_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_