-
Notifications
You must be signed in to change notification settings - Fork 0
/
RayTracer.cc
115 lines (91 loc) · 3.59 KB
/
RayTracer.cc
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
#include <cmath>
#include <iostream>
#include <string>
#include "Camera.h"
#include "RTWeekend.h"
using std::cout, std::string;
int main(int argc, char* argv[])
{
// Defaults
int imageWidth = 800;
int samples = 25;
int depth = 50;
int fieldOfView = 20;
Vector3 lookFrom(13, 2, 3);
Ray r;
// Using command line arguments for setting render quality
for (int i = 0; i < argc; i++)
{
if (string(argv[i]) == "-Size" && i + 1 < argc)
imageWidth = std::stoi(argv[i + 1]);
else if (string(argv[i]) == "-Depth" && i + 1 < argc)
depth = std::stoi(argv[i + 1]);
else if (string(argv[i]) == "-Samples" && i + 1 < argc)
samples = std::stoi(argv[i + 1]);
else if (string(argv[i]) == "-FOV" && i + 1 < argc)
fieldOfView = std::stoi(argv[i + 1]);
else if (string(argv[i]) == "-CameraPos" && i + 3 < argc)
lookFrom
= Vector3(std::stoi(argv[i + 1]), std::stoi(argv[i + 2]), std::stoi(argv[i + 3]));
}
// World
HittableList world;
auto groundMaterial = make_shared<Lambertian>(Color(0.1, 0.1, 0));
world.Add(make_shared<Sphere>(Point3(0, -1000, -1), 1000, groundMaterial));
for (int a = -11; a < 11; a++)
{
for (int b = -11; b < 11; b++)
{
double chooseMaterial = RandomDouble();
Point3 center(a + 0.9 * RandomDouble(), 0.2, b + 0.9 * RandomDouble());
if ((center - Point3(4, 0.2, 0)).Length() > 0.9)
{
shared_ptr<Material> sphereMaterial;
if (chooseMaterial < 0.40) // Diffuse
{
Color albedo = Color::Random() * Color::Random();
sphereMaterial = make_shared<Lambertian>(albedo);
double chooseBounce = RandomDouble();
if (chooseBounce < 0.5)
{
Point3 center2 = center + Vector3(0, RandomDouble(0, 0.5), 0);
world.Add(make_shared<Sphere>(center, center2, 0.2, sphereMaterial));
continue;
}
}
else if (chooseMaterial < 0.70)
{
Color albedo = Color::Random(0.5, 1);
double fuzz = RandomDouble(0, 0.4);
sphereMaterial = make_shared<Metal>(albedo, fuzz);
}
else // Glass
{
Color albedo = Color::Random(0.8, 1);
sphereMaterial = make_shared<Dielectric>(1.5, albedo);
}
world.Add(make_shared<Sphere>(center, 0.2, sphereMaterial));
}
}
}
auto backMaterial = make_shared<Lambertian>(Color(0.8, 0.1, 0.1));
auto middleMaterial = make_shared<Metal>(Color(0.7, 0.6, 0.5), 0.03);
auto frontMaterial = make_shared<Dielectric>(1.33, Color(0.95, 0.95, 0.8));
world.Add(make_shared<Sphere>(Point3(-4, 1, 0), 1.0, backMaterial));
world.Add(make_shared<Sphere>(Point3(0, 1, 0), 1.0, middleMaterial));
world.Add(make_shared<Sphere>(Point3(4, 1, 0), 1.0, frontMaterial));
world = HittableList(make_shared<BVHNode>(world));
Camera camera;
camera.aspectRatio = 16.0 / 9.0;
camera.imageWidth = imageWidth;
camera.samplesPerPixel = samples;
camera.maxDepth = depth;
camera.verticalFOV = fieldOfView;
camera.lookFrom = lookFrom;
camera.lookAt = Point3(0, 0, 0);
camera.vUp = Vector3(0, 1, 0);
camera.defocusAngle = 0.2;
camera.focusDistance = 8;
camera.Render(world);
return 0;
}