Initial fork

This commit is contained in:
Ricardo Mazeto 2020-01-15 12:36:41 -07:00
commit ac32e68c31
3 changed files with 153 additions and 0 deletions

24
README Normal file
View File

@ -0,0 +1,24 @@
Nanoise - Nano Noise
Nanoise is my implementation of the algorithm used by
Toby Schachman on these videos:
http://tobyschachman.com/Shadershop/.
I contacted him, but he don't remember where he saw that algo.
He think it is from the shader comunity. Therefore, this code
bare no licence.
This algorithm is very different from Perlin Noise.
If you want to intuitively understand how this algorithm works,
watch the videos on that page and follow the source.
I just finished the implementation, so there's a lot of room
for improvements and optimizations.
To run the program, type `chmod +x nanoise.c` and run
`nanoise.c`. Yes, run the source!
It depends on imagemagick to convert from pgm to png.
But ffmpeg also do the trick. And many other tools.

90
nanoise.c Executable file
View File

@ -0,0 +1,90 @@
#include <stdio.h> /* printf */
#include <stdlib.h> /* atof, atoi, srandom, random, puts */
#include <time.h> /* time, to seed random */
#include <inttypes.h> /* uint_8, uint_16 */
#include "nanoise.h"
#ifdef SKIP
: ${D=_2D}
tcc -D$D nanoise.c \
&& echo 'running a.out' \
&& a.out 8 0.01 > nanoise$D.pgm \
&& echo 'converting to png' \
&& convert nanoise$D.pgm nanoise$D.png \
&& rm nanoise$D.pgm
exit 0
#endif
/* nanoise - Nano Noise */
int main(int argc, char ** argv){
#ifdef _1D
if(argc!=4) return 1;
float i, x, l;
x = (float) atof(argv[1]); /* initial value */
l = (float) atof(argv[2])+x; /* upper limit */
i = (float) atof(argv[3]); /* increment */
while(x<l)
printf("%f %f\n", x,
(noise1d(x))+(noise1d((x/0.01))*0.01)),
x += i;
/* 1D Noise, refactoring...
uint_16 * array = malloc(sizeof(short)*0xff);
NANOISE_1D(array, 0x00, 0xff, 0x0f, 0x02);
uint_8 i = 0;
do{ printf("%u %u ", i, array[i]); i++;}while(i);
*/
#endif
#ifdef _2D
if(argc!=3) return 1;
uint32_t s, S;
float y, x, i;
s = atoi(argv[1]);
i = (float) atof(argv[2]);
S = (uint32_t) ((float)s)/i;
srandom(time(0x00));
puts("P2");
printf("%d %d 255\n", S, S);
/* refactoring...
uint8_t * canvas = malloc(0xff * 0xff);
NANOISE_2D(canvas, 0x00, 0xff, 0x0f);
x=y=0;
while((int)y<)
*/
x=y=0;
while((int)y<s){
x=0;
while((int)x<s){
printf(
"%u ",
(unsigned int)
scale( (float)(
(NOISE_2D(x,y )*0.5f ) +
(NOISE_2D(x/0.5f,y/0.5f)*0.25f ) +
(NOISE_2D(x/0.2f,y/0.2f)*0.125f) +
(NOISE_2D(x/0.1f,y/0.1f)*0.125f) ),
(float) 0,
(float) RAND_MAX,
(float) 0,
(float) 256
)
);
x+=i;
};
puts("");
y+=i;
};
#endif
return 0;
}

39
nanoise.h Normal file
View File

@ -0,0 +1,39 @@
/* get value V, that is something between min and max,
and scale it to something between a and b */
#define scale(v,min,max,a,b) ( (v-min)*(b-a)/(max-min)+a )
/* Cubic Sigmoid easing function, where 0.0 < N < 1.0 */
#define S(N) (N*N*((N-1.0)*-2.0+1.0))
#ifndef NOISE_1D
/* returns a value between 0 and RAND_MAX */
#define NOISE_1D(V) ( \
srandom((int)V), \
( (random())/2 * \
S( (V-(int)V) ) \
) + \
( srandom((int)V-1), \
(random()/2) * \
S( (1-(V-(int)V))) \
) \
)
#endif
#define FRACT(F) ( (F) - ((int)(F)) )
#define FLOOR(F) ((int)(F))
#define SMOOTH_SAW(F) ( S( FRACT(F) ) )
#define STEP_RANDOM_2D(x,y)\
(srandom((FLOOR(x+2))*(FLOOR(y+2))), (float)(random()))
#define HORIZONTAL(x,y) \
( \
( SMOOTH_SAW(x) * STEP_RANDOM_2D(x,y)) + \
( (1-SMOOTH_SAW(x)) * STEP_RANDOM_2D(x-1,y)) \
)
#define NOISE_2D(x,y)( \
(1-SMOOTH_SAW((y))) * HORIZONTAL((x),(y )) + \
( SMOOTH_SAW((y)) * HORIZONTAL((x),(y+1)) ))