-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.cpp
More file actions
138 lines (109 loc) · 3.62 KB
/
Main.cpp
File metadata and controls
138 lines (109 loc) · 3.62 KB
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
#include "scene.hpp"
#include "Sphere.hpp"
#include "Plane.hpp"
#include "Light.hpp"
#include "Vec.hpp"
#include <fstream>
void init(Scene *scene) {
Object* node;
std::ifstream fp("samplescene.txt");
char c;
while (fp >> c) {
if (c == 's') { // sphere detected
Vec center;
double radius;
double x, y, z;
double r, g, b;
fp >> x >> y >> z;
center.set(x, y, z);
fp >> radius;
fp >> r >> g >> b;
Color color(r, g, b);
// pass color into constructor twice for simplicity, as this will disable sphere checkering
Sphere* sphere = new Sphere(center, radius, color, color);
scene->objs.push_back(sphere); // add sphere to linked list
}
else if (c == 'p') { // plane detected
Vec normal;
int checker;
double D;
double x, y, z;
fp >> x >> y >> z;
normal.set(x, y, z);
fp >> D;
double r1, r2, g1, g2, b1, b2;
fp >> r1 >> g1 >> b1;
Color color1(r1, g1, b1);
fp >> r2 >> g2 >> b2;
Color color2(r2, g2, b2);
Plane* plane = new Plane(normal, D, color1, color2);
scene->objs.push_back(plane); // add plane to linked list
}
}
fp.close();
}
Color trace(const RAY_T& ray, const Scene& scene) {
Color color(0.3, 0.3, 0.5); // hardcoded background color
double closest_t = 1000.0;
Vec closest_int_pt;
Vec closest_normal;
Object* closest_obj = nullptr;
// iterate through the scene
for (std::list<Object*>::const_iterator i = scene.objs.begin(); i != scene.objs.end(); ++i) {
Object* curr = *i;
if (curr != nullptr) {
double t;
Vec int_pt;
Vec normal;
if (curr->intersect(ray, t, int_pt, normal)) {
if (t < closest_t && t > 0) {
closest_t = t;
closest_int_pt = int_pt;
closest_normal = normal;
closest_obj = curr;
}
}
}
}
if (closest_t < 1000.0 && closest_obj != nullptr) {
color = scene.light.illuminate(ray, scene, closest_obj, closest_int_pt, closest_normal);
}
color.cap(); // cap the color before returning
return color;
}
int main() {
Scene scene;
Vec lightPosition(5.0, 10.0, -2.0); // hardcoded default
scene.light = Light(lightPosition);
init(&scene);
// hardcoded defaults
int width = 640;
int height = 480;
scene.pixel_size = 1.0 / height;
scene.start_y = 0.5;
scene.start_x = -(double)width / height / 2.0;
FILE *fp;
fp = fopen("image.ppm", "wb");
fprintf(fp, "P6\n%d %d\n255\n", width, height); // initialize immage
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
// set ray using Vec operations
RAY_T ray;
double rayX, rayY, rayZ;
ray.origin.set(0.0, 0.0, 0.0);
rayX = scene.start_x + x * scene.pixel_size;
rayY = scene.start_y - y * scene.pixel_size;
rayZ = 1.0; // hardcoded default
ray.dir.set(rayX, rayY, rayZ);
ray.dir.normalize();
Color color = trace(ray, scene);
// write to file
unsigned char R = (unsigned char)(color.getR() * 255);
unsigned char G = (unsigned char)(color.getG() * 255);
unsigned char B = (unsigned char)(color.getB() * 255);
fprintf(fp, "%c%c%c", R, G, B);
}
}
fclose(fp);
return 0;
}