-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
297 lines (219 loc) · 6.94 KB
/
main.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include "qbmp/qdbmp.h"
int limit = 64;
float fPow = 8;
float shiftValue = 0.95;
float epsilon = 0.0001;
int itLimit = 64;
/**********************************************************************************************************************/
typedef struct Vector {
float x, y, z;
} Vector;
typedef struct Ray {
Vector pos, dir;
} Ray;
typedef struct Pair {
float x, y;
} Pair;
typedef struct Color {
unsigned char r, g, b;
} Color;
typedef struct Hit {
float distance;
int depth;
} Hit;
typedef struct Camera {
Vector pos, dir, up, u, w, v;
float view_plane_distance, ratio, shift_multiplier;
int width, height;
} Camera;
/**********************************************************************************************************************/
float len(Vector * vector) {
return sqrtf(vector->x * vector->x + vector->y * vector->y + vector->z * vector->z);
}
void normalize(Vector * vector) {
float l = len(vector);
if (l == 0.0f) {
vector->x = 0.0;
vector->y = 0.0;
vector->z = 0.0;
}
else {
vector->x = vector->x / l;
vector->y = vector->y / l;
vector->z = vector->z / l;
}
}
void add(Vector * a, Vector * b, Vector * result) {
result->x = a->x + b->x;
result->y = a->y + b->y;
result->z = a->z + b->z;
}
void sub(Vector * a, Vector * b, Vector * result) {
result->x = a->x - b->x;
result->y = a->y - b->y;
result->z = a->z - b->z;
}
void cross(Vector * v1, Vector * v2, Vector * result) {
float a = v2->x, b = v2->y, c = v2->z;
float x = v1->x, y = v1->y, z = v1->z;
result->x = y * c - z * b;
result->y = x * c - a * z;
result->z = x * b - y * a;
}
float dot(Vector * v1, Vector * v2) {
return v1->x * v2->x + v1->y * v2->y + v1->z * v2->z;
}
void scalar_mul(Vector *v, float s, Vector *result) {
result->x = v->x * s;
result->y = v->y * s;
result->z = v->z * s;
}
/**********************************************************************************************************************/
void pow_vec(Vector *v, Vector *result) {
float ph = atanf(v->y / v->x);
float th = acosf(v->z / len(v));
result->x = sinf(fPow * th) * cosf(fPow * ph);
result->y = sinf(fPow * th) * sinf(fPow * ph);
result->z = cosf(fPow * th);
scalar_mul(result, powf(len(v), fPow), result);
}
Pair iterate_z(float dr, Vector z, Vector *c) {
Pair pair;
pair.x = 0.0f;
pair.y = dr;
for (int i = 0;; i++) {
float r = len(&z);
Vector zn;
pow_vec(&z, &zn);
add(&zn, c, &zn);
if (i > limit || r > 2.0f) {
pair.x = r;
pair.y = dr;
break;
} else {
dr = powf(r, fPow - 1.0f) * fPow * dr + 1.0f;
z = zn;
}
}
return pair;
}
float distance(Vector * point) {
Pair p = iterate_z(1.0f, *point, point);
return (0.5f * logf(p.x) * p.x) / p.y;
}
Hit march_ray(Ray *ray, float pathLen) {
Hit hit = { .distance = INFINITY };
Vector temp;
for (int i = 0; i < itLimit; i++) {
float d = distance(&ray->pos);
if (d < epsilon && !(isinf(d) || isnan(d))) {
hit.distance = pathLen;
hit.depth = i;
break;
} else {
scalar_mul(&ray->dir, d * shiftValue, &temp);
add(&temp,&ray->pos, &ray->pos);
pathLen += d * shiftValue;
}
}
return hit;
}
Camera build_camera(
int width,
int height,
float shift_multiplier,
float view_plane_distance,
float ratio,
Vector position,
Vector target,
Vector up) {
Camera camera = {
.pos = position,
.width = width,
.height = height,
.ratio = ratio,
.shift_multiplier = shift_multiplier,
.up = up,
.view_plane_distance = view_plane_distance
};
sub(&target, &position, &camera.dir);
normalize(&camera.dir);
sub(&position, &target, &camera.w);
normalize(&camera.w);
cross(&up, &camera.w, &camera.u);
normalize(&camera.u);
cross(&camera.w, &camera.u, &camera.v);
normalize(&camera.v);
return camera;
}
void move_ray_to_view_plane(Ray * ray, Camera * camera) {
float shift_value = 1.0f / dot(&ray->dir, &camera->dir);
Vector to_add;
scalar_mul(&ray->dir, shift_value * camera->shift_multiplier, &to_add);
add(&ray->pos, &to_add, &ray->pos);
}
Hit trace_ray(Camera *camera, float x, float y) {
Ray ray = {};
Vector temp;
scalar_mul(&camera->u, x * camera->ratio, &ray.dir);
scalar_mul(&camera->v, y, &temp);
add(&temp, &ray.dir, &ray.dir);
scalar_mul(&camera->w, camera->view_plane_distance, &temp);
sub(&ray.dir, &temp, &ray.dir);
normalize(&ray.dir);
ray.pos = camera->pos;
move_ray_to_view_plane(&ray, camera);
return march_ray(&ray, 0.0f);
}
/**********************************************************************************************************************/
float clip(float x, float min, float max) {
return x > max ? max : x < min ? min : x;
}
/**********************************************************************************************************************/
int main() {
int width = 2000, height = 2000;
int m_width = width / 2, m_height = height / 2;
float brightness = 1.25f;
BMP* bmp = BMP_Create(width, height, 32);
Vector camera_position = {.x = 1.0f, .y = 0.0f, .z = 1.0f};
Vector camera_target = {.x = 0.0f, .y = 0.0f, .z = 0.0f};
Vector camera_up = {.x = 0.0f, .y = 1.0f, .z = 0.0f};
float view_plane_distance = 1.0f;
float shift_multiplier = view_plane_distance >= 1.0f ? 0.25f / view_plane_distance : view_plane_distance / 4.0f;
float ratio = 1.0f;
Camera camera = build_camera(
width,
height,
shift_multiplier,
view_plane_distance,
ratio,
camera_position,
camera_target,
camera_up
);
#pragma omp parallel for
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
float x = ((float)i - ((float)m_width)) / ((float)m_width);
float y = ((float)j - ((float)m_height)) / ((float)m_height);
Hit hit = trace_ray(&camera, x, y);
Color color = {.r = 0, .g = 0, .b = 0};
if (!isinf(hit.distance)) {
float color_strength = 1.0f - (float)hit.depth / (float)itLimit;
color.r = 0;
color.g = (unsigned char)clip((color_strength * 255.0f) * brightness, 0.0f, 255.0f);
color.b = (unsigned char)clip((color_strength * 255.0f) * brightness, 0.0f, 255.0f);
}
BMP_SetPixelRGB(bmp, j, i,
color.r,
color.g,
color.b
);
}
}
BMP_WriteFile(bmp, "result.bmp");
return 0;
}