Skip to content

Commit d075393

Browse files
committed
+ ch 6 and 7
1 parent e99331e commit d075393

12 files changed

+137392
-47146
lines changed

.vscode/settings.json

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"files.associations": {
3+
"iostream": "cpp",
4+
"iosfwd": "cpp",
5+
"random": "cpp",
6+
"cstdlib": "cpp"
7+
}
8+
}

a.out

59.8 KB
Binary file not shown.

camera.h

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#ifndef CAMERA_H
2+
#define CAMERA_H
3+
4+
#include "rtweekend.h"
5+
6+
class camera {
7+
public:
8+
camera() {
9+
auto aspect_ratio = 16.0 / 9.0;
10+
auto viewport_height = 2.0;
11+
auto viewport_width = aspect_ratio * viewport_height;
12+
auto focal_length = 1.0;
13+
14+
origin = point3(0, 0, 0);
15+
horizontal = vec3(viewport_width, 0.0, 0.0);
16+
vertical = vec3(0.0, viewport_height, 0.0);
17+
lower_left_corner = origin - horizontal/2 - vertical/2 - vec3(0, 0, focal_length);
18+
}
19+
20+
ray get_ray(double u, double v) const {
21+
return ray(origin, lower_left_corner + u*horizontal + v*vertical - origin);
22+
}
23+
24+
private:
25+
point3 origin;
26+
point3 lower_left_corner;
27+
vec3 horizontal;
28+
vec3 vertical;
29+
};
30+
#endif

color.h

+16-4
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,23 @@
55

66
#include <iostream>
77

8-
void write_color(std::ostream &out, color pixel_color) {
8+
void write_color(std::ostream &out, color pixel_color, int samples_per_pixel) {
9+
auto r = pixel_color.x();
10+
auto g = pixel_color.y();
11+
auto b = pixel_color.z();
12+
13+
// Divide the color by the number of samples.
14+
auto scale = 1.0 / samples_per_pixel;
15+
r = sqrt(scale * r);
16+
g = sqrt(scale * g);
17+
b = sqrt(scale * b);
18+
19+
20+
921
// Write the translated [0,255] value of each color component.
10-
out << static_cast<int>(255.999 * pixel_color.x()) << ' '
11-
<< static_cast<int>(255.999 * pixel_color.y()) << ' '
12-
<< static_cast<int>(255.999 * pixel_color.z()) << '\n';
22+
out << static_cast<int>(256 * clamp(r, 0.0, 0.999)) << ' '
23+
<< static_cast<int>(256 * clamp(g, 0.0, 0.999)) << ' '
24+
<< static_cast<int>(256 * clamp(b, 0.0, 0.999)) << '\n';
1325
}
1426

1527
#endif

hittable.h

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#ifndef HITTABLE_H
2+
#define HITTABLE_H
3+
4+
#include "ray.h"
5+
6+
struct hit_record {
7+
point3 p;
8+
vec3 normal;
9+
double t;
10+
bool front_face;
11+
12+
inline void set_face_normal(const ray& r, const vec3& outward_normal) {
13+
front_face = dot(r.direction(), outward_normal) < 0;
14+
normal = front_face ? outward_normal :-outward_normal;
15+
}
16+
17+
};
18+
19+
class hittable {
20+
public:
21+
virtual bool hit(const ray& r, double t_min, double t_max, hit_record& rec) const = 0;
22+
};
23+
24+
#endif

hittable_list.h

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#ifndef HITTABLE_LIST_H
2+
#define HITTABLE_LIST_H
3+
4+
#include "hittable.h"
5+
6+
#include <memory>
7+
#include <vector>
8+
9+
using std::shared_ptr;
10+
using std::make_shared;
11+
12+
class hittable_list : public hittable {
13+
public:
14+
hittable_list() {}
15+
hittable_list(shared_ptr<hittable> object) { add(object); }
16+
17+
void clear() { objects.clear(); }
18+
void add(shared_ptr<hittable> object) { objects.push_back(object); }
19+
20+
virtual bool hit(
21+
const ray& r, double t_min, double t_max, hit_record& rec) const override;
22+
23+
public:
24+
std::vector<shared_ptr<hittable>> objects;
25+
};
26+
27+
bool hittable_list::hit(const ray& r, double t_min, double t_max, hit_record& rec) const {
28+
hit_record temp_rec;
29+
bool hit_anything = false;
30+
auto closest_so_far = t_max;
31+
32+
for (const auto& object : objects) {
33+
if (object->hit(r, t_min, closest_so_far, temp_rec)) {
34+
hit_anything = true;
35+
closest_so_far = temp_rec.t;
36+
rec = temp_rec;
37+
}
38+
}
39+
40+
return hit_anything;
41+
}
42+
43+
#endif

0 commit comments

Comments
 (0)