Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
memo committed Feb 22, 2013
0 parents commit aca522a
Show file tree
Hide file tree
Showing 4 changed files with 430 additions and 0 deletions.
85 changes: 85 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# Some general ignore patterns

*/bin/*
!*/bin/data/
# for bin folder in root
/bin/*
!/bin/data/

build/
obj/
*.o
Debug*/
Release*/
*.mode*
*.app/
*.pyc
.svn/

# IDE-specific ignore patterns (e.g. user-specific files)

#XCode
*.pbxuser
*.perspective
*.perspectivev3
*.mode1v3
*.mode2v3
#XCode 4
xcuserdata
*.xcworkspace

#Code::Blocks
*.depend
*.layout
*.cbTemp

#Visual Studio
*.sdf
*.opensdf
*.suo
ipch/

#Eclipse
.metadata
local.properties
.externalToolBuilders

# OS-specific ignore patterns

#Linux
*~
# KDE
.directory

#OSX
.DS_Store
*.swp
*~.nib
# Thumbnails
._*

#Windows
# Windows image file caches
Thumbs.db
# Folder config file
Desktop.ini

#Android
.csettings

# Packages
# it's better to unpack these files and commit the raw source
# git has its own built in compression methods
*.7z
*.dmg
*.gz
*.iso
*.jar
*.rar
*.tar
*.zip

