-
Notifications
You must be signed in to change notification settings - Fork 0
/
perlinNoise2.c
63 lines (47 loc) · 1.67 KB
/
perlinNoise2.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// experiment with perlin noise
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <time.h>
#include <SDL2/SDL.h>
#define WIDTH 512
#define HEIGHT 300
#define PI 3.14159265359
inline float rand_fl() {
return (float)rand() / (float)RAND_MAX;
}
float interpolate(float pa, float pb, float px) { // first point, second point and px is the gradiant (a value between 0 and 1)
float f = (1 - cos(px * PI)) * 0.5;
return (pa * (1 - f) + pb * f);
}
int main(int argc, char *args[]) {
srand(time(NULL));
SDL_Init(SDL_INIT_VIDEO);
SDL_Window *window = SDL_CreateWindow("Pelin Noise", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WIDTH, HEIGHT, SDL_WINDOW_OPENGL);
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
SDL_Event event;
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
float y = HEIGHT * 2 / 3; // altitude
float amp = 96; // amplitude
int wl = 32; // wavelength == # of waves across the WIDTH
float a = rand_fl();
float b = rand_fl();
for (int x = 0; x < WIDTH; x++) {
if (x % wl == 0){
a = b;
b = rand_fl();
y = HEIGHT * 2 / 3 + a * amp;
}
else
y = HEIGHT * 2 / 3 + interpolate(a, b, (float)(x % wl) / wl) * amp;
SDL_RenderDrawPoint(renderer, x, (int)y);
}
SDL_RenderPresent(renderer);
while(SDL_TRUE)
if (SDL_PollEvent(&event) && (event.type == SDL_QUIT || event.type == SDL_KEYDOWN)) break;
else SDL_Delay(256);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
exit(EXIT_SUCCESS);
}