From 6a001c4840140e71ba1672a4576d52d1a62c0922 Mon Sep 17 00:00:00 2001 From: tylermurphy534 Date: Sat, 1 Oct 2022 14:35:16 -0400 Subject: rename src files, move .o to /bin --- Makefile | 14 +- engine/xe_render_system.cpp | 5 +- engine/xe_render_system.hpp | 7 +- src/PerlinNoise.hpp | 659 ----------------------------------- src/chunk.hpp | 2 +- src/chunk_noise.hpp | 659 +++++++++++++++++++++++++++++++++++ src/chunk_renderer.cpp | 41 +++ src/chunk_renderer.hpp | 37 ++ src/first_app.cpp | 95 ----- src/first_app.hpp | 46 --- src/keyboard_movement_controller.cpp | 43 --- src/keyboard_movement_controller.hpp | 43 --- src/main.cpp | 4 +- src/minecraft.cpp | 95 +++++ src/minecraft.hpp | 46 +++ src/player_controller.cpp | 43 +++ src/player_controller.hpp | 43 +++ src/simple_renderer.cpp | 41 --- src/simple_renderer.hpp | 37 -- 19 files changed, 978 insertions(+), 982 deletions(-) delete mode 100644 src/PerlinNoise.hpp create mode 100644 src/chunk_noise.hpp create mode 100644 src/chunk_renderer.cpp create mode 100644 src/chunk_renderer.hpp delete mode 100755 src/first_app.cpp delete mode 100755 src/first_app.hpp delete mode 100644 src/keyboard_movement_controller.cpp delete mode 100644 src/keyboard_movement_controller.hpp create mode 100755 src/minecraft.cpp create mode 100755 src/minecraft.hpp create mode 100644 src/player_controller.cpp create mode 100644 src/player_controller.hpp delete mode 100644 src/simple_renderer.cpp delete mode 100644 src/simple_renderer.hpp diff --git a/Makefile b/Makefile index 3288181..adfd289 100644 --- a/Makefile +++ b/Makefile @@ -19,10 +19,10 @@ LDFLAGS += -lalut LDFLAGS += -lvulkan LDFLAGS += $(INCFLAGS) +BIN = bin SRC = $(shell find src -name "*.cpp") SRC += $(shell find engine -name "*.cpp") -OBJ = $(SRC:.cpp=.o) -BIN = bin +OBJ = $(SRC:%.cpp=$(BIN)/%.o) VERTSRC = $(shell find ./res/shaders -type f -name "*.vert") VERTOBJ = $(patsubst %.vert, %.vert.spv, $(VERTSRC)) @@ -35,7 +35,8 @@ all: dirs shader build dirs: mkdir -p ./$(BIN) - + mkdir -p ./$(BIN)/src + mkdir -p ./$(BIN)/engine shader: $(VERTOBJ) $(FRAGOBJ) @@ -48,11 +49,10 @@ build: dirs shader ${OBJ} %.spv: % glslc -o $@ $< -%.o: %.cpp +$(BIN)/%.o: %.cpp $(CC) -o $@ -c $< $(CCFLAGS) clean: rm -rf app - rm -rf $(BIN) $(OBJ) - rm -rf res/shaders/*.spv - rm -rf lib/glfw/CMakeCache.txt + rm -rf $(BIN) + rm -rf res/shaders/*.spv \ No newline at end of file diff --git a/engine/xe_render_system.cpp b/engine/xe_render_system.cpp index 770bfd5..ff745e6 100644 --- a/engine/xe_render_system.cpp +++ b/engine/xe_render_system.cpp @@ -3,7 +3,6 @@ namespace xe { RenderSystem::RenderSystem( - Engine &xeEngine, std::string vert, std::string frag, std::map uniformBindings, @@ -13,8 +12,8 @@ RenderSystem::RenderSystem( bool cullingEnabled, std::vector attributeDescptions, uint32_t vertexSize -) : xeDevice{xeEngine.xeDevice}, - xeRenderer{xeEngine.xeRenderer}, +) : xeDevice{Engine::getInstance()->xeDevice}, + xeRenderer{Engine::getInstance()->xeRenderer}, pushCunstantDataSize{pushCunstantDataSize}, uniformBindings{uniformBindings}, imageBindings{imageBindings}, diff --git a/engine/xe_render_system.hpp b/engine/xe_render_system.hpp index bbf96f8..a07b5c0 100644 --- a/engine/xe_render_system.hpp +++ b/engine/xe_render_system.hpp @@ -22,7 +22,7 @@ class RenderSystem { class Builder { public: - Builder(Engine &xeEngine, std::string vert, std::string frag) : xeEngine{xeEngine}, vert{vert}, frag{frag} {} + Builder(std::string vert, std::string frag) : vert{vert}, frag{frag} {} Builder& addVertexBindingf(uint32_t binding, uint32_t dimension, uint32_t offset){ if(dimension == 1) @@ -75,7 +75,7 @@ class RenderSystem { } std::unique_ptr build() { - return std::make_unique(xeEngine, std::move(vert), std::move(frag), std::move(uniformBindings), std::move(imageBindings), std::move(imageArrayBindings), std::move(pushCunstantDataSize), std::move(cullingEnabled), std::move(attributeDescptions), std::move(vertexSize)); + return std::make_unique(std::move(vert), std::move(frag), std::move(uniformBindings), std::move(imageBindings), std::move(imageArrayBindings), std::move(pushCunstantDataSize), std::move(cullingEnabled), std::move(attributeDescptions), std::move(vertexSize)); } private: @@ -92,12 +92,9 @@ class RenderSystem { std::string frag; bool cullingEnabled{false}; - - Engine &xeEngine; }; RenderSystem( - Engine &xeEngine, std::string vert, std::string frag, std::map uniformBindings, diff --git a/src/PerlinNoise.hpp b/src/PerlinNoise.hpp deleted file mode 100644 index 91e29c5..0000000 --- a/src/PerlinNoise.hpp +++ /dev/null @@ -1,659 +0,0 @@ -//---------------------------------------------------------------------------------------- -// -// siv::PerlinNoise -// Perlin noise library for modern C++ -// -// Copyright (C) 2013-2021 Ryo Suzuki -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files(the "Software"), to deal -// in the Software without restriction, including without limitation the rights -// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell -// copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions : -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -// THE SOFTWARE. -// -//---------------------------------------------------------------------------------------- - -# pragma once -# include -# include -# include -# include -# include -# include -# include - -# if __has_include() && defined(__cpp_concepts) -# include -# endif - - -// Library major version -# define SIVPERLIN_VERSION_MAJOR 3 - -// Library minor version -# define SIVPERLIN_VERSION_MINOR 0 - -// Library revision version -# define SIVPERLIN_VERSION_REVISION 0 - -// Library version -# define SIVPERLIN_VERSION ((SIVPERLIN_VERSION_MAJOR * 100 * 100) + (SIVPERLIN_VERSION_MINOR * 100) + (SIVPERLIN_VERSION_REVISION)) - - -// [[nodiscard]] for constructors -# if (201907L <= __has_cpp_attribute(nodiscard)) -# define SIVPERLIN_NODISCARD_CXX20 [[nodiscard]] -# else -# define SIVPERLIN_NODISCARD_CXX20 -# endif - - -// std::uniform_random_bit_generator concept -# if __cpp_lib_concepts -# define SIVPERLIN_CONCEPT_URBG template -# define SIVPERLIN_CONCEPT_URBG_ template -# else -# define SIVPERLIN_CONCEPT_URBG template , std::is_unsigned>>>* = nullptr> -# define SIVPERLIN_CONCEPT_URBG_ template , std::is_unsigned>>>*> -# endif - - -// arbitrary value for increasing entropy -# ifndef SIVPERLIN_DEFAULT_Y -# define SIVPERLIN_DEFAULT_Y (0.12345) -# endif - -// arbitrary value for increasing entropy -# ifndef SIVPERLIN_DEFAULT_Z -# define SIVPERLIN_DEFAULT_Z (0.34567) -# endif - - -namespace app -{ - template - class BasicPerlinNoise - { - public: - - static_assert(std::is_floating_point_v); - - /////////////////////////////////////// - // - // Typedefs - // - - using state_type = std::array; - - using value_type = Float; - - using default_random_engine = std::mt19937; - - using seed_type = typename default_random_engine::result_type; - - /////////////////////////////////////// - // - // Constructors - // - - SIVPERLIN_NODISCARD_CXX20 - constexpr BasicPerlinNoise() noexcept; - - SIVPERLIN_NODISCARD_CXX20 - explicit BasicPerlinNoise(seed_type seed); - - SIVPERLIN_CONCEPT_URBG - SIVPERLIN_NODISCARD_CXX20 - explicit BasicPerlinNoise(URBG&& urbg); - - /////////////////////////////////////// - // - // Reseed - // - - void reseed(seed_type seed); - - SIVPERLIN_CONCEPT_URBG - void reseed(URBG&& urbg); - - /////////////////////////////////////// - // - // Serialization - // - - [[nodiscard]] - constexpr const state_type& serialize() const noexcept; - - constexpr void deserialize(const state_type& state) noexcept; - - /////////////////////////////////////// - // - // Noise (The result is in the range [-1, 1]) - // - - [[nodiscard]] - value_type noise1D(value_type x) const noexcept; - - [[nodiscard]] - value_type noise2D(value_type x, value_type y) const noexcept; - - [[nodiscard]] - value_type noise3D(value_type x, value_type y, value_type z) const noexcept; - - /////////////////////////////////////// - // - // Noise (The result is remapped to the range [0, 1]) - // - - [[nodiscard]] - value_type noise1D_01(value_type x) const noexcept; - - [[nodiscard]] - value_type noise2D_01(value_type x, value_type y) const noexcept; - - [[nodiscard]] - value_type noise3D_01(value_type x, value_type y, value_type z) const noexcept; - - /////////////////////////////////////// - // - // Octave noise (The result can be out of the range [-1, 1]) - // - - [[nodiscard]] - value_type octave1D(value_type x, std::int32_t octaves, value_type persistence = value_type(0.5)) const noexcept; - - [[nodiscard]] - value_type octave2D(value_type x, value_type y, std::int32_t octaves, value_type persistence = value_type(0.5)) const noexcept; - - [[nodiscard]] - value_type octave3D(value_type x, value_type y, value_type z, std::int32_t octaves, value_type persistence = value_type(0.5)) const noexcept; - - /////////////////////////////////////// - // - // Octave noise (The result is clamped to the range [-1, 1]) - // - - [[nodiscard]] - value_type octave1D_11(value_type x, std::int32_t octaves, value_type persistence = value_type(0.5)) const noexcept; - - [[nodiscard]] - value_type octave2D_11(value_type x, value_type y, std::int32_t octaves, value_type persistence = value_type(0.5)) const noexcept; - - [[nodiscard]] - value_type octave3D_11(value_type x, value_type y, value_type z, std::int32_t octaves, value_type persistence = value_type(0.5)) const noexcept; - - /////////////////////////////////////// - // - // Octave noise (The result is clamped and remapped to the range [0, 1]) - // - - [[nodiscard]] - value_type octave1D_01(value_type x, std::int32_t octaves, value_type persistence = value_type(0.5)) const noexcept; - - [[nodiscard]] - value_type octave2D_01(value_type x, value_type y, std::int32_t octaves, value_type persistence = value_type(0.5)) const noexcept; - - [[nodiscard]] - value_type octave3D_01(value_type x, value_type y, value_type z, std::int32_t octaves, value_type persistence = value_type(0.5)) const noexcept; - - /////////////////////////////////////// - // - // Octave noise (The result is normalized to the range [-1, 1]) - // - - [[nodiscard]] - value_type normalizedOctave1D(value_type x, std::int32_t octaves, value_type persistence = value_type(0.5)) const noexcept; - - [[nodiscard]] - value_type normalizedOctave2D(value_type x, value_type y, std::int32_t octaves, value_type persistence = value_type(0.5)) const noexcept; - - [[nodiscard]] - value_type normalizedOctave3D(value_type x, value_type y, value_type z, std::int32_t octaves, value_type persistence = value_type(0.5)) const noexcept; - - /////////////////////////////////////// - // - // Octave noise (The result is normalized and remapped to the range [0, 1]) - // - - [[nodiscard]] - value_type normalizedOctave1D_01(value_type x, std::int32_t octaves, value_type persistence = value_type(0.5)) const noexcept; - - [[nodiscard]] - value_type normalizedOctave2D_01(value_type x, value_type y, std::int32_t octaves, value_type persistence = value_type(0.5)) const noexcept; - - [[nodiscard]] - value_type normalizedOctave3D_01(value_type x, value_type y, value_type z, std::int32_t octaves, value_type persistence = value_type(0.5)) const noexcept; - - private: - - state_type m_permutation; - }; - - using PerlinNoise = BasicPerlinNoise; - - namespace perlin_detail - { - //////////////////////////////////////////////// - // - // These functions are provided for consistency. - // You may get different results from std::shuffle() with different standard library implementations. - // - SIVPERLIN_CONCEPT_URBG - [[nodiscard]] - inline std::uint64_t Random(const std::uint64_t max, URBG&& urbg) - { - return (urbg() % (max + 1)); - } - - template - inline void Shuffle(RandomIt first, RandomIt last, URBG&& urbg) - { - if (first == last) - { - return; - } - - using difference_type = typename std::iterator_traits::difference_type; - - for (RandomIt it = first + 1; it < last; ++it) - { - const std::uint64_t n = static_cast(it - first); - std::iter_swap(it, first + static_cast(Random(n, std::forward(urbg)))); - } - } - // - //////////////////////////////////////////////// - - template - [[nodiscard]] - inline constexpr Float Fade(const Float t) noexcept - { - return t * t * t * (t * (t * 6 - 15) + 10); - } - - template - [[nodiscard]] - inline constexpr Float Lerp(const Float a, const Float b, const Float t) noexcept - { - return (a + (b - a) * t); - } - - template - [[nodiscard]] - inline constexpr Float Grad(const std::uint8_t hash, const Float x, const Float y, const Float z) noexcept - { - const std::uint8_t h = hash & 15; - const Float u = h < 8 ? x : y; - const Float v = h < 4 ? y : h == 12 || h == 14 ? x : z; - return ((h & 1) == 0 ? u : -u) + ((h & 2) == 0 ? v : -v); - } - - template - [[nodiscard]] - inline constexpr Float Remap_01(const Float x) noexcept - { - return (x * Float(0.5) + Float(0.5)); - } - - template - [[nodiscard]] - inline constexpr Float Clamp_11(const Float x) noexcept - { - return std::clamp(x, Float(-1.0), Float(1.0)); - } - - template - [[nodiscard]] - inline constexpr Float RemapClamp_01(const Float x) noexcept - { - if (x <= Float(-1.0)) - { - return Float(0.0); - } - else if (Float(1.0) <= x) - { - return Float(1.0); - } - - return (x * Float(0.5) + Float(0.5)); - } - - template - [[nodiscard]] - inline auto Octave1D(const Noise& noise, Float x, const std::int32_t octaves, const Float persistence) noexcept - { - using value_type = Float; - value_type result = 0; - value_type amplitude = 1; - - for (std::int32_t i = 0; i < octaves; ++i) - { - result += (noise.noise1D(x) * amplitude); - x *= 2; - amplitude *= persistence; - } - - return result; - } - - template - [[nodiscard]] - inline auto Octave2D(const Noise& noise, Float x, Float y, const std::int32_t octaves, const Float persistence) noexcept - { - using value_type = Float; - value_type result = 0; - value_type amplitude = 1; - - for (std::int32_t i = 0; i < octaves; ++i) - { - result += (noise.noise2D(x, y) * amplitude); - x *= 2; - y *= 2; - amplitude *= persistence; - } - - return result; - } - - template - [[nodiscard]] - inline auto Octave3D(const Noise& noise, Float x, Float y, Float z, const std::int32_t octaves, const Float persistence) noexcept - { - using value_type = Float; - value_type result = 0; - value_type amplitude = 1; - - for (std::int32_t i = 0; i < octaves; ++i) - { - result += (noise.noise3D(x, y, z) * amplitude); - x *= 2; - y *= 2; - z *= 2; - amplitude *= persistence; - } - - return result; - } - - template - [[nodiscard]] - inline constexpr Float MaxAmplitude(const std::int32_t octaves, const Float persistence) noexcept - { - using value_type = Float; - value_type result = 0; - value_type amplitude = 1; - - for (std::int32_t i = 0; i < octaves; ++i) - { - result += amplitude; - amplitude *= persistence; - } - - return result; - } - } - - /////////////////////////////////////// - - template - inline constexpr BasicPerlinNoise::BasicPerlinNoise() noexcept - : m_permutation{ 151,160,137,91,90,15, - 131,13,201,95,96,53,194,233,7,225,140,36,103,30,69,142,8,99,37,240,21,10,23, - 190, 6,148,247,120,234,75,0,26,197,62,94,252,219,203,117,35,11,32,57,177,33, - 88,237,149,56,87,174,20,125,136,171,168, 68,175,74,165,71,134,139,48,27,166, - 77,146,158,231,83,111,229,122,60,211,133,230,220,105,92,41,55,46,245,40,244, - 102,143,54, 65,25,63,161, 1,216,80,73,209,76,132,187,208, 89,18,169,200,196, - 135,130,116,188,159,86,164,100,109,198,173,186, 3,64,52,217,226,250,124,123, - 5,202,38,147,118,126,255,82,85,212,207,206,59,227,47,16,58,17,182,189,28,42, - 223,183,170,213,119,248,152, 2,44,154,163, 70,221,153,101,155,167, 43,172,9, - 129,22,39,253, 19,98,108,110,79,113,224,232,178,185, 112,104,218,246,97,228, - 251,34,242,193,238,210,144,12,191,179,162,241, 81,51,145,235,249,14,239,107, - 49,192,214, 31,181,199,106,157,184, 84,204,176,115,121,50,45,127, 4,150,254, - 138,236,205,93,222,114,67,29,24,72,243,141,128,195,78,66,215,61,156,180 } {} - - template - inline BasicPerlinNoise::BasicPerlinNoise(const seed_type seed) - { - reseed(seed); - } - - template - SIVPERLIN_CONCEPT_URBG_ - inline BasicPerlinNoise::BasicPerlinNoise(URBG&& urbg) - { - reseed(std::forward(urbg)); - } - - /////////////////////////////////////// - - template - inline void BasicPerlinNoise::reseed(const seed_type seed) - { - reseed(default_random_engine{ seed }); - } - - template - SIVPERLIN_CONCEPT_URBG_ - inline void BasicPerlinNoise::reseed(URBG&& urbg) - { - std::iota(m_permutation.begin(), m_permutation.end(), uint8_t{ 0 }); - - perlin_detail::Shuffle(m_permutation.begin(), m_permutation.end(), std::forward(urbg)); - } - - /////////////////////////////////////// - - template - inline constexpr const typename BasicPerlinNoise::state_type& BasicPerlinNoise::serialize() const noexcept - { - return m_permutation; - } - - template - inline constexpr void BasicPerlinNoise::deserialize(const state_type& state) noexcept - { - m_permutation = state; - } - - /////////////////////////////////////// - - template - inline typename BasicPerlinNoise::value_type BasicPerlinNoise::noise1D(const value_type x) const noexcept - { - return noise3D(x, - static_cast(SIVPERLIN_DEFAULT_Y), - static_cast(SIVPERLIN_DEFAULT_Z)); - } - - template - inline typename BasicPerlinNoise::value_type BasicPerlinNoise::noise2D(const value_type x, const value_type y) const noexcept - { - return noise3D(x, - y, - static_cast(SIVPERLIN_DEFAULT_Z)); - } - - template - inline typename BasicPerlinNoise::value_type BasicPerlinNoise::noise3D(const value_type x, const value_type y, const value_type z) const noexcept - { - const value_type _x = std::floor(x); - const value_type _y = std::floor(y); - const value_type _z = std::floor(z); - - const std::int32_t ix = static_cast(_x) & 255; - const std::int32_t iy = static_cast(_y) & 255; - const std::int32_t iz = static_cast(_z) & 255; - - const value_type fx = (x - _x); - const value_type fy = (y - _y); - const value_type fz = (z - _z); - - const value_type u = perlin_detail::Fade(fx); - const value_type v = perlin_detail::Fade(fy); - const value_type w = perlin_detail::Fade(fz); - - const std::uint8_t A = (m_permutation[ix & 255] + iy) & 255; - const std::uint8_t B = (m_permutation[(ix + 1) & 255] + iy) & 255; - - const std::uint8_t AA = (m_permutation[A] + iz) & 255; - const std::uint8_t AB = (m_permutation[(A + 1) & 255] + iz) & 255; - - const std::uint8_t BA = (m_permutation[B] + iz) & 255; - const std::uint8_t BB = (m_permutation[(B + 1) & 255] + iz) & 255; - - const value_type p0 = perlin_detail::Grad(m_permutation[AA], fx, fy, fz); - const value_type p1 = perlin_detail::Grad(m_permutation[BA], fx - 1, fy, fz); - const value_type p2 = perlin_detail::Grad(m_permutation[AB], fx, fy - 1, fz); - const value_type p3 = perlin_detail::Grad(m_permutation[BB], fx - 1, fy - 1, fz); - const value_type p4 = perlin_detail::Grad(m_permutation[(AA + 1) & 255], fx, fy, fz - 1); - const value_type p5 = perlin_detail::Grad(m_permutation[(BA + 1) & 255], fx - 1, fy, fz - 1); - const value_type p6 = perlin_detail::Grad(m_permutation[(AB + 1) & 255], fx, fy - 1, fz - 1); - const value_type p7 = perlin_detail::Grad(m_permutation[(BB + 1) & 255], fx - 1, fy - 1, fz - 1); - - const value_type q0 = perlin_detail::Lerp(p0, p1, u); - const value_type q1 = perlin_detail::Lerp(p2, p3, u); - const value_type q2 = perlin_detail::Lerp(p4, p5, u); - const value_type q3 = perlin_detail::Lerp(p6, p7, u); - - const value_type r0 = perlin_detail::Lerp(q0, q1, v); - const value_type r1 = perlin_detail::Lerp(q2, q3, v); - - return perlin_detail::Lerp(r0, r1, w); - } - - /////////////////////////////////////// - - template - inline typename BasicPerlinNoise::value_type BasicPerlinNoise::noise1D_01(const value_type x) const noexcept - { - return perlin_detail::Remap_01(noise1D(x)); - } - - template - inline typename BasicPerlinNoise::value_type BasicPerlinNoise::noise2D_01(const value_type x, const value_type y) const noexcept - { - return perlin_detail::Remap_01(noise2D(x, y)); - } - - template - inline typename BasicPerlinNoise::value_type BasicPerlinNoise::noise3D_01(const value_type x, const value_type y, const value_type z) const noexcept - { - return perlin_detail::Remap_01(noise3D(x, y, z)); - } - - /////////////////////////////////////// - - template - inline typename BasicPerlinNoise::value_type BasicPerlinNoise::octave1D(const value_type x, const std::int32_t octaves, const value_type persistence) const noexcept - { - return perlin_detail::Octave1D(*this, x, octaves, persistence); - } - - template - inline typename BasicPerlinNoise::value_type BasicPerlinNoise::octave2D(const value_type x, const value_type y, const std::int32_t octaves, const value_type persistence) const noexcept - { - return perlin_detail::Octave2D(*this, x, y, octaves, persistence); - } - - template - inline typename BasicPerlinNoise::value_type BasicPerlinNoise::octave3D(const value_type x, const value_type y, const value_type z, const std::int32_t octaves, const value_type persistence) const noexcept - { - return perlin_detail::Octave3D(*this, x, y, z, octaves, persistence); - } - - /////////////////////////////////////// - - template - inline typename BasicPerlinNoise::value_type BasicPerlinNoise::octave1D_11(const value_type x, const std::int32_t octaves, const value_type persistence) const noexcept - { - return perlin_detail::Clamp_11(octave1D(x, octaves, persistence)); - } - - template - inline typename BasicPerlinNoise::value_type BasicPerlinNoise::octave2D_11(const value_type x, const value_type y, const std::int32_t octaves, const value_type persistence) const noexcept - { - return perlin_detail::Clamp_11(octave2D(x, y, octaves, persistence)); - } - - template - inline typename BasicPerlinNoise::value_type BasicPerlinNoise::octave3D_11(const value_type x, const value_type y, const value_type z, const std::int32_t octaves, const value_type persistence) const noexcept - { - return perlin_detail::Clamp_11(octave3D(x, y, z, octaves, persistence)); - } - - /////////////////////////////////////// - - template - inline typename BasicPerlinNoise::value_type BasicPerlinNoise::octave1D_01(const value_type x, const std::int32_t octaves, const value_type persistence) const noexcept - { - return perlin_detail::RemapClamp_01(octave1D(x, octaves, persistence)); - } - - template - inline typename BasicPerlinNoise::value_type BasicPerlinNoise::octave2D_01(const value_type x, const value_type y, const std::int32_t octaves, const value_type persistence) const noexcept - { - return perlin_detail::RemapClamp_01(octave2D(x, y, octaves, persistence)); - } - - template - inline typename BasicPerlinNoise::value_type BasicPerlinNoise::octave3D_01(const value_type x, const value_type y, const value_type z, const std::int32_t octaves, const value_type persistence) const noexcept - { - return perlin_detail::RemapClamp_01(octave3D(x, y, z, octaves, persistence)); - } - - /////////////////////////////////////// - - template - inline typename BasicPerlinNoise::value_type BasicPerlinNoise::normalizedOctave1D(const value_type x, const std::int32_t octaves, const value_type persistence) const noexcept - { - return (octave1D(x, octaves, persistence) / perlin_detail::MaxAmplitude(octaves, persistence)); - } - - template - inline typename BasicPerlinNoise::value_type BasicPerlinNoise::normalizedOctave2D(const value_type x, const value_type y, const std::int32_t octaves, const value_type persistence) const noexcept - { - return (octave2D(x, y, octaves, persistence) / perlin_detail::MaxAmplitude(octaves, persistence)); - } - - template - inline typename BasicPerlinNoise::value_type BasicPerlinNoise::normalizedOctave3D(const value_type x, const value_type y, const value_type z, const std::int32_t octaves, const value_type persistence) const noexcept - { - return (octave3D(x, y, z, octaves, persistence) / perlin_detail::MaxAmplitude(octaves, persistence)); - } - - /////////////////////////////////////// - - template - inline typename BasicPerlinNoise::value_type BasicPerlinNoise::normalizedOctave1D_01(const value_type x, const std::int32_t octaves, const value_type persistence) const noexcept - { - return perlin_detail::Remap_01(normalizedOctave1D(x, octaves, persistence)); - } - - template - inline typename BasicPerlinNoise::value_type BasicPerlinNoise::normalizedOctave2D_01(const value_type x, const value_type y, const std::int32_t octaves, const value_type persistence) const noexcept - { - return perlin_detail::Remap_01(normalizedOctave2D(x, y, octaves, persistence)); - } - - template - inline typename BasicPerlinNoise::value_type BasicPerlinNoise::normalizedOctave3D_01(const value_type x, const value_type y, const value_type z, const std::int32_t octaves, const value_type persistence) const noexcept - { - return perlin_detail::Remap_01(normalizedOctave3D(x, y, z, octaves, persistence)); - } -} - -# undef SIVPERLIN_NODISCARD_CXX20 -# undef SIVPERLIN_CONCEPT_URBG -# undef SIVPERLIN_CONCEPT_URBG_ diff --git a/src/chunk.hpp b/src/chunk.hpp index 59264fd..6223fc7 100644 --- a/src/chunk.hpp +++ b/src/chunk.hpp @@ -4,7 +4,7 @@ #include "xe_engine.hpp" #include "xe_image.hpp" -#include "PerlinNoise.hpp" +#include "chunk_noise.hpp" #include #include diff --git a/src/chunk_noise.hpp b/src/chunk_noise.hpp new file mode 100644 index 0000000..91e29c5 --- /dev/null +++ b/src/chunk_noise.hpp @@ -0,0 +1,659 @@ +//---------------------------------------------------------------------------------------- +// +// siv::PerlinNoise +// Perlin noise library for modern C++ +// +// Copyright (C) 2013-2021 Ryo Suzuki +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files(the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions : +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +// +//---------------------------------------------------------------------------------------- + +# pragma once +# include +# include +# include +# include +# include +# include +# include + +# if __has_include() && defined(__cpp_concepts) +# include +# endif + + +// Library major version +# define SIVPERLIN_VERSION_MAJOR 3 + +// Library minor version +# define SIVPERLIN_VERSION_MINOR 0 + +// Library revision version +# define SIVPERLIN_VERSION_REVISION 0 + +// Library version +# define SIVPERLIN_VERSION ((SIVPERLIN_VERSION_MAJOR * 100 * 100) + (SIVPERLIN_VERSION_MINOR * 100) + (SIVPERLIN_VERSION_REVISION)) + + +// [[nodiscard]] for constructors +# if (201907L <= __has_cpp_attribute(nodiscard)) +# define SIVPERLIN_NODISCARD_CXX20 [[nodiscard]] +# else +# define SIVPERLIN_NODISCARD_CXX20 +# endif + + +// std::uniform_random_bit_generator concept +# if __cpp_lib_concepts +# define SIVPERLIN_CONCEPT_URBG template +# define SIVPERLIN_CONCEPT_URBG_ template +# else +# define SIVPERLIN_CONCEPT_URBG template , std::is_unsigned>>>* = nullptr> +# define SIVPERLIN_CONCEPT_URBG_ template , std::is_unsigned>>>*> +# endif + + +// arbitrary value for increasing entropy +# ifndef SIVPERLIN_DEFAULT_Y +# define SIVPERLIN_DEFAULT_Y (0.12345) +# endif + +// arbitrary value for increasing entropy +# ifndef SIVPERLIN_DEFAULT_Z +# define SIVPERLIN_DEFAULT_Z (0.34567) +# endif + + +namespace app +{ + template + class BasicPerlinNoise + { + public: + + static_assert(std::is_floating_point_v); + + /////////////////////////////////////// + // + // Typedefs + // + + using state_type = std::array; + + using value_type = Float; + + using default_random_engine = std::mt19937; + + using seed_type = typename default_random_engine::result_type; + + /////////////////////////////////////// + // + // Constructors + // + + SIVPERLIN_NODISCARD_CXX20 + constexpr BasicPerlinNoise() noexcept; + + SIVPERLIN_NODISCARD_CXX20 + explicit BasicPerlinNoise(seed_type seed); + + SIVPERLIN_CONCEPT_URBG + SIVPERLIN_NODISCARD_CXX20 + explicit BasicPerlinNoise(URBG&& urbg); + + /////////////////////////////////////// + // + // Reseed + // + + void reseed(seed_type seed); + + SIVPERLIN_CONCEPT_URBG + void reseed(URBG&& urbg); + + /////////////////////////////////////// + // + // Serialization + // + + [[nodiscard]] + constexpr const state_type& serialize() const noexcept; + + constexpr void deserialize(const state_type& state) noexcept; + + /////////////////////////////////////// + // + // Noise (The result is in the range [-1, 1]) + // + + [[nodiscard]] + value_type noise1D(value_type x) const noexcept; + + [[nodiscard]] + value_type noise2D(value_type x, value_type y) const noexcept; + + [[nodiscard]] + value_type noise3D(value_type x, value_type y, value_type z) const noexcept; + + /////////////////////////////////////// + // + // Noise (The result is remapped to the range [0, 1]) + // + + [[nodiscard]] + value_type noise1D_01(value_type x) const noexcept; + + [[nodiscard]] + value_type noise2D_01(value_type x, value_type y) const noexcept; + + [[nodiscard]] + value_type noise3D_01(value_type x, value_type y, value_type z) const noexcept; + + /////////////////////////////////////// + // + // Octave noise (The result can be out of the range [-1, 1]) + // + + [[nodiscard]] + value_type octave1D(value_type x, std::int32_t octaves, value_type persistence = value_type(0.5)) const noexcept; + + [[nodiscard]] + value_type octave2D(value_type x, value_type y, std::int32_t octaves, value_type persistence = value_type(0.5)) const noexcept; + + [[nodiscard]] + value_type octave3D(value_type x, value_type y, value_type z, std::int32_t octaves, value_type persistence = value_type(0.5)) const noexcept; + + /////////////////////////////////////// + // + // Octave noise (The result is clamped to the range [-1, 1]) + // + + [[nodiscard]] + value_type octave1D_11(value_type x, std::int32_t octaves, value_type persistence = value_type(0.5)) const noexcept; + + [[nodiscard]] + value_type octave2D_11(value_type x, value_type y, std::int32_t octaves, value_type persistence = value_type(0.5)) const noexcept; + + [[nodiscard]] + value_type octave3D_11(value_type x, value_type y, value_type z, std::int32_t octaves, value_type persistence = value_type(0.5)) const noexcept; + + /////////////////////////////////////// + // + // Octave noise (The result is clamped and remapped to the range [0, 1]) + // + + [[nodiscard]] + value_type octave1D_01(value_type x, std::int32_t octaves, value_type persistence = value_type(0.5)) const noexcept; + + [[nodiscard]] + value_type octave2D_01(value_type x, value_type y, std::int32_t octaves, value_type persistence = value_type(0.5)) const noexcept; + + [[nodiscard]] + value_type octave3D_01(value_type x, value_type y, value_type z, std::int32_t octaves, value_type persistence = value_type(0.5)) const noexcept; + + /////////////////////////////////////// + // + // Octave noise (The result is normalized to the range [-1, 1]) + // + + [[nodiscard]] + value_type normalizedOctave1D(value_type x, std::int32_t octaves, value_type persistence = value_type(0.5)) const noexcept; + + [[nodiscard]] + value_type normalizedOctave2D(value_type x, value_type y, std::int32_t octaves, value_type persistence = value_type(0.5)) const noexcept; + + [[nodiscard]] + value_type normalizedOctave3D(value_type x, value_type y, value_type z, std::int32_t octaves, value_type persistence = value_type(0.5)) const noexcept; + + /////////////////////////////////////// + // + // Octave noise (The result is normalized and remapped to the range [0, 1]) + // + + [[nodiscard]] + value_type normalizedOctave1D_01(value_type x, std::int32_t octaves, value_type persistence = value_type(0.5)) const noexcept; + + [[nodiscard]] + value_type normalizedOctave2D_01(value_type x, value_type y, std::int32_t octaves, value_type persistence = value_type(0.5)) const noexcept; + + [[nodiscard]] + value_type normalizedOctave3D_01(value_type x, value_type y, value_type z, std::int32_t octaves, value_type persistence = value_type(0.5)) const noexcept; + + private: + + state_type m_permutation; + }; + + using PerlinNoise = BasicPerlinNoise; + + namespace perlin_detail + { + //////////////////////////////////////////////// + // + // These functions are provided for consistency. + // You may get different results from std::shuffle() with different standard library implementations. + // + SIVPERLIN_CONCEPT_URBG + [[nodiscard]] + inline std::uint64_t Random(const std::uint64_t max, URBG&& urbg) + { + return (urbg() % (max + 1)); + } + + template + inline void Shuffle(RandomIt first, RandomIt last, URBG&& urbg) + { + if (first == last) + { + return; + } + + using difference_type = typename std::iterator_traits::difference_type; + + for (RandomIt it = first + 1; it < last; ++it) + { + const std::uint64_t n = static_cast(it - first); + std::iter_swap(it, first + static_cast(Random(n, std::forward(urbg)))); + } + } + // + //////////////////////////////////////////////// + + template + [[nodiscard]] + inline constexpr Float Fade(const Float t) noexcept + { + return t * t * t * (t * (t * 6 - 15) + 10); + } + + template + [[nodiscard]] + inline constexpr Float Lerp(const Float a, const Float b, const Float t) noexcept + { + return (a + (b - a) * t); + } + + template + [[nodiscard]] + inline constexpr Float Grad(const std::uint8_t hash, const Float x, const Float y, const Float z) noexcept + { + const std::uint8_t h = hash & 15; + const Float u = h < 8 ? x : y; + const Float v = h < 4 ? y : h == 12 || h == 14 ? x : z; + return ((h & 1) == 0 ? u : -u) + ((h & 2) == 0 ? v : -v); + } + + template + [[nodiscard]] + inline constexpr Float Remap_01(const Float x) noexcept + { + return (x * Float(0.5) + Float(0.5)); + } + + template + [[nodiscard]] + inline constexpr Float Clamp_11(const Float x) noexcept + { + return std::clamp(x, Float(-1.0), Float(1.0)); + } + + template + [[nodiscard]] + inline constexpr Float RemapClamp_01(const Float x) noexcept + { + if (x <= Float(-1.0)) + { + return Float(0.0); + } + else if (Float(1.0) <= x) + { + return Float(1.0); + } + + return (x * Float(0.5) + Float(0.5)); + } + + template + [[nodiscard]] + inline auto Octave1D(const Noise& noise, Float x, const std::int32_t octaves, const Float persistence) noexcept + { + using value_type = Float; + value_type result = 0; + value_type amplitude = 1; + + for (std::int32_t i = 0; i < octaves; ++i) + { + result += (noise.noise1D(x) * amplitude); + x *= 2; + amplitude *= persistence; + } + + return result; + } + + template + [[nodiscard]] + inline auto Octave2D(const Noise& noise, Float x, Float y, const std::int32_t octaves, const Float persistence) noexcept + { + using value_type = Float; + value_type result = 0; + value_type amplitude = 1; + + for (std::int32_t i = 0; i < octaves; ++i) + { + result += (noise.noise2D(x, y) * amplitude); + x *= 2; + y *= 2; + amplitude *= persistence; + } + + return result; + } + + template + [[nodiscard]] + inline auto Octave3D(const Noise& noise, Float x, Float y, Float z, const std::int32_t octaves, const Float persistence) noexcept + { + using value_type = Float; + value_type result = 0; + value_type amplitude = 1; + + for (std::int32_t i = 0; i < octaves; ++i) + { + result += (noise.noise3D(x, y, z) * amplitude); + x *= 2; + y *= 2; + z *= 2; + amplitude *= persistence; + } + + return result; + } + + template + [[nodiscard]] + inline constexpr Float MaxAmplitude(const std::int32_t octaves, const Float persistence) noexcept + { + using value_type = Float; + value_type result = 0; + value_type amplitude = 1; + + for (std::int32_t i = 0; i < octaves; ++i) + { + result += amplitude; + amplitude *= persistence; + } + + return result; + } + } + + /////////////////////////////////////// + + template + inline constexpr BasicPerlinNoise::BasicPerlinNoise() noexcept + : m_permutation{ 151,160,137,91,90,15, + 131,13,201,95,96,53,194,233,7,225,140,36,103,30,69,142,8,99,37,240,21,10,23, + 190, 6,148,247,120,234,75,0,26,197,62,94,252,219,203,117,35,11,32,57,177,33, + 88,237,149,56,87,174,20,125,136,171,168, 68,175,74,165,71,134,139,48,27,166, + 77,146,158,231,83,111,229,122,60,211,133,230,220,105,92,41,55,46,245,40,244, + 102,143,54, 65,25,63,161, 1,216,80,73,209,76,132,187,208, 89,18,169,200,196, + 135,130,116,188,159,86,164,100,109,198,173,186, 3,64,52,217,226,250,124,123, + 5,202,38,147,118,126,255,82,85,212,207,206,59,227,47,16,58,17,182,189,28,42, + 223,183,170,213,119,248,152, 2,44,154,163, 70,221,153,101,155,167, 43,172,9, + 129,22,39,253, 19,98,108,110,79,113,224,232,178,185, 112,104,218,246,97,228, + 251,34,242,193,238,210,144,12,191,179,162,241, 81,51,145,235,249,14,239,107, + 49,192,214, 31,181,199,106,157,184, 84,204,176,115,121,50,45,127, 4,150,254, + 138,236,205,93,222,114,67,29,24,72,243,141,128,195,78,66,215,61,156,180 } {} + + template + inline BasicPerlinNoise::BasicPerlinNoise(const seed_type seed) + { + reseed(seed); + } + + template + SIVPERLIN_CONCEPT_URBG_ + inline BasicPerlinNoise::BasicPerlinNoise(URBG&& urbg) + { + reseed(std::forward(urbg)); + } + + /////////////////////////////////////// + + template + inline void BasicPerlinNoise::reseed(const seed_type seed) + { + reseed(default_random_engine{ seed }); + } + + template + SIVPERLIN_CONCEPT_URBG_ + inline void BasicPerlinNoise::reseed(URBG&& urbg) + { + std::iota(m_permutation.begin(), m_permutation.end(), uint8_t{ 0 }); + + perlin_detail::Shuffle(m_permutation.begin(), m_permutation.end(), std::forward(urbg)); + } + + /////////////////////////////////////// + + template + inline constexpr const typename BasicPerlinNoise::state_type& BasicPerlinNoise::serialize() const noexcept + { + return m_permutation; + } + + template + inline constexpr void BasicPerlinNoise::deserialize(const state_type& state) noexcept + { + m_permutation = state; + } + + /////////////////////////////////////// + + template + inline typename BasicPerlinNoise::value_type BasicPerlinNoise::noise1D(const value_type x) const noexcept + { + return noise3D(x, + static_cast(SIVPERLIN_DEFAULT_Y), + static_cast(SIVPERLIN_DEFAULT_Z)); + } + + template + inline typename BasicPerlinNoise::value_type BasicPerlinNoise::noise2D(const value_type x, const value_type y) const noexcept + { + return noise3D(x, + y, + static_cast(SIVPERLIN_DEFAULT_Z)); + } + + template + inline typename BasicPerlinNoise::value_type BasicPerlinNoise::noise3D(const value_type x, const value_type y, const value_type z) const noexcept + { + const value_type _x = std::floor(x); + const value_type _y = std::floor(y); + const value_type _z = std::floor(z); + + const std::int32_t ix = static_cast(_x) & 255; + const std::int32_t iy = static_cast(_y) & 255; + const std::int32_t iz = static_cast(_z) & 255; + + const value_type fx = (x - _x); + const value_type fy = (y - _y); + const value_type fz = (z - _z); + + const value_type u = perlin_detail::Fade(fx); + const value_type v = perlin_detail::Fade(fy); + const value_type w = perlin_detail::Fade(fz); + + const std::uint8_t A = (m_permutation[ix & 255] + iy) & 255; + const std::uint8_t B = (m_permutation[(ix + 1) & 255] + iy) & 255; + + const std::uint8_t AA = (m_permutation[A] + iz) & 255; + const std::uint8_t AB = (m_permutation[(A + 1) & 255] + iz) & 255; + + const std::uint8_t BA = (m_permutation[B] + iz) & 255; + const std::uint8_t BB = (m_permutation[(B + 1) & 255] + iz) & 255; + + const value_type p0 = perlin_detail::Grad(m_permutation[AA], fx, fy, fz); + const value_type p1 = perlin_detail::Grad(m_permutation[BA], fx - 1, fy, fz); + const value_type p2 = perlin_detail::Grad(m_permutation[AB], fx, fy - 1, fz); + const value_type p3 = perlin_detail::Grad(m_permutation[BB], fx - 1, fy - 1, fz); + const value_type p4 = perlin_detail::Grad(m_permutation[(AA + 1) & 255], fx, fy, fz - 1); + const value_type p5 = perlin_detail::Grad(m_permutation[(BA + 1) & 255], fx - 1, fy, fz - 1); + const value_type p6 = perlin_detail::Grad(m_permutation[(AB + 1) & 255], fx, fy - 1, fz - 1); + const value_type p7 = perlin_detail::Grad(m_permutation[(BB + 1) & 255], fx - 1, fy - 1, fz - 1); + + const value_type q0 = perlin_detail::Lerp(p0, p1, u); + const value_type q1 = perlin_detail::Lerp(p2, p3, u); + const value_type q2 = perlin_detail::Lerp(p4, p5, u); + const value_type q3 = perlin_detail::Lerp(p6, p7, u); + + const value_type r0 = perlin_detail::Lerp(q0, q1, v); + const value_type r1 = perlin_detail::Lerp(q2, q3, v); + + return perlin_detail::Lerp(r0, r1, w); + } + + /////////////////////////////////////// + + template + inline typename BasicPerlinNoise::value_type BasicPerlinNoise::noise1D_01(const value_type x) const noexcept + { + return perlin_detail::Remap_01(noise1D(x)); + } + + template + inline typename BasicPerlinNoise::value_type BasicPerlinNoise::noise2D_01(const value_type x, const value_type y) const noexcept + { + return perlin_detail::Remap_01(noise2D(x, y)); + } + + template + inline typename BasicPerlinNoise::value_type BasicPerlinNoise::noise3D_01(const value_type x, const value_type y, const value_type z) const noexcept + { + return perlin_detail::Remap_01(noise3D(x, y, z)); + } + + /////////////////////////////////////// + + template + inline typename BasicPerlinNoise::value_type BasicPerlinNoise::octave1D(const value_type x, const std::int32_t octaves, const value_type persistence) const noexcept + { + return perlin_detail::Octave1D(*this, x, octaves, persistence); + } + + template + inline typename BasicPerlinNoise::value_type BasicPerlinNoise::octave2D(const value_type x, const value_type y, const std::int32_t octaves, const value_type persistence) const noexcept + { + return perlin_detail::Octave2D(*this, x, y, octaves, persistence); + } + + template + inline typename BasicPerlinNoise::value_type BasicPerlinNoise::octave3D(const value_type x, const value_type y, const value_type z, const std::int32_t octaves, const value_type persistence) const noexcept + { + return perlin_detail::Octave3D(*this, x, y, z, octaves, persistence); + } + + /////////////////////////////////////// + + template + inline typename BasicPerlinNoise::value_type BasicPerlinNoise::octave1D_11(const value_type x, const std::int32_t octaves, const value_type persistence) const noexcept + { + return perlin_detail::Clamp_11(octave1D(x, octaves, persistence)); + } + + template + inline typename BasicPerlinNoise::value_type BasicPerlinNoise::octave2D_11(const value_type x, const value_type y, const std::int32_t octaves, const value_type persistence) const noexcept + { + return perlin_detail::Clamp_11(octave2D(x, y, octaves, persistence)); + } + + template + inline typename BasicPerlinNoise::value_type BasicPerlinNoise::octave3D_11(const value_type x, const value_type y, const value_type z, const std::int32_t octaves, const value_type persistence) const noexcept + { + return perlin_detail::Clamp_11(octave3D(x, y, z, octaves, persistence)); + } + + /////////////////////////////////////// + + template + inline typename BasicPerlinNoise::value_type BasicPerlinNoise::octave1D_01(const value_type x, const std::int32_t octaves, const value_type persistence) const noexcept + { + return perlin_detail::RemapClamp_01(octave1D(x, octaves, persistence)); + } + + template + inline typename BasicPerlinNoise::value_type BasicPerlinNoise::octave2D_01(const value_type x, const value_type y, const std::int32_t octaves, const value_type persistence) const noexcept + { + return perlin_detail::RemapClamp_01(octave2D(x, y, octaves, persistence)); + } + + template + inline typename BasicPerlinNoise::value_type BasicPerlinNoise::octave3D_01(const value_type x, const value_type y, const value_type z, const std::int32_t octaves, const value_type persistence) const noexcept + { + return perlin_detail::RemapClamp_01(octave3D(x, y, z, octaves, persistence)); + } + + /////////////////////////////////////// + + template + inline typename BasicPerlinNoise::value_type BasicPerlinNoise::normalizedOctave1D(const value_type x, const std::int32_t octaves, const value_type persistence) const noexcept + { + return (octave1D(x, octaves, persistence) / perlin_detail::MaxAmplitude(octaves, persistence)); + } + + template + inline typename BasicPerlinNoise::value_type BasicPerlinNoise::normalizedOctave2D(const value_type x, const value_type y, const std::int32_t octaves, const value_type persistence) const noexcept + { + return (octave2D(x, y, octaves, persistence) / perlin_detail::MaxAmplitude(octaves, persistence)); + } + + template + inline typename BasicPerlinNoise::value_type BasicPerlinNoise::normalizedOctave3D(const value_type x, const value_type y, const value_type z, const std::int32_t octaves, const value_type persistence) const noexcept + { + return (octave3D(x, y, z, octaves, persistence) / perlin_detail::MaxAmplitude(octaves, persistence)); + } + + /////////////////////////////////////// + + template + inline typename BasicPerlinNoise::value_type BasicPerlinNoise::normalizedOctave1D_01(const value_type x, const std::int32_t octaves, const value_type persistence) const noexcept + { + return perlin_detail::Remap_01(normalizedOctave1D(x, octaves, persistence)); + } + + template + inline typename BasicPerlinNoise::value_type BasicPerlinNoise::normalizedOctave2D_01(const value_type x, const value_type y, const std::int32_t octaves, const value_type persistence) const noexcept + { + return perlin_detail::Remap_01(normalizedOctave2D(x, y, octaves, persistence)); + } + + template + inline typename BasicPerlinNoise::value_type BasicPerlinNoise::normalizedOctave3D_01(const value_type x, const value_type y, const value_type z, const std::int32_t octaves, const value_type persistence) const noexcept + { + return perlin_detail::Remap_01(normalizedOctave3D(x, y, z, octaves, persistence)); + } +} + +# undef SIVPERLIN_NODISCARD_CXX20 +# undef SIVPERLIN_CONCEPT_URBG +# undef SIVPERLIN_CONCEPT_URBG_ diff --git a/src/chunk_renderer.cpp b/src/chunk_renderer.cpp new file mode 100644 index 0000000..2dba6cb --- /dev/null +++ b/src/chunk_renderer.cpp @@ -0,0 +1,41 @@ +#include "chunk_renderer.hpp" +#include "chunk.hpp" + +namespace app { + +ChunkRenderer::ChunkRenderer(std::vector &images) { + xeRenderSystem = xe::RenderSystem::Builder("res/shaders/simple_shader.vert.spv", "res/shaders/simple_shader.frag.spv") + .addVertexBindingf(0, 3, 0) // position + .addVertexBindingf(1, 3, 12) // normal + .addVertexBindingf(2, 2, 24) // uvs + .addVertexBindingi(3, 1, 32) // texture + .setVertexSize(36) + .addPushConstant(sizeof(PushConstant)) + .addUniformBinding(0, sizeof(UniformBuffer)) + .addTextureArrayBinding(1, images) + .setCulling(true) + .build(); +} + +void ChunkRenderer::render(std::vector &gameObjects, xe::Camera &xeCamera) { + + xeRenderSystem->start(); + + UniformBuffer ubo{}; + ubo.projectionView = xeCamera.getProjection() * xeCamera.getView(); + xeRenderSystem->loadUniformObject(0, &ubo); + + for(auto &obj : gameObjects) { + if(obj.model == nullptr) continue; + PushConstant pc{}; + pc.modelMatrix = obj.transform.mat4(); + pc.normalMatrix = obj.transform.normalMatrix(); + xeRenderSystem->loadPushConstant(&pc); + xeRenderSystem->render(obj); + } + + xeRenderSystem->stop(); + +} + +} \ No newline at end of file diff --git a/src/chunk_renderer.hpp b/src/chunk_renderer.hpp new file mode 100644 index 0000000..e539f29 --- /dev/null +++ b/src/chunk_renderer.hpp @@ -0,0 +1,37 @@ +#pragma once + +#include "xe_render_system.hpp" + +#include + +namespace app { + +struct UniformBuffer { + alignas(16) glm::mat4 projectionView{1.f}; + alignas(4) glm::vec3 lightDirection = glm::normalize(glm::vec3{-1.f, 3.f, 1.f}); +}; + +struct PushConstant { + alignas(16) glm::mat4 modelMatrix{1.f}; + alignas(16) glm::mat4 normalMatrix{1.f}; +}; + +class ChunkRenderer { + + public: + + ChunkRenderer(std::vector &images); + + ~ChunkRenderer() {}; + + ChunkRenderer(const ChunkRenderer&) = delete; + ChunkRenderer operator=(const ChunkRenderer&) = delete; + + void render(std::vector &gameObjects, xe::Camera &xeCamera); + + private: + std::unique_ptr xeRenderSystem; + +}; + +} \ No newline at end of file diff --git a/src/first_app.cpp b/src/first_app.cpp deleted file mode 100755 index f6a9d57..0000000 --- a/src/first_app.cpp +++ /dev/null @@ -1,95 +0,0 @@ -#include "first_app.hpp" - -#include -using namespace std::chrono; - -namespace app { - -FirstApp::FirstApp() : xeEngine{WIDTH, HEIGHT, "Minecraft Vulkan", "res/image/icon.png"} {}; - -FirstApp::~FirstApp() {} - -void FirstApp::run() { - - Chunk::load(); - - auto viewerObject = xe::GameObject::createGameObject(); - viewerObject.transform.translation = {0.f, 40.f, 0.f}; - viewerObject.transform.rotation.y = glm::radians(45.f); - - createGameObjects(viewerObject); - - SimpleRenderer renderer{xeEngine, Chunk::getTextures()}; - - xe::Sound sound{"res/sound/when_the_world_ends.wav"}; - sound.setLooping(true); - sound.play(); - - KeyboardMovementController cameraController{xeEngine.getInput(), viewerObject}; - - while (xeEngine.poll()) { - - float frameTime = xeEngine.getFrameTime(); - - cameraController.update(frameTime); - xeEngine.getCamera().setViewYXZ(viewerObject.transform.translation, viewerObject.transform.rotation); - - if(xeEngine.beginFrame()) { - renderer.render(loadedChunks, xeEngine.getCamera()); - xeEngine.endFrame(); - } - - reloadLoadedChunks(viewerObject); - - } - - xeEngine.close(); - - Chunk::unload(); - -} - -void FirstApp::createGameObjects(xe::GameObject& viewer) { - int width = 2*RENDER_DISTANCE+1; - loadedChunks.clear(); - for(int32_t x = 0; x < width; x++) { - for(int32_t z = 0; z < width; z++) { - auto gameObject = xe::GameObject::createGameObject(); - gameObject.transform.translation = glm::vec3(0.f); - loadedChunks.push_back(std::move(gameObject)); - } - } -} - -void FirstApp::reloadLoadedChunks(xe::GameObject& viewer) { - viewX = static_cast(floor(viewer.transform.translation.x / Chunk::CHUNK_SIZE.x)); - viewZ = static_cast(floor(viewer.transform.translation.z / Chunk::CHUNK_SIZE.z)); - int width = 2*RENDER_DISTANCE+1; - int minX = viewX - RENDER_DISTANCE; - int minZ = viewZ - RENDER_DISTANCE; - int maxX = viewX + RENDER_DISTANCE; - int maxZ = viewZ + RENDER_DISTANCE; - for(int32_t x = 0; x < width; x++) { - for(int32_t z = 0; z < width; z++) { - auto& gameObject = loadedChunks[x + z * width]; - int gridX = static_cast(floor(gameObject.transform.translation.x / Chunk::CHUNK_SIZE.x)); - int gridZ = static_cast(floor(gameObject.transform.translation.z / Chunk::CHUNK_SIZE.z)); - int newGridX = minX + x; - int newGridZ = minZ + z; - if(gridX < minX || gridZ < minZ || gridX > maxX || gridZ > maxZ) - Chunk::deleteChunk(gridX, gridZ); - Chunk* chunk = Chunk::getChunk(newGridX, newGridZ); - if(chunk == nullptr) { - chunk = Chunk::newChunk(newGridX, newGridZ, 12345); - Chunk::generateAsync(chunk); - } - if(chunk->getMesh() == nullptr){ - Chunk::createMeshAsync(chunk); - } - gameObject.model = chunk->getMesh(); - gameObject.transform.translation = glm::vec3(newGridX * Chunk::CHUNK_SIZE.x, 0, newGridZ * Chunk::CHUNK_SIZE.z); - } - } -} - -} \ No newline at end of file diff --git a/src/first_app.hpp b/src/first_app.hpp deleted file mode 100755 index d361296..0000000 --- a/src/first_app.hpp +++ /dev/null @@ -1,46 +0,0 @@ -#pragma once - -#include "xe_engine.hpp" - -#include "keyboard_movement_controller.hpp" -#include "simple_renderer.hpp" -#include "chunk.hpp" - -#define GLM_FORCE_RADIANS -#define GLM_FORCE_DEPTH_ZERO_TO_ONE -#include -#include -#include -#include -#include -#include -#include - -namespace app { -class FirstApp { - public: - - FirstApp(); - ~FirstApp(); - - FirstApp(const FirstApp &) = delete; - FirstApp operator=(const FirstApp &) = delete; - - void run(); - - private: - - static constexpr int WIDTH = 800; - static constexpr int HEIGHT = 600; - static constexpr int RENDER_DISTANCE = 10; - - void createGameObjects(xe::GameObject& viewer); - void reloadLoadedChunks(xe::GameObject& viewer); - - - int viewX, viewZ; - - xe::Engine xeEngine; - std::vector loadedChunks; -}; -} \ No newline at end of file diff --git a/src/keyboard_movement_controller.cpp b/src/keyboard_movement_controller.cpp deleted file mode 100644 index c2e6005..0000000 --- a/src/keyboard_movement_controller.cpp +++ /dev/null @@ -1,43 +0,0 @@ -#include "keyboard_movement_controller.hpp" - -namespace app { - -KeyboardMovementController::KeyboardMovementController(xe::Input &input, xe::GameObject &viewerObject) - : input{input}, viewerObject{viewerObject} {}; - -KeyboardMovementController::~KeyboardMovementController() {}; - -void KeyboardMovementController::update(float dt) { - glm::vec3 rotate{0}; - if(input.isKeyPressed(keys.lookRight)) rotate.y += 1.f; - if(input.isKeyPressed(keys.lookLeft)) rotate.y -= 1.f; - if(input.isKeyPressed(keys.lookUp)) rotate.x -= 1.f; - if(input.isKeyPressed(keys.lookDown)) rotate.x += 1.f; - - if (glm::dot(rotate, rotate) > std::numeric_limits::epsilon()) { - viewerObject.transform.rotation += lookSpeed * dt * glm::normalize(rotate); - } - - viewerObject.transform.rotation.x = glm::clamp(viewerObject.transform.rotation.x, -1.5f, 1.5f); - viewerObject.transform.rotation.y = glm::mod(viewerObject.transform.rotation.y, glm::two_pi()); - - float yaw = viewerObject.transform.rotation.y; - const glm::vec3 forwardDir{sin(yaw), 0.f, cos(yaw)}; - const glm::vec3 rightDir{forwardDir.z, 0.f, -forwardDir.x}; - const glm::vec3 upDir{0.f, 01.f, 0.f}; - - glm::vec3 moveDir{0}; - if(input.isKeyPressed(keys.moveForward)) moveDir += forwardDir; - if(input.isKeyPressed(keys.moveBackward)) moveDir -= forwardDir; - if(input.isKeyPressed(keys.moveRight)) moveDir += rightDir; - if(input.isKeyPressed(keys.moveLeft)) moveDir -= rightDir; - if(input.isKeyPressed(keys.moveUp)) moveDir += upDir; - if(input.isKeyPressed(keys.moveDown)) moveDir -= upDir; - - if (glm::dot(moveDir, moveDir) > std::numeric_limits::epsilon()) { - viewerObject.transform.translation += moveSpeed * dt * glm::normalize(moveDir); - } - -} - -} \ No newline at end of file diff --git a/src/keyboard_movement_controller.hpp b/src/keyboard_movement_controller.hpp deleted file mode 100644 index ff7faf8..0000000 --- a/src/keyboard_movement_controller.hpp +++ /dev/null @@ -1,43 +0,0 @@ -#pragma once - -#include "xe_game_object.hpp" -#include "xe_input.hpp" - -#include -#include -#include -#include - -namespace app { - - class KeyboardMovementController { - - public: - - KeyboardMovementController(xe::Input &input, xe::GameObject &viewerObject); - ~KeyboardMovementController(); - - struct KeyMappings { - int moveLeft = KEY_A; - int moveRight = KEY_D; - int moveForward = KEY_W; - int moveBackward = KEY_S; - int moveUp = KEY_E; - int moveDown = KEY_Q; - int lookLeft = KEY_LEFT; - int lookRight = KEY_RIGHT; - int lookUp = KEY_UP; - int lookDown = KEY_DOWN; - }; - - void update(float dt); - - xe::Input &input; - xe::GameObject &viewerObject; - - KeyMappings keys{}; - float moveSpeed{250.f}; - float lookSpeed{1.5f}; - - }; -} \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index fa0804d..8b616af 100755 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,11 +1,11 @@ -#include "first_app.hpp" +#include "minecraft.hpp" #include #include #include int main() { - app::FirstApp app{}; + app::Minecraft app{}; try { app.run(); diff --git a/src/minecraft.cpp b/src/minecraft.cpp new file mode 100755 index 0000000..a11421d --- /dev/null +++ b/src/minecraft.cpp @@ -0,0 +1,95 @@ +#include "minecraft.hpp" + +#include +using namespace std::chrono; + +namespace app { + +Minecraft::Minecraft() : xeEngine{WIDTH, HEIGHT, "Minecraft Vulkan", "res/image/icon.png"} {}; + +Minecraft::~Minecraft() {} + +void Minecraft::run() { + + Chunk::load(); + + auto viewerObject = xe::GameObject::createGameObject(); + viewerObject.transform.translation = {0.f, 40.f, 0.f}; + viewerObject.transform.rotation.y = glm::radians(45.f); + + createGameObjects(viewerObject); + + ChunkRenderer renderer{Chunk::getTextures()}; + + xe::Sound sound{"res/sound/when_the_world_ends.wav"}; + sound.setLooping(true); + sound.play(); + + PlayerController cameraController{xeEngine.getInput(), viewerObject}; + + while (xeEngine.poll()) { + + float frameTime = xeEngine.getFrameTime(); + + cameraController.update(frameTime); + xeEngine.getCamera().setViewYXZ(viewerObject.transform.translation, viewerObject.transform.rotation); + + if(xeEngine.beginFrame()) { + renderer.render(loadedChunks, xeEngine.getCamera()); + xeEngine.endFrame(); + } + + reloadLoadedChunks(viewerObject); + + } + + xeEngine.close(); + + Chunk::unload(); + +} + +void Minecraft::createGameObjects(xe::GameObject& viewer) { + int width = 2*RENDER_DISTANCE+1; + loadedChunks.clear(); + for(int32_t x = 0; x < width; x++) { + for(int32_t z = 0; z < width; z++) { + auto gameObject = xe::GameObject::createGameObject(); + gameObject.transform.translation = glm::vec3(0.f); + loadedChunks.push_back(std::move(gameObject)); + } + } +} + +void Minecraft::reloadLoadedChunks(xe::GameObject& viewer) { + viewX = static_cast(floor(viewer.transform.translation.x / Chunk::CHUNK_SIZE.x)); + viewZ = static_cast(floor(viewer.transform.translation.z / Chunk::CHUNK_SIZE.z)); + int width = 2*RENDER_DISTANCE+1; + int minX = viewX - RENDER_DISTANCE; + int minZ = viewZ - RENDER_DISTANCE; + int maxX = viewX + RENDER_DISTANCE; + int maxZ = viewZ + RENDER_DISTANCE; + for(int32_t x = 0; x < width; x++) { + for(int32_t z = 0; z < width; z++) { + auto& gameObject = loadedChunks[x + z * width]; + int gridX = static_cast(floor(gameObject.transform.translation.x / Chunk::CHUNK_SIZE.x)); + int gridZ = static_cast(floor(gameObject.transform.translation.z / Chunk::CHUNK_SIZE.z)); + int newGridX = minX + x; + int newGridZ = minZ + z; + if(gridX < minX || gridZ < minZ || gridX > maxX || gridZ > maxZ) + Chunk::deleteChunk(gridX, gridZ); + Chunk* chunk = Chunk::getChunk(newGridX, newGridZ); + if(chunk == nullptr) { + chunk = Chunk::newChunk(newGridX, newGridZ, 12345); + Chunk::generateAsync(chunk); + } + if(chunk->getMesh() == nullptr){ + Chunk::createMeshAsync(chunk); + } + gameObject.model = chunk->getMesh(); + gameObject.transform.translation = glm::vec3(newGridX * Chunk::CHUNK_SIZE.x, 0, newGridZ * Chunk::CHUNK_SIZE.z); + } + } +} + +} \ No newline at end of file diff --git a/src/minecraft.hpp b/src/minecraft.hpp new file mode 100755 index 0000000..5639ac7 --- /dev/null +++ b/src/minecraft.hpp @@ -0,0 +1,46 @@ +#pragma once + +#include "xe_engine.hpp" + +#include "player_controller.hpp" +#include "chunk_renderer.hpp" +#include "chunk.hpp" + +#define GLM_FORCE_RADIANS +#define GLM_FORCE_DEPTH_ZERO_TO_ONE +#include +#include +#include +#include +#include +#include +#include + +namespace app { +class Minecraft { + public: + + Minecraft(); + ~Minecraft(); + + Minecraft(const Minecraft &) = delete; + Minecraft operator=(const Minecraft &) = delete; + + void run(); + + private: + + static constexpr int WIDTH = 800; + static constexpr int HEIGHT = 600; + static constexpr int RENDER_DISTANCE = 10; + + void createGameObjects(xe::GameObject& viewer); + void reloadLoadedChunks(xe::GameObject& viewer); + + + int viewX, viewZ; + + xe::Engine xeEngine; + std::vector loadedChunks; +}; +} \ No newline at end of file diff --git a/src/player_controller.cpp b/src/player_controller.cpp new file mode 100644 index 0000000..1956f8e --- /dev/null +++ b/src/player_controller.cpp @@ -0,0 +1,43 @@ +#include "player_controller.hpp" + +namespace app { + +PlayerController::PlayerController(xe::Input &input, xe::GameObject &viewerObject) + : input{input}, viewerObject{viewerObject} {}; + +PlayerController::~PlayerController() {}; + +void PlayerController::update(float dt) { + glm::vec3 rotate{0}; + if(input.isKeyPressed(keys.lookRight)) rotate.y += 1.f; + if(input.isKeyPressed(keys.lookLeft)) rotate.y -= 1.f; + if(input.isKeyPressed(keys.lookUp)) rotate.x -= 1.f; + if(input.isKeyPressed(keys.lookDown)) rotate.x += 1.f; + + if (glm::dot(rotate, rotate) > std::numeric_limits::epsilon()) { + viewerObject.transform.rotation += lookSpeed * dt * glm::normalize(rotate); + } + + viewerObject.transform.rotation.x = glm::clamp(viewerObject.transform.rotation.x, -1.5f, 1.5f); + viewerObject.transform.rotation.y = glm::mod(viewerObject.transform.rotation.y, glm::two_pi()); + + float yaw = viewerObject.transform.rotation.y; + const glm::vec3 forwardDir{sin(yaw), 0.f, cos(yaw)}; + const glm::vec3 rightDir{forwardDir.z, 0.f, -forwardDir.x}; + const glm::vec3 upDir{0.f, 01.f, 0.f}; + + glm::vec3 moveDir{0}; + if(input.isKeyPressed(keys.moveForward)) moveDir += forwardDir; + if(input.isKeyPressed(keys.moveBackward)) moveDir -= forwardDir; + if(input.isKeyPressed(keys.moveRight)) moveDir += rightDir; + if(input.isKeyPressed(keys.moveLeft)) moveDir -= rightDir; + if(input.isKeyPressed(keys.moveUp)) moveDir += upDir; + if(input.isKeyPressed(keys.moveDown)) moveDir -= upDir; + + if (glm::dot(moveDir, moveDir) > std::numeric_limits::epsilon()) { + viewerObject.transform.translation += moveSpeed * dt * glm::normalize(moveDir); + } + +} + +} \ No newline at end of file diff --git a/src/player_controller.hpp b/src/player_controller.hpp new file mode 100644 index 0000000..e34f1ed --- /dev/null +++ b/src/player_controller.hpp @@ -0,0 +1,43 @@ +#pragma once + +#include "xe_game_object.hpp" +#include "xe_input.hpp" + +#include +#include +#include +#include + +namespace app { + + class PlayerController { + + public: + + PlayerController(xe::Input &input, xe::GameObject &viewerObject); + ~PlayerController(); + + struct KeyMappings { + int moveLeft = KEY_A; + int moveRight = KEY_D; + int moveForward = KEY_W; + int moveBackward = KEY_S; + int moveUp = KEY_E; + int moveDown = KEY_Q; + int lookLeft = KEY_LEFT; + int lookRight = KEY_RIGHT; + int lookUp = KEY_UP; + int lookDown = KEY_DOWN; + }; + + void update(float dt); + + xe::Input &input; + xe::GameObject &viewerObject; + + KeyMappings keys{}; + float moveSpeed{250.f}; + float lookSpeed{1.5f}; + + }; +} \ No newline at end of file diff --git a/src/simple_renderer.cpp b/src/simple_renderer.cpp deleted file mode 100644 index 9e30fb7..0000000 --- a/src/simple_renderer.cpp +++ /dev/null @@ -1,41 +0,0 @@ -#include "simple_renderer.hpp" -#include "chunk.hpp" - -namespace app { - -SimpleRenderer::SimpleRenderer(xe::Engine &xeEngine, std::vector &images) { - xeRenderSystem = xe::RenderSystem::Builder(xeEngine, "res/shaders/simple_shader.vert.spv", "res/shaders/simple_shader.frag.spv") - .addVertexBindingf(0, 3, 0) // position - .addVertexBindingf(1, 3, 12) // normal - .addVertexBindingf(2, 2, 24) // uvs - .addVertexBindingi(3, 1, 32) // texture - .setVertexSize(36) - .addPushConstant(sizeof(PushConstant)) - .addUniformBinding(0, sizeof(UniformBuffer)) - .addTextureArrayBinding(1, images) - .setCulling(true) - .build(); -} - -void SimpleRenderer::render(std::vector &gameObjects, xe::Camera &xeCamera) { - - xeRenderSystem->start(); - - UniformBuffer ubo{}; - ubo.projectionView = xeCamera.getProjection() * xeCamera.getView(); - xeRenderSystem->loadUniformObject(0, &ubo); - - for(auto &obj : gameObjects) { - if(obj.model == nullptr) continue; - PushConstant pc{}; - pc.modelMatrix = obj.transform.mat4(); - pc.normalMatrix = obj.transform.normalMatrix(); - xeRenderSystem->loadPushConstant(&pc); - xeRenderSystem->render(obj); - } - - xeRenderSystem->stop(); - -} - -} \ No newline at end of file diff --git a/src/simple_renderer.hpp b/src/simple_renderer.hpp deleted file mode 100644 index 4db1446..0000000 --- a/src/simple_renderer.hpp +++ /dev/null @@ -1,37 +0,0 @@ -#pragma once - -#include "xe_render_system.hpp" - -#include - -namespace app { - -struct UniformBuffer { - alignas(16) glm::mat4 projectionView{1.f}; - alignas(4) glm::vec3 lightDirection = glm::normalize(glm::vec3{-1.f, 3.f, 1.f}); -}; - -struct PushConstant { - alignas(16) glm::mat4 modelMatrix{1.f}; - alignas(16) glm::mat4 normalMatrix{1.f}; -}; - -class SimpleRenderer { - - public: - - SimpleRenderer(xe::Engine &xeEngine, std::vector &images); - - ~SimpleRenderer() {}; - - SimpleRenderer(const SimpleRenderer&) = delete; - SimpleRenderer operator=(const SimpleRenderer&) = delete; - - void render(std::vector &gameObjects, xe::Camera &xeCamera); - - private: - std::unique_ptr xeRenderSystem; - -}; - -} \ No newline at end of file -- cgit v1.2.3-freya