summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authortylermurphy534 <tylermurphy534@gmail.com>2022-09-25 19:05:56 -0400
committertylermurphy534 <tylermurphy534@gmail.com>2022-09-25 19:05:56 -0400
commit29e2d9db80e31003e9d58db986ee0966ec844df8 (patch)
tree58d30f380a8ebb547a8d10e1d3e393f9a2b14926 /src
parentmerge in input (diff)
downloadminecraftvulkan-29e2d9db80e31003e9d58db986ee0966ec844df8.tar.gz
minecraftvulkan-29e2d9db80e31003e9d58db986ee0966ec844df8.tar.bz2
minecraftvulkan-29e2d9db80e31003e9d58db986ee0966ec844df8.zip
3D Chunks rendering
Diffstat (limited to '')
-rw-r--r--src/PerlinNoise.hpp659
-rw-r--r--src/chunk.cpp155
-rw-r--r--src/chunk.hpp119
-rwxr-xr-xsrc/first_app.cpp16
-rw-r--r--src/keyboard_movement_controller.cpp17
-rw-r--r--src/keyboard_movement_controller.hpp10
-rw-r--r--src/simple_renderer.cpp8
7 files changed, 971 insertions, 13 deletions
diff --git a/src/PerlinNoise.hpp b/src/PerlinNoise.hpp
new file mode 100644
index 0000000..91e29c5
--- /dev/null
+++ b/src/PerlinNoise.hpp
@@ -0,0 +1,659 @@
+//----------------------------------------------------------------------------------------
+//
+// siv::PerlinNoise
+// Perlin noise library for modern C++
+//
+// Copyright (C) 2013-2021 Ryo Suzuki <reputeless@gmail.com>
+//
+// 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 <cstdint>
+# include <algorithm>
+# include <array>
+# include <iterator>
+# include <numeric>
+# include <random>
+# include <type_traits>
+
+# if __has_include(<concepts>) && defined(__cpp_concepts)
+# include <concepts>
+# 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 <std::uniform_random_bit_generator URBG>
+# define SIVPERLIN_CONCEPT_URBG_ template <std::uniform_random_bit_generator URBG>
+# else
+# define SIVPERLIN_CONCEPT_URBG template <class URBG, std::enable_if_t<std::conjunction_v<std::is_invocable<URBG&>, std::is_unsigned<std::invoke_result_t<URBG&>>>>* = nullptr>
+# define SIVPERLIN_CONCEPT_URBG_ template <class URBG, std::enable_if_t<std::conjunction_v<std::is_invocable<URBG&>, std::is_unsigned<std::invoke_result_t<URBG&>>>>*>
+# 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 Float>
+ class BasicPerlinNoise
+ {
+ public:
+
+ static_assert(std::is_floating_point_v<Float>);
+
+ ///////////////////////////////////////
+ //
+ // Typedefs
+ //
+
+ using state_type = std::array<std::uint8_t, 256>;
+
+ 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<double>;
+
+ 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 <class RandomIt, class URBG>
+ inline void Shuffle(RandomIt first, RandomIt last, URBG&& urbg)
+ {
+ if (first == last)
+ {
+ return;
+ }
+
+ using difference_type = typename std::iterator_traits<RandomIt>::difference_type;
+
+ for (RandomIt it = first + 1; it < last; ++it)
+ {
+ const std::uint64_t n = static_cast<std::uint64_t>(it - first);
+ std::iter_swap(it, first + static_cast<difference_type>(Random(n, std::forward<URBG>(urbg))));
+ }
+ }
+ //
+ ////////////////////////////////////////////////
+
+ template <class Float>
+ [[nodiscard]]
+ inline constexpr Float Fade(const Float t) noexcept
+ {
+ return t * t * t * (t * (t * 6 - 15) + 10);
+ }
+
+ template <class Float>
+ [[nodiscard]]
+ inline constexpr Float Lerp(const Float a, const Float b, const Float t) noexcept
+ {
+ return (a + (b - a) * t);
+ }
+
+ template <class Float>
+ [[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 <class Float>
+ [[nodiscard]]
+ inline constexpr Float Remap_01(const Float x) noexcept
+ {
+ return (x * Float(0.5) + Float(0.5));
+ }
+
+ template <class Float>
+ [[nodiscard]]
+ inline constexpr Float Clamp_11(const Float x) noexcept
+ {
+ return std::clamp(x, Float(-1.0), Float(1.0));
+ }
+
+ template <class Float>
+ [[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 <class Noise, class Float>
+ [[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 <class Noise, class Float>
+ [[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 <class Noise, class Float>
+ [[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 <class Float>
+ [[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 <class Float>
+ inline constexpr BasicPerlinNoise<Float>::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 <class Float>
+ inline BasicPerlinNoise<Float>::BasicPerlinNoise(const seed_type seed)
+ {
+ reseed(seed);
+ }
+
+ template <class Float>
+ SIVPERLIN_CONCEPT_URBG_
+ inline BasicPerlinNoise<Float>::BasicPerlinNoise(URBG&& urbg)
+ {
+ reseed(std::forward<URBG>(urbg));
+ }
+
+ ///////////////////////////////////////
+
+ template <class Float>
+ inline void BasicPerlinNoise<Float>::reseed(const seed_type seed)
+ {
+ reseed(default_random_engine{ seed });
+ }
+
+ template <class Float>
+ SIVPERLIN_CONCEPT_URBG_
+ inline void BasicPerlinNoise<Float>::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>(urbg));
+ }
+
+ ///////////////////////////////////////
+
+ template <class Float>
+ inline constexpr const typename BasicPerlinNoise<Float>::state_type& BasicPerlinNoise<Float>::serialize() const noexcept
+ {
+ return m_permutation;
+ }
+
+ template <class Float>
+ inline constexpr void BasicPerlinNoise<Float>::deserialize(const state_type& state) noexcept
+ {
+ m_permutation = state;
+ }
+
+ ///////////////////////////////////////
+
+ template <class Float>
+ inline typename BasicPerlinNoise<Float>::value_type BasicPerlinNoise<Float>::noise1D(const value_type x) const noexcept
+ {
+ return noise3D(x,
+ static_cast<value_type>(SIVPERLIN_DEFAULT_Y),
+ static_cast<value_type>(SIVPERLIN_DEFAULT_Z));
+ }
+
+ template <class Float>
+ inline typename BasicPerlinNoise<Float>::value_type BasicPerlinNoise<Float>::noise2D(const value_type x, const value_type y) const noexcept
+ {
+ return noise3D(x,
+ y,
+ static_cast<value_type>(SIVPERLIN_DEFAULT_Z));
+ }
+
+ template <class Float>
+ inline typename BasicPerlinNoise<Float>::value_type BasicPerlinNoise<Float>::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<std::int32_t>(_x) & 255;
+ const std::int32_t iy = static_cast<std::int32_t>(_y) & 255;
+ const std::int32_t iz = static_cast<std::int32_t>(_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 <class Float>
+ inline typename BasicPerlinNoise<Float>::value_type BasicPerlinNoise<Float>::noise1D_01(const value_type x) const noexcept
+ {
+ return perlin_detail::Remap_01(noise1D(x));
+ }
+
+ template <class Float>
+ inline typename BasicPerlinNoise<Float>::value_type BasicPerlinNoise<Float>::noise2D_01(const value_type x, const value_type y) const noexcept
+ {
+ return perlin_detail::Remap_01(noise2D(x, y));
+ }
+
+ template <class Float>
+ inline typename BasicPerlinNoise<Float>::value_type BasicPerlinNoise<Float>::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 <class Float>
+ inline typename BasicPerlinNoise<Float>::value_type BasicPerlinNoise<Float>::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 <class Float>
+ inline typename BasicPerlinNoise<Float>::value_type BasicPerlinNoise<Float>::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 <class Float>
+ inline typename BasicPerlinNoise<Float>::value_type BasicPerlinNoise<Float>::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 <class Float>
+ inline typename BasicPerlinNoise<Float>::value_type BasicPerlinNoise<Float>::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 <class Float>
+ inline typename BasicPerlinNoise<Float>::value_type BasicPerlinNoise<Float>::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 <class Float>
+ inline typename BasicPerlinNoise<Float>::value_type BasicPerlinNoise<Float>::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 <class Float>
+ inline typename BasicPerlinNoise<Float>::value_type BasicPerlinNoise<Float>::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 <class Float>
+ inline typename BasicPerlinNoise<Float>::value_type BasicPerlinNoise<Float>::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 <class Float>
+ inline typename BasicPerlinNoise<Float>::value_type BasicPerlinNoise<Float>::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 <class Float>
+ inline typename BasicPerlinNoise<Float>::value_type BasicPerlinNoise<Float>::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 <class Float>
+ inline typename BasicPerlinNoise<Float>::value_type BasicPerlinNoise<Float>::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 <class Float>
+ inline typename BasicPerlinNoise<Float>::value_type BasicPerlinNoise<Float>::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 <class Float>
+ inline typename BasicPerlinNoise<Float>::value_type BasicPerlinNoise<Float>::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 <class Float>
+ inline typename BasicPerlinNoise<Float>::value_type BasicPerlinNoise<Float>::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 <class Float>
+ inline typename BasicPerlinNoise<Float>::value_type BasicPerlinNoise<Float>::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.cpp b/src/chunk.cpp
new file mode 100644
index 0000000..751b44e
--- /dev/null
+++ b/src/chunk.cpp
@@ -0,0 +1,155 @@
+#include "chunk.hpp"
+#include "xe_engine.hpp"
+
+namespace app {
+
+static std::map<std::string, std::unique_ptr<Chunk>> chunks{};
+
+Chunk::Chunk(uint32_t gridX, uint32_t gridZ, uint32_t world_seed)
+ : world_seed{world_seed},
+ chunk_seed{(world_seed * gridX) + (world_seed * gridZ) / 2},
+ gridX{gridX},
+ gridZ{gridZ} {
+ generate();
+}
+
+Chunk* Chunk::newChunk(uint32_t gridX, uint32_t gridZ, uint32_t world_seed) {
+ std::unique_ptr<Chunk> chunk = std::make_unique<Chunk>(gridX, gridZ, world_seed);
+ std::string key = gridX + "." + gridZ;
+ chunks[key] = std::move(chunk);
+ return chunks[key].get();
+}
+
+void Chunk::reset() {
+ chunks.clear();
+}
+
+std::shared_ptr<xe::Model> Chunk::getMesh() {
+ if(reloadRequired) {
+ delete chunkMesh.get();
+ xe::Model::Builder builder{};
+ builder.vertexData = vertexData;
+ builder.vertexSize = 44;
+ chunkMesh = std::make_shared<xe::Model>(xe::Engine::getInstance()->getDevice(), builder);
+ }
+ return chunkMesh;
+}
+
+uint8_t Chunk::getBlock(uint32_t x, uint32_t y, uint32_t z) {
+ if(y < 0 || y > 256) return AIR;
+ int chunkX = gridX;
+ int chunkZ = gridZ;
+ if(x < 0) {
+ chunkX--;
+ x = 15;
+ } else if(x > 16) {
+ chunkX ++;
+ x = 0;
+ }
+ if(z < 0) {
+ chunkZ--;
+ z = 15;
+ } else if(z > 16) {
+ chunkZ ++;
+ z = 0;
+ }
+ if(chunkX == gridX && chunkZ == gridZ) {
+ int index = (z * 16 * 256) + (y * 16) + x;
+ return blocks[index];
+ } else {
+ Chunk* chunk = getChunk(chunkX, chunkZ);
+ if(chunk == nullptr) {
+ return AIR;
+ } else {
+ int index = (z * 16 * 256) + (y * 16) + x;
+ return chunk->blocks[index];
+ }
+ }
+}
+
+void Chunk::setBlock(uint32_t x, uint32_t y, uint32_t z, uint8_t block) {
+ int index = (z * 16 * 256) + (y * 16) + x;
+ blocks[index] = block;
+}
+
+Chunk* Chunk::getChunk(uint32_t x, uint32_t z) {
+ std::string key = x + "." + z;
+ if(chunks.count(key))
+ return chunks[key].get();
+ else
+ return nullptr;
+}
+
+void Chunk::createMeshAsync() {
+ if(working) return;
+ // worker = std::thread(createMesh);
+}
+
+void Chunk::createMesh() {
+ working = true;
+ vertexData.clear();
+ for(int x=0;x<16;x++) {
+ for(int y=0; y<256; y++) {
+ for(int z=0; z<16; z++) {
+ uint8_t block = getBlock(x,y,z);
+ if(block == AIR) continue;
+ if(getBlock(x+1,y,z) == AIR) {
+ addVerticies(0, x, y, z);
+ }
+ if(getBlock(x-1,y,z) == AIR) {
+ addVerticies(1, x, y, z);
+ }
+ if(getBlock(x,y+1,z) == AIR) {
+ addVerticies(2, x, y, z);
+ }
+ if(getBlock(x,y-1,z) == AIR) {
+ addVerticies(3, x, y, z);
+ }
+ if(getBlock(x,y,z+1) == AIR) {
+ addVerticies(4, x, y, z);
+ }
+ if(getBlock(x,y,z-1) == AIR) {
+ addVerticies(5, x, y, z);
+ }
+ }
+ }
+ }
+ working = false;
+ reloadRequired = true;
+}
+
+void Chunk::addVerticies(uint32_t side, uint32_t x, uint32_t y, uint32_t z) {
+ for(int i = 0; i < 6; i ++) {
+ vertexData.push_back(px[side * 6 + i][0] + x);
+ vertexData.push_back(px[side * 6 + i][1] + y);
+ vertexData.push_back(px[side * 6 + i][2] + z);
+ vertexData.push_back(1.f);
+ vertexData.push_back(1.f);
+ vertexData.push_back(1.f);
+ vertexData.push_back(nm[side][0]);
+ vertexData.push_back(nm[side][1]);
+ vertexData.push_back(nm[side][2]);
+ vertexData.push_back(uv[i][0]);
+ vertexData.push_back(uv[i][1]);
+ }
+}
+
+void Chunk::generate() {
+ blocks.resize(16*16*256);
+
+ const PerlinNoise perlin{123};
+
+ for(int x = 0; x < 16; x++) {
+ for(int z = 0; z < 16; z++) {
+ int height = perlin.octave2D_01((x * 0.01), (z * 0.01), 4) * 5;
+ for(int y = 0; y < 256; y++) {
+ if(y <= height)
+ setBlock(x, y, z, DIRT);
+ else
+ setBlock(x, y, z, AIR);
+ }
+ }
+ }
+}
+
+} \ No newline at end of file
diff --git a/src/chunk.hpp b/src/chunk.hpp
new file mode 100644
index 0000000..5a50983
--- /dev/null
+++ b/src/chunk.hpp
@@ -0,0 +1,119 @@
+#pragma once
+
+#include "xe_model.hpp"
+#include "PerlinNoise.hpp"
+
+#include <glm/common.hpp>
+#include <glm/fwd.hpp>
+#include <vector>
+#include <thread>
+#include <memory>
+#include <string>
+#include <map>
+
+#define AIR 0
+#define DIRT 1
+
+namespace app {
+
+class Chunk {
+
+ public:
+
+ static Chunk* newChunk(uint32_t gridX, uint32_t gridZ, uint32_t world_seed);
+ static void reset();
+
+ Chunk(uint32_t gridX, uint32_t gridZ, uint32_t world_seed);
+ ~Chunk() {};
+
+ const uint32_t gridX, gridZ, world_seed, chunk_seed;
+
+ void createMesh();
+ void createMeshAsync();
+ std::shared_ptr<xe::Model> getMesh();
+
+ uint8_t getBlock(uint32_t x, uint32_t y, uint32_t z);
+ void setBlock(uint32_t x, uint32_t y, uint32_t z, uint8_t block);
+
+ static Chunk* getChunk(uint32_t x, uint32_t z);
+
+ private:
+
+ void generate();
+ void addVerticies(uint32_t side, uint32_t x, uint32_t y, uint32_t z);
+
+ bool reloadRequired{false};
+ bool working{false};
+
+ std::shared_ptr<xe::Model> chunkMesh;
+ std::vector<float> vertexData;
+ std::vector<uint8_t> blocks;
+ std::thread worker;
+
+};
+
+const float px[36][3] = {
+ // POS X
+ {0.5f,0.5f,0.5f},
+ {0.5f,-0.5f,0.5f},
+ {0.5f,-0.5f,-0.5f},
+ {0.5f,-0.5f,-0.5f},
+ {0.5f,0.5f,-0.5f},
+ {0.5f,0.5f,0.5f},
+ // NEG X
+ {-0.5f,0.5f,-0.5f},
+ {-0.5f,-0.5f,-0.5f},
+ {-0.5f,-0.5f,0.5f},
+ {-0.5f,-0.5f,0.5f},
+ {-0.5f,0.5f,0.5f},
+ {-0.5f,0.5f,-0.5f},
+ // POS Y
+ {0.5f,0.5f,-0.5f},
+ {-0.5f,0.5f,-0.5f},
+ {-0.5f,0.5f,0.5f},
+ {-0.5f,0.5f,0.5f},
+ {0.5f,0.5f,0.5f},
+ {0.5f,0.5f,-0.5f},
+ // NEG Y
+ {-0.5f,-0.5f,0.5f},
+ {-0.5f,-0.5f,-0.5f},
+ {0.5f,-0.5f,-0.5f},
+ {0.5f,-0.5f,-0.5f},
+ {0.5f,-0.5f,0.5f},
+ {-0.5f,-0.5f,0.5f},
+ // POS Z
+ {-0.5f,0.5f,0.5f},
+ {-0.5f,-0.5f,0.5f},
+ {0.5f,-0.5f,0.5f},
+ {0.5f,-0.5f,0.5f},
+ {0.5f,0.5f,0.5f},
+ {-0.5f,0.5f,0.5f},
+ // NEG Z
+ {0.5f,0.5f,-0.5f},
+ {0.5f,-0.5f,-0.5f},
+ {-0.5f,-0.5f,-0.5f},
+ {-0.5f,-0.5f,-0.5f},
+ {-0.5f,0.5f,-0.5f},
+ {0.5f,0.5f,-0.5f}
+};
+
+const float nm[6][3] = {
+ {1.f,0.f,0.f},
+ {-1.f,0.f,0.f},
+ {0.f,1.f,0.f},
+ {0.f,-1.f,0.f},
+ {0.f,0.f,1.f},
+ {0.f,0.f,-1.f}
+};
+
+
+const float uv[6][2] = {
+ {0.f,0.f},
+ {1.f,0.f},
+ {0.f,1.f},
+ {0.f,1.f},
+ {1.f,0.f},
+ {1.f,1.f}
+};
+
+} \ No newline at end of file
diff --git a/src/first_app.cpp b/src/first_app.cpp
index b09134b..b4aa3d5 100755
--- a/src/first_app.cpp
+++ b/src/first_app.cpp
@@ -1,4 +1,5 @@
#include "first_app.hpp"
+#include "chunk.hpp"
namespace app {
@@ -22,13 +23,13 @@ void FirstApp::run() {
auto viewerObject = xe::GameObject::createGameObject();
viewerObject.transform.translation = {-7.f, 3.f, -7.f};
viewerObject.transform.rotation.y = glm::radians(45.f);
- KeyboardMovementController cameraController{};
+ KeyboardMovementController cameraController{xeEngine.getInput(), viewerObject};
while (xeEngine.poll()) {
float frameTime = xeEngine.getFrameTime();
- cameraController.update(xeEngine.getInput(), viewerObject, frameTime);
+ cameraController.update(frameTime);
xeEngine.getCamera().setViewYXZ(viewerObject.transform.translation, viewerObject.transform.rotation);
if(xeEngine.beginFrame()) {
@@ -38,6 +39,8 @@ void FirstApp::run() {
}
+ Chunk::reset();
+
xeEngine.close();
}
@@ -57,6 +60,15 @@ void FirstApp::loadGameObjects() {
dragon2.transform.rotation.y = glm::radians(90.f);
dragon2.transform.scale = {.35f, .35f, .35f};
gameObjects.push_back(std::move(dragon2));
+
+ Chunk* chunk = Chunk::newChunk(0, 0, 123);
+ chunk->createMesh();
+
+ auto chunkObject = xe::GameObject::createGameObject();
+ chunkObject.model = chunk->getMesh();
+ chunkObject.transform.translation = {5.f, 5.f, 5.f};
+ gameObjects.push_back(std::move(chunkObject));
+
}
} \ No newline at end of file
diff --git a/src/keyboard_movement_controller.cpp b/src/keyboard_movement_controller.cpp
index c087ed8..c2e6005 100644
--- a/src/keyboard_movement_controller.cpp
+++ b/src/keyboard_movement_controller.cpp
@@ -2,7 +2,12 @@
namespace app {
-void KeyboardMovementController::update(xe::Input &input, xe::GameObject& gameObject, float dt) {
+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;
@@ -10,13 +15,13 @@ void KeyboardMovementController::update(xe::Input &input, xe::GameObject& gameOb
if(input.isKeyPressed(keys.lookDown)) rotate.x += 1.f;
if (glm::dot(rotate, rotate) > std::numeric_limits<float>::epsilon()) {
- gameObject.transform.rotation += lookSpeed * dt * glm::normalize(rotate);
+ viewerObject.transform.rotation += lookSpeed * dt * glm::normalize(rotate);
}
- gameObject.transform.rotation.x = glm::clamp(gameObject.transform.rotation.x, -1.5f, 1.5f);
- gameObject.transform.rotation.y = glm::mod(gameObject.transform.rotation.y, glm::two_pi<float>());
+ 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>());
- float yaw = gameObject.transform.rotation.y;
+ 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};
@@ -30,7 +35,7 @@ void KeyboardMovementController::update(xe::Input &input, xe::GameObject& gameOb
if(input.isKeyPressed(keys.moveDown)) moveDir -= upDir;
if (glm::dot(moveDir, moveDir) > std::numeric_limits<float>::epsilon()) {
- gameObject.transform.translation += moveSpeed * dt * glm::normalize(moveDir);
+ viewerObject.transform.translation += moveSpeed * dt * glm::normalize(moveDir);
}
}
diff --git a/src/keyboard_movement_controller.hpp b/src/keyboard_movement_controller.hpp
index e7fa04b..75264c0 100644
--- a/src/keyboard_movement_controller.hpp
+++ b/src/keyboard_movement_controller.hpp
@@ -9,9 +9,14 @@
#include <limits>
namespace app {
+
class KeyboardMovementController {
public:
+
+ KeyboardMovementController(xe::Input &input, xe::GameObject &viewerObject);
+ ~KeyboardMovementController();
+
struct KeyMappings {
int moveLeft = KEY_A;
int moveRight = KEY_D;
@@ -25,7 +30,10 @@ namespace app {
int lookDown = KEY_DOWN;
};
- void update(xe::Input &input, xe::GameObject& gameObject, float dt);
+ void update(float dt);
+
+ xe::Input &input;
+ xe::GameObject &viewerObject;
KeyMappings keys{};
float moveSpeed{3.f};
diff --git a/src/simple_renderer.cpp b/src/simple_renderer.cpp
index 55b52df..89d33b8 100644
--- a/src/simple_renderer.cpp
+++ b/src/simple_renderer.cpp
@@ -4,10 +4,10 @@ namespace app {
SimpleRenderer::SimpleRenderer(xe::Engine &xeEngine, xe::Image *xeImage) {
xeRenderSystem = xe::RenderSystem::Builder(xeEngine, "res/shaders/simple_shader.vert.spv", "res/shaders/simple_shader.frag.spv")
- .addVertexBinding(0, 3, 0)
- .addVertexBinding(1, 3, 12)
- .addVertexBinding(2, 3, 24)
- .addVertexBinding(3, 2, 36)
+ .addVertexBinding(0, 3, 0) // position
+ .addVertexBinding(1, 3, 12) // color
+ .addVertexBinding(2, 3, 24) // normal
+ .addVertexBinding(3, 2, 36) // uvs
.setVertexSize(sizeof(Vertex))
.addPushConstant(sizeof(PushConstant))
.addUniformBinding(0, sizeof(UniformBuffer))