forked from eldesh/aobench_sml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaobench.c
executable file
·373 lines (286 loc) · 8.5 KB
/
aobench.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/**
*
* [aobench]
* https://code.google.com/p/aobench/
* real world floating point benchmark
*
* $ gcc -std=gnu99 -Wall --pedantic-errors -o aobench aobench.c
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <math.h>
#define WIDTH 256
#define HEIGHT 256
#define NSUBSAMPLES 2
#define NAO_SAMPLES 8
typedef struct _vec
{
double x;
double y;
double z;
} vec;
typedef struct _Isect
{
double t;
vec p;
vec n;
int hit;
} Isect;
typedef struct _Sphere
{
vec center;
double radius;
} Sphere;
typedef struct _Plane
{
vec p;
vec n;
} Plane;
typedef struct _Ray
{
vec org;
vec dir;
} Ray;
Sphere spheres[3];
Plane plane;
static void print_vec (vec v) {
printf ("vec={x=%5.3lf, y=%5.3lf, z=%5.3lf}", v.x, v.y, v.z);
}
static void print_ray(Ray ray) __attribute__ ((unused));
static void print_isect(Isect isect) __attribute__ ((unused));
void print_ray(Ray ray) {
printf ("{ org="); print_vec(ray.org);
printf (", dir="); print_vec(ray.dir);
printf ("}");
}
void print_isect(Isect isect) {
printf ("isect{t=%5.3lf, ", isect.t);
print_vec(isect.p);
printf (", ");
print_vec(isect.n);
printf (" t:%d}\n", isect.hit);
}
static double vdot(vec v0, vec v1)
{
return v0.x * v1.x + v0.y * v1.y + v0.z * v1.z;
}
static void vcross(vec *c, vec v0, vec v1)
{
c->x = v0.y * v1.z - v0.z * v1.y;
c->y = v0.z * v1.x - v0.x * v1.z;
c->z = v0.x * v1.y - v0.y * v1.x;
}
static void vnormalize(vec *c)
{
double length = sqrt(vdot((*c), (*c)));
if (fabs(length) > 1.0e-17) {
c->x /= length;
c->y /= length;
c->z /= length;
}
}
void
ray_sphere_intersect(Isect *isect, const Ray *ray, const Sphere *sphere)
{
vec rs;
rs.x = ray->org.x - sphere->center.x;
rs.y = ray->org.y - sphere->center.y;
rs.z = ray->org.z - sphere->center.z;
double B = vdot(rs, ray->dir);
double C = vdot(rs, rs) - sphere->radius * sphere->radius;
double D = B * B - C;
if (D > 0.0) {
double t = -B - sqrt(D);
if ((t > 0.0) && (t < isect->t)) {
isect->t = t;
isect->hit = 1;
isect->p.x = ray->org.x + ray->dir.x * t;
isect->p.y = ray->org.y + ray->dir.y * t;
isect->p.z = ray->org.z + ray->dir.z * t;
isect->n.x = isect->p.x - sphere->center.x;
isect->n.y = isect->p.y - sphere->center.y;
isect->n.z = isect->p.z - sphere->center.z;
vnormalize(&(isect->n));
}
}
}
void
ray_plane_intersect(Isect *isect, const Ray *ray, const Plane *plane)
{
double d = -vdot(plane->p, plane->n);
double v = vdot(ray->dir, plane->n);
if (fabs(v) < 1.0e-17) return;
double t = -(vdot(ray->org, plane->n) + d) / v;
if ((t > 0.0) && (t < isect->t)) {
isect->t = t;
isect->hit = 1;
isect->p.x = ray->org.x + ray->dir.x * t;
isect->p.y = ray->org.y + ray->dir.y * t;
isect->p.z = ray->org.z + ray->dir.z * t;
isect->n = plane->n;
}
}
void
orthoBasis(vec *basis, vec n)
{
basis[2] = n;
basis[1].x = 0.0; basis[1].y = 0.0; basis[1].z = 0.0;
if ((n.x < 0.6) && (n.x > -0.6)) {
basis[1].x = 1.0;
} else if ((n.y < 0.6) && (n.y > -0.6)) {
basis[1].y = 1.0;
} else if ((n.z < 0.6) && (n.z > -0.6)) {
basis[1].z = 1.0;
} else {
basis[1].x = 1.0;
}
vcross(&basis[0], basis[1], basis[2]);
vnormalize(&basis[0]);
vcross(&basis[1], basis[2], basis[0]);
vnormalize(&basis[1]);
}
void ambient_occlusion(vec *col, const Isect *isect)
{
int i, j;
int ntheta = NAO_SAMPLES;
int nphi = NAO_SAMPLES;
double eps = 0.0001;
vec p;
p.x = isect->p.x + eps * isect->n.x;
p.y = isect->p.y + eps * isect->n.y;
p.z = isect->p.z + eps * isect->n.z;
vec basis[3];
orthoBasis(basis, isect->n);
double occlusion = 0.0;
for (j = 0; j < ntheta; j++) {
for (i = 0; i < nphi; i++) {
double theta = sqrt(drand48());
double phi = 2.0 * M_PI * drand48();
double x = cos(phi) * theta;
double y = sin(phi) * theta;
double z = sqrt(1.0 - theta * theta);
// local -> global
double rx = x * basis[0].x + y * basis[1].x + z * basis[2].x;
double ry = x * basis[0].y + y * basis[1].y + z * basis[2].y;
double rz = x * basis[0].z + y * basis[1].z + z * basis[2].z;
Ray ray;
ray.org = p;
ray.dir.x = rx;
ray.dir.y = ry;
ray.dir.z = rz;
Isect occIsect;
occIsect.t = 1.0e+17;
occIsect.hit = 0;
ray_sphere_intersect(&occIsect, &ray, &spheres[0]);
ray_sphere_intersect(&occIsect, &ray, &spheres[1]);
ray_sphere_intersect(&occIsect, &ray, &spheres[2]);
ray_plane_intersect (&occIsect, &ray, &plane);
if (occIsect.hit) occlusion += 1.0;
}
}
occlusion = (ntheta * nphi - occlusion) / (double)(ntheta * nphi);
col->x = occlusion;
col->y = occlusion;
col->z = occlusion;
}
unsigned char
clamp(double f)
{
int i = (int)(f * 255.5);
if (i < 0) i = 0;
if (i > 255) i = 255;
return (unsigned char)i;
}
void
render(unsigned char *img, int w, int h, int nsubsamples)
{
int x, y;
int u, v;
double *fimg = (double *)malloc(sizeof(double) * w * h * 3);
memset((void *)fimg, 0, sizeof(double) * w * h * 3);
for (y = 0; y < h; y++) {
for (x = 0; x < w; x++) {
for (v = 0; v < nsubsamples; v++) {
for (u = 0; u < nsubsamples; u++) {
double px = (x + (u / (double)nsubsamples) - (w / 2.0)) / (w / 2.0);
double py = -(y + (v / (double)nsubsamples) - (h / 2.0)) / (h / 2.0);
Ray ray;
ray.org.x = 0.0;
ray.org.y = 0.0;
ray.org.z = 0.0;
ray.dir.x = px;
ray.dir.y = py;
ray.dir.z = -1.0;
vnormalize(&(ray.dir));
Isect isect;
isect.t = 1.0e+17;
isect.hit = 0;
isect.p = (vec){.x=0.0, .y=0.0, .z=0.0};
isect.n = (vec){.x=0.0, .y=0.0, .z=0.0};
ray_sphere_intersect(&isect, &ray, &spheres[0]);
ray_sphere_intersect(&isect, &ray, &spheres[1]);
ray_sphere_intersect(&isect, &ray, &spheres[2]);
ray_plane_intersect (&isect, &ray, &plane);
if (isect.hit) {
vec col;
ambient_occlusion(&col, &isect);
fimg[3 * (y * w + x) + 0] += col.x;
fimg[3 * (y * w + x) + 1] += col.y;
fimg[3 * (y * w + x) + 2] += col.z;
}
}
}
fimg[3 * (y * w + x) + 0] /= (double)(nsubsamples * nsubsamples);
fimg[3 * (y * w + x) + 1] /= (double)(nsubsamples * nsubsamples);
fimg[3 * (y * w + x) + 2] /= (double)(nsubsamples * nsubsamples);
img[3 * (y * w + x) + 0] = clamp(fimg[3 *(y * w + x) + 0]);
img[3 * (y * w + x) + 1] = clamp(fimg[3 *(y * w + x) + 1]);
img[3 * (y * w + x) + 2] = clamp(fimg[3 *(y * w + x) + 2]);
}
}
}
void
init_scene()
{
spheres[0].center.x = -2.0;
spheres[0].center.y = 0.0;
spheres[0].center.z = -3.5;
spheres[0].radius = 0.5;
spheres[1].center.x = -0.5;
spheres[1].center.y = 0.0;
spheres[1].center.z = -3.0;
spheres[1].radius = 0.5;
spheres[2].center.x = 1.0;
spheres[2].center.y = 0.0;
spheres[2].center.z = -2.2;
spheres[2].radius = 0.5;
plane.p.x = 0.0;
plane.p.y = -0.5;
plane.p.z = 0.0;
plane.n.x = 0.0;
plane.n.y = 1.0;
plane.n.z = 0.0;
}
void
saveppm(const char *fname, int w, int h, unsigned char *img)
{
FILE *fp;
fp = fopen(fname, "wb");
assert(fp);
fprintf(fp, "P6\n");
fprintf(fp, "%d %d\n", w, h);
fprintf(fp, "255\n");
fwrite(img, w * h * 3, 1, fp);
fclose(fp);
}
int
main(int argc, char **argv)
{
unsigned char *img = (unsigned char *)malloc(WIDTH * HEIGHT * 3);
init_scene();
render(img, WIDTH, HEIGHT, NSUBSAMPLES);
saveppm(argc==2 ? argv[1] : "ao.ppm", WIDTH, HEIGHT, img);
return 0;
}