diff options
Diffstat (limited to 'src')
41 files changed, 120 insertions, 189 deletions
diff --git a/src/Compute/Queue.hpp b/src/Compute/Queue.hpp index 53acc2d..77aed7f 100644 --- a/src/Compute/Queue.hpp +++ b/src/Compute/Queue.hpp @@ -3,7 +3,6 @@ #include <functional> #include <sys/types.h> #include <mutex> -#include <utility> #include <vector> #include <queue> #include <thread> @@ -15,10 +14,10 @@ class Queue { public: explicit Queue(uint workers) : m_control(std::make_shared<Control>()) { for (uint w = 0; w < workers; w++) { - std::thread thread{[=]() { Queue::run_thread(m_control); }}; + std::thread thread{[=]{ Queue::run_thread(m_control); }}; thread.detach(); } - }; + } void add(I id, float priority, std::function<X()> execute) { std::scoped_lock work_lock(m_control->work.mutex); @@ -34,7 +33,7 @@ public: std::vector<Result> done_results; std::scoped_lock result_lock(m_control->results.mutex); - for (auto r : m_control->results.results) { + for (auto &r : m_control->results.results) { done_results.push_back(r); } m_control->results.results.clear(); diff --git a/src/GFX/Binder.hpp b/src/GFX/Binder.hpp index 92faebe..773518d 100644 --- a/src/GFX/Binder.hpp +++ b/src/GFX/Binder.hpp @@ -21,7 +21,7 @@ private: ) : m_vao(vao), m_vertex_count(vertex_count), m_has_indices(vertex_count > 0), - m_attribute_count(attribute_count) {}; + m_attribute_count(attribute_count) {} uint32_t m_vao; size_t m_vertex_count; @@ -33,7 +33,7 @@ private: class Binder { public: - Binder() = default;; + Binder() = default; static BindableMesh load(const Mesh& mesh); diff --git a/src/GFX/Camera.cpp b/src/GFX/Camera.cpp index 6b25347..dbc3444 100644 --- a/src/GFX/Camera.cpp +++ b/src/GFX/Camera.cpp @@ -2,7 +2,7 @@ namespace MC::GFX { -Vector<3> Camera::position() { +Vector<3> Camera::position() const { return m_position; } @@ -10,18 +10,18 @@ void Camera::set_position(Vector<3> position) { m_position = position; } -void Camera::move(Vector<3> vector) { - m_position = m_position + vector; +void Camera::move(Vector<3> by) { + m_position = m_position + by; } 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); + move(Vector<3>{result.elements}); } -Rotation Camera::angles() { +Rotation Camera::angles() const { return m_angles; } diff --git a/src/GFX/Camera.hpp b/src/GFX/Camera.hpp index 77a210f..2109749 100644 --- a/src/GFX/Camera.hpp +++ b/src/GFX/Camera.hpp @@ -8,12 +8,12 @@ class Camera { public: Camera() = default; - Vector<3> position(); + Vector<3> position() const; void set_position(Vector<3> position); void move(Vector<3> by); void move_relative(Vector<3> by); - Rotation angles(); + Rotation angles() const; void set_angles(Rotation angles); void rotate(Rotation by); diff --git a/src/GFX/Image/PPMParser.cpp b/src/GFX/Image/PPMParser.cpp index cf1bf77..f002db8 100644 --- a/src/GFX/Image/PPMParser.cpp +++ b/src/GFX/Image/PPMParser.cpp @@ -66,7 +66,7 @@ RawImage::Pixel PPMParser::parse_pixel(uint8_t 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; }; + auto map_to_range = [=](uint64_t s) -> uint8_t { return s * 255 / max_color; }; RawImage::Pixel pixel{}; pixel.r = map_to_range(r_sample); @@ -120,9 +120,8 @@ std::string_view PPMParser::chomp_part() { } void PPMParser::skip_whitespace() { - uint8_t c; while (!is_eof()) { - c = m_source[m_cursor]; + uint8_t c = m_source[m_cursor]; if (c == '#') { skip_comment(); continue; @@ -147,7 +146,7 @@ void PPMParser::skip_comment() { } } -bool PPMParser::is_eof() { +bool PPMParser::is_eof() const { return m_cursor >= m_source.size(); } diff --git a/src/GFX/Image/PPMParser.hpp b/src/GFX/Image/PPMParser.hpp index 3909cee..07ff3c2 100644 --- a/src/GFX/Image/PPMParser.hpp +++ b/src/GFX/Image/PPMParser.hpp @@ -8,7 +8,7 @@ namespace MC::GFX::Image { class PPMParser { public: - explicit PPMParser(std::string_view source) : m_source(source) {}; + explicit PPMParser(std::string_view source) : m_source(source) {} RawImage parse(); private: @@ -35,7 +35,7 @@ private: void skip_whitespace(); void skip_comment(); - bool is_eof(); + bool is_eof() const; std::string_view m_source; uint64_t m_cursor = 0; diff --git a/src/GFX/Image/RawImage.cpp b/src/GFX/Image/RawImage.cpp index 1222fab..6cb06b2 100644 --- a/src/GFX/Image/RawImage.cpp +++ b/src/GFX/Image/RawImage.cpp @@ -1,21 +1,9 @@ #include "RawImage.hpp" +#include <sstream> namespace MC::GFX::Image { -RawImage::RawImage(Util::Sampler<2, float>& sampler, uint32_t width, uint32_t height) - : m_pixels(), m_width(width), m_height(height) -{ - m_pixels.reserve(width * height); - for (int y = 0; y < height; y++) { - for (int x = 0; x < width; x++) { - auto result = sampler.sample({(float)x, (float)y}); - auto intensity = (uint8_t)(result * 255); - add({intensity, intensity, intensity}); - } - } -} - -void RawImage::add(RawImage::Pixel pixel) { +void RawImage::add(Pixel pixel) { m_pixels.push_back(pixel); } @@ -35,18 +23,14 @@ uint32_t RawImage::height() const { return m_height; } -uint8_t RawImage::channels() const { - return m_channels; -} - std::string RawImage::string() const { std::stringstream str{}; bool comma = false; str << "["; - for (const auto& pixel : m_pixels) { + for (const auto [r, g, b] : m_pixels) { if (comma) { str << ", "; } - str << "{r=" << (uint)pixel.r << ", g=" << (uint)pixel.g << ", b=" << (uint)pixel.r << "}"; + str << "{r=" << (uint)r << ", g=" << (uint)g << ", b=" << (uint)r << "}"; comma = true; } str << "]"; diff --git a/src/GFX/Image/RawImage.hpp b/src/GFX/Image/RawImage.hpp index 8a837e6..be3c83a 100644 --- a/src/GFX/Image/RawImage.hpp +++ b/src/GFX/Image/RawImage.hpp @@ -4,21 +4,17 @@ #include <cstddef> #include <string> #include <vector> -#include "../../Util/Sampler.hpp" namespace MC::GFX::Image { class RawImage { public: - RawImage() : m_pixels(), m_width(0), m_height(0) {}; + RawImage() : m_width(0), m_height(0) {} - RawImage(uint32_t width, uint32_t height) - : m_pixels(), m_width(width), m_height(height) { + RawImage(uint32_t width, uint32_t height) : m_width(width), m_height(height) { m_pixels.reserve(width * height); } - RawImage(Util::Sampler<2, float>& sampler, uint32_t width, uint32_t height); - struct Pixel { uint8_t r, g, b; }; @@ -30,14 +26,11 @@ public: uint32_t width() const; uint32_t height() const; - uint8_t channels() const; std::string string() const; private: std::vector<Pixel> m_pixels; - uint32_t m_width, m_height; - uint8_t m_channels = 3; }; } diff --git a/src/GFX/Mesh.hpp b/src/GFX/Mesh.hpp index 1d14ba2..ec21e53 100644 --- a/src/GFX/Mesh.hpp +++ b/src/GFX/Mesh.hpp @@ -17,18 +17,18 @@ public: attribute_size(S), type_size(sizeof(T)) { data = copy(v.data(), v.size() * S * type_size); - }; + } Attribute( const Attribute& other ) : data(copy(other.data, other.data_size * other.attribute_size * other.type_size)), data_size(other.data_size), attribute_size(other.attribute_size), - type_size(other.type_size) {}; + type_size(other.type_size) {} static void* copy(void* ptr, uint32_t size) { auto* buffer = new uint8_t[size]; - std::copy((uint8_t*)ptr, (uint8_t*)ptr + size, buffer); + std::copy_n((uint8_t*)ptr, size, buffer); return buffer; } @@ -42,12 +42,11 @@ public: std::vector<Attribute> attributes, std::vector<uint32_t> indices ) : m_attributes(std::move(attributes)), - m_indices(std::move(indices)) {}; + m_indices(std::move(indices)) {} - Mesh( + explicit Mesh( std::vector<Attribute> attributes - ) : m_attributes(std::move(attributes)), - m_indices() {}; + ) : m_attributes(std::move(attributes)) {} const std::vector<uint32_t>& indices() const; const std::vector<Attribute>& attributes() const; diff --git a/src/GFX/Mouse.hpp b/src/GFX/Mouse.hpp index 3ed57a2..1e2111e 100644 --- a/src/GFX/Mouse.hpp +++ b/src/GFX/Mouse.hpp @@ -1,6 +1,5 @@ #pragma once -#include <cstdint> #include <GLFW/glfw3.h> #include "../Math/Vector.hpp" diff --git a/src/GFX/Shading/Program.hpp b/src/GFX/Shading/Program.hpp index 6b28de0..8a50617 100644 --- a/src/GFX/Shading/Program.hpp +++ b/src/GFX/Shading/Program.hpp @@ -1,9 +1,7 @@ #pragma once #include <string> -#include <vector> #include "Shader.hpp" -#include "../../Math/Common.hpp" #include "Uniform.hpp" namespace MC::GFX::Shading { diff --git a/src/GFX/Shading/Shader.cpp b/src/GFX/Shading/Shader.cpp index f1502c2..6dfac34 100644 --- a/src/GFX/Shading/Shader.cpp +++ b/src/GFX/Shading/Shader.cpp @@ -4,8 +4,8 @@ namespace MC::GFX::Shading { -Shader::Shader(Shader::Type type, const char* source) { - uint32_t gl_type; +Shader::Shader(Type type, const char* source) { + uint32_t gl_type = 0; switch (type) { case Type::Vertex: gl_type = GL_VERTEX_SHADER; diff --git a/src/GFX/Shading/Uniform.cpp b/src/GFX/Shading/Uniform.cpp index 9448574..71786ef 100644 --- a/src/GFX/Shading/Uniform.cpp +++ b/src/GFX/Shading/Uniform.cpp @@ -3,11 +3,11 @@ namespace MC::GFX::Shading { -void Uniform::set(Matrix<4, 4> value) const { +void Uniform::set(const Matrix<4, 4>& value) const { glUniformMatrix4fv(m_index, 1, GL_TRUE, value.elements); } -void Uniform::set(Vector<3> value) const { +void Uniform::set(const 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 index 3c14315..a270e65 100644 --- a/src/GFX/Shading/Uniform.hpp +++ b/src/GFX/Shading/Uniform.hpp @@ -2,7 +2,6 @@ #include <cstdint> #include <string> -#include <utility> #include "../../Math/Common.hpp" namespace MC::GFX::Shading { @@ -10,10 +9,10 @@ namespace MC::GFX::Shading { class Uniform { public: Uniform(std::string name, uint32_t index) - : m_name(std::move(name)), m_index(index) {}; + : m_name(std::move(name)), m_index(index) {} - void set(Matrix<4, 4> value) const; - void set(Vector<3> value) const; + void set(const Matrix<4, 4>& value) const; + void set(const Vector<3>& value) const; private: std::string m_name; diff --git a/src/GFX/Texture.cpp b/src/GFX/Texture.cpp index ef48631..64151a5 100644 --- a/src/GFX/Texture.cpp +++ b/src/GFX/Texture.cpp @@ -23,11 +23,11 @@ Texture::Texture(const Image::RawImage& image) { glBindTexture(GL_TEXTURE_2D, 0); } -void Texture::bind() { +void Texture::bind() const { glBindTexture(GL_TEXTURE_2D, m_texture); } -void Texture::unbind() { +void Texture::unbind() const { glBindTexture(GL_TEXTURE_2D, 0); } } \ No newline at end of file diff --git a/src/GFX/Texture.hpp b/src/GFX/Texture.hpp index ff86634..2981b2c 100644 --- a/src/GFX/Texture.hpp +++ b/src/GFX/Texture.hpp @@ -8,10 +8,10 @@ class Texture { public: explicit Texture(const Image::RawImage& image); - void bind(); - void unbind(); + void bind() const; + void unbind() const; private: - uint32_t m_texture; + uint32_t m_texture = 0; }; } \ No newline at end of file diff --git a/src/GFX/Window.cpp b/src/GFX/Window.cpp index 0a1828c..ea1fde4 100644 --- a/src/GFX/Window.cpp +++ b/src/GFX/Window.cpp @@ -23,11 +23,11 @@ Window::~Window() { glfwDestroyWindow(m_window); } -bool Window::should_close() { +bool Window::should_close() const { return glfwWindowShouldClose(m_window); } -GLFWwindow* Window::get() { +GLFWwindow* Window::get() const { return m_window; } @@ -39,8 +39,12 @@ Vector<2> Window::mouse_delta() { return m_mouse.update(m_window); } -bool Window::key(int key, int type) { - return (glfwGetKey(m_window, key) == type); +bool Window::key(int key, int type) const { + return glfwGetKey(m_window, key) == type; +} + +bool Window::mouse(int key, int type) const { + return glfwGetMouseButton(m_window, key) == type; } void Window::start_frame() { @@ -49,7 +53,7 @@ void Window::start_frame() { } void Window::on_size_change(void (callback)(GLFWwindow*, int, int)) { - glfwSetFramebufferSizeCallback(m_window, static_cast<GLFWframebuffersizefun>(callback)); + glfwSetFramebufferSizeCallback(m_window, callback); } } \ No newline at end of file diff --git a/src/GFX/Window.hpp b/src/GFX/Window.hpp index 63e8446..966925c 100644 --- a/src/GFX/Window.hpp +++ b/src/GFX/Window.hpp @@ -12,7 +12,7 @@ public: Window(const char* title, uint32_t width, uint32_t height); ~Window(); - GLFWwindow* get(); + GLFWwindow* get() const; void on_size_change(void (* callback)(GLFWwindow*, int, int)); @@ -20,8 +20,9 @@ public: void start_frame(); Vector<2> mouse_delta(); - bool key(int key, int type); - bool should_close(); + bool key(int key, int type) const; + bool mouse(int key, int type) const; + bool should_close() const; private: GLFWwindow* m_window; Mouse m_mouse; diff --git a/src/Math/Grid.hpp b/src/Math/Grid.hpp index 1c1a7ca..e9907a0 100644 --- a/src/Math/Grid.hpp +++ b/src/Math/Grid.hpp @@ -1,7 +1,6 @@ #pragma once -#include "Common.hpp" -#include "Matrix.hpp" +#include "Vector.hpp" namespace Math { diff --git a/src/Math/Interpolation.cpp b/src/Math/Interpolation.cpp index a1e6b69..d5b2564 100644 --- a/src/Math/Interpolation.cpp +++ b/src/Math/Interpolation.cpp @@ -1,5 +1,4 @@ #include "Interpolation.hpp" -#include <functional> #include "Common.hpp" namespace Math { @@ -15,7 +14,7 @@ float bilinear_interpolation(Matrix<2, 2> val, GridCellBoundaries cell, Vector<2 return linear_interpolation({r1, r2}, cell.y1, cell.y2, pos.y()); } -float trilinear_interpolation(Matrix<2, 2> val_front, Matrix<2, 2> val_back, CubeCellBoundaries cell, Vector<3> pos) { +float trilinear_interpolation(Matrix<2, 2> val_front, Matrix<2, 2> val_back, const CubeCellBoundaries& cell, Vector<3> pos) { auto r1 = bilinear_interpolation(val_front, cell.grid_cell(), {pos.x(), pos.y()}); auto r2 = bilinear_interpolation(val_back, cell.grid_cell(), {pos.x(), pos.y()}); diff --git a/src/Math/Interpolation.hpp b/src/Math/Interpolation.hpp index 9eaf604..eaaa67d 100644 --- a/src/Math/Interpolation.hpp +++ b/src/Math/Interpolation.hpp @@ -1,6 +1,5 @@ #pragma once -#include <functional> #include "Common.hpp" #include "Grid.hpp" @@ -8,6 +7,6 @@ namespace Math { float linear_interpolation(Vector<2> val, float left, float right, float pos); float bilinear_interpolation(Matrix<2, 2> val, GridCellBoundaries cell, Vector<2> pos); -float trilinear_interpolation(Matrix<2, 2> val_front, Matrix<2, 2> val_back, CubeCellBoundaries cell, Vector<3> pos); +float trilinear_interpolation(Matrix<2, 2> val_front, Matrix<2, 2> val_back, const CubeCellBoundaries& cell, Vector<3> pos); } \ No newline at end of file diff --git a/src/Math/MVP.cpp b/src/Math/MVP.cpp index dd5b5e7..de771d3 100644 --- a/src/Math/MVP.cpp +++ b/src/Math/MVP.cpp @@ -19,7 +19,7 @@ Matrix<4, 4> view(Vector<3> position, Rotation angles) { } Matrix<4, 4> perspective_projection(float aspect, float fov, float near, float far) { - auto fov_radians = Math::radians(fov); + auto fov_radians = radians(fov); float x_scale = 1.0f / (tan(fov_radians / 2.0f) * aspect); float y_scale = 1.0f / tan(fov_radians / 2.0f); diff --git a/src/Math/Matrix.hpp b/src/Math/Matrix.hpp index d467f72..56663e0 100644 --- a/src/Math/Matrix.hpp +++ b/src/Math/Matrix.hpp @@ -7,18 +7,18 @@ template <size_t R, size_t C, typename T = float> struct Matrix { - Matrix() : elements{} {}; + Matrix() : elements{} {} explicit Matrix(const T scalar) { std::fill(elements, elements + R * C, scalar); - }; + } - template<typename ...Args, typename std::enable_if_t<sizeof...(Args) == R * C, int> = 0> - Matrix(Args... args): elements{ args... } {}; + template<typename ...Args, std::enable_if_t<sizeof...(Args) == R * C, int> = 0> + Matrix(Args... args): elements{ args... } {} explicit Matrix(const T values[R * C]) { std::copy(values, values + R * C, elements); - }; + } static Matrix<R, R, T> identity() { Matrix<R, R, T> result{}; @@ -68,7 +68,7 @@ struct Matrix { } Vector<C, T> row(size_t index) const { - return { &elements[index * C] }; + return Vector<C, T>{ &elements[index * C] }; } Vector<R, T> col(size_t index) const { @@ -124,7 +124,7 @@ struct Matrix { Vector<R, T> operator*(const Vector<R, T> vector) const { Matrix<R, 1, T> matrix(vector.elements); matrix = this->operator*(matrix); - return { matrix.elements }; + return Vector<R, T>{ matrix.elements }; } const T& operator()(const size_t x, const size_t y) const { diff --git a/src/Math/Perlin.cpp b/src/Math/Perlin.cpp index e0982cb..5614bb6 100644 --- a/src/Math/Perlin.cpp +++ b/src/Math/Perlin.cpp @@ -9,7 +9,7 @@ float ease(float t) { } uint8_t hash(uint8_t x) { - auto rot = (x * 5) % 8; + auto rot = x * 5 % 8; return x << rot | x >> (8 - rot); } diff --git a/src/Math/Random.cpp b/src/Math/Random.cpp index e35cda7..aa0ff55 100644 --- a/src/Math/Random.cpp +++ b/src/Math/Random.cpp @@ -18,7 +18,7 @@ float to_float(uint8_t u) { } uint8_t hash(uint8_t x) { - auto o = ((x ^ 0xAA) * 5); + auto o = (x ^ 0xAA) * 5; auto rot = o % 8; return o << rot | o >> (8 - rot); } diff --git a/src/Math/Rotation.hpp b/src/Math/Rotation.hpp index 0c5c606..a3420ed 100644 --- a/src/Math/Rotation.hpp +++ b/src/Math/Rotation.hpp @@ -4,16 +4,15 @@ #include "Vector.hpp" struct Rotation { -public: - Rotation() : vector{} {}; + Rotation() = default; - Rotation(Vector<3> vector) : vector{ wrap(vector) } {}; + Rotation(Vector<3> vector) : vector{ wrap(vector) } {} - Rotation(float angles[3]) : Rotation(angles[0], angles[1], angles[2]) {}; + explicit Rotation(float angles[3]) : Rotation(angles[0], angles[1], angles[2]) {} Rotation(float pitch, float yaw, float roll) { vector = wrap({pitch, yaw, roll }); - }; + } Rotation operator+(Rotation other) const { return wrap(vector + other.vector); diff --git a/src/Math/Trig.hpp b/src/Math/Trig.hpp index 2a415f5..00489a9 100644 --- a/src/Math/Trig.hpp +++ b/src/Math/Trig.hpp @@ -6,7 +6,7 @@ namespace Math { template<typename T> T radians(T degrees) { - return (degrees * PI) / 180.0f; + return degrees * PI / 180.0f; } template<typename T> diff --git a/src/Math/Vector.hpp b/src/Math/Vector.hpp index 54207f0..2d87c4b 100644 --- a/src/Math/Vector.hpp +++ b/src/Math/Vector.hpp @@ -7,18 +7,18 @@ template <size_t S, typename T = float> struct Vector { - Vector(): elements{} {}; + Vector(): elements{} {} template<typename ...Args, std::enable_if_t<sizeof...(Args) == S, int> = 0> - Vector(Args... args) : elements{ args... } {}; + Vector(Args... args) : elements{ args... } {} - Vector(const T values[S]) { + explicit Vector(const T values[S]) { std::copy(values, values + S, elements); - }; + } explicit Vector(const T scalar) { std::fill(elements, elements + S, scalar); - }; + } Vector(const Vector<S - 1, T> vector, const T scalar) { std::copy(vector.elements, vector.elements + S - 1, elements); diff --git a/src/Util/ImageViewer.cpp b/src/Util/ImageViewer.cpp index 7fd5867..f94ccd0 100644 --- a/src/Util/ImageViewer.cpp +++ b/src/Util/ImageViewer.cpp @@ -5,12 +5,12 @@ namespace MC::Util { ImageViewer::ImageViewer( - GFX::Image::RawImage& image, + const GFX::Image::RawImage& image, float window_aspect ) : m_texture(image), m_program( - {GFX::Shading::Shader::Type::Vertex, ImageViewer::vertex}, - {GFX::Shading::Shader::Type::Fragment, ImageViewer::fragment} + {GFX::Shading::Shader::Type::Vertex, vertex}, + {GFX::Shading::Shader::Type::Fragment, fragment} ), m_mesh(GFX::Binder::load(create_mesh(window_aspect, image.width(), image.height()))) { m_program.bind(); @@ -25,7 +25,7 @@ ImageViewer::ImageViewer( m_program.unbind(); } -void ImageViewer::render() { +void ImageViewer::render() const { m_program.bind(); m_texture.bind(); m_mesh.bind(); diff --git a/src/Util/ImageViewer.hpp b/src/Util/ImageViewer.hpp index 2e4efe3..59c0bf3 100644 --- a/src/Util/ImageViewer.hpp +++ b/src/Util/ImageViewer.hpp @@ -10,18 +10,18 @@ namespace MC::Util { class ImageViewer { public: - explicit ImageViewer(GFX::Image::RawImage& image, float window_aspect); + explicit ImageViewer(const GFX::Image::RawImage& image, float window_aspect); - void render(); + void render() const; private: static GFX::Mesh create_mesh(float window_aspect, uint32_t image_width, uint32_t image_height); static const char* vertex; static const char* fragment; - MC::GFX::BindableMesh m_mesh; - MC::GFX::Shading::Program m_program; - MC::GFX::Texture m_texture; + GFX::BindableMesh m_mesh; + GFX::Shading::Program m_program; + GFX::Texture m_texture; }; } \ No newline at end of file diff --git a/src/Util/Sampler.hpp b/src/Util/Sampler.hpp deleted file mode 100644 index 3a4ff4f..0000000 --- a/src/Util/Sampler.hpp +++ /dev/null @@ -1,36 +0,0 @@ -#pragma once - -#include <functional> -#include "../Math/Vector.hpp" -#include "../Math/Grid.hpp" - -namespace MC::Util { - -template<size_t D, typename T> -class Sampler { - using Pos = Vector<D>; - using Sample = std::function<T(Pos)>; - using Interpolate = std::function<T(Pos, Sample)>; -public: - Sampler( - Sample sample, - Pos offset = {}, - float scale = 1.0f, - Interpolate interpolate = nearest_interpolation - ) : m_sample([=](Sampler::Pos pos) -> T { - return interpolate(pos, [=](Sampler::Pos p) -> T { return sample(p * scale + offset); }); - }) {} - - T sample(Pos at) { - return m_sample(at); - } - - static T nearest_interpolation(Pos pos, Sample sample) { - return sample(pos); - } - -private: - Sample m_sample; -}; - -} \ No newline at end of file diff --git a/src/World/Chunk.cpp b/src/World/Chunk.cpp index b75516f..079575a 100644 --- a/src/World/Chunk.cpp +++ b/src/World/Chunk.cpp @@ -15,7 +15,7 @@ bool Chunk::is_empty(uint32_t x, uint32_t y, uint32_t z) const { return get(x, y, z).empty(); } -Vector<3> Chunk::position() { +Vector<3> Chunk::position() const { return m_position; } diff --git a/src/World/Chunk.hpp b/src/World/Chunk.hpp index 8780700..b3eb06f 100644 --- a/src/World/Chunk.hpp +++ b/src/World/Chunk.hpp @@ -39,7 +39,7 @@ public: void set_details(const Details& details) { m_details = details; } Details& details(){ return m_details; } - Vector<3> position(); + Vector<3> position() const; static bool is_valid_position(uint32_t x, uint32_t y, uint32_t z); private: diff --git a/src/World/ChunkIndex.hpp b/src/World/ChunkIndex.hpp index 4701581..ef61f24 100644 --- a/src/World/ChunkIndex.hpp +++ b/src/World/ChunkIndex.hpp @@ -1,8 +1,6 @@ #pragma once #include <cstdint> -#include <cstdlib> -#include <functional> namespace MC::World { @@ -27,6 +25,6 @@ template<> struct std::equal_to<MC::World::ChunkIndex> { template<> struct std::hash<MC::World::ChunkIndex> { size_t operator()(const MC::World::ChunkIndex& i) const noexcept { - return ((int64_t)i.x << 32) | i.y; + return (int64_t)i.x << 32 | i.y; } }; \ No newline at end of file diff --git a/src/World/Generation/ChunkMeshing.cpp b/src/World/Generation/ChunkMeshing.cpp index 54abc85..0494da4 100644 --- a/src/World/Generation/ChunkMeshing.cpp +++ b/src/World/Generation/ChunkMeshing.cpp @@ -83,7 +83,7 @@ std::array<Vector<3>, 4> face_normals(BlockSide side) { return {normal, normal, normal, normal}; } -bool is_face_visible(Chunk& chunk, ChunkMeshing::ChunkNeighbors neighbors, uint32_t x, uint32_t y, uint32_t z, BlockSide side) { +bool is_face_visible(Chunk& chunk, const ChunkMeshing::ChunkNeighbors& neighbors, uint32_t x, uint32_t y, uint32_t z, BlockSide side) { Vector<3, int32_t> offset{}; switch (side) { case BlockSide::Front: @@ -128,15 +128,15 @@ bool is_face_visible(Chunk& chunk, ChunkMeshing::ChunkNeighbors neighbors, uint3 chunk_to_ask = &chunk; } - auto neighbor = chunk_to_ask->get(neighbor_pos.x(), neighbor_pos.y(), neighbor_pos.z()); - if (neighbor.type == BlockType::Air) { + auto [neighbor] = chunk_to_ask->get(neighbor_pos.x(), neighbor_pos.y(), neighbor_pos.z()); + if (neighbor == BlockType::Air) { return true; } return false; } -GFX::Mesh ChunkMeshing::create_mesh_for_chunk(Chunk& chunk, ChunkMeshing::ChunkNeighbors neighbors) { +GFX::Mesh ChunkMeshing::create_mesh_for_chunk(Chunk& chunk, const ChunkNeighbors& neighbors) { std::vector<Vector<3>> positions{}; std::vector<Vector<3>> normals{}; std::vector<Vector<2>> tex_coords{}; @@ -174,7 +174,7 @@ GFX::Mesh ChunkMeshing::create_mesh_for_chunk(Chunk& chunk, ChunkMeshing::ChunkN } } - return { + return GFX::Mesh{ {positions, normals, tex_coords}, indices, }; diff --git a/src/World/Generation/ChunkMeshing.hpp b/src/World/Generation/ChunkMeshing.hpp index a4fed25..6ec77af 100644 --- a/src/World/Generation/ChunkMeshing.hpp +++ b/src/World/Generation/ChunkMeshing.hpp @@ -6,6 +6,6 @@ namespace MC::World::Generation::ChunkMeshing { struct ChunkNeighbors { Chunk &north, &east, &south, &west; }; -GFX::Mesh create_mesh_for_chunk(Chunk& chunk, ChunkNeighbors neighbors); +GFX::Mesh create_mesh_for_chunk(Chunk& chunk, const ChunkNeighbors& neighbors); } diff --git a/src/World/Generation/Generator.cpp b/src/World/Generation/Generator.cpp index af3c54d..19a3dc8 100644 --- a/src/World/Generation/Generator.cpp +++ b/src/World/Generation/Generator.cpp @@ -9,11 +9,11 @@ Chunk Generator::generate(int64_t chunk_x, int64_t chunk_y) { auto landmass_map = generate_landmass_map(chunk_x, chunk_y); auto hill_map = generate_hill_map(chunk_x, chunk_y); - auto height_map = generate_height_map(landmass_map, hill_map, chunk_x, chunk_y); + auto height_map = generate_height_map(landmass_map, hill_map); auto temperature_map = generate_temperature_map(chunk_x, chunk_y); auto humidity_map = generate_humidity_map(chunk_x, chunk_y); - auto biome_map = generate_biome_map(landmass_map, hill_map, temperature_map, humidity_map, chunk_x, chunk_y); + auto biome_map = generate_biome_map(landmass_map, hill_map, temperature_map, humidity_map); auto terrain_map = generate_terrain(height_map, chunk_x, chunk_y); decorate_soil(chunk, biome_map, terrain_map); @@ -38,7 +38,7 @@ Generator::Map2D<float> Generator::name(int64_t chunk_x, int64_t chunk_y) { SIMPLE_MAP_GENERATOR(generate_landmass_map, get_landmass) SIMPLE_MAP_GENERATOR(generate_hill_map, get_hill) -Generator::Map2D<float> Generator::generate_height_map(Map2D<float>& landmass_map, Map2D<float>& hill_map, int64_t chunk_x, int64_t chunk_y) { +Generator::Map2D<float> Generator::generate_height_map(Map2D<float>& landmass_map, Map2D<float>& hill_map) { Map2D<float> height_map{}; for (uint x = 0; x < Chunk::Width; x++) { @@ -63,8 +63,7 @@ SIMPLE_MAP_GENERATOR(generate_humidity_map, get_humidity) Generator::Map2D<BiomeType> Generator::generate_biome_map( Map2D<float>& landmass_map, Map2D<float>& hill_map, - Map2D<float>& temperature_map, Map2D<float>& humidity_map, - int64_t chunk_x, int64_t chunk_y + Map2D<float>& temperature_map, Map2D<float>& humidity_map ) { Map2D<BiomeType> biome_map{}; @@ -187,9 +186,9 @@ void Generator::decorate_soil(Chunk& chunk, Map2D<BiomeType>& biome_map, Map3D<b } } -#define CURVE_START(y) constexpr auto lerp = Math::linear_interpolation; float _py = y; float _px = 0.0f; -#define CURVE_POINT(x, y) if (v < x) return lerp({_py, y}, _px, x, v); _py = y; _px = x -#define CURVE_END(y) return lerp({_py, y}, _px, 1.0f, v); +#define CURVE_START(y) constexpr auto lerp = Math::linear_interpolation; float _py = (y); float _px = 0.0f; +#define CURVE_POINT(x, y) if (v < (x)) return lerp({_py, (y)}, _px, (x), v); _py = y; _px = (x) +#define CURVE_END(y) return lerp({_py, (y)}, _px, 1.0f, v); float Generator::get_landmass(Vector<2> pos) const { auto v = m_landmass_noise.at(pos); diff --git a/src/World/Generation/Generator.hpp b/src/World/Generation/Generator.hpp index 464e36f..c18de45 100644 --- a/src/World/Generation/Generator.hpp +++ b/src/World/Generation/Generator.hpp @@ -20,15 +20,14 @@ private: Map2D<float> generate_landmass_map(int64_t chunk_x, int64_t chunk_y); Map2D<float> generate_hill_map(int64_t chunk_x, int64_t chunk_y); - Map2D<float> generate_height_map(Map2D<float>& landmass_map, Map2D<float>& hill_map, int64_t chunk_x, int64_t chunk_y); + Map2D<float> generate_height_map(Map2D<float>& landmass_map, Map2D<float>& hill_map); Map2D<float> generate_temperature_map(int64_t chunk_x, int64_t chunk_y); Map2D<float> generate_humidity_map(int64_t chunk_x, int64_t chunk_y); Map2D<BiomeType> generate_biome_map( Map2D<float>& landmass_map, Map2D<float>& hill_map, - Map2D<float>& temperature_map, Map2D<float>& humidity_map, - int64_t chunk_x, int64_t chunk_y + Map2D<float>& temperature_map, Map2D<float>& humidity_map ); Map3D<bool> generate_terrain(Map2D<float>& height_map, int64_t chunk_x, int64_t chunk_y); diff --git a/src/World/World.cpp b/src/World/World.cpp index 6e8489a..7570d8c 100644 --- a/src/World/World.cpp +++ b/src/World/World.cpp @@ -45,7 +45,7 @@ Chunk* World::get_chunk_for_position(Vector<3> position) { return nullptr; } -void World::process_chunk_visibility_updates(std::unordered_set<ChunkIndex>& new_chunks, Vector<3> player) { +void World::process_chunk_visibility_updates(const std::unordered_set<ChunkIndex>& new_chunks, const Vector<3> player) { for (auto new_index: new_chunks) { auto& data = get(new_index); if (data.status == ChunkStatus::Empty) { @@ -55,7 +55,7 @@ void World::process_chunk_visibility_updates(std::unordered_set<ChunkIndex>& new } } -std::unordered_set<ChunkIndex> World::get_visible_chunk_indices(Vector<3> position) const { +std::unordered_set<ChunkIndex> World::get_visible_chunk_indices(const Vector<3> position) const { int32_t center_x = std::round(position.x() / Chunk::Width); int32_t center_y = std::round(position.z() / Chunk::Width); @@ -119,8 +119,8 @@ void World::try_to_create_mesh_for_chunk(ChunkData& data) { auto south = get({index.x, index.y + 1}); auto west = get({index.x - 1, index.y}); - auto no_terrain = [](ChunkData& data){ - return !data.chunk.has_value(); + auto no_terrain = [](const ChunkData& d){ + return !d.chunk.has_value(); }; if (no_terrain(north) || no_terrain(east) || no_terrain(south) || no_terrain(west)) { @@ -140,7 +140,7 @@ void World::log_chunk_time(uint64_t chunk_time_ms) { m_statistics.average_chunk_time_ms += ((float)chunk_time_ms - m_statistics.average_chunk_time_ms) / m_statistics.chunk_time_sample_count; } -float World::get_average_chunk_time() { +float World::get_average_chunk_time() const { return m_statistics.average_chunk_time_ms; } diff --git a/src/World/World.hpp b/src/World/World.hpp index ae8b37f..bc008b3 100644 --- a/src/World/World.hpp +++ b/src/World/World.hpp @@ -32,11 +32,11 @@ public: std::vector<ChunkData*> get_visible_chunks(Vector<3> position); Chunk* get_chunk_for_position(Vector<3> position); - float get_average_chunk_time(); + float get_average_chunk_time() const; private: std::unordered_set<ChunkIndex> get_visible_chunk_indices(Vector<3> position) const; std::unordered_set<ChunkIndex> load_finished_chunks_from_queue(); - void process_chunk_visibility_updates(std::unordered_set<ChunkIndex>& new_chunks, Vector<3> player); + void process_chunk_visibility_updates(const std::unordered_set<ChunkIndex>& new_chunks, Vector<3> player); void request_generation(ChunkIndex index, float priority); void try_to_create_mesh_for_chunk(ChunkData& data); diff --git a/src/main.cpp b/src/main.cpp index a264f3e..0995b5b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -13,17 +13,17 @@ #define APP_NAME "Meowcraft" -#define WINDOW_WIDTH 1000 -#define WINDOW_HEIGHT 800 -#define ASPECT static_cast<float>(WINDOW_WIDTH) / WINDOW_HEIGHT +#define WINDOW_WIDTH 800 +#define WINDOW_HEIGHT 600 +#define ASPECT (static_cast<float>(WINDOW_WIDTH) / WINDOW_HEIGHT) #define FOV 90 void run(); -void render(MC::GFX::BindableMesh&, MC::GFX::Texture&); +void render(const MC::GFX::BindableMesh&, const MC::GFX::Texture&); void process_input(MC::GFX::Window&, MC::GFX::Camera&); void setup_gl(); -void fix_macos_render(MC::GFX::Window&); +void fix_macos_render(const MC::GFX::Window&); int main() { glfwInit(); @@ -45,7 +45,7 @@ void run() { setup_gl(); glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT); - window.on_size_change([](GLFWwindow* window, int w, int h) { + window.on_size_change([](GLFWwindow* _, int w, int h) { glViewport(0, 0, w, h); }); @@ -112,7 +112,7 @@ void run() { } } -void render(MC::GFX::BindableMesh& mesh, MC::GFX::Texture& texture) { +void render(const MC::GFX::BindableMesh& mesh, const MC::GFX::Texture& texture) { texture.bind(); mesh.bind(); glDrawElements(GL_TRIANGLES, mesh.size(), GL_UNSIGNED_INT, nullptr); @@ -127,7 +127,7 @@ void process_input(MC::GFX::Window& window, MC::GFX::Camera& camera) { auto r = window.mouse_delta(); - auto key = [&](int key) -> float { return window.key(key, GLFW_PRESS); }; + auto key = [&](int k) -> float { return window.key(k, GLFW_PRESS); }; float x = key(GLFW_KEY_D) - key(GLFW_KEY_A); float y = key(GLFW_KEY_SPACE) - key(GLFW_KEY_LEFT_SHIFT); @@ -150,7 +150,7 @@ void setup_gl() { } } -void fix_macos_render(MC::GFX::Window& window) { +void fix_macos_render(const MC::GFX::Window& window) { static bool moved = false; if(!moved) { |
