forked from dendibakh/perf-ninja
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathao_helpers.cpp
51 lines (40 loc) · 851 Bytes
/
ao_helpers.cpp
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
#include "ao.h"
double vdot(vec v0, vec v1)
{
return v0.x * v1.x + v0.y * v1.y + v0.z * v1.z;
}
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;
}
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;
}
}
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
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);
}