Skip to content

Commit f581afa

Browse files
committed
metaballs
1 parent 81ff9bf commit f581afa

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

gfx/metaballs.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include <stdio.h>
2+
#include <unistd.h>
3+
#include <math.h>
4+
5+
#define FPS 24
6+
#define WIDTH 80
7+
#define HEIGHT 50
8+
#define CLAMP(x, low, high) (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
9+
10+
float iTime = 0;
11+
12+
void mainImage(int fragCoord_x, int fragCoord_y) { // kinda shadertoy naming :)
13+
float u = (2.*fragCoord_x - WIDTH )/HEIGHT;
14+
float v = (2.*fragCoord_y - HEIGHT)/HEIGHT;
15+
float d1 = .6/sqrt(pow(sin(iTime*.5) -u, 2) + pow(sin(iTime*.5)-v, 2)); // linear motion
16+
float d2 = .6/sqrt(pow(sin(iTime*.5) -u, 2) + pow(cos(iTime*.5)-v, 2)); // circular motion
17+
float d3 = .6/sqrt(pow(sin(iTime*.25)-u, 2) + pow(sin(iTime) -v, 2)); // wave
18+
float sdf = d1 + d2 + d3 - 2.2; // metaballs signed distance function
19+
float fragColor_r = 255*1.7*(sdf+1); // orange halo (red and green channels)
20+
float fragColor_g = 255*0.8*(sdf+1);
21+
float fragColor_b = 255*(sdf<0 ? 0 : 1); // cold-white metaballs (step function)
22+
printf("%d;%d;%d", CLAMP((int)fragColor_r,0,255), CLAMP((int)fragColor_g, 0, 255), CLAMP((int)fragColor_b, 0, 255));
23+
}
24+
25+
int main() {
26+
printf("\033[2J\033[?25l"); // clear screen and hide cursor
27+
for (;;) {
28+
printf("\033[H"); // home
29+
for (int j = 0; j<HEIGHT; j+=2) {
30+
for (int i = 0; i<WIDTH; i++) {
31+
printf("\033[48;2;"); mainImage(i, j+0); printf("m"); // set background color
32+
printf("\033[38;2;"); mainImage(i, j+1); printf("m"); // set foreground color
33+
printf("\xE2\x96\x83"); // half-block Unicode symbol
34+
}
35+
printf("\033[49m\n");
36+
}
37+
usleep(1000000/FPS);
38+
iTime += 1./FPS;
39+
}
40+
return 0;
41+
}

0 commit comments

Comments
 (0)