diff --git a/CMakeLists.txt b/CMakeLists.txt index c8e77db..e1e709f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -25,13 +25,13 @@ endif() # set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -ggdb") add_executable(CodingGame - src/prelude.cpp - src/file_manager.cpp - src/shader.cpp - src/block.cpp - src/vertex.cpp - src/texture.cpp - src/model.cpp + src/IO/file_manager.cpp + + src/renderer/debug.cpp + src/renderer/shader.cpp + src/renderer/texture.cpp + src/renderer/wavefront.cpp + src/main.cpp ) diff --git a/include/file_manager.h b/include/IO/file_manager.h similarity index 100% rename from include/file_manager.h rename to include/IO/file_manager.h diff --git a/include/block.h b/include/block.h deleted file mode 100644 index d9ad401..0000000 --- a/include/block.h +++ /dev/null @@ -1,16 +0,0 @@ -#ifndef BLOCK_H_ -#define BLOCK_H_ -#include - -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 \ No newline at end of file diff --git a/include/model.h b/include/model.h deleted file mode 100644 index 381ed54..0000000 --- a/include/model.h +++ /dev/null @@ -1,141 +0,0 @@ -#ifndef MODEL_H_ -#define MODEL_H_ -#include -#include -#include -#include -#include -#include - -#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 m_items; -public: - Face() - : m_items(std::vector()) {} -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 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) { 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 m_vertexBuffer; - std::vector 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 m_vertices; - std::vector m_normals; - std::vector m_texCoords; - - std::vector m_meshes; - - std::unordered_map> 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); - std::shared_ptr GetMaterial(std::string name); -private: - Mesh& GetLastMesh(); - void CreateNewMesh(const std::string& materialName); -public: - void Render(Shader& shader); -}; - -#endif // MODEL_H_ \ No newline at end of file diff --git a/include/prelude.h b/include/prelude.h deleted file mode 100644 index 098e83d..0000000 --- a/include/prelude.h +++ /dev/null @@ -1,43 +0,0 @@ -#ifndef PRELUDE_H_ -#define PRELUDE_H_ - -#ifndef WIN32 -#define GLEW_STATIC -#endif -#include - -#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_ \ No newline at end of file diff --git a/include/renderer/basics.h b/include/renderer/basics.h new file mode 100644 index 0000000..b50492f --- /dev/null +++ b/include/renderer/basics.h @@ -0,0 +1,18 @@ +#ifndef RENDERER_BASICS_H +#define RENDERER_BASICS_H + +#include + +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 \ No newline at end of file diff --git a/include/renderer/debug.h b/include/renderer/debug.h new file mode 100644 index 0000000..a2afaf2 --- /dev/null +++ b/include/renderer/debug.h @@ -0,0 +1,16 @@ +#ifndef RENDERER_DEBUG_ +#define RENDERER_DEBUG_ + +#include + +#include + +void MessageCallback(GLenum source, + GLenum type, + GLuint id, + GLenum severity, + GLsizei length, + const GLchar* message, + const void* userParam); + +#endif // RENDERER_DEBUG_ \ No newline at end of file diff --git a/include/renderer/material.h b/include/renderer/material.h new file mode 100644 index 0000000..57b84fe --- /dev/null +++ b/include/renderer/material.h @@ -0,0 +1,42 @@ +#ifndef MATERIAL_H_ +#define MATERIAL_H_ + +#include +#include + +#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 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) { 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_ \ No newline at end of file diff --git a/include/shader.h b/include/renderer/shader.h similarity index 100% rename from include/shader.h rename to include/renderer/shader.h diff --git a/include/texture.h b/include/renderer/texture.h similarity index 100% rename from include/texture.h rename to include/renderer/texture.h diff --git a/include/renderer/wavefront.h b/include/renderer/wavefront.h new file mode 100644 index 0000000..53c7cff --- /dev/null +++ b/include/renderer/wavefront.h @@ -0,0 +1,65 @@ +#ifndef MODEL_H_ +#define MODEL_H_ +#include +#include +#include +#include +#include +#include + +#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 m_vertexBuffer; + std::vector 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 m_vertices; + std::vector m_normals; + std::vector m_texCoords; + + std::vector m_meshes; + + std::unordered_map> 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); + std::shared_ptr GetMaterial(std::string name); +private: + Mesh& GetLastMesh(); + void CreateNewMesh(const std::string& materialName); +public: + void Render(Shader& shader); +}; + +#endif // MODEL_H_ \ No newline at end of file diff --git a/include/vertex.h b/include/vertex.h deleted file mode 100644 index a63f5d8..0000000 --- a/include/vertex.h +++ /dev/null @@ -1,37 +0,0 @@ -#ifndef VERTEX_H_ -#define VERTEX_H_ -#include -#include - -#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 m_items; - std::vector 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_ diff --git a/src/file_manager.cpp b/src/IO/file_manager.cpp similarity index 95% rename from src/file_manager.cpp rename to src/IO/file_manager.cpp index d798f30..745f7cc 100644 --- a/src/file_manager.cpp +++ b/src/IO/file_manager.cpp @@ -1,4 +1,4 @@ -#include "file_manager.h" +#include "IO/file_manager.h" #include #include diff --git a/src/block.cpp b/src/block.cpp deleted file mode 100644 index d6951e1..0000000 --- a/src/block.cpp +++ /dev/null @@ -1,6 +0,0 @@ -#include "block.h" - -Block::Block(glm::vec3 position, glm::vec4 color) { - this->m_position = position; - this->m_color = m_color; -} \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 7b0c3dc..8450012 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,6 +5,10 @@ #include // #endif +#ifndef WIN32 +#define GLEW_STATIC +#endif + #include #include #include @@ -13,12 +17,10 @@ #include #include -#include "shader.h" -#include "file_manager.h" -#include "prelude.h" -#include "block.h" -#include "vertex.h" -#include "model.h" +#include "renderer/shader.h" +#include "IO/file_manager.h" +#include "renderer/debug.h" +#include "renderer/wavefront.h" #define WIDTH 1024 #define HEIGHT 768 diff --git a/src/prelude.cpp b/src/prelude.cpp deleted file mode 100644 index df7da97..0000000 --- a/src/prelude.cpp +++ /dev/null @@ -1,192 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include "prelude.h" - -#ifndef WIN32 -#define GLEW_STATIC -#endif -#include - -#define SCREEN_WIDTH 1024 -#define SCREEN_HEIGHT 768 - -void panic_errno(const char *fmt, ...) -{ - fprintf(stderr, "ERROR: "); - - va_list args; - va_start(args, fmt); - vfprintf(stderr, fmt, args); - va_end(args); - - fprintf(stderr, ": %s\n", strerror(errno)); - - exit(1); -} - -char *slurp_file(const char *file_path) -{ -#define SLURP_FILE_PANIC panic_errno("Could not read file `%s`", file_path) - FILE *f = fopen(file_path, "r"); - if (f == NULL) SLURP_FILE_PANIC; - if (fseek(f, 0, SEEK_END) < 0) SLURP_FILE_PANIC; - - long size = ftell(f); - if (size < 0) SLURP_FILE_PANIC; - - char *buffer = (char*)malloc(size + 1); - if (buffer == NULL) SLURP_FILE_PANIC; - - if (fseek(f, 0, SEEK_SET) < 0) SLURP_FILE_PANIC; - - fread(buffer, 1, size, f); - if (ferror(f) < 0) SLURP_FILE_PANIC; - - buffer[size] = '\0'; - - if (fclose(f) < 0) SLURP_FILE_PANIC; - - return buffer; -#undef SLURP_FILE_PANIC -} - -bool compile_shader_source(const GLchar *source, GLenum shader_type, GLuint *shader) -{ - *shader = glCreateShader(shader_type); - glShaderSource(*shader, 1, &source, NULL); - glCompileShader(*shader); - - GLint compiled = 0; - glGetShaderiv(*shader, GL_COMPILE_STATUS, &compiled); - - if (!compiled) { - GLchar message[1024]; - GLsizei message_size = 0; - glGetShaderInfoLog(*shader, sizeof(message), &message_size, message); - fprintf(stderr, "%.*s\n", message_size, message); - return false; - } - - return true; -} - -bool compile_shader_file(const char *file_path, GLenum shader_type, GLuint *shader) -{ - char *source = slurp_file(file_path); - bool err = compile_shader_source(source, shader_type, shader); - free(source); - return err; -} - -bool link_program(GLuint vert_shader, GLuint frag_shader, GLuint *program) -{ - *program = glCreateProgram(); - - glAttachShader(*program, vert_shader); - glAttachShader(*program, frag_shader); - glLinkProgram(*program); - - GLint linked = 0; - glGetProgramiv(*program, GL_LINK_STATUS, &linked); - if (!linked) { - GLsizei message_size = 0; - GLchar message[1024]; - - glGetProgramInfoLog(*program, sizeof(message), &message_size, message); - fprintf(stderr, "Program Linking: %.*s\n", message_size, message); - } - - glDeleteShader(vert_shader); - glDeleteShader(frag_shader); - - return program; -} - -void reload_shaders(RenderContext* context) -{ - glDeleteProgram(context->program); - - context->program_failed = false; - glClearColor(0.0f, 0.0f, 0.0f, 0.0f); - - GLuint vert = 0; - if (!compile_shader_file("./main.vert", GL_VERTEX_SHADER, &vert)) { - glClearColor(1.0f, 0.0f, 0.0f, 1.0f); - context->program_failed = true; - return; - } - - GLuint frag = 0; - if (!compile_shader_file("./main.frag", GL_FRAGMENT_SHADER, &frag)) { - glClearColor(1.0f, 0.0f, 0.0f, 1.0f); - context->program_failed = true; - return; - } - - if (!link_program(vert, frag, &context->program)) { - glClearColor(1.0f, 0.0f, 0.0f, 1.0f); - context->program_failed = true; - return; - } - - glUseProgram(context->program); - - context->resolution_location = glGetUniformLocation(context->program, "resolution"); - context->time_location = glGetUniformLocation(context->program, "time"); - - printf("Successfully Reload the Shaders\n"); -} - -void window_size_callback(SDL_Window* window, int width, int height) -{ - (void) window; - glViewport( - width / 2 - SCREEN_WIDTH / 2, - height / 2 - SCREEN_HEIGHT / 2, - SCREEN_WIDTH, - SCREEN_HEIGHT); -} - -void MessageCallback(GLenum source, - GLenum type, - GLuint id, - GLenum severity, - GLsizei length, - const GLchar* message, - const void* userParam) -{ - (void) source; - (void) id; - (void) length; - (void) userParam; - fprintf(stderr, "GL CALLBACK: %s type = 0x%x, severity = 0x%x, message = %s\n", - (type == GL_DEBUG_TYPE_ERROR ? "** GL ERROR **" : ""), - type, severity, message); -} - -void process_prelude(RenderContext *context) -{ - // glfwSetKeyCallback(window, key_callback); - // glfwSetFramebufferSizeCallback(window, window_size_callback); - - glClear(GL_COLOR_BUFFER_BIT); - - if (!context->program_failed) { - glUniform2f(context->resolution_location, - SCREEN_WIDTH, - SCREEN_HEIGHT); - glUniform1f(context->time_location, context->time); - - glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); - } - - int64_t cur_time = SDL_GetTicks(); - if (!context->pause) { - context->time += cur_time - context->prev_time; - } - context->prev_time = cur_time; -} diff --git a/src/renderer/debug.cpp b/src/renderer/debug.cpp new file mode 100644 index 0000000..bed02de --- /dev/null +++ b/src/renderer/debug.cpp @@ -0,0 +1,22 @@ +#include "renderer/debug.h" + +void MessageCallback(GLenum source, + GLenum type, + GLuint id, + GLenum severity, + GLsizei length, + const GLchar* message, + const void* userParam) +{ + (void) source; + (void) id; + (void) length; + (void) userParam; + std::cerr << "GL CALLBACK: " << (type == GL_DEBUG_TYPE_ERROR ? "** GL ERROR **" : "") + << " type = 0x" << type + << ", severity = 0x" << severity + << ", message = " << message << std::endl; + // std::cerr << "GL CALLBACK: %s type = 0x%x, severity = 0x%x, message = %s\n", + // (type == GL_DEBUG_TYPE_ERROR ? "** GL ERROR **" : ""), + // type, severity, message); +} \ No newline at end of file diff --git a/src/shader.cpp b/src/renderer/shader.cpp similarity index 99% rename from src/shader.cpp rename to src/renderer/shader.cpp index 91a67e1..9a71ee6 100644 --- a/src/shader.cpp +++ b/src/renderer/shader.cpp @@ -1,4 +1,4 @@ -#include "shader.h" +#include "renderer/shader.h" Shader::Shader() { diff --git a/src/texture.cpp b/src/renderer/texture.cpp similarity index 97% rename from src/texture.cpp rename to src/renderer/texture.cpp index 0fbd5f4..193e9ce 100644 --- a/src/texture.cpp +++ b/src/renderer/texture.cpp @@ -1,7 +1,8 @@ #include #include + #include -#include "texture.h" +#include "renderer/texture.h" #define STB_IMAGE_IMPLEMENTATION #include "stb_image.h" diff --git a/src/model.cpp b/src/renderer/wavefront.cpp similarity index 98% rename from src/model.cpp rename to src/renderer/wavefront.cpp index 76bdd0f..13dba56 100644 --- a/src/model.cpp +++ b/src/renderer/wavefront.cpp @@ -5,7 +5,7 @@ #include #include -#include "model.h" +#include "renderer/wavefront.h" ObjElement toElement(const std::string &s) { if (s == "#") return ObjElement::OHASH; @@ -47,11 +47,6 @@ void Vertex::DefineAttrib() glEnableVertexAttribArray(2); } -void Face::PushItem(const FaceItem& item) -{ - m_items.push_back(item); -} - inline int Object::NormalizeIndex(const std::string &s, int baseCount) { if (s.empty()) return -1; int idx = std::stoi(s); @@ -284,8 +279,6 @@ Object Object::LoadFile(const std::string& filename) { { auto& mesh = obj.GetLastMesh(); std::string token; - - Face fv; while (iss >> token) { std::string a, b, c; diff --git a/src/vertex.cpp b/src/vertex.cpp deleted file mode 100644 index dc96f63..0000000 --- a/src/vertex.cpp +++ /dev/null @@ -1,151 +0,0 @@ -#include - -#include "vertex.h" - -#define BLOCK_SIZE 0.5f - -Point::Point(glm::vec3 position, glm::vec3 normal, glm::vec4 color) -{ - m_position = position; - m_normal = normal; - m_color = color; -} - -Vertices::Vertices() -{ - m_items = std::vector(); - - glGenVertexArrays(1, &m_vao); - glGenBuffers(1, &m_vbo); - glGenBuffers(1, &m_ebo); - - glBindVertexArray(m_vao); - - // VBO (vertex buffer) - glBindBuffer(GL_ARRAY_BUFFER, m_vbo); - glBufferData(GL_ARRAY_BUFFER, 0, nullptr, GL_DYNAMIC_DRAW); - - // EBO (index buffer) - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ebo); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, 0, nullptr, GL_DYNAMIC_DRAW); - - // glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ebo); - // glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(triangleIndices), triangleIndices, GL_STATIC_DRAW); - - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Point), (void*)0); - glEnableVertexAttribArray(0); - - glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Point), (void*)(3 * sizeof(float))); - glEnableVertexAttribArray(1); - - glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, sizeof(Point), (void*)(6 * sizeof(float))); - glEnableVertexAttribArray(2); - - glBindBuffer(GL_ARRAY_BUFFER, 0); - glBindVertexArray(0); -} - -// Call this after you finish adding vertices (or call it each time after PushBlock) -void Vertices::Upload() -{ - glBindVertexArray(m_vao); - - glBindBuffer(GL_ARRAY_BUFFER, m_vbo); - glBufferData(GL_ARRAY_BUFFER, m_items.size() * sizeof(Point), m_items.data(), GL_DYNAMIC_DRAW); - - // Upload indices - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ebo); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, m_indices.size() * sizeof(unsigned int), m_indices.data(), GL_STATIC_DRAW); - - glBindVertexArray(0); -} - -// void Vertices::PushBlock(const Block& block) -// { -// // 1 face -// m_items.emplace_back(block.Position(), block.Color()); -// m_items.emplace_back(block.Position() + glm::vec3(0.f, -BLOCK_SIZE, 0.f), block.Color()); -// m_items.emplace_back(block.Position() + glm::vec3(BLOCK_SIZE, -BLOCK_SIZE, 0.f), block.Color()); - -// // 2 face -// m_items.emplace_back(block.Position(), block.Color()); -// m_items.emplace_back(block.Position() + glm::vec3(BLOCK_SIZE, 0.f, 0.f), block.Color()); -// m_items.emplace_back(block.Position() + glm::vec3(BLOCK_SIZE, -BLOCK_SIZE, 0.f), block.Color()); - -// // 3 face -// m_items.emplace_back(block.Position() + glm::vec3(0.f, 0.f, -BLOCK_SIZE), block.Color()); -// m_items.emplace_back(block.Position() + glm::vec3(0.f, -BLOCK_SIZE, -BLOCK_SIZE), block.Color()); -// m_items.emplace_back(block.Position() + glm::vec3(BLOCK_SIZE, -BLOCK_SIZE, -BLOCK_SIZE), block.Color()); - -// // 4 face -// m_items.emplace_back(block.Position() + glm::vec3(0.f, 0.f, -BLOCK_SIZE), block.Color()); -// m_items.emplace_back(block.Position() + glm::vec3(BLOCK_SIZE, 0.f, -BLOCK_SIZE), block.Color()); -// m_items.emplace_back(block.Position() + glm::vec3(BLOCK_SIZE, -BLOCK_SIZE, -BLOCK_SIZE), block.Color()); - -// // 5 face -// m_items.emplace_back(block.Position() + glm::vec3(0.f, 0.f, -BLOCK_SIZE), block.Color()); -// m_items.emplace_back(block.Position(), block.Color()); -// m_items.emplace_back(block.Position() + glm::vec3(0.f, -BLOCK_SIZE, 0.f), block.Color()); - -// // 6 face -// m_items.emplace_back(block.Position() + glm::vec3(0.f, 0.f, -BLOCK_SIZE), block.Color()); -// m_items.emplace_back(block.Position() + glm::vec3(0.f, -BLOCK_SIZE, -BLOCK_SIZE), block.Color()); -// m_items.emplace_back(block.Position() + glm::vec3(0.f, -BLOCK_SIZE, 0.f), block.Color()); - -// // 7 face -// m_items.emplace_back(block.Position() + glm::vec3(BLOCK_SIZE, 0.f, 0.f), block.Color()); -// m_items.emplace_back(block.Position() + glm::vec3(BLOCK_SIZE, 0.f, -BLOCK_SIZE), block.Color()); -// m_items.emplace_back(block.Position() + glm::vec3(BLOCK_SIZE, -BLOCK_SIZE, 0.f), block.Color()); - -// // 8 face -// m_items.emplace_back(block.Position() + glm::vec3(BLOCK_SIZE, 0.f, -BLOCK_SIZE), block.Color()); -// m_items.emplace_back(block.Position() + glm::vec3(BLOCK_SIZE, -BLOCK_SIZE, -BLOCK_SIZE), block.Color()); -// m_items.emplace_back(block.Position() + glm::vec3(BLOCK_SIZE, -BLOCK_SIZE, 0.f), block.Color()); - -// // 9 face -// m_items.emplace_back(block.Position() + glm::vec3(0.f, 0.f, -BLOCK_SIZE), block.Color()); -// m_items.emplace_back(block.Position() + glm::vec3(BLOCK_SIZE, 0.f, 0.f), block.Color()); -// m_items.emplace_back(block.Position(), block.Color()); - -// // 10 face -// m_items.emplace_back(block.Position() + glm::vec3(0.f, 0.f, -BLOCK_SIZE), block.Color()); -// m_items.emplace_back(block.Position() + glm::vec3(BLOCK_SIZE, 0.f, -BLOCK_SIZE), block.Color()); -// m_items.emplace_back(block.Position() + glm::vec3(BLOCK_SIZE, 0.f, 0.f), block.Color()); - -// // 11 face -// m_items.emplace_back(block.Position() + glm::vec3(0.f, BLOCK_SIZE, -BLOCK_SIZE), block.Color()); -// m_items.emplace_back(block.Position() + glm::vec3(BLOCK_SIZE, BLOCK_SIZE, 0.f), block.Color()); -// m_items.emplace_back(block.Position() + glm::vec3(0.f, BLOCK_SIZE, 0.f), block.Color()); - -// // 12 face -// m_items.emplace_back(block.Position() + glm::vec3(0.f, BLOCK_SIZE, -BLOCK_SIZE), block.Color()); -// m_items.emplace_back(block.Position() + glm::vec3(BLOCK_SIZE, BLOCK_SIZE, -BLOCK_SIZE), block.Color()); -// m_items.emplace_back(block.Position() + glm::vec3(BLOCK_SIZE, BLOCK_SIZE, 0.f), block.Color()); -// } - -void Vertices::PushVertex(const Point& point) -{ - m_items.push_back(point); -} - -void Vertices::PushIndex(unsigned int index) -{ - m_indices.push_back(index); -} - -void Vertices::Bind() -{ - glBindVertexArray(m_vao); -} - -void Vertices::Unbind() -{ - glBindVertexArray(0); -} - -void Vertices::Draw() -{ - Bind(); - glDrawElements(GL_TRIANGLES, static_cast(m_indices.size()), GL_UNSIGNED_INT, 0); - Unbind(); -} \ No newline at end of file