From c207be10b9f57b7e04335b7be0549ee0d9585346 Mon Sep 17 00:00:00 2001 From: David Rose Date: Fri, 7 Oct 2005 22:38:34 +0000 Subject: [PATCH] stackedPerlinNoise --- panda/src/mathutil/Sources.pp | 15 +++-- panda/src/mathutil/mathutil_composite1.cxx | 4 +- panda/src/mathutil/mathutil_composite2.cxx | 12 ++-- panda/src/mathutil/perlinNoise.I | 15 ++++- panda/src/mathutil/perlinNoise.h | 3 + panda/src/mathutil/stackedPerlinNoise2.I | 38 +++++++++++++ panda/src/mathutil/stackedPerlinNoise2.cxx | 64 +++++++++++++++++++++ panda/src/mathutil/stackedPerlinNoise2.h | 52 +++++++++++++++++ panda/src/mathutil/stackedPerlinNoise3.I | 38 +++++++++++++ panda/src/mathutil/stackedPerlinNoise3.cxx | 65 ++++++++++++++++++++++ panda/src/mathutil/stackedPerlinNoise3.h | 52 +++++++++++++++++ 11 files changed, 343 insertions(+), 15 deletions(-) create mode 100644 panda/src/mathutil/stackedPerlinNoise2.I create mode 100644 panda/src/mathutil/stackedPerlinNoise2.cxx create mode 100644 panda/src/mathutil/stackedPerlinNoise2.h create mode 100644 panda/src/mathutil/stackedPerlinNoise3.I create mode 100644 panda/src/mathutil/stackedPerlinNoise3.cxx create mode 100644 panda/src/mathutil/stackedPerlinNoise3.h diff --git a/panda/src/mathutil/Sources.pp b/panda/src/mathutil/Sources.pp index 6abdc08e51..12dfa3b2f7 100644 --- a/panda/src/mathutil/Sources.pp +++ b/panda/src/mathutil/Sources.pp @@ -30,8 +30,10 @@ perlinNoise2.h perlinNoise2.I \ perlinNoise3.h perlinNoise3.I \ plane.h plane_src.I plane_src.cxx \ - plane_src.h rotate_to.h rotate_to_src.cxx - + plane_src.h rotate_to.h rotate_to_src.cxx \ + stackedPerlinNoise2.h stackedPerlinNoise2.I \ + stackedPerlinNoise3.h stackedPerlinNoise3.I + #define INCLUDED_SOURCES \ boundingHexahedron.cxx boundingLine.cxx \ boundingPlane.cxx \ @@ -45,6 +47,8 @@ perlinNoise.cxx \ perlinNoise2.cxx \ perlinNoise3.cxx \ + stackedPerlinNoise2.cxx \ + stackedPerlinNoise3.cxx \ plane.cxx rotate_to.cxx #define INSTALL_HEADERS \ @@ -62,8 +66,11 @@ perlinNoise.h perlinNoise.I \ perlinNoise2.h perlinNoise2.I \ perlinNoise3.h perlinNoise3.I \ - plane.h plane_src.I \ - plane_src.h rotate_to.h + plane.h plane_src.I plane_src.cxx \ + plane_src.h rotate_to.h rotate_to_src.cxx \ + stackedPerlinNoise2.h stackedPerlinNoise2.I \ + stackedPerlinNoise3.h stackedPerlinNoise3.I + #define IGATESCAN all diff --git a/panda/src/mathutil/mathutil_composite1.cxx b/panda/src/mathutil/mathutil_composite1.cxx index 69f70f10bb..c7937a7cae 100644 --- a/panda/src/mathutil/mathutil_composite1.cxx +++ b/panda/src/mathutil/mathutil_composite1.cxx @@ -1,10 +1,8 @@ - -#include "boundingVolume.cxx" #include "boundingHexahedron.cxx" #include "boundingLine.cxx" #include "boundingPlane.cxx" #include "boundingSphere.cxx" +#include "boundingVolume.cxx" #include "finiteBoundingVolume.cxx" #include "geometricBoundingVolume.cxx" #include "omniBoundingVolume.cxx" - diff --git a/panda/src/mathutil/mathutil_composite2.cxx b/panda/src/mathutil/mathutil_composite2.cxx index 39a3560039..fc55149285 100644 --- a/panda/src/mathutil/mathutil_composite2.cxx +++ b/panda/src/mathutil/mathutil_composite2.cxx @@ -1,12 +1,12 @@ - #include "config_mathutil.cxx" +#include "fftCompressor.cxx" +#include "linmath_events.cxx" +#include "look_at.cxx" +#include "mersenne.cxx" #include "perlinNoise.cxx" #include "perlinNoise2.cxx" #include "perlinNoise3.cxx" #include "plane.cxx" #include "rotate_to.cxx" -#include "look_at.cxx" -#include "linmath_events.cxx" -#include "mersenne.cxx" -#include "fftCompressor.cxx" - +#include "stackedPerlinNoise2.cxx" +#include "stackedPerlinNoise3.cxx" diff --git a/panda/src/mathutil/perlinNoise.I b/panda/src/mathutil/perlinNoise.I index 31b54268fd..1efce2085f 100644 --- a/panda/src/mathutil/perlinNoise.I +++ b/panda/src/mathutil/perlinNoise.I @@ -55,7 +55,7 @@ random_int(int range) { //////////////////////////////////////////////////////////////////// INLINE double PerlinNoise:: random_real(double range) { - return (range * _mersenne.get_uint31()) / ((double)0xffffffff + 1.0f); + return (range * _mersenne.get_uint31()) / ((double)0x80000000); } //////////////////////////////////////////////////////////////////// @@ -71,7 +71,7 @@ random_real_unit() { //////////////////////////////////////////////////////////////////// // Function: PerlinNoise::get_next_seed // Access: Protected, Static -// Description: Returns a random seed value for the next-in-sequence +// Description: Returns a random seed value for the next global // PerlinNoise object. //////////////////////////////////////////////////////////////////// INLINE unsigned long PerlinNoise:: @@ -83,3 +83,14 @@ get_next_seed() { return _next_seed.get_uint31(); } +//////////////////////////////////////////////////////////////////// +// Function: PerlinNoise::get_seed +// Access: Published +// Description: Returns a unique seed value based on the seed value +// passed to this PerlinNoise object (and on its current +// state). +//////////////////////////////////////////////////////////////////// +INLINE unsigned long PerlinNoise:: +get_seed() { + return _mersenne.get_uint31(); +} diff --git a/panda/src/mathutil/perlinNoise.h b/panda/src/mathutil/perlinNoise.h index 5234afe122..71e88bc96c 100644 --- a/panda/src/mathutil/perlinNoise.h +++ b/panda/src/mathutil/perlinNoise.h @@ -44,6 +44,9 @@ protected: INLINE static unsigned long get_next_seed(); +PUBLISHED: + INLINE unsigned long get_seed(); + protected: int _table_size; diff --git a/panda/src/mathutil/stackedPerlinNoise2.I b/panda/src/mathutil/stackedPerlinNoise2.I new file mode 100644 index 0000000000..51ce142937 --- /dev/null +++ b/panda/src/mathutil/stackedPerlinNoise2.I @@ -0,0 +1,38 @@ +// Filename: stackedPerlinNoise2.I +// Created by: drose (05Oct05) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) 2001 - 2004, Disney Enterprises, Inc. All rights reserved +// +// All use of this software is subject to the terms of the Panda 3d +// Software license. You should have received a copy of this license +// along with this source code; you will also find a current copy of +// the license at http://etc.cmu.edu/panda3d/docs/license/ . +// +// To contact the maintainers of this program write to +// panda3d-general@lists.sourceforge.net . +// +//////////////////////////////////////////////////////////////////// + + +//////////////////////////////////////////////////////////////////// +// Function: StackedPerlinNoise2::noise +// Access: Published +// Description: Returns the noise function of the three inputs. +//////////////////////////////////////////////////////////////////// +INLINE double StackedPerlinNoise2:: +noise(double x, double y) { + return noise(LVecBase2d(x, y)); +} + +//////////////////////////////////////////////////////////////////// +// Function: StackedPerlinNoise2::noise +// Access: Published +// Description: Returns the noise function of the three inputs. +//////////////////////////////////////////////////////////////////// +INLINE float StackedPerlinNoise2:: +noise(const LVecBase2f &value) { + return (float)noise(value[0], value[1]); +} diff --git a/panda/src/mathutil/stackedPerlinNoise2.cxx b/panda/src/mathutil/stackedPerlinNoise2.cxx new file mode 100644 index 0000000000..2ab05c8094 --- /dev/null +++ b/panda/src/mathutil/stackedPerlinNoise2.cxx @@ -0,0 +1,64 @@ +// Filename: stackedPerlinNoise2.cxx +// Created by: drose (05Oct05) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) 2001 - 2004, Disney Enterprises, Inc. All rights reserved +// +// All use of this software is subject to the terms of the Panda 3d +// Software license. You should have received a copy of this license +// along with this source code; you will also find a current copy of +// the license at http://etc.cmu.edu/panda3d/docs/license/ . +// +// To contact the maintainers of this program write to +// panda3d-general@lists.sourceforge.net . +// +//////////////////////////////////////////////////////////////////// + +#include "stackedPerlinNoise2.h" + +//////////////////////////////////////////////////////////////////// +// Function: StackedPerlinNoise2::Constructor +// Access: Published +// Description: Creates num_levels nested PerlinNoise2 objects. Each +// stacked Perlin object will have a scale of 1 / +// scale_factor times the previous object (so that it is +// higher-frequency, if scale_factor > 1), and an +// amplitude of amp_scale times the previous object (so +// that it is less important, if amp_scale < 1). +//////////////////////////////////////////////////////////////////// +StackedPerlinNoise2:: +StackedPerlinNoise2(double sx, double sy, int num_levels, + double scale_factor, double amp_scale, + int table_size, unsigned long seed) : + _amp_scale(amp_scale) +{ + _noises.reserve(num_levels); + for (int i = 0; i < num_levels; ++i) { + PerlinNoise2 noise(sx, sy, table_size, seed); + _noises.push_back(noise); + seed = noise.get_seed(); + sx /= scale_factor; + sy /= scale_factor; + } +} + +//////////////////////////////////////////////////////////////////// +// Function: StackedPerlinNoise2::noise +// Access: Published +// Description: Returns the noise function of the three inputs. +//////////////////////////////////////////////////////////////////// +double StackedPerlinNoise2:: +noise(const LVecBase2d &value) { + double result = 0.0f; + double amp = 1.0f; + + Noises::iterator ni; + for (ni = _noises.begin(); ni != _noises.end(); ++ni) { + result += (*ni).noise(value) * amp; + amp *= _amp_scale; + } + + return result; +} diff --git a/panda/src/mathutil/stackedPerlinNoise2.h b/panda/src/mathutil/stackedPerlinNoise2.h new file mode 100644 index 0000000000..976764225e --- /dev/null +++ b/panda/src/mathutil/stackedPerlinNoise2.h @@ -0,0 +1,52 @@ +// Filename: stackedPerlinNoise2.h +// Created by: drose (05Oct05) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) 2001 - 2004, Disney Enterprises, Inc. All rights reserved +// +// All use of this software is subject to the terms of the Panda 3d +// Software license. You should have received a copy of this license +// along with this source code; you will also find a current copy of +// the license at http://etc.cmu.edu/panda3d/docs/license/ . +// +// To contact the maintainers of this program write to +// panda3d-general@lists.sourceforge.net . +// +//////////////////////////////////////////////////////////////////// + +#ifndef STACKEDPERLINNOISE2_H +#define STACKEDPERLINNOISE2_H + +#include "pandabase.h" +#include "perlinNoise2.h" +#include "pvector.h" + +//////////////////////////////////////////////////////////////////// +// Class : StackedPerlinNoise2 +// Description : Implements a multi-layer PerlinNoise, with one or +// more high-frequency noise functions added to a +// lower-frequency base noise function. +//////////////////////////////////////////////////////////////////// +class EXPCL_PANDA StackedPerlinNoise2 { +PUBLISHED: + StackedPerlinNoise2(double sx, double sy, int num_levels = 3, + double scale_factor = 4.0f, double amp_scale = 0.5f, + int table_size = 256, unsigned long seed = 0); + + INLINE double noise(double x, double y); + INLINE float noise(const LVecBase2f &value); + double noise(const LVecBase2d &value); + +private: + double _amp_scale; + + typedef pvector Noises; + Noises _noises; +}; + +#include "stackedPerlinNoise2.I" + +#endif + diff --git a/panda/src/mathutil/stackedPerlinNoise3.I b/panda/src/mathutil/stackedPerlinNoise3.I new file mode 100644 index 0000000000..106d84d347 --- /dev/null +++ b/panda/src/mathutil/stackedPerlinNoise3.I @@ -0,0 +1,38 @@ +// Filename: stackedPerlinNoise3.I +// Created by: drose (05Oct05) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) 2001 - 2004, Disney Enterprises, Inc. All rights reserved +// +// All use of this software is subject to the terms of the Panda 3d +// Software license. You should have received a copy of this license +// along with this source code; you will also find a current copy of +// the license at http://etc.cmu.edu/panda3d/docs/license/ . +// +// To contact the maintainers of this program write to +// panda3d-general@lists.sourceforge.net . +// +//////////////////////////////////////////////////////////////////// + + +//////////////////////////////////////////////////////////////////// +// Function: StackedPerlinNoise3::noise +// Access: Published +// Description: Returns the noise function of the three inputs. +//////////////////////////////////////////////////////////////////// +INLINE double StackedPerlinNoise3:: +noise(double x, double y, double z) { + return noise(LVecBase3d(x, y, z)); +} + +//////////////////////////////////////////////////////////////////// +// Function: StackedPerlinNoise3::noise +// Access: Published +// Description: Returns the noise function of the three inputs. +//////////////////////////////////////////////////////////////////// +INLINE float StackedPerlinNoise3:: +noise(const LVecBase3f &value) { + return (float)noise(value[0], value[1], value[2]); +} diff --git a/panda/src/mathutil/stackedPerlinNoise3.cxx b/panda/src/mathutil/stackedPerlinNoise3.cxx new file mode 100644 index 0000000000..999e964cd3 --- /dev/null +++ b/panda/src/mathutil/stackedPerlinNoise3.cxx @@ -0,0 +1,65 @@ +// Filename: stackedPerlinNoise3.cxx +// Created by: drose (05Oct05) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) 2001 - 2004, Disney Enterprises, Inc. All rights reserved +// +// All use of this software is subject to the terms of the Panda 3d +// Software license. You should have received a copy of this license +// along with this source code; you will also find a current copy of +// the license at http://etc.cmu.edu/panda3d/docs/license/ . +// +// To contact the maintainers of this program write to +// panda3d-general@lists.sourceforge.net . +// +//////////////////////////////////////////////////////////////////// + +#include "stackedPerlinNoise3.h" + +//////////////////////////////////////////////////////////////////// +// Function: StackedPerlinNoise3::Constructor +// Access: Published +// Description: Creates num_levels nested PerlinNoise3 objects. Each +// stacked Perlin object will have a scale of 1 / +// scale_factor times the previous object (so that it is +// higher-frequency, if scale_factor > 1), and an +// amplitude of amp_scale times the previous object (so +// that it is less important, if amp_scale < 1). +//////////////////////////////////////////////////////////////////// +StackedPerlinNoise3:: +StackedPerlinNoise3(double sx, double sy, double sz, int num_levels, + double scale_factor, double amp_scale, + int table_size, unsigned long seed) : + _amp_scale(amp_scale) +{ + _noises.reserve(num_levels); + for (int i = 0; i < num_levels; ++i) { + PerlinNoise3 noise(sx, sy, sz, table_size, seed); + _noises.push_back(noise); + seed = noise.get_seed(); + sx /= scale_factor; + sy /= scale_factor; + sz /= scale_factor; + } +} + +//////////////////////////////////////////////////////////////////// +// Function: StackedPerlinNoise3::noise +// Access: Published +// Description: Returns the noise function of the three inputs. +//////////////////////////////////////////////////////////////////// +double StackedPerlinNoise3:: +noise(const LVecBase3d &value) { + double result = 0.0f; + double amp = 1.0f; + + Noises::iterator ni; + for (ni = _noises.begin(); ni != _noises.end(); ++ni) { + result += (*ni).noise(value) * amp; + amp *= _amp_scale; + } + + return result; +} diff --git a/panda/src/mathutil/stackedPerlinNoise3.h b/panda/src/mathutil/stackedPerlinNoise3.h new file mode 100644 index 0000000000..d7eb1b4b9a --- /dev/null +++ b/panda/src/mathutil/stackedPerlinNoise3.h @@ -0,0 +1,52 @@ +// Filename: stackedPerlinNoise3.h +// Created by: drose (05Oct05) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) 2001 - 2004, Disney Enterprises, Inc. All rights reserved +// +// All use of this software is subject to the terms of the Panda 3d +// Software license. You should have received a copy of this license +// along with this source code; you will also find a current copy of +// the license at http://etc.cmu.edu/panda3d/docs/license/ . +// +// To contact the maintainers of this program write to +// panda3d-general@lists.sourceforge.net . +// +//////////////////////////////////////////////////////////////////// + +#ifndef STACKEDPERLINNOISE3_H +#define STACKEDPERLINNOISE3_H + +#include "pandabase.h" +#include "perlinNoise3.h" +#include "pvector.h" + +//////////////////////////////////////////////////////////////////// +// Class : StackedPerlinNoise3 +// Description : Implements a multi-layer PerlinNoise, with one or +// more high-frequency noise functions added to a +// lower-frequency base noise function. +//////////////////////////////////////////////////////////////////// +class EXPCL_PANDA StackedPerlinNoise3 { +PUBLISHED: + StackedPerlinNoise3(double sx, double sy, double sz, int num_levels = 3, + double scale_factor = 4.0f, double amp_scale = 0.5f, + int table_size = 256, unsigned long seed = 0); + + INLINE double noise(double x, double y, double z); + INLINE float noise(const LVecBase3f &value); + double noise(const LVecBase3d &value); + +private: + double _amp_scale; + + typedef pvector Noises; + Noises _noises; +}; + +#include "stackedPerlinNoise3.I" + +#endif +