From 6ed978051668c08f5a957c97570f364dd580c807 Mon Sep 17 00:00:00 2001 From: Mel Date: Fri, 21 Oct 2022 01:03:18 +0200 Subject: Namespace and Folder refactor --- CMakeLists.txt | 2 +- src/Binder.cpp | 65 ------------------ src/Binder.hpp | 38 ----------- src/Camera.cpp | 42 ------------ src/Camera.hpp | 26 -------- src/GFX/Binder.cpp | 65 ++++++++++++++++++ src/GFX/Binder.hpp | 38 +++++++++++ src/GFX/Camera.cpp | 42 ++++++++++++ src/GFX/Camera.hpp | 26 ++++++++ src/GFX/Image/PPMParser.cpp | 154 ++++++++++++++++++++++++++++++++++++++++++ src/GFX/Image/PPMParser.hpp | 44 ++++++++++++ src/GFX/Image/RawImage.cpp | 29 ++++++++ src/GFX/Image/RawImage.hpp | 37 +++++++++++ src/GFX/Mesh.cpp | 29 ++++++++ src/GFX/Mesh.hpp | 34 ++++++++++ src/GFX/Mouse.cpp | 24 +++++++ src/GFX/Mouse.hpp | 21 ++++++ src/GFX/Shading/Program.cpp | 42 ++++++++++++ src/GFX/Shading/Program.hpp | 25 +++++++ src/GFX/Shading/Shader.cpp | 24 +++++++ src/GFX/Shading/Shader.hpp | 29 ++++++++ src/GFX/Shading/Uniform.cpp | 14 ++++ src/GFX/Shading/Uniform.hpp | 23 +++++++ src/GFX/Texture.cpp | 29 ++++++++ src/GFX/Texture.hpp | 17 +++++ src/GFX/Window.cpp | 55 +++++++++++++++ src/GFX/Window.hpp | 30 +++++++++ src/Image/PPMParser.cpp | 155 ------------------------------------------- src/Image/PPMParser.hpp | 44 ------------ src/Image/RawImage.cpp | 29 -------- src/Image/RawImage.hpp | 37 ----------- src/Mesh.cpp | 25 ------- src/Mesh.hpp | 34 ---------- src/Mouse.cpp | 25 ------- src/Mouse.hpp | 19 ------ src/Shader/Shader.cpp | 24 ------- src/Shader/Shader.hpp | 29 -------- src/Shader/ShaderProgram.cpp | 42 ------------ src/Shader/ShaderProgram.hpp | 25 ------- src/Shader/Uniform.cpp | 14 ---- src/Shader/Uniform.hpp | 23 ------- src/Texture.cpp | 29 -------- src/Texture.hpp | 17 ----- src/Window.cpp | 51 -------------- src/Window.hpp | 27 -------- src/World/BlockSide.hpp | 2 +- src/World/BlockType.hpp | 2 +- src/World/Chunk.cpp | 4 +- src/World/Chunk.hpp | 9 +-- src/World/Generator.cpp | 2 +- src/World/Generator.hpp | 2 +- src/main.cpp | 51 +++++++------- 52 files changed, 868 insertions(+), 857 deletions(-) delete mode 100644 src/Binder.cpp delete mode 100644 src/Binder.hpp delete mode 100644 src/Camera.cpp delete mode 100644 src/Camera.hpp create mode 100644 src/GFX/Binder.cpp create mode 100644 src/GFX/Binder.hpp create mode 100644 src/GFX/Camera.cpp create mode 100644 src/GFX/Camera.hpp create mode 100644 src/GFX/Image/PPMParser.cpp create mode 100644 src/GFX/Image/PPMParser.hpp create mode 100644 src/GFX/Image/RawImage.cpp create mode 100644 src/GFX/Image/RawImage.hpp create mode 100644 src/GFX/Mesh.cpp create mode 100644 src/GFX/Mesh.hpp create mode 100644 src/GFX/Mouse.cpp create mode 100644 src/GFX/Mouse.hpp create mode 100644 src/GFX/Shading/Program.cpp create mode 100644 src/GFX/Shading/Program.hpp create mode 100644 src/GFX/Shading/Shader.cpp create mode 100644 src/GFX/Shading/Shader.hpp create mode 100644 src/GFX/Shading/Uniform.cpp create mode 100644 src/GFX/Shading/Uniform.hpp create mode 100644 src/GFX/Texture.cpp create mode 100644 src/GFX/Texture.hpp create mode 100644 src/GFX/Window.cpp create mode 100644 src/GFX/Window.hpp delete mode 100644 src/Image/PPMParser.cpp delete mode 100644 src/Image/PPMParser.hpp delete mode 100644 src/Image/RawImage.cpp delete mode 100644 src/Image/RawImage.hpp delete mode 100644 src/Mesh.cpp delete mode 100644 src/Mesh.hpp delete mode 100644 src/Mouse.cpp delete mode 100644 src/Mouse.hpp delete mode 100644 src/Shader/Shader.cpp delete mode 100644 src/Shader/Shader.hpp delete mode 100644 src/Shader/ShaderProgram.cpp delete mode 100644 src/Shader/ShaderProgram.hpp delete mode 100644 src/Shader/Uniform.cpp delete mode 100644 src/Shader/Uniform.hpp delete mode 100644 src/Texture.cpp delete mode 100644 src/Texture.hpp delete mode 100644 src/Window.cpp delete mode 100644 src/Window.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 99d9c81..f1834cf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,7 +10,7 @@ if (LINUX) find_package(OpenGL REQUIRED) endif (LINUX) -add_executable(meowcraft src/main.cpp src/Window.cpp src/Window.hpp src/Mesh.cpp src/Mesh.hpp src/Math/Vector.hpp src/Math/Math.hpp src/Binder.cpp src/Binder.hpp src/Shader/Shader.cpp src/Shader/Shader.hpp src/Shader/ShaderProgram.cpp src/Shader/ShaderProgram.hpp src/Math/Matrix.hpp src/Math/MVP.cpp src/Math/MVP.hpp src/Camera.cpp src/Camera.hpp src/Math/Rotation.hpp src/Shader/Uniform.cpp src/Shader/Uniform.hpp src/Mouse.cpp src/Mouse.hpp src/Math/Trig.hpp src/Texture.cpp src/Texture.hpp src/Assets.cpp src/Assets.hpp src/Image/RawImage.cpp src/Image/RawImage.hpp src/Image/PPMParser.cpp src/Image/PPMParser.hpp src/World/Chunk.cpp src/World/Chunk.hpp src/World/BlockType.hpp src/World/Generator.cpp src/World/Generator.hpp src/World/BlockSide.hpp) +add_executable(meowcraft src/main.cpp src/GFX/Window.cpp src/GFX/Window.hpp src/GFX/Mesh.cpp src/GFX/Mesh.hpp src/Math/Vector.hpp src/Math/Math.hpp src/GFX/Binder.cpp src/GFX/Binder.hpp src/GFX/Shading/Shader.cpp src/GFX/Shading/Shader.hpp src/GFX/Shading/Program.cpp src/GFX/Shading/Program.hpp src/Math/Matrix.hpp src/Math/MVP.cpp src/Math/MVP.hpp src/GFX/Camera.cpp src/GFX/Camera.hpp src/Math/Rotation.hpp src/GFX/Shading/Uniform.cpp src/GFX/Shading/Uniform.hpp src/GFX/Mouse.cpp src/GFX/Mouse.hpp src/Math/Trig.hpp src/GFX/Texture.cpp src/GFX/Texture.hpp src/Assets.cpp src/Assets.hpp src/GFX/Image/RawImage.cpp src/GFX/Image/RawImage.hpp src/GFX/Image/PPMParser.cpp src/GFX/Image/PPMParser.hpp src/World/Chunk.cpp src/World/Chunk.hpp src/World/BlockType.hpp src/World/Generator.cpp src/World/Generator.hpp src/World/BlockSide.hpp) target_link_libraries(meowcraft glfw GLEW::GLEW) if (LINUX) diff --git a/src/Binder.cpp b/src/Binder.cpp deleted file mode 100644 index d013f08..0000000 --- a/src/Binder.cpp +++ /dev/null @@ -1,65 +0,0 @@ -#include -#include "Binder.hpp" -#include "Mesh.hpp" - -namespace MC { - -BindableMesh Binder::load(Mesh& mesh) { - auto vao = create_vao(); - if (mesh.indices_size() > 0) { - store_indices(mesh.raw_indices(), mesh.indices_size()); - } - store_in_attribute_list(0, 3, mesh.raw(), mesh.size() * 3); - store_in_attribute_list(1, 2, mesh.raw_tex_coords(), mesh.tex_coords_size() * 2); - unbind_vao(); - - return {vao, mesh.indices_size()}; -} - -uint32_t Binder::create_vao() { - GLuint vao; - glGenVertexArrays(1, &vao); - glBindVertexArray(vao); - - return static_cast(vao); -} - -uint32_t Binder::unbind_vao() { - glBindVertexArray(0); -} - -void Binder::store_indices(uint32_t* indices, size_t indices_size) { - GLuint ebo; - glGenBuffers(1, &ebo); - - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices_size * sizeof(float), indices, GL_STATIC_DRAW); -} - -void Binder::store_in_attribute_list(uint32_t attribute, size_t size, float* data, size_t data_size) { - GLuint vbo; - glGenBuffers(1, &vbo); - - glBindBuffer(GL_ARRAY_BUFFER, vbo); - glBufferData(GL_ARRAY_BUFFER, data_size * sizeof(float), data, GL_STATIC_DRAW); - glVertexAttribPointer(attribute, size, GL_FLOAT, GL_FALSE, size * sizeof(float), nullptr); - glBindBuffer(GL_ARRAY_BUFFER, 0); -} - -void BindableMesh::bind() const { - glBindVertexArray(m_vao); - glEnableVertexAttribArray(0); - glEnableVertexAttribArray(1); -} - -void BindableMesh::unbind() { - glBindVertexArray(0); - glDisableVertexAttribArray(0); - glDisableVertexAttribArray(1); -} - -size_t BindableMesh::size() const { - return m_vertex_count; -} - -} \ No newline at end of file diff --git a/src/Binder.hpp b/src/Binder.hpp deleted file mode 100644 index 9108a65..0000000 --- a/src/Binder.hpp +++ /dev/null @@ -1,38 +0,0 @@ -#pragma once - -#include -#include "Mesh.hpp" - -namespace MC { - -class BindableMesh { -public: - void bind() const; - void unbind(); - - size_t size() const; - -private: - BindableMesh(uint32_t vao, size_t vertex_count) : m_vao(vao), m_vertex_count(vertex_count) {}; - - uint32_t m_vao; - size_t m_vertex_count; - - friend class Binder; -}; - -class Binder { -public: - Binder() = default;; - - static BindableMesh load(Mesh& mesh); - -private: - static uint32_t create_vao(); - static uint32_t unbind_vao(); - - static void store_in_attribute_list(uint32_t attribute, size_t size, float* data, size_t data_size); - static void store_indices(uint32_t* indices, size_t indices_size); -}; - -} \ No newline at end of file diff --git a/src/Camera.cpp b/src/Camera.cpp deleted file mode 100644 index 3a9d7cd..0000000 --- a/src/Camera.cpp +++ /dev/null @@ -1,42 +0,0 @@ -#include "Camera.hpp" - -namespace MC { - -Vector<3> Camera::position() { - return m_position; -} - -void Camera::set_position(Vector<3> position) { - m_position = position; -} - -void Camera::move(Vector<3> vector) { - m_position = m_position + vector; -} - -void Camera::move_relative(Vector<3> by) { - auto rotation = Matrix<4, 4>::rotation(m_angles); - - auto result = rotation.transpose() * Vector<4>{by.x(), by.y(), by.z(), 1.0f}; - move(result.elements); -} - -Rotation Camera::angles() { - return m_angles; -} - -void Camera::set_angles(Rotation angles) { - m_angles = angles; -} - -void Camera::rotate(Rotation by) { - m_angles = m_angles + by; - - if (m_angles.pitch() > 89.0f) { - m_angles.pitch() = 89.0f; - } else if (m_angles.pitch() < -89.0f) { - m_angles.pitch() = -89.0f; - } -} - -} diff --git a/src/Camera.hpp b/src/Camera.hpp deleted file mode 100644 index 705f8b9..0000000 --- a/src/Camera.hpp +++ /dev/null @@ -1,26 +0,0 @@ -#pragma once - -#include "Math/Math.hpp" -#include "Math/Rotation.hpp" - -namespace MC { - -class Camera { -public: - Camera() = default; - - Vector<3> position(); - void set_position(Vector<3> position); - void move(Vector<3> by); - void move_relative(Vector<3> by); - - Rotation angles(); - void set_angles(Rotation angles); - void rotate(Rotation by); - -private: - Vector<3> m_position = {}; - Rotation m_angles = {}; -}; - -} \ No newline at end of file diff --git a/src/GFX/Binder.cpp b/src/GFX/Binder.cpp new file mode 100644 index 0000000..e7b7e4c --- /dev/null +++ b/src/GFX/Binder.cpp @@ -0,0 +1,65 @@ +#include +#include "Binder.hpp" +#include "Mesh.hpp" + +namespace MC::GFX { + +BindableMesh Binder::load(Mesh& mesh) { + auto vao = create_vao(); + if (mesh.indices_size() > 0) { + store_indices(mesh.raw_indices(), mesh.indices_size()); + } + store_in_attribute_list(0, 3, mesh.raw(), mesh.size() * 3); + store_in_attribute_list(1, 2, mesh.raw_tex_coords(), mesh.tex_coords_size() * 2); + unbind_vao(); + + return {vao, mesh.indices_size()}; +} + +uint32_t Binder::create_vao() { + GLuint vao; + glGenVertexArrays(1, &vao); + glBindVertexArray(vao); + + return static_cast(vao); +} + +void Binder::unbind_vao() { + glBindVertexArray(0); +} + +void Binder::store_indices(uint32_t* indices, size_t indices_size) { + GLuint ebo; + glGenBuffers(1, &ebo); + + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices_size * sizeof(float), indices, GL_STATIC_DRAW); +} + +void Binder::store_in_attribute_list(uint32_t attribute, size_t size, float* data, size_t data_size) { + GLuint vbo; + glGenBuffers(1, &vbo); + + glBindBuffer(GL_ARRAY_BUFFER, vbo); + glBufferData(GL_ARRAY_BUFFER, data_size * sizeof(float), data, GL_STATIC_DRAW); + glVertexAttribPointer(attribute, size, GL_FLOAT, GL_FALSE, size * sizeof(float), nullptr); + glBindBuffer(GL_ARRAY_BUFFER, 0); +} + +void BindableMesh::bind() const { + glBindVertexArray(m_vao); + glEnableVertexAttribArray(0); + glEnableVertexAttribArray(1); +} + +void BindableMesh::unbind() { + glBindVertexArray(0); + glDisableVertexAttribArray(0); + glDisableVertexAttribArray(1); +} + +size_t BindableMesh::size() const { + return m_vertex_count; +} + +} \ No newline at end of file diff --git a/src/GFX/Binder.hpp b/src/GFX/Binder.hpp new file mode 100644 index 0000000..99f9791 --- /dev/null +++ b/src/GFX/Binder.hpp @@ -0,0 +1,38 @@ +#pragma once + +#include +#include "Mesh.hpp" + +namespace MC::GFX { + +class BindableMesh { +public: + void bind() const; + void unbind(); + + size_t size() const; + +private: + BindableMesh(uint32_t vao, size_t vertex_count) : m_vao(vao), m_vertex_count(vertex_count) {}; + + uint32_t m_vao; + size_t m_vertex_count; + + friend class Binder; +}; + +class Binder { +public: + Binder() = default;; + + static BindableMesh load(Mesh& mesh); + +private: + static uint32_t create_vao(); + static void unbind_vao(); + + static void store_in_attribute_list(uint32_t attribute, size_t size, float* data, size_t data_size); + static void store_indices(uint32_t* indices, size_t indices_size); +}; + +} \ No newline at end of file diff --git a/src/GFX/Camera.cpp b/src/GFX/Camera.cpp new file mode 100644 index 0000000..6b25347 --- /dev/null +++ b/src/GFX/Camera.cpp @@ -0,0 +1,42 @@ +#include "Camera.hpp" + +namespace MC::GFX { + +Vector<3> Camera::position() { + return m_position; +} + +void Camera::set_position(Vector<3> position) { + m_position = position; +} + +void Camera::move(Vector<3> vector) { + m_position = m_position + vector; +} + +void Camera::move_relative(Vector<3> by) { + auto rotation = Matrix<4, 4>::rotation(m_angles); + + auto result = rotation.transpose() * Vector<4>{by.x(), by.y(), by.z(), 1.0f}; + move(result.elements); +} + +Rotation Camera::angles() { + return m_angles; +} + +void Camera::set_angles(Rotation angles) { + m_angles = angles; +} + +void Camera::rotate(Rotation by) { + m_angles = m_angles + by; + + if (m_angles.pitch() > 89.0f) { + m_angles.pitch() = 89.0f; + } else if (m_angles.pitch() < -89.0f) { + m_angles.pitch() = -89.0f; + } +} + +} diff --git a/src/GFX/Camera.hpp b/src/GFX/Camera.hpp new file mode 100644 index 0000000..f03f009 --- /dev/null +++ b/src/GFX/Camera.hpp @@ -0,0 +1,26 @@ +#pragma once + +#include "../Math/Math.hpp" +#include "../Math/Rotation.hpp" + +namespace MC::GFX { + +class Camera { +public: + Camera() = default; + + Vector<3> position(); + void set_position(Vector<3> position); + void move(Vector<3> by); + void move_relative(Vector<3> by); + + Rotation angles(); + void set_angles(Rotation angles); + void rotate(Rotation by); + +private: + Vector<3> m_position = {}; + Rotation m_angles = {}; +}; + +} \ No newline at end of file diff --git a/src/GFX/Image/PPMParser.cpp b/src/GFX/Image/PPMParser.cpp new file mode 100644 index 0000000..7fc2359 --- /dev/null +++ b/src/GFX/Image/PPMParser.cpp @@ -0,0 +1,154 @@ +#include +#include +#include "PPMParser.hpp" + +namespace MC::GFX::Image { + +RawImage PPMParser::parse() { + auto header = parse_header(); + + if (header.max_color != 255) { + throw std::logic_error("PPM max color values other than 255 are not implemented."); + } + + if (header.type != P3) { + throw std::logic_error("Raw PPM not implemented."); + } + + auto pixel_count = header.width * header.height; + + RawImage image(pixel_count, header.width, header.height, 3); + for (uint64_t pixel_index = 0; pixel_index < pixel_count; pixel_index++) { + RawImage::Pixel pixel = parse_pixel(header.max_color); + image.add(pixel); + } + + return image; +} + +PPMParser::PPMHeader PPMParser::parse_header() { + PPMHeader header{}; + + skip_whitespace(); + + auto type_part = chomp_part(); + skip_whitespace(); + + if (type_part == "P3") { + header.type = P3; + } else if (type_part == "P6") { + header.type = P6; + } else { + throw std::runtime_error("Unknown PPM type."); + } + + auto width_part = chomp_number(); + skip_whitespace(); + header.width = width_part; + + auto height_part = chomp_number(); + skip_whitespace(); + header.height = height_part; + + auto max_color_part = chomp_number(); + skip_whitespace(); + header.max_color = max_color_part; + + return header; +} + +RawImage::Pixel PPMParser::parse_pixel(uint8_t max_color) { + auto r_sample = parse_sample(); + auto g_sample = parse_sample(); + auto b_sample = parse_sample(); + + if (r_sample > max_color || g_sample > max_color || b_sample > max_color) { + throw std::runtime_error("Sample can not be greater than Maxval."); + } + + auto map_to_range = [=](uint64_t s) -> uint8_t { return (s * 255) / max_color; }; + + RawImage::Pixel pixel{}; + pixel.r = map_to_range(r_sample); + pixel.g = map_to_range(g_sample); + pixel.b = map_to_range(b_sample); + + return pixel; +} + +uint64_t PPMParser::parse_sample() { + skip_whitespace(); + auto sample = chomp_number(); + skip_whitespace(); + return sample; +} + +uint64_t PPMParser::chomp_number() { + auto raw = chomp_part(); + + uint64_t number = 0; + for (uint8_t digit_ascii : raw) { + if (digit_ascii < '0' || digit_ascii > '9') { + throw std::runtime_error("Number contains non ASCII digits."); + } + + uint8_t digit = digit_ascii - '0'; + + number *= 10; + number += digit; + } + + return number; +} + +std::string_view PPMParser::chomp_part() { + uint64_t length = 0; + + while (!is_eof()) { + auto c = m_source[m_cursor + length]; + if (std::isspace(c)) { + break; + } + + length++; + } + + auto part = m_source.substr(m_cursor, length); + m_cursor += length; + + return part; +} + +void PPMParser::skip_whitespace() { + uint8_t c; + while (!is_eof()) { + c = m_source[m_cursor]; + if (c == '#') { + skip_comment(); + continue; + } + + if (!std::isspace(c)) { + break; + } + + m_cursor++; + } +} + +void PPMParser::skip_comment() { + uint8_t c = m_source[m_cursor]; + if (c != '#') { + return; + } + + while (c != '\n' && !is_eof()) { + c = m_source[++m_cursor]; + } +} + +bool PPMParser::is_eof() { + return m_cursor >= m_source.size(); +} + +} \ No newline at end of file diff --git a/src/GFX/Image/PPMParser.hpp b/src/GFX/Image/PPMParser.hpp new file mode 100644 index 0000000..3909cee --- /dev/null +++ b/src/GFX/Image/PPMParser.hpp @@ -0,0 +1,44 @@ +#pragma once + +#include +#include +#include "RawImage.hpp" + +namespace MC::GFX::Image { + +class PPMParser { +public: + explicit PPMParser(std::string_view source) : m_source(source) {}; + + RawImage parse(); +private: + enum PPMType { + None, + P3, + P6 + }; + + struct PPMHeader { + PPMType type; + uint32_t width; + uint32_t height; + uint8_t max_color; + }; + + PPMHeader parse_header(); + RawImage::Pixel parse_pixel(uint8_t max_color); + uint64_t parse_sample(); + + uint64_t chomp_number(); + std::string_view chomp_part(); + + void skip_whitespace(); + void skip_comment(); + + bool is_eof(); + + std::string_view m_source; + uint64_t m_cursor = 0; +}; + +} diff --git a/src/GFX/Image/RawImage.cpp b/src/GFX/Image/RawImage.cpp new file mode 100644 index 0000000..aca8fbc --- /dev/null +++ b/src/GFX/Image/RawImage.cpp @@ -0,0 +1,29 @@ +#include "RawImage.hpp" + +namespace MC::GFX::Image { + +void RawImage::add(RawImage::Pixel pixel) { + m_pixels.push_back(pixel); +} + +size_t RawImage::size() const { + return m_pixels.size(); +} + +uint8_t* RawImage::raw() const { + return (uint8_t*)m_pixels.data(); +} + +uint32_t RawImage::width() const { + return m_width; +} + +uint32_t RawImage::height() const { + return m_height; +} + +uint8_t RawImage::channels() const { + return m_channels; +} + +} \ No newline at end of file diff --git a/src/GFX/Image/RawImage.hpp b/src/GFX/Image/RawImage.hpp new file mode 100644 index 0000000..916671b --- /dev/null +++ b/src/GFX/Image/RawImage.hpp @@ -0,0 +1,37 @@ +#pragma once + +#include +#include +#include + +namespace MC::GFX::Image { + +class RawImage { +public: + RawImage() : m_pixels(), m_width(0), m_height(0), m_channels(0) {}; + + explicit RawImage(size_t pixel_count, uint32_t width, uint32_t height, uint8_t channels) + : m_pixels(), m_width(width), m_height(height), m_channels(channels) { + m_pixels.reserve(pixel_count); + } + + struct Pixel { + uint8_t r, g, b; + }; + + void add(Pixel pixel); + + size_t size() const; + uint8_t* raw() const; + + uint32_t width() const; + uint32_t height() const; + uint8_t channels() const; +private: + std::vector m_pixels; + + uint32_t m_width, m_height; + uint8_t m_channels; +}; + +} diff --git a/src/GFX/Mesh.cpp b/src/GFX/Mesh.cpp new file mode 100644 index 0000000..12f8aaa --- /dev/null +++ b/src/GFX/Mesh.cpp @@ -0,0 +1,29 @@ +#include "Mesh.hpp" + +namespace MC::GFX { + +float* Mesh::raw() { + return (float*) m_positions.data(); +} + +size_t Mesh::size() { + return m_positions.size(); +} + +uint32_t* Mesh::raw_indices() { + return m_indices.data(); +} + +size_t Mesh::indices_size() { + return m_indices.size(); +} + +float* Mesh::raw_tex_coords() { + return (float*) m_tex_coords.data(); +} + +size_t Mesh::tex_coords_size() { + return m_tex_coords.size(); +} + +} \ No newline at end of file diff --git a/src/GFX/Mesh.hpp b/src/GFX/Mesh.hpp new file mode 100644 index 0000000..f027c8c --- /dev/null +++ b/src/GFX/Mesh.hpp @@ -0,0 +1,34 @@ +#pragma once + +#include +#include +#include +#include "../Math/Math.hpp" + +namespace MC::GFX { + +class Mesh { +public: + Mesh(std::vector> positions, std::vector> tex_coords, std::vector indices) + : m_positions(std::move(positions)), m_tex_coords(std::move(tex_coords)), m_indices(std::move(indices)) {}; + + Mesh(std::vector> positions, std::vector> tex_coords) + : m_positions(std::move(positions)), m_tex_coords(std::move(tex_coords)), m_indices() {}; + + float* raw(); + size_t size(); + + uint32_t* raw_indices(); + size_t indices_size(); + + float* raw_tex_coords(); + size_t tex_coords_size(); + +private: + std::vector> m_positions; + std::vector> m_tex_coords; + std::vector m_indices; + +}; + +} \ No newline at end of file diff --git a/src/GFX/Mouse.cpp b/src/GFX/Mouse.cpp new file mode 100644 index 0000000..5cd2698 --- /dev/null +++ b/src/GFX/Mouse.cpp @@ -0,0 +1,24 @@ +#include "Mouse.hpp" + +namespace MC::GFX { + +Vector<2> Mouse::update(GLFWwindow* window) { + double x, y; + glfwGetCursorPos(window, &x, &y); + + if (m_first_event) { + m_last_x = x; + m_last_y = y; + + m_first_event = false; + } + + Vector<2> movement{static_cast(x) - m_last_x, static_cast(y) - m_last_y}; + + m_last_x = x; + m_last_y = y; + + return movement; +} + +} \ No newline at end of file diff --git a/src/GFX/Mouse.hpp b/src/GFX/Mouse.hpp new file mode 100644 index 0000000..3ed57a2 --- /dev/null +++ b/src/GFX/Mouse.hpp @@ -0,0 +1,21 @@ +#pragma once + +#include +#include +#include "../Math/Vector.hpp" + +namespace MC::GFX { + +class Mouse { +public: + Mouse() = default; + + Vector<2> update(GLFWwindow* window); +private: + bool m_first_event = true; + + float m_last_x = 0.0f; + float m_last_y = 0.0f; +}; + +} \ No newline at end of file diff --git a/src/GFX/Shading/Program.cpp b/src/GFX/Shading/Program.cpp new file mode 100644 index 0000000..39393f8 --- /dev/null +++ b/src/GFX/Shading/Program.cpp @@ -0,0 +1,42 @@ +#include +#include +#include "Program.hpp" + +namespace MC::GFX::Shading { + +Program::Program(Shader fragment, Shader vertex) { + m_program = glCreateProgram(); + + glAttachShader(m_program, fragment.get()); + glAttachShader(m_program, vertex.get()); + + glLinkProgram(m_program); + + glDeleteShader(fragment.get()); + glDeleteShader(vertex.get()); + + GLint success; + glGetProgramiv(m_program, GL_LINK_STATUS, &success); + if(!success) { + char message[512] = {}; + glGetProgramInfoLog(m_program, 512, nullptr, message); + + throw std::runtime_error(message); + } +} + +void Program::bind() const { + glUseProgram(m_program); +} + +Uniform Program::uniform(const std::string& name) const { + auto index = glGetUniformLocation(m_program, name.c_str()); + + return {name, static_cast(index)}; +} + +uint32_t Program::get() const { + return m_program; +} + +} diff --git a/src/GFX/Shading/Program.hpp b/src/GFX/Shading/Program.hpp new file mode 100644 index 0000000..15c9899 --- /dev/null +++ b/src/GFX/Shading/Program.hpp @@ -0,0 +1,25 @@ +#pragma once + +#include +#include +#include "Shader.hpp" +#include "../../Math/Math.hpp" +#include "Uniform.hpp" + +namespace MC::GFX::Shading { + +class Program { +public: + Program(Shader fragment, Shader vertex); + + uint32_t get() const; + + Uniform uniform(const std::string& name) const; + + void bind() const; + +private: + uint32_t m_program; +}; + +} \ No newline at end of file diff --git a/src/GFX/Shading/Shader.cpp b/src/GFX/Shading/Shader.cpp new file mode 100644 index 0000000..ff954a5 --- /dev/null +++ b/src/GFX/Shading/Shader.cpp @@ -0,0 +1,24 @@ +#include +#include +#include "Shader.hpp" + +namespace MC::GFX::Shading { + +Shader::Shader(uint32_t type, const char* source) { + m_shader = glCreateShader(type); + + glShaderSource(m_shader, 1, &source, nullptr); + glCompileShader(m_shader); + + GLint success; + glGetShaderiv(m_shader, GL_COMPILE_STATUS, &success); + if(!success) { + char message[512] = {}; + glGetShaderInfoLog(m_shader, 512, nullptr, message); + + throw std::runtime_error(message); + } +} + + +} \ No newline at end of file diff --git a/src/GFX/Shading/Shader.hpp b/src/GFX/Shading/Shader.hpp new file mode 100644 index 0000000..4a3d9cf --- /dev/null +++ b/src/GFX/Shading/Shader.hpp @@ -0,0 +1,29 @@ +#pragma once + +#include +#include "../../Assets.hpp" + +namespace MC::GFX::Shading { + +class Shader { + +public: + uint32_t get() const { + return m_shader; + } + + static Shader create_vertex() { + return {GL_VERTEX_SHADER, Assets::Shaders::vertex}; + } + + static Shader create_fragment() { + return {GL_FRAGMENT_SHADER, Assets::Shaders::fragment}; + } + +private: + Shader(uint32_t type, const char* source); + + uint32_t m_shader; +}; + +} \ No newline at end of file diff --git a/src/GFX/Shading/Uniform.cpp b/src/GFX/Shading/Uniform.cpp new file mode 100644 index 0000000..9448574 --- /dev/null +++ b/src/GFX/Shading/Uniform.cpp @@ -0,0 +1,14 @@ +#include +#include "Uniform.hpp" + +namespace MC::GFX::Shading { + +void Uniform::set(Matrix<4, 4> value) const { + glUniformMatrix4fv(m_index, 1, GL_TRUE, value.elements); +} + +void Uniform::set(Vector<3> value) const { + glUniform3f(m_index, value.x(), value.y(), value.z()); +} + +} diff --git a/src/GFX/Shading/Uniform.hpp b/src/GFX/Shading/Uniform.hpp new file mode 100644 index 0000000..8035dfe --- /dev/null +++ b/src/GFX/Shading/Uniform.hpp @@ -0,0 +1,23 @@ +#pragma once + +#include +#include +#include +#include "../../Math/Math.hpp" + +namespace MC::GFX::Shading { + +class Uniform { +public: + Uniform(std::string name, uint32_t index) + : m_name(std::move(name)), m_index(index) {}; + + void set(Matrix<4, 4> value) const; + void set(Vector<3> value) const; + +private: + std::string m_name; + uint32_t m_index; +}; + +} diff --git a/src/GFX/Texture.cpp b/src/GFX/Texture.cpp new file mode 100644 index 0000000..1942128 --- /dev/null +++ b/src/GFX/Texture.cpp @@ -0,0 +1,29 @@ +#include +#include "Texture.hpp" + +namespace MC::GFX { + +Texture::Texture(const Image::RawImage& image) { + glGenTextures(1, &m_texture); + glBindTexture(GL_TEXTURE_2D, m_texture); + + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); + + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, image.width(), image.height(), 0, GL_RGB, GL_UNSIGNED_BYTE, image.raw()); + glGenerateMipmap(GL_TEXTURE_2D); + + glBindTexture(GL_TEXTURE_2D, 0); +} + +void Texture::bind() { + glBindTexture(GL_TEXTURE_2D, m_texture); +} + +void Texture::unbind() { + glBindTexture(GL_TEXTURE_2D, 0); +} +} \ No newline at end of file diff --git a/src/GFX/Texture.hpp b/src/GFX/Texture.hpp new file mode 100644 index 0000000..ff86634 --- /dev/null +++ b/src/GFX/Texture.hpp @@ -0,0 +1,17 @@ +#pragma once + +#include "Image/RawImage.hpp" + +namespace MC::GFX { + +class Texture { +public: + explicit Texture(const Image::RawImage& image); + + void bind(); + void unbind(); +private: + uint32_t m_texture; +}; + +} \ No newline at end of file diff --git a/src/GFX/Window.cpp b/src/GFX/Window.cpp new file mode 100644 index 0000000..0a1828c --- /dev/null +++ b/src/GFX/Window.cpp @@ -0,0 +1,55 @@ +#include +#include "Window.hpp" + +namespace MC::GFX { + +Window::Window(const char *title, uint32_t width, uint32_t height) { + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); + glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); + glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); + glfwWindowHint(GLFW_DOUBLEBUFFER, GL_TRUE); + + m_window = glfwCreateWindow(width, height, title, nullptr, nullptr); + if (m_window == nullptr) { + throw std::runtime_error("Failed to create window."); + } + + glfwMakeContextCurrent(m_window); + glfwSetInputMode(m_window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); +} + +Window::~Window() { + glfwDestroyWindow(m_window); +} + +bool Window::should_close() { + return glfwWindowShouldClose(m_window); +} + +GLFWwindow* Window::get() { + return m_window; +} + +void Window::close() { + glfwSetWindowShouldClose(m_window, true); +} + +Vector<2> Window::mouse_delta() { + return m_mouse.update(m_window); +} + +bool Window::key(int key, int type) { + return (glfwGetKey(m_window, key) == type); +} + +void Window::start_frame() { + glfwSwapBuffers(m_window); + glfwPollEvents(); +} + +void Window::on_size_change(void (callback)(GLFWwindow*, int, int)) { + glfwSetFramebufferSizeCallback(m_window, static_cast(callback)); +} + +} \ No newline at end of file diff --git a/src/GFX/Window.hpp b/src/GFX/Window.hpp new file mode 100644 index 0000000..63e8446 --- /dev/null +++ b/src/GFX/Window.hpp @@ -0,0 +1,30 @@ +#pragma once + +#include +#include +#include "../Math/Vector.hpp" +#include "Mouse.hpp" + +namespace MC::GFX { + +class Window { +public: + Window(const char* title, uint32_t width, uint32_t height); + ~Window(); + + GLFWwindow* get(); + + void on_size_change(void (* callback)(GLFWwindow*, int, int)); + + void close(); + void start_frame(); + Vector<2> mouse_delta(); + + bool key(int key, int type); + bool should_close(); +private: + GLFWwindow* m_window; + Mouse m_mouse; +}; + +} \ No newline at end of file diff --git a/src/Image/PPMParser.cpp b/src/Image/PPMParser.cpp deleted file mode 100644 index d7d8c6a..0000000 --- a/src/Image/PPMParser.cpp +++ /dev/null @@ -1,155 +0,0 @@ -#include -#include -#include -#include "PPMParser.hpp" - -namespace MC::Image { - -RawImage PPMParser::parse() { - auto header = parse_header(); - - if (header.max_color != 255) { - throw std::logic_error("PPM max color values other than 255 are not implemented."); - } - - if (header.type != P3) { - throw std::logic_error("Raw PPM not implemented."); - } - - auto pixel_count = header.width * header.height; - - RawImage image(pixel_count, header.width, header.height, 3); - for (uint64_t pixel_index = 0; pixel_index < pixel_count; pixel_index++) { - RawImage::Pixel pixel = parse_pixel(header.max_color); - image.add(pixel); - } - - return image; -} - -PPMParser::PPMHeader PPMParser::parse_header() { - PPMHeader header{}; - - skip_whitespace(); - - auto type_part = chomp_part(); - skip_whitespace(); - - if (type_part == "P3") { - header.type = P3; - } else if (type_part == "P6") { - header.type = P6; - } else { - throw std::runtime_error("Unknown PPM type."); - } - - auto width_part = chomp_number(); - skip_whitespace(); - header.width = width_part; - - auto height_part = chomp_number(); - skip_whitespace(); - header.height = height_part; - - auto max_color_part = chomp_number(); - skip_whitespace(); - header.max_color = max_color_part; - - return header; -} - -RawImage::Pixel PPMParser::parse_pixel(uint8_t max_color) { - auto r_sample = parse_sample(); - auto g_sample = parse_sample(); - auto b_sample = parse_sample(); - - if (r_sample > max_color || g_sample > max_color || b_sample > max_color) { - throw std::runtime_error("Sample can not be greater than Maxval."); - } - - auto map_to_range = [=](uint64_t s) -> uint8_t { return (s * 255) / max_color; }; - - RawImage::Pixel pixel{}; - pixel.r = map_to_range(r_sample); - pixel.g = map_to_range(g_sample); - pixel.b = map_to_range(b_sample); - - return pixel; -} - -uint64_t PPMParser::parse_sample() { - skip_whitespace(); - auto sample = chomp_number(); - skip_whitespace(); - return sample; -} - -uint64_t PPMParser::chomp_number() { - auto raw = chomp_part(); - - uint64_t number = 0; - for (uint8_t digit_ascii : raw) { - if (digit_ascii < '0' || digit_ascii > '9') { - throw std::runtime_error("Number contains non ASCII digits."); - } - - uint8_t digit = digit_ascii - '0'; - - number *= 10; - number += digit; - } - - return number; -} - -std::string_view PPMParser::chomp_part() { - uint64_t length = 0; - - while (!is_eof()) { - auto c = m_source[m_cursor + length]; - if (std::isspace(c)) { - break; - } - - length++; - } - - auto part = m_source.substr(m_cursor, length); - m_cursor += length; - - return part; -} - -void PPMParser::skip_whitespace() { - uint8_t c; - while (!is_eof()) { - c = m_source[m_cursor]; - if (c == '#') { - skip_comment(); - continue; - } - - if (!std::isspace(c)) { - break; - } - - m_cursor++; - } -} - -void PPMParser::skip_comment() { - uint8_t c = m_source[m_cursor]; - if (c != '#') { - return; - } - - while (c != '\n' && !is_eof()) { - c = m_source[++m_cursor]; - } -} - -bool PPMParser::is_eof() { - return m_cursor >= m_source.size(); -} - -} \ No newline at end of file diff --git a/src/Image/PPMParser.hpp b/src/Image/PPMParser.hpp deleted file mode 100644 index 7d81cb8..0000000 --- a/src/Image/PPMParser.hpp +++ /dev/null @@ -1,44 +0,0 @@ -#pragma once - -#include -#include -#include "RawImage.hpp" - -namespace MC::Image { - -class PPMParser { -public: - explicit PPMParser(std::string_view source) : m_source(source) {}; - - RawImage parse(); -private: - enum PPMType { - None, - P3, - P6 - }; - - struct PPMHeader { - PPMType type; - uint32_t width; - uint32_t height; - uint8_t max_color; - }; - - PPMHeader parse_header(); - RawImage::Pixel parse_pixel(uint8_t max_color); - uint64_t parse_sample(); - - uint64_t chomp_number(); - std::string_view chomp_part(); - - void skip_whitespace(); - void skip_comment(); - - bool is_eof(); - - std::string_view m_source; - uint64_t m_cursor = 0; -}; - -} diff --git a/src/Image/RawImage.cpp b/src/Image/RawImage.cpp deleted file mode 100644 index 00bfb0c..0000000 --- a/src/Image/RawImage.cpp +++ /dev/null @@ -1,29 +0,0 @@ -#include "RawImage.hpp" - -namespace MC::Image { - -void RawImage::add(RawImage::Pixel pixel) { - m_pixels.push_back(pixel); -} - -size_t RawImage::size() const { - return m_pixels.size(); -} - -uint8_t* RawImage::raw() const { - return (uint8_t*)m_pixels.data(); -} - -uint32_t RawImage::width() const { - return m_width; -} - -uint32_t RawImage::height() const { - return m_height; -} - -uint8_t RawImage::channels() const { - return m_channels; -} - -} \ No newline at end of file diff --git a/src/Image/RawImage.hpp b/src/Image/RawImage.hpp deleted file mode 100644 index 6c4b122..0000000 --- a/src/Image/RawImage.hpp +++ /dev/null @@ -1,37 +0,0 @@ -#pragma once - -#include -#include -#include - -namespace MC::Image { - -class RawImage { -public: - RawImage() : m_pixels(), m_width(0), m_height(0), m_channels(0) {}; - - explicit RawImage(size_t pixel_count, uint32_t width, uint32_t height, uint8_t channels) - : m_pixels(), m_width(width), m_height(height), m_channels(channels) { - m_pixels.reserve(pixel_count); - } - - struct Pixel { - uint8_t r, g, b; - }; - - void add(Pixel pixel); - - size_t size() const; - uint8_t* raw() const; - - uint32_t width() const; - uint32_t height() const; - uint8_t channels() const; -private: - std::vector m_pixels; - - uint32_t m_width, m_height; - uint8_t m_channels; -}; - -} diff --git a/src/Mesh.cpp b/src/Mesh.cpp deleted file mode 100644 index 56ce9dc..0000000 --- a/src/Mesh.cpp +++ /dev/null @@ -1,25 +0,0 @@ -#include "Mesh.hpp" - -float* MC::Mesh::raw() { - return (float*)m_positions.data(); -} - -size_t MC::Mesh::size() { - return m_positions.size(); -} - -uint32_t* MC::Mesh::raw_indices() { - return m_indices.data(); -} - -size_t MC::Mesh::indices_size() { - return m_indices.size(); -} - -float* MC::Mesh::raw_tex_coords() { - return (float*)m_tex_coords.data(); -} - -size_t MC::Mesh::tex_coords_size() { - return m_tex_coords.size(); -} diff --git a/src/Mesh.hpp b/src/Mesh.hpp deleted file mode 100644 index b601228..0000000 --- a/src/Mesh.hpp +++ /dev/null @@ -1,34 +0,0 @@ -#pragma once - -#include -#include -#include -#include "Math/Math.hpp" - -namespace MC { - -class Mesh { -public: - Mesh(std::vector> positions, std::vector> tex_coords, std::vector indices) - : m_positions(std::move(positions)), m_tex_coords(std::move(tex_coords)), m_indices(std::move(indices)) {}; - - Mesh(std::vector> positions, std::vector> tex_coords) - : m_positions(std::move(positions)), m_tex_coords(std::move(tex_coords)), m_indices() {}; - - float* raw(); - size_t size(); - - uint32_t* raw_indices(); - size_t indices_size(); - - float* raw_tex_coords(); - size_t tex_coords_size(); - -private: - std::vector> m_positions; - std::vector> m_tex_coords; - std::vector m_indices; - -}; - -} \ No newline at end of file diff --git a/src/Mouse.cpp b/src/Mouse.cpp deleted file mode 100644 index 4715362..0000000 --- a/src/Mouse.cpp +++ /dev/null @@ -1,25 +0,0 @@ -#include "Mouse.hpp" -#include "Window.hpp" - -namespace MC { - -Vector<2> Mouse::update(Window& window) { - double x, y; - glfwGetCursorPos(window.get(), &x, &y); - - if (m_first_event) { - m_last_x = x; - m_last_y = y; - - m_first_event = false; - } - - Vector<2> movement{static_cast(x) - m_last_x, static_cast(y) - m_last_y}; - - m_last_x = x; - m_last_y = y; - - return movement; -} - -} \ No newline at end of file diff --git a/src/Mouse.hpp b/src/Mouse.hpp deleted file mode 100644 index c546a7e..0000000 --- a/src/Mouse.hpp +++ /dev/null @@ -1,19 +0,0 @@ -#pragma once - -#include -#include "Math/Vector.hpp" -#include "Window.hpp" - -namespace MC { - -class Mouse { -public: - Mouse() = default; - - Vector<2> update(Window &window); -private: - bool m_first_event = true; - float m_last_x, m_last_y = 0.0f; -}; - -} \ No newline at end of file diff --git a/src/Shader/Shader.cpp b/src/Shader/Shader.cpp deleted file mode 100644 index 0cd6ab3..0000000 --- a/src/Shader/Shader.cpp +++ /dev/null @@ -1,24 +0,0 @@ -#include -#include -#include "Shader.hpp" - -namespace MC { - -Shader::Shader(uint32_t type, const char* source) { - m_shader = glCreateShader(type); - - glShaderSource(m_shader, 1, &source, nullptr); - glCompileShader(m_shader); - - GLint success; - glGetShaderiv(m_shader, GL_COMPILE_STATUS, &success); - if(!success) { - char message[512] = {}; - glGetShaderInfoLog(m_shader, 512, nullptr, message); - - throw std::runtime_error(message); - } -} - - -} \ No newline at end of file diff --git a/src/Shader/Shader.hpp b/src/Shader/Shader.hpp deleted file mode 100644 index 76a1197..0000000 --- a/src/Shader/Shader.hpp +++ /dev/null @@ -1,29 +0,0 @@ -#pragma once - -#include -#include "../Assets.hpp" - -namespace MC { - -class Shader { - -public: - uint32_t get() const { - return m_shader; - } - - static Shader create_vertex() { - return {GL_VERTEX_SHADER, Assets::Shaders::vertex}; - } - - static Shader create_fragment() { - return {GL_FRAGMENT_SHADER, Assets::Shaders::fragment}; - } - -private: - Shader(uint32_t type, const char* source); - - uint32_t m_shader; -}; - -} \ No newline at end of file diff --git a/src/Shader/ShaderProgram.cpp b/src/Shader/ShaderProgram.cpp deleted file mode 100644 index 4d0b684..0000000 --- a/src/Shader/ShaderProgram.cpp +++ /dev/null @@ -1,42 +0,0 @@ -#include -#include -#include "ShaderProgram.hpp" - -namespace MC { - -ShaderProgram::ShaderProgram(Shader fragment, Shader vertex) { - m_program = glCreateProgram(); - - glAttachShader(m_program, fragment.get()); - glAttachShader(m_program, vertex.get()); - - glLinkProgram(m_program); - - glDeleteShader(fragment.get()); - glDeleteShader(vertex.get()); - - GLint success; - glGetProgramiv(m_program, GL_LINK_STATUS, &success); - if(!success) { - char message[512] = {}; - glGetProgramInfoLog(m_program, 512, nullptr, message); - - throw std::runtime_error(message); - } -} - -void ShaderProgram::bind() const { - glUseProgram(m_program); -} - -Uniform ShaderProgram::uniform(const std::string& name) const { - auto index = glGetUniformLocation(m_program, name.c_str()); - - return {name, static_cast(index)}; -} - -uint32_t ShaderProgram::get() const { - return m_program; -} - -} diff --git a/src/Shader/ShaderProgram.hpp b/src/Shader/ShaderProgram.hpp deleted file mode 100644 index 857dc2f..0000000 --- a/src/Shader/ShaderProgram.hpp +++ /dev/null @@ -1,25 +0,0 @@ -#pragma once - -#include -#include -#include "Shader.hpp" -#include "../Math/Math.hpp" -#include "Uniform.hpp" - -namespace MC { - -class ShaderProgram { -public: - ShaderProgram(Shader fragment, Shader vertex); - - uint32_t get() const; - - Uniform uniform(const std::string& name) const; - - void bind() const; - -private: - uint32_t m_program; -}; - -} \ No newline at end of file diff --git a/src/Shader/Uniform.cpp b/src/Shader/Uniform.cpp deleted file mode 100644 index 71b7633..0000000 --- a/src/Shader/Uniform.cpp +++ /dev/null @@ -1,14 +0,0 @@ -#include -#include "Uniform.hpp" - -namespace MC { - -void Uniform::set(Matrix<4, 4> value) const { - glUniformMatrix4fv(m_index, 1, GL_TRUE, value.elements); -} - -void Uniform::set(Vector<3> value) const { - glUniform3f(m_index, value.x(), value.y(), value.z()); -} - -} diff --git a/src/Shader/Uniform.hpp b/src/Shader/Uniform.hpp deleted file mode 100644 index 58cdc28..0000000 --- a/src/Shader/Uniform.hpp +++ /dev/null @@ -1,23 +0,0 @@ -#pragma once - -#include -#include -#include -#include "../Math/Math.hpp" - -namespace MC { - -class Uniform { -public: - Uniform(std::string name, uint32_t index) - : m_name(std::move(name)), m_index(index) {}; - - void set(Matrix<4, 4> value) const; - void set(Vector<3> value) const; - -private: - std::string m_name; - uint32_t m_index; -}; - -} diff --git a/src/Texture.cpp b/src/Texture.cpp deleted file mode 100644 index d2e1465..0000000 --- a/src/Texture.cpp +++ /dev/null @@ -1,29 +0,0 @@ -#include -#include "Texture.hpp" - -namespace MC { - -Texture::Texture(const Image::RawImage& image) { - glGenTextures(1, &m_texture); - glBindTexture(GL_TEXTURE_2D, m_texture); - - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); - - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, image.width(), image.height(), 0, GL_RGB, GL_UNSIGNED_BYTE, image.raw()); - glGenerateMipmap(GL_TEXTURE_2D); - - glBindTexture(GL_TEXTURE_2D, 0); -} - -void Texture::bind() { - glBindTexture(GL_TEXTURE_2D, m_texture); -} - -void Texture::unbind() { - glBindTexture(GL_TEXTURE_2D, 0); -} -} \ No newline at end of file diff --git a/src/Texture.hpp b/src/Texture.hpp deleted file mode 100644 index 91b5e54..0000000 --- a/src/Texture.hpp +++ /dev/null @@ -1,17 +0,0 @@ -#pragma once - -#include "Image/RawImage.hpp" - -namespace MC { - -class Texture { -public: - Texture(const Image::RawImage& image); - - void bind(); - void unbind(); -private: - uint32_t m_texture; -}; - -} \ No newline at end of file diff --git a/src/Window.cpp b/src/Window.cpp deleted file mode 100644 index 2f1c2c0..0000000 --- a/src/Window.cpp +++ /dev/null @@ -1,51 +0,0 @@ -#include -#include "Window.hpp" - -namespace MC { - -Window::Window(const char *title, uint32_t width, uint32_t height) { - glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); - glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); - glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); - glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); - glfwWindowHint(GLFW_DOUBLEBUFFER, GL_TRUE); - - m_window = glfwCreateWindow(width, height, title, nullptr, nullptr); - if (m_window == nullptr) { - throw std::runtime_error("Failed to create window."); - } - - glfwMakeContextCurrent(m_window); - glfwSetInputMode(m_window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); -} - -Window::~Window() { - glfwDestroyWindow(m_window); -} - -bool Window::should_close() { - return glfwWindowShouldClose(m_window); -} - -GLFWwindow* Window::get() { - return m_window; -} - -void Window::close() { - glfwSetWindowShouldClose(m_window, true); -} - -bool Window::key(int key, int type) { - return (glfwGetKey(m_window, key) == type); -} - -void Window::start_frame() { - glfwSwapBuffers(m_window); - glfwPollEvents(); -} - -void Window::on_size_change(void (callback)(GLFWwindow*, int, int)) { - glfwSetFramebufferSizeCallback(m_window, static_cast(callback)); -} - -} \ No newline at end of file diff --git a/src/Window.hpp b/src/Window.hpp deleted file mode 100644 index 5a2b640..0000000 --- a/src/Window.hpp +++ /dev/null @@ -1,27 +0,0 @@ -#pragma once - -#include -#include - -namespace MC { - -class Window { -public: - Window(const char* title, uint32_t width, uint32_t height); - ~Window(); - - GLFWwindow* get(); - - void on_size_change(void (* callback)(GLFWwindow*, int, int)); - - void close(); - void start_frame(); - - bool key(int key, int type); - bool should_close(); -private: - GLFWwindow* m_window; - -}; - -} \ No newline at end of file diff --git a/src/World/BlockSide.hpp b/src/World/BlockSide.hpp index 45efb11..57d3c68 100644 --- a/src/World/BlockSide.hpp +++ b/src/World/BlockSide.hpp @@ -5,7 +5,7 @@ #include #include "../Math/Vector.hpp" -namespace MC { +namespace MC::World { class BlockSide { public: diff --git a/src/World/BlockType.hpp b/src/World/BlockType.hpp index e35daf8..8d83be0 100644 --- a/src/World/BlockType.hpp +++ b/src/World/BlockType.hpp @@ -1,6 +1,6 @@ #pragma -namespace MC { +namespace MC::World { enum class BlockType : uint8_t { Air, diff --git a/src/World/Chunk.cpp b/src/World/Chunk.cpp index 1d541f7..0d86ddb 100644 --- a/src/World/Chunk.cpp +++ b/src/World/Chunk.cpp @@ -1,13 +1,13 @@ #include "Chunk.hpp" #include "BlockSide.hpp" -namespace MC { +namespace MC::World { void Chunk::set(uint32_t x, uint32_t y, uint32_t z, BlockType type) { m_blocks[x][y][z].type = type; } -Mesh Chunk::mesh() { +GFX::Mesh Chunk::mesh() { std::vector> positions{}; std::vector> tex_coords{}; std::vector indices{}; diff --git a/src/World/Chunk.hpp b/src/World/Chunk.hpp index 0a5f269..7a93f5a 100644 --- a/src/World/Chunk.hpp +++ b/src/World/Chunk.hpp @@ -1,22 +1,23 @@ #pragma once #include +#include #include "BlockType.hpp" -#include "../Mesh.hpp" +#include "../GFX/Mesh.hpp" #include "BlockSide.hpp" +#include "../GFX/Binder.hpp" #define CHUNK_WIDTH 16 #define CHUNK_HEIGHT 32 -namespace MC { +namespace MC::World { class Chunk { public: Chunk() : m_blocks{} {}; void set(uint32_t x, uint32_t y, uint32_t z, BlockType type); - - Mesh mesh(); + GFX::Mesh mesh(); private: bool is_face_visible(uint32_t x, uint32_t y, uint32_t z, BlockSide side); diff --git a/src/World/Generator.cpp b/src/World/Generator.cpp index 5b8b9b5..d7bb038 100644 --- a/src/World/Generator.cpp +++ b/src/World/Generator.cpp @@ -1,6 +1,6 @@ #include "Generator.hpp" -namespace MC { +namespace MC::World { Chunk Generator::generate(uint32_t _x, uint32_t _y) { Chunk chunk; diff --git a/src/World/Generator.hpp b/src/World/Generator.hpp index 8e67ee1..9ddac66 100644 --- a/src/World/Generator.hpp +++ b/src/World/Generator.hpp @@ -3,7 +3,7 @@ #include #include "Chunk.hpp" -namespace MC { +namespace MC::World { class Generator { public: diff --git a/src/main.cpp b/src/main.cpp index d69dccd..3a8b55d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,18 +1,15 @@ #include #include #include -#include #include -#include "Window.hpp" -#include "Mesh.hpp" -#include "Camera.hpp" -#include "Binder.hpp" +#include "GFX/Window.hpp" +#include "GFX/Camera.hpp" +#include "GFX/Binder.hpp" #include "Math/MVP.hpp" -#include "Shader/ShaderProgram.hpp" -#include "Mouse.hpp" -#include "Texture.hpp" -#include "Image/PPMParser.hpp" +#include "GFX/Shading/Program.hpp" +#include "GFX/Texture.hpp" +#include "GFX/Image/PPMParser.hpp" #include "World/Generator.hpp" #define APP_NAME "Meowcraft" @@ -24,10 +21,10 @@ #define FOV 45 void run(); -void render(MC::BindableMesh&, MC::Texture&); -void process_input(MC::Window&, MC::Mouse&, MC::Camera&); +void render(MC::GFX::BindableMesh&, MC::GFX::Texture&); +void process_input(MC::GFX::Window&, MC::GFX::Camera&); void setup_gl(); -void fix_macos_render(MC::Window&); +void fix_macos_render(MC::GFX::Window&); int main() { glfwInit(); @@ -45,8 +42,7 @@ int main() { } void run() { - MC::Window window(APP_NAME, WINDOW_WIDTH, WINDOW_HEIGHT); - MC::Mouse mouse{}; + MC::GFX::Window window(APP_NAME, WINDOW_WIDTH, WINDOW_HEIGHT); setup_gl(); glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT); @@ -54,19 +50,22 @@ void run() { glViewport(0, 0, w, h); }); - auto image = MC::Image::PPMParser(MC::Assets::Images::atlas).parse(); - auto texture = MC::Texture(image); + auto image = MC::GFX::Image::PPMParser(MC::Assets::Images::atlas).parse(); + auto texture = MC::GFX::Texture(image); - MC::Generator generator; + MC::World::Generator generator; auto chunk = generator.generate(0, 0); auto chunk_mesh = chunk.mesh(); - auto mesh = MC::Binder::load(chunk_mesh); + auto mesh = MC::GFX::Binder::load(chunk_mesh); - MC::Camera camera{}; + MC::GFX::Camera camera{}; camera.set_position({0.0f, 0.0f, 3.0f}); - MC::ShaderProgram program(MC::Shader::create_fragment(), MC::Shader::create_vertex()); + MC::GFX::Shading::Program program( + MC::GFX::Shading::Shader::create_fragment(), + MC::GFX::Shading::Shader::create_vertex() + ); auto model_uniform = program.uniform("model_matrix"); auto view_uniform = program.uniform("view_matrix"); @@ -91,7 +90,7 @@ void run() { fix_macos_render(window); #endif - process_input(window, mouse, camera); + process_input(window, camera); program.bind(); @@ -106,23 +105,23 @@ void run() { } } -void render(MC::BindableMesh& mesh, MC::Texture& texture) { +void render(MC::GFX::BindableMesh& mesh, MC::GFX::Texture& texture) { glClearColor(0.85f, 0.85f, 0.85f, 1.0f); // #DBDBDB glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); texture.bind(); mesh.bind(); - glDrawElements(GL_TRIANGLES, mesh.size(), GL_UNSIGNED_INT, 0); + glDrawElements(GL_TRIANGLES, mesh.size(), GL_UNSIGNED_INT, nullptr); mesh.unbind(); texture.unbind(); } -void process_input(MC::Window& window, MC::Mouse& mouse, MC::Camera& camera) { +void process_input(MC::GFX::Window& window, MC::GFX::Camera& camera) { if (window.key(GLFW_KEY_ESCAPE, GLFW_PRESS)) { window.close(); } - auto r = mouse.update(window); + auto r = window.mouse_delta(); auto key = [&](int key) -> float { return window.key(key, GLFW_PRESS); }; @@ -145,7 +144,7 @@ void setup_gl() { } } -void fix_macos_render(MC::Window& window) { +void fix_macos_render(MC::GFX::Window& window) { static bool moved = false; if(!moved) { -- cgit 1.4.1