forked from miloyip/light2d
-
Notifications
You must be signed in to change notification settings - Fork 0
/
m2.c
112 lines (99 loc) · 3.76 KB
/
m2.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include "svpng.inc"
#include <math.h> // fabsf(), fminf(), fmaxf(), sinf(), cosf(), sqrt()
#include <stdlib.h> // rand(), RAND_MAX
#define TWO_PI 6.28318530718f
#define W 1024
#define H 1024
#define N 256
#define MAX_STEP 64
#define MAX_DISTANCE 5.0f
#define EPSILON 1e-6f
#define BIAS 1e-4f
#define MAX_DEPTH 5
typedef struct { float sd, emissive, reflectivity, eta; } Result;
unsigned char img[W * H * 3];
float circleSDF(float x, float y, float cx, float cy, float r) {
float ux = x - cx, uy = y - cy;
return sqrtf(ux * ux + uy * uy) - r;
}
float capsuleSDF(float x, float y, float ax, float ay, float bx, float by, float r) {
float vx = x - ax, vy = y - ay, ux = bx - ax, uy = by - ay;
float t = fmaxf(fminf((vx * ux + vy * uy) / (ux * ux + uy * uy), 1.0f), 0.0f);
float dx = vx - ux * t, dy = vy - uy * t;
return sqrtf(dx * dx + dy * dy) - r;
}
Result unionOp(Result a, Result b) {
return a.sd < b.sd ? a : b;
}
Result scene(float x, float y) {
x = fabsf(x - 0.5f) + 0.5f;
Result a = { capsuleSDF(x, y, 0.75f, 0.25f, 0.75f, 0.75f, 0.05f), 0.0f, 0.2f, 1.5f };
Result b = { capsuleSDF(x, y, 0.75f, 0.25f, 0.50f, 0.75f, 0.05f), 0.0f, 0.2f, 1.5f };
y = fabsf(y - 0.5f) + 0.5f;
Result c = { circleSDF(x, y, 1.05f, 1.05f, 0.05f), 5.0f, 0.0f, 0.0f };
return unionOp(a, unionOp(b, c));
}
void gradient(float x, float y, float* nx, float* ny) {
*nx = (scene(x + EPSILON, y).sd - scene(x - EPSILON, y).sd) * (0.5f / EPSILON);
*ny = (scene(x, y + EPSILON).sd - scene(x, y - EPSILON).sd) * (0.5f / EPSILON);
}
void reflect(float ix, float iy, float nx, float ny, float* rx, float* ry) {
float idotn2 = (ix * nx + iy * ny) * 2.0f;
*rx = ix - idotn2 * nx;
*ry = iy - idotn2 * ny;
}
int refract(float ix, float iy, float nx, float ny, float eta, float* rx, float* ry) {
float idotn = ix * nx + iy * ny;
float k = 1.0f - eta * eta * (1.0f - idotn * idotn);
if (k < 0.0f)
return 0; // Total internal reflection
float a = eta * idotn + sqrtf(k);
*rx = eta * ix - a * nx;
*ry = eta * iy - a * ny;
return 1;
}
float trace(float ox, float oy, float dx, float dy, int depth) {
float t = 1e-3f;
float sign = scene(ox, oy).sd > 0.0f ? 1.0f : -1.0f;
for (int i = 0; i < MAX_STEP && t < MAX_DISTANCE; i++) {
float x = ox + dx * t, y = oy + dy * t;
Result r = scene(x, y);
if (r.sd * sign < EPSILON) {
float sum = r.emissive;
if (depth < MAX_DEPTH && (r.reflectivity > 0.0f || r.eta > 0.0f)) {
float nx, ny, rx, ry, refl = r.reflectivity;
gradient(x, y, &nx, &ny);
nx *= sign;
ny *= sign;
if (r.eta > 0.0f) {
if (refract(dx, dy, nx, ny, sign < 0.0f ? r.eta : 1.0f / r.eta, &rx, &ry))
sum += (1.0f - refl) * trace(x - nx * BIAS, y - ny * BIAS, rx, ry, depth + 1);
else
refl = 1.0f; // Total internal reflection
}
if (refl > 0.0f) {
reflect(dx, dy, nx, ny, &rx, &ry);
sum += refl * trace(x + nx * BIAS, y + ny * BIAS, rx, ry, depth + 1);
}
}
return sum;
}
t += r.sd * sign;
}
return 0.0f;
}
float sample(float x, float y) {
float sum = 0.0f;
for (int i = 0; i < N; i++) {
float a = TWO_PI * (i + (float)rand() / RAND_MAX) / N;
sum += trace(x, y, cosf(a), sinf(a), 0);
}
return sum / N;
}
int main() {
unsigned char* p = img;
for (int y = 0; y < H; y++)
for (int x = 0; x < W; x++, p += 3)
p[0] = p[1] = p[2] = (int)(fminf(sample((float)x / W, (float)y / H) * 255.0f, 255.0f));
svpng(fopen("m2.png", "wb"), W, H, img, 0);
}