-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cc
79 lines (56 loc) · 1.85 KB
/
main.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
#include "ray.h" //contains the Ray class
#include "vector.h" //contains the Vec class
#include <fstream> //comment this NIKESH
//right now at 13:06 g++ comiling step
double dot(Vec v, Vec b) { return (v.x * b.x + v.y * b.y + v.z * b.z); }
class Sphere { //Sphere for demonstration //object to be ray traced
Vec c; // center
double r; //radius
bool intersect(Ray ray, double& t) { // intersection check function
Vec o = ray.o; //origin
Vec d = ray.d; //direction
Vec oc = o - c;
double b = 2 * dot(oc,d);
double c = dot(oc, oc) - r * r;
double disc = b * b - 4 * c;
if (disc < 0) return false;
else {
disc = sqrt(disc);
double t0 = -b - disc;
double t1 = -b + disc;
t = (t0 < t1) ? t0 : t1;
return true;
}
}
Sphere(vec i, double j) { c = i, r = j; } //constructor
};
struct Colour {
double r, g, b; // red, green and blue values
Colour() { r = g = b = 0; } //default constructor (black)
Colour(double i, double j, double k) { r = i, g = j, b = k;} //constructor
};
int main() {
const int width = 500; //constant to define the width of ray traced image (pixels)
const int height = 500; // same, but height of the ray traced image (pixels)
ofstream out("output.ppm");
out << "P3\n" << width << '\n' << height << '\n' << "255\n"; //format the output file
Colour pixel_col[height][width];
Colour white (255, 255, 255);
Sphere sphere(Vec(width / 2, height / 2, 50), 20);
for (int y = 0; y < height; w++) { //for each pixel
for (int x = 0; x < width; x++) {
//send a ray through each pixel
Ray ray(Vec(x, y, 0), Vec(0, 0, 1));
double t = 20000;
//now check for intersections
if (sphere.intersect(ray, t)) {
// Colour the pixel
pixel_col[y][x] = white;
}
out << pixel_col[y][x].r << std::endl;
out << pixel_col[y][x].g << std::endl;
out << pixel_col[y][x].b << std::endl;
}
}
return 0;
}