| @@ -45,7 +45,8 @@ liblolcore_headers = \ | |||||
| lol/math/geometry.h lol/math/interp.h lol/math/rand.h lol/math/array2d.h \ | lol/math/geometry.h lol/math/interp.h lol/math/rand.h lol/math/array2d.h \ | ||||
| lol/math/array3d.h lol/math/constants.h lol/math/matrix.h lol/math/ops.h \ | lol/math/array3d.h lol/math/constants.h lol/math/matrix.h lol/math/ops.h \ | ||||
| lol/math/transform.h lol/math/polynomial.h \ | lol/math/transform.h lol/math/polynomial.h \ | ||||
| lol/math/noise/gradient.h lol/math/noise/simplex.h \ | |||||
| lol/math/noise/gradient.h lol/math/noise/perlin.h \ | |||||
| lol/math/noise/simplex.h \ | |||||
| \ | \ | ||||
| lol/algorithm/all.h \ | lol/algorithm/all.h \ | ||||
| lol/algorithm/sort.h lol/algorithm/portal.h lol/algorithm/aabb_tree.h \ | lol/algorithm/sort.h lol/algorithm/portal.h lol/algorithm/aabb_tree.h \ | ||||
| @@ -25,4 +25,6 @@ | |||||
| #include <lol/math/polynomial.h> | #include <lol/math/polynomial.h> | ||||
| #include <lol/math/noise/gradient.h> | #include <lol/math/noise/gradient.h> | ||||
| #include <lol/math/noise/perlin.h> | |||||
| #include <lol/math/noise/simplex.h> | #include <lol/math/noise/simplex.h> | ||||
| @@ -0,0 +1,86 @@ | |||||
| // | |||||
| // Lol Engine | |||||
| // | |||||
| // Copyright: (c) 2010-2014 Sam Hocevar <sam@hocevar.net> | |||||
| // (c) 2013-2014 Benjamin "Touky" Huet <huet.benjamin@gmail.com> | |||||
| // (c) 2013-2014 Guillaume Bittoun <guillaume.bittoun@gmail.com> | |||||
| // This program is free software; you can redistribute it and/or | |||||
| // modify it under the terms of the Do What The Fuck You Want To | |||||
| // Public License, Version 2, as published by Sam Hocevar. See | |||||
| // http://www.wtfpl.net/ for more details. | |||||
| // | |||||
| #pragma once | |||||
| #include <lol/math/noise/gradient.h> | |||||
| namespace lol | |||||
| { | |||||
| template<int N> | |||||
| class perlin_noise : public gradient_provider<N> | |||||
| { | |||||
| public: | |||||
| perlin_noise() | |||||
| : gradient_provider<N>() | |||||
| { | |||||
| } | |||||
| perlin_noise(int seed) | |||||
| : gradient_provider<N>(seed) | |||||
| { | |||||
| } | |||||
| /* Evaluate noise at a given point */ | |||||
| inline float eval(vec_t<float, N> position) const | |||||
| { | |||||
| int const cells = 1 << N; | |||||
| /* Retrieve the containing hypercube origin */ | |||||
| vec_t<int, N> origin; | |||||
| for (int i = 0; i < N; ++i) | |||||
| origin[i] = (int)position[i] - (position[i] < 0); | |||||
| vec_t<float, N> delta = position - (vec_t<float, N>)origin; | |||||
| vec_t<float, N> t = delta; | |||||
| /* DEBUG: original Perlin noise polynomial */ | |||||
| //t = (vec_t<float, N>(3.f) - 2.f * t) * t * t; | |||||
| /* Improved polynomial (with null second derivative) */ | |||||
| t = ((6.f * t - vec_t<float, N>(15.f)) | |||||
| * t + vec_t<float, N>(10.f)) * (t * t * t); | |||||
| /* Compute all gradient contributions */ | |||||
| array<float> values; | |||||
| values.Resize(cells); | |||||
| for (int i = 0; i < cells; ++i) | |||||
| { | |||||
| /* FIXME: find another way to travel the hypercube that | |||||
| * causes fewer bit flips, and compute the return value | |||||
| * directly without allocating an array value. */ | |||||
| vec_t<float, N> v = delta; | |||||
| vec_t<int, N> corner = origin; | |||||
| for (int bit = 0; bit < N; ++bit) | |||||
| if ((1 << bit) & i) | |||||
| { | |||||
| ++corner[bit]; | |||||
| v[bit] = v[bit] - 1.f; | |||||
| } | |||||
| values[i] = dot(v, this->get_gradient(corner)); | |||||
| } | |||||
| /* Interpolate all contributions together */ | |||||
| for (int bit = N; bit--; ) | |||||
| { | |||||
| for (int i = 0; i < (1 << bit); ++i) | |||||
| values[i] = (1.f - t[bit]) * values[i] | |||||
| + t[bit] * values[i + (1 << bit)]; | |||||
| } | |||||
| return values[0]; | |||||
| } | |||||
| }; | |||||
| } | |||||
| @@ -213,7 +213,7 @@ protected: | |||||
| { | { | ||||
| // Finding floor point index | // Finding floor point index | ||||
| for (int i = 0; i < N; ++i) | for (int i = 0; i < N; ++i) | ||||
| origin[i] = ((int)world_position[i]) - (world_position[i] < 0); | |||||
| origin[i] = (int)world_position[i] - (world_position[i] < 0); | |||||
| // Extracting decimal part from simplex sample | // Extracting decimal part from simplex sample | ||||
| pos = world_position - (vec_t<float, N>)origin; | pos = world_position - (vec_t<float, N>)origin; | ||||
| @@ -333,6 +333,7 @@ | |||||
| <ClInclude Include="lol\math\interp.h" /> | <ClInclude Include="lol\math\interp.h" /> | ||||
| <ClInclude Include="lol\math\matrix.h" /> | <ClInclude Include="lol\math\matrix.h" /> | ||||
| <ClInclude Include="lol\math\noise\gradient.h" /> | <ClInclude Include="lol\math\noise\gradient.h" /> | ||||
| <ClInclude Include="lol\math\noise\perlin.h" /> | |||||
| <ClInclude Include="lol\math\noise\simplex.h" /> | <ClInclude Include="lol\math\noise\simplex.h" /> | ||||
| <ClInclude Include="lol\math\ops.h" /> | <ClInclude Include="lol\math\ops.h" /> | ||||
| <ClInclude Include="lol\math\polynomial.h" /> | <ClInclude Include="lol\math\polynomial.h" /> | ||||
| @@ -456,6 +456,9 @@ | |||||
| <ClInclude Include="lol\math\noise\gradient.h"> | <ClInclude Include="lol\math\noise\gradient.h"> | ||||
| <Filter>lol\math\noise</Filter> | <Filter>lol\math\noise</Filter> | ||||
| </ClInclude> | </ClInclude> | ||||
| <ClInclude Include="lol\math\noise\perlin.h"> | |||||
| <Filter>lol\math\noise</Filter> | |||||
| </ClInclude> | |||||
| <ClInclude Include="lol\math\noise\simplex.h"> | <ClInclude Include="lol\math\noise\simplex.h"> | ||||
| <Filter>lol\math\noise</Filter> | <Filter>lol\math\noise</Filter> | ||||
| </ClInclude> | </ClInclude> | ||||