# Logs and databases
*.log
*.sql
*.sqlite
16 changes: 16 additions & 0 deletions license.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
The code in this repository is available under the [MIT License](https://secure.wikimedia.org/wikipedia/en/wiki/Mit_license).

Copyright (c) 2008-2012 Memo Akten, [www.memo.tv](http://www.memo.tv)
The Mega Super Awesome Visuals Company

// April-Mai 2009 optimized and extended by Maa (http://www.lagraine.com/ - new content coming soon)

/* Portions Copyright (c) 2010, The Cinder Project, http://libcinder.org */



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.
269 changes: 269 additions & 0 deletions src/ofxMSAPerlin.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,269 @@
/* coherent noise function over 1, 2 or 3 dimensions */
/* (copyright Ken Perlin) */

#include <stdlib.h>
#include <stdio.h>
#include <math.h>

#include "ofxMSAPerlin.h"

namespace msa {

#define B SAMPLE_SIZE
#define BM (SAMPLE_SIZE-1)

#define N 0x1000
#define NP 12 /* 2^N */
#define NM 0xfff

//#define fade(t) ( t * t * (3.0f - 2.0f * t) )
#define fade(t) (t * t * t * (t * (t * 6 - 15) + 10))
#define lerp(t, a, b) ( a + t * (b - a) )

#define SETUP(i,b0,b1,r0,r1)\
t = vec[i] + N;\
b0 = ((int)t) & BM;\
b1 = (b0+1) & BM;\
r0 = t - (int)t;\
r1 = r0 - 1.0f;

float Perlin::noise1(float arg)
{
int bx0, bx1;
float rx0, rx1, sx, t, u, v, vec[1];

vec[0] = arg;

if (mStart)
{
srand(mSeed);
mStart = false;
init();
}

SETUP(0, bx0,bx1, rx0,rx1);

sx = fade(rx0);

u = rx0 * g1[ p[ bx0 ] ];
v = rx1 * g1[ p[ bx1 ] ];

return lerp(sx, u, v);
}

float Perlin::noise2(float vec[2])
{
int bx0, bx1, by0, by1, b00, b10, b01, b11;
float rx0, rx1, ry0, ry1, *q, sx, sy, a, b, t, u, v;
int i, j;

if (mStart)
{
srand(mSeed);
mStart = false;
init();
}

SETUP(0,bx0,bx1,rx0,rx1);
SETUP(1,by0,by1,ry0,ry1);

i = p[bx0];
j = p[bx1];

b00 = p[i + by0];
b10 = p[j + by0];
b01 = p[i + by1];
b11 = p[j + by1];

sx = fade(rx0);
sy = fade(ry0);

#define at2(rx,ry) ( rx * q[0] + ry * q[1] )

q = g2[b00];
u = at2(rx0,ry0);
q = g2[b10];
v = at2(rx1,ry0);
a = lerp(sx, u, v);

q = g2[b01];
u = at2(rx0,ry1);
q = g2[b11];
v = at2(rx1,ry1);
b = lerp(sx, u, v);

return lerp(sy, a, b);
}

float Perlin::noise3(float vec[3])
{
int bx0, bx1, by0, by1, bz0, bz1, b00, b10, b01, b11;
float rx0, rx1, ry0, ry1, rz0, rz1, *q, sy, sz, a, b, c, d, t, u, v;
int i, j;

if (mStart)
{
srand(mSeed);
mStart = false;
init();
}

SETUP(0, bx0,bx1, rx0,rx1);
SETUP(1, by0,by1, ry0,ry1);
SETUP(2, bz0,bz1, rz0,rz1);

i = p[ bx0 ];
j = p[ bx1 ];

b00 = p[ i + by0 ];
b10 = p[ j + by0 ];
b01 = p[ i + by1 ];
b11 = p[ j + by1 ];

t = fade(rx0);
sy = fade(ry0);
sz = fade(rz0);

#define at3(rx,ry,rz) ( rx * q[0] + ry * q[1] + rz * q[2] )

q = g3[ b00 + bz0 ] ; u = at3(rx0,ry0,rz0);
q = g3[ b10 + bz0 ] ; v = at3(rx1,ry0,rz0);
a = lerp(t, u, v);

q = g3[ b01 + bz0 ] ; u = at3(rx0,ry1,rz0);
q = g3[ b11 + bz0 ] ; v = at3(rx1,ry1,rz0);
b = lerp(t, u, v);

c = lerp(sy, a, b);

q = g3[ b00 + bz1 ] ; u = at3(rx0,ry0,rz1);
q = g3[ b10 + bz1 ] ; v = at3(rx1,ry0,rz1);
a = lerp(t, u, v);

q = g3[ b01 + bz1 ] ; u = at3(rx0,ry1,rz1);
q = g3[ b11 + bz1 ] ; v = at3(rx1,ry1,rz1);
b = lerp(t, u, v);

d = lerp(sy, a, b);

return lerp(sz, c, d);
}

void Perlin::normalize2(float v[2])
{
float s;

s = (float)sqrt(v[0] * v[0] + v[1] * v[1]);
s = 1.0f/s;
v[0] = v[0] * s;
v[1] = v[1] * s;
}

void Perlin::normalize3(float v[3])
{
float s;

s = (float)sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
s = 1.0f/s;

v[0] = v[0] * s;
v[1] = v[1] * s;
v[2] = v[2] * s;
}

void Perlin::init(void)
{
int i, j, k;

for (i = 0 ; i < B ; i++)
{
p[i] = i;
g1[i] = (float)((rand() % (B + B)) - B) / B;
for (j = 0 ; j < 2 ; j++)
g2[i][j] = (float)((rand() % (B + B)) - B) / B;
normalize2(g2[i]);
for (j = 0 ; j < 3 ; j++)
g3[i][j] = (float)((rand() % (B + B)) - B) / B;
normalize3(g3[i]);
}

while (--i)
{
k = p[i];
p[i] = p[j = rand() % B];
p[j] = k;
}

for (i = 0 ; i < B + 2 ; i++)
{
p[B + i] = p[i];
g1[B + i] = g1[i];
for (j = 0 ; j < 2 ; j++)
g2[B + i][j] = g2[i][j];
for (j = 0 ; j < 3 ; j++)
g3[B + i][j] = g3[i][j];
}

}


float Perlin::perlin_noise_2D(float vec[2])
{
int terms = mOctaves;
// float freq = mFrequency;
float result = 0.0f;
float amp = mAmplitude;

vec[0]*=mFrequency;
vec[1]*=mFrequency;

for( int i=0; i<terms; i++ )
{
result += noise2(vec)*amp;
vec[0] *= 2.0f;
vec[1] *= 2.0f;
amp*=0.5f;
}
return result;
}


float Perlin::perlin_noise_3D(float vec[3])
{
int terms = mOctaves;
// float freq = mFrequency;
float result = 0.0f;
float amp = mAmplitude;

vec[0]*=mFrequency;
vec[1]*=mFrequency;
vec[2]*=mFrequency;

for( int i=0; i<terms; i++ )
{
result += noise3(vec) * amp;
vec[0] *= 2.0f;
vec[1] *= 2.0f;
vec[2] *= 2.0f;
amp*=0.5f;
}
return result;
}



Perlin::Perlin(int octaves,float freq,float amp,int seed)
{
setup(octaves, freq, amp, seed);
}


void Perlin::setup(int octaves,float freq,float amp,int seed) {
mOctaves = octaves;
mFrequency = freq;
mAmplitude = amp;
mSeed = seed;
mStart = true;
}

}
Loading

0 comments on commit aca522a

Please sign in to comment.