diff --git a/CMakeLists.txt b/CMakeLists.txt index 9d7faaf..bfe05ea 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,7 @@ cmake_minimum_required(VERSION 3.22) project(perlin-shadows) -set(CMAKE_CXX_STANDARD 14) +set(CMAKE_CXX_STANDARD 23) include_directories(src) diff --git a/src/camera.h b/src/camera.h index 6b8c39d..fd56ca0 100644 --- a/src/camera.h +++ b/src/camera.h @@ -26,16 +26,18 @@ class Camera { aspect_ = static_cast(width) / static_cast(height); } - const glm::vec3 &getPosition() const { return position_; } + [[nodiscard]] const glm::vec3 &getPosition() const { return position_; } private: - glm::vec3 look_vector() const { + [[nodiscard]] glm::vec3 look_vector() const { return rotateZ(rotateY(glm::vec3(1., 0., 0.), rotation_.y), rotation_.z); } - glm::vec3 up_vector() const { + [[nodiscard]] glm::vec3 up_vector() const { return rotateZ(rotateY(glm::vec3(0., 0., 1.), rotation_.y), rotation_.z); } - glm::vec3 normal_vector() const { return cross(up_vector(), look_vector()); } + [[nodiscard]] glm::vec3 normal_vector() const { + return cross(up_vector(), look_vector()); + } glm::vec3 position_{kGeographyShort / 2, kGeographyLong *kGeographyCountLong / 2, diff --git a/src/geography.h b/src/geography.h index 3c1bcd9..f989e4c 100644 --- a/src/geography.h +++ b/src/geography.h @@ -10,8 +10,8 @@ class Geography final : public Renderable { void Randomize(bool); - float min() const { return height_.min(); } - float max() const { return height_.max(); } + [[nodiscard]] float min() const { return height_.min(); } + [[nodiscard]] float max() const { return height_.max(); } protected: void SetData() override; diff --git a/src/grid.cpp b/src/grid.cpp index b167106..64fd085 100644 --- a/src/grid.cpp +++ b/src/grid.cpp @@ -1,7 +1,6 @@ #include "grid.h" #include -#include #include #include "constants.h" @@ -59,10 +58,10 @@ glm::vec3 Grid::normal_at(const size_t x, const size_t y, const auto x_diff = glm::vec3(static_cast(high_x - low_x), 0, - amplification * (get(high_x, y) - get(low_x, y))); + amplification * ((*this)[high_x, y] - (*this)[low_x, y])); const auto y_diff = glm::vec3(0, static_cast(high_y - low_y), - amplification * (get(x, high_y) - get(x, low_y))); + amplification * ((*this)[x, high_y] - (*this)[x, low_y])); return normalize(cross(x_diff, y_diff)); } @@ -136,12 +135,11 @@ Grid *Grid::PerlinNoise(const int globalX, const int globalY, // Interpolates between the dot_major results of the four grid vectors // around the current point - const auto val = Interpolate( + (*grid)[x, y] = Interpolate( x_offset, Interpolate(y_offset, dot_major(false, false), dot_major(false, true)), Interpolate(y_offset, dot_major(true, false), dot_major(true, true))); - grid->set(x, y, val); } } diff --git a/src/grid.h b/src/grid.h index 851f641..e7607ca 100644 --- a/src/grid.h +++ b/src/grid.h @@ -17,33 +17,37 @@ constexpr std::size_t index(const std::size_t x, const std::size_t y) { class Grid { public: - float get(const std::size_t x, const std::size_t y) const { + float &operator[](const std::size_t x, const std::size_t y) { return (*data_)[index(x, y)]; } - void set(const std::size_t x, const std::size_t y, const float value) const { - (*data_)[index(x, y)] = value; + float &operator[](const std::size_t x, const std::size_t y) const { + return (*data_)[index(x, y)]; } - Grid operator+(const Grid &) const; + [[nodiscard]] Grid operator+(const Grid &) const; void operator+=(const Grid &) const; - Grid operator-() const; - Grid operator-(const Grid &) const; + [[nodiscard]] Grid operator-() const; + [[nodiscard]] Grid operator-(const Grid &) const; void operator-=(const Grid &) const; - Grid operator*(float) const; + [[nodiscard]] Grid operator*(float) const; void operator*=(float) const; - Grid operator/(float) const; + [[nodiscard]] Grid operator/(float) const; void operator/=(float) const; - glm::vec3 normal_at(std::size_t, std::size_t, float = 1.) const; + [[nodiscard]] glm::vec3 normal_at(std::size_t, std::size_t, float = 1.) const; - static Grid *PerlinNoise(int, int, std::size_t); + [[nodiscard]] static Grid *PerlinNoise(int, int, std::size_t); - std::array *vertices() const; - static std::array *indices(); + [[nodiscard]] std::array *vertices() const; + [[nodiscard]] static std::array *indices(); static void RandomizeBase() { base_random_ = device_() << 4; } - float min() const { return *std::min_element(data_->begin(), data_->end()); } - float max() const { return *std::max_element(data_->begin(), data_->end()); } + [[nodiscard]] float min() const { + return *std::min_element(data_->begin(), data_->end()); + } + [[nodiscard]] float max() const { + return *std::max_element(data_->begin(), data_->end()); + } private: std::unique_ptr> data_{ @@ -54,6 +58,6 @@ class Grid { std::uniform_real_distribution dist_{0, glm::two_pi()}; static std::mt19937::result_type base_random_; - float RandomAngle() { return dist_(engine_); } + [[nodiscard]] float RandomAngle() { return dist_(engine_); } void AngleSeed(const std::mt19937::result_type seed) { engine_.seed(seed); } }; diff --git a/src/noise_math.h b/src/noise_math.h index eb3003d..b5d1d08 100644 --- a/src/noise_math.h +++ b/src/noise_math.h @@ -3,7 +3,7 @@ // Based on Ken Perlin's smoother step function: // https://en.wikipedia.org/wiki/Smoothstep#Variations Returns value in range // [0, 1] -constexpr float SmootherStep(const float x) { +[[nodiscard]] constexpr float SmootherStep(const float x) { if (x < 0) { return 0; } @@ -17,7 +17,7 @@ constexpr float SmootherStep(const float x) { // Interpolates between two doubles // If x <= 0 returns bound_0, if x >= 1 returns bound_1, // otherwise smoothly transitions between the two -constexpr float Interpolate(const float x, const float bound_0, - const float bound_1) { +[[nodiscard]] constexpr float Interpolate(const float x, const float bound_0, + const float bound_1) { return bound_0 + SmootherStep(x) * (bound_1 - bound_0); } diff --git a/src/point_light.in.h b/src/point_light.in.h index d04de69..ceee2d7 100644 --- a/src/point_light.in.h +++ b/src/point_light.in.h @@ -16,7 +16,7 @@ class PointLight final : public Renderable { void LoadData(const Shader *) const; void GenerateCubeMaps(const std::vector &) const; - GLuint getDepthTexture() const { return depth_; } + [[nodiscard]] GLuint getDepthTexture() const { return depth_; } void setPosition(const glm::vec3 &pos) { pos_ = pos; CleanUp(); diff --git a/src/shader.cpp b/src/shader.cpp index 1a3d507..d773af5 100644 --- a/src/shader.cpp +++ b/src/shader.cpp @@ -1,6 +1,5 @@ #include "shader.h" -#include #include #include diff --git a/src/shader.h b/src/shader.h index 49554c0..7ff02d3 100644 --- a/src/shader.h +++ b/src/shader.h @@ -15,22 +15,26 @@ class Shader { Shader(const std::string &, const std::string &, const std::string & = ""); ~Shader(); - bool CopyDataToUniform(const glm::mat4 &, const std::string &) const; - bool CopyDataToUniform(int, const glm::mat4 *, const std::string &) const; - bool CopyDataToUniform(const glm::vec4 &, const std::string &) const; - bool CopyDataToUniform(const glm::vec3 &, const std::string &) const; - bool CopyDataToUniform(float, const std::string &) const; - bool CopyDataToUniform(int, const std::string &) const; - bool CopyDataToUniform(bool, const std::string &) const; + [[nodiscard]] bool CopyDataToUniform(const glm::mat4 &, + const std::string &) const; + [[nodiscard]] bool CopyDataToUniform(int, const glm::mat4 *, + const std::string &) const; + [[nodiscard]] bool CopyDataToUniform(const glm::vec4 &, + const std::string &) const; + [[nodiscard]] bool CopyDataToUniform(const glm::vec3 &, + const std::string &) const; + [[nodiscard]] bool CopyDataToUniform(float, const std::string &) const; + [[nodiscard]] bool CopyDataToUniform(int, const std::string &) const; + [[nodiscard]] bool CopyDataToUniform(bool, const std::string &) const; void PrintStatus() const; - GLuint id() const { return id_; } + [[nodiscard]] GLuint id() const { return id_; } private: GLuint id_; - static GLuint CompileShader(const std::string &, GLenum); + [[nodiscard]] static GLuint CompileShader(const std::string &, GLenum); static void CheckGLError(const std::string &); static void CheckProgramivError(GLuint, GLenum, const std::string &);