Browse Source

Added OpenSimplex noise (implementation by Arthur Tombs) and noise wrapper to base

master
Macoy Madson 5 years ago
parent
commit
9d04980ec4
4 changed files with 2708 additions and 2 deletions
  1. +2553
    -0
      3rdParty/OpenSimplexNoise.hh
  2. +13
    -2
      makefile
  3. +90
    -0
      noise/noise.cpp
  4. +52
    -0
      noise/noise.hpp

+ 2553
- 0
3rdParty/OpenSimplexNoise.hh
File diff suppressed because it is too large
View File


+ 13
- 2
makefile View File

@ -2,10 +2,13 @@ FLAGS=g++ -std=c++11 -c -Os -Wall
ARCHIVE_NAME=lib/base.a
OBJ_DIR=LOCAL_obj
all: eptParser.o math.o collision.o timer.o profiler.o graphics.o input.o tileMap.o multilayerMap.o tileCamera.o animation.o sound.o
all: noise.o eptParser.o math.o collision.o timer.o profiler.o graphics.o input.o tileMap.o multilayerMap.o tileCamera.o animation.o sound.o
@echo -------------------Building archive...
ar rvs $(ARCHIVE_NAME) eptParser.o math.o collision.o graphics.o input.o tileMap.o multilayerMap.o tileCamera.o animation.o timer.o profiler.o sound.o
ar rvs $(ARCHIVE_NAME) noise.o eptParser.o math.o collision.o graphics.o input.o tileMap.o multilayerMap.o tileCamera.o animation.o timer.o profiler.o sound.o
#mv *.o $(OBJ_DIR)
noise.o: noise/noise.hpp noise/noise.cpp
$(FLAGS) "noise/noise.hpp"
$(FLAGS) "noise/noise.cpp"
eptParser.o: ept/eptParser.hpp ept/eptParser.cpp
$(FLAGS) "ept/eptParser.hpp"
$(FLAGS) "ept/eptParser.cpp"
@ -72,6 +75,8 @@ clean:
rm sound/*.gch
rm ept/*.gch
rm animation/*.gch
rm noise/*.gch
#rm 3rdParty/*.gch
@echo -------------------If base is installed, removing it... [root priveleges]
rm /usr/lib/libbase2.0.a
rm -r /usr/include/base2.0
@ -90,6 +95,8 @@ install:
cp lib/base.a /usr/lib/libbase2.0.a
@echo -------------------Preparing /usr/include/base2.0...
mkdir /usr/include/base2.0
mkdir /usr/include/base2.0/3rdParty
mkdir /usr/include/base2.0/noise
mkdir /usr/include/base2.0/ept
mkdir /usr/include/base2.0/math
mkdir /usr/include/base2.0/collision
@ -101,6 +108,8 @@ install:
mkdir /usr/include/base2.0/sound
mkdir /usr/include/base2.0/animation
@echo -------------------Installing header files to /usr/include/base2.0...
cp 3rdParty/OpenSimplexNoise.hh /usr/include/base2.0/3rdParty/OpenSimplexNoise.hh
cp noise/noise.hpp /usr/include/base2.0/noise/noise.hpp
cp math/math.hpp /usr/include/base2.0/math/math.hpp
cp ept/eptParser.hpp /usr/include/base2.0/ept/eptParser.hpp
cp collision/collision.hpp /usr/include/base2.0/collision/collision.hpp
@ -117,6 +126,8 @@ update:
@echo -------------------Installing /lib/base.a to /usr/lib/libbase2.0.a...
cp lib/base.a /usr/lib/libbase2.0.a
@echo -------------------Installing header files to /usr/include/base2.0...
cp 3rdParty/OpenSimplexNoise.hh /usr/include/base2.0/3rdParty/OpenSimplexNoise.hh
cp noise/noise.hpp /usr/include/base2.0/noise/noise.hpp
cp math/math.hpp /usr/include/base2.0/math/math.hpp
cp ept/eptParser.hpp /usr/include/base2.0/ept/eptParser.hpp
cp collision/collision.hpp /usr/include/base2.0/collision/collision.hpp


+ 90
- 0
noise/noise.cpp View File

@ -0,0 +1,90 @@
#ifndef NOISE_CPP
#define NOISE_CPP
#include "noise.hpp"
#include "../3rdParty/OpenSimplexNoise.hh"
noiseHolder::noiseHolder()
{
}
noiseHolder::~noiseHolder()
{
}
float noiseHolder::eval(float x, float y)
{
return 0;
}
class noise2dHolder:public noiseHolder
{
private:
OSN::Noise<2> noise; //Internal OpenSimplex noise implementation
public:
noise2dHolder(int seed):noise(seed)
{
}
~noise2dHolder()
{
}
virtual float eval(float x, float y)
{
return noise.eval<float>(x, y);
}
};
Noise2d::Noise2d(int seed)
{
noise = new noise2dHolder(seed);
}
Noise2d::~Noise2d()
{
delete noise;
}
float Noise2d::rawNoise2d(float x, float y)
{
return noise->eval(x, y);
}
float scaleNoiseValue(float value, float lowBound, float highBound)
{
//Because raw noise is between -1 and 1, we add 1 to the value to
//get 0 to 2, so we need half range to get low bound to high bound
float halfRange = (highBound - lowBound) / 2;
float valueScaled = value + 1;
valueScaled = (valueScaled * halfRange) + lowBound;
if (valueScaled > highBound) valueScaled = highBound;
return valueScaled;
}
float Noise2d::scaledRawNoise2d(float x, float y, float lowBound, float highBound)
{
float value = rawNoise2d(x, y);
return scaleNoiseValue(value, lowBound, highBound);
}
float Noise2d::scaledOctaveNoise2d(float x, float y,
float lowBound, float highBound, int octaves, float scale, float persistence, float lacunarity)
{
float frequency = scale;
float amplitude = 1;
float maxAmplitude = 0;
float value = 0;
for (int i=0; i < octaves; i++)
{
value += rawNoise2d(x * frequency, y * frequency) * amplitude;
frequency *= lacunarity;
maxAmplitude += amplitude;
amplitude *= persistence;
}
return scaleNoiseValue(value / maxAmplitude, lowBound, highBound);
}
#endif

+ 52
- 0
noise/noise.hpp View File

@ -0,0 +1,52 @@
#ifndef NOISE_HPP
#define NOISE_HPP
//Needed to avoid multiple defintions of OpenSimplex noise template metaprogram thingies
class noiseHolder
{
public:
noiseHolder();
virtual ~noiseHolder();
virtual float eval(float x, float y);
};
/* --Noise2d--
* This class uses OpenSimplex noise to generate noise given an x and y
* coordinate.
* */
class Noise2d
{
private:
noiseHolder* noise;
public:
Noise2d(int seed);
~Noise2d();
//Returns a noise value between -1 and 1
float rawNoise2d(float x, float y);
//Scales a raw noise value to be between lowBound and highBound
float scaledRawNoise2d(float x, float y, float lowBound, float highBound);
/* Adds multiple layers of varying amplitudes together, then scales
* the value to be between lowBound and highBound
*
* Parameters:
* x, y The point
* lowBound, highBound The lowest and highest value to map the noise to
* scale The starting scale of the first octave
* octaves The number of octaves to add
* persistence Multiplied by the amplitude each octave, making
* subsequent octave values have less weight
* lacunarity The scale is multiplied by this value every
* octave, changing the frequency. The true
* "octave" value for lacunarity is 2
*
* See http://libnoise.sourceforge.net/glossary/#octave for a great
* description of how this works
* */
float scaledOctaveNoise2d(float x, float y,
float lowBound, float highBound, int octaves, float scale, float persistence, float lacunarity);
};
#endif

Loading…
Cancel
Save