From 2ce6365b3fbaf78e7598d9f1b1c320a7ce9f73b3 Mon Sep 17 00:00:00 2001 From: Michael Bradley Date: Tue, 25 Feb 2025 22:59:15 -0500 Subject: [PATCH 1/4] Update standard to C++23 --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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) From 618b75792b13623988f0dea61dedbb4d9a5e67d2 Mon Sep 17 00:00:00 2001 From: Michael Bradley Date: Fri, 28 Feb 2025 19:09:25 -0500 Subject: [PATCH 2/4] Remove unused `#include`s --- src/grid.cpp | 1 - src/shader.cpp | 1 - 2 files changed, 2 deletions(-) diff --git a/src/grid.cpp b/src/grid.cpp index b167106..473dc52 100644 --- a/src/grid.cpp +++ b/src/grid.cpp @@ -1,7 +1,6 @@ #include "grid.h" #include -#include #include #include "constants.h" 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 From b564918e1c44dd54e3de51e8fbf5f5fad559b3b7 Mon Sep 17 00:00:00 2001 From: Michael Bradley Date: Fri, 28 Feb 2025 19:29:27 -0500 Subject: [PATCH 3/4] Use multidimensional subscript operator in Grid --- src/grid.cpp | 7 +++---- src/grid.h | 6 +++--- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/grid.cpp b/src/grid.cpp index 473dc52..64fd085 100644 --- a/src/grid.cpp +++ b/src/grid.cpp @@ -58,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)); } @@ -135,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..5d94c9e 100644 --- a/src/grid.h +++ b/src/grid.h @@ -17,11 +17,11 @@ 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; From 9a7969b63a37ddfa1a35ba738b9a783aa5a4932d Mon Sep 17 00:00:00 2001 From: Michael Bradley Date: Fri, 28 Feb 2025 20:46:42 -0500 Subject: [PATCH 4/4] Incorporate clangd recommendations --- src/camera.h | 10 ++++++---- src/geography.h | 4 ++-- src/grid.h | 28 ++++++++++++++++------------ src/noise_math.h | 6 +++--- src/point_light.in.h | 2 +- src/shader.h | 22 +++++++++++++--------- 6 files changed, 41 insertions(+), 31 deletions(-) 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.h b/src/grid.h index 5d94c9e..e7607ca 100644 --- a/src/grid.h +++ b/src/grid.h @@ -24,26 +24,30 @@ class Grid { 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.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 &);