feat: initial commit
This commit is contained in:
16
include/block.h
Normal file
16
include/block.h
Normal file
@ -0,0 +1,16 @@
|
||||
#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
|
15
include/file_manager.h
Normal file
15
include/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
|
134
include/model.h
Normal file
134
include/model.h
Normal file
@ -0,0 +1,134 @@
|
||||
#ifndef MODEL_H_
|
||||
#define MODEL_H_
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#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 };
|
||||
|
||||
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(); }
|
||||
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); }
|
||||
};
|
||||
|
||||
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::string& 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_
|
39
include/prelude.h
Normal file
39
include/prelude.h
Normal file
@ -0,0 +1,39 @@
|
||||
#ifndef PRELUDE_H_
|
||||
#define PRELUDE_H_
|
||||
#define GLEW_STATIC
|
||||
#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_
|
48
include/shader.h
Normal file
48
include/shader.h
Normal 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
|
17
include/texture.h
Normal file
17
include/texture.h
Normal 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_
|
37
include/vertex.h
Normal file
37
include/vertex.h
Normal file
@ -0,0 +1,37 @@
|
||||
#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_
|
Reference in New Issue
Block a user