-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathbasic_raytracer.cpp
89 lines (76 loc) · 2.33 KB
/
basic_raytracer.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
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
#include <fstream>
#include <cmath>
struct Vec3 {
double x,y,z;
Vec3(double x, double y, double z) : x(x), y(y), z(z) {}
Vec3 operator + (const Vec3& v) const { return Vec3(x+v.x, y+v.y, z+v.z); }
Vec3 operator - (const Vec3& v) const { return Vec3(x-v.x, y-v.y, z-v.z); }
Vec3 operator * (double d) const { return Vec3(x*d, y*d, z*d); }
Vec3 operator / (double d) const { return Vec3(x/d, y/d, z/d); }
Vec3 normalize() const {
double mg = sqrt(x*x + y*y + z*z);
return Vec3(x/mg,y/mg,z/mg);
}
};
inline double dot(const Vec3& a, const Vec3& b) {
return (a.x*b.x + a.y*b.y + a.z*b.z);
}
struct Ray {
Vec3 o,d;
Ray(const Vec3& o, const Vec3& d) : o(o), d(d) {}
};
struct Sphere {
Vec3 c;
double r;
Sphere(const Vec3& c, double r) : c(c), r(r) {}
Vec3 getNormal(const Vec3& pi) const { return (pi - c) / r; }
bool intersect(const Ray& ray, double &t) const {
const Vec3 o = ray.o;
const Vec3 d = ray.d;
const Vec3 oc = o - c;
const double b = 2 * dot(oc, d);
const double c = dot(oc, oc) - r*r;
double disc = b*b - 4 * c;
if (disc < 1e-4) return false;
disc = sqrt(disc);
const double t0 = -b - disc;
const double t1 = -b + disc;
t = (t0 < t1) ? t0 : t1;
return true;
}
};
void clamp255(Vec3& col) {
col.x = (col.x > 255) ? 255 : (col.x < 0) ? 0 : col.x;
col.y = (col.y > 255) ? 255 : (col.y < 0) ? 0 : col.y;
col.z = (col.z > 255) ? 255 : (col.z < 0) ? 0 : col.z;
}
int main() {
const int H = 500;
const int W = 500;
const Vec3 white(255, 255, 255);
const Vec3 black(0, 0, 0);
const Vec3 red(255, 0, 0);
const Sphere sphere(Vec3(W*0.5, H*0.5, 50), 50);
const Sphere light(Vec3(0, 0, 50), 1);
std::ofstream out("out.ppm");
out << "P3\n" << W << ' ' << H << ' ' << "255\n";
double t;
Vec3 pix_col(black);
for (int y = 0; y < H; ++y) {
for (int x = 0; x < W; ++x) {
pix_col = black;
const Ray ray(Vec3(x,y,0),Vec3(0,0,1));
if (sphere.intersect(ray, t)) {
const Vec3 pi = ray.o + ray.d*t;
const Vec3 L = light.c - pi;
const Vec3 N = sphere.getNormal(pi);
const double dt = dot(L.normalize(), N.normalize());
pix_col = (red + white*dt) * 0.5;
clamp255(pix_col);
}
out << (int)pix_col.x << ' '
<< (int)pix_col.y << ' '
<< (int)pix_col.z << '\n';
}
}
}