-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrt.cpp~
More file actions
88 lines (76 loc) · 2.38 KB
/
rt.cpp~
File metadata and controls
88 lines (76 loc) · 2.38 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
#include "rt.hpp"
#include "obj.hpp"
#include "Light.hpp"
#include <iostream>
void read_objs (OBJ_T **list) {
//declare variables, set *list to null
double x,y,z,r,R,G,B;
Vector ctr;
OBJ_T *node;
*list = NULL;
while(std::cin >> x >> y >> z >> r >> R >> G >> B){
//assign space to node
node = new OBJ_T;
//assign variables to node.
ctr.set(x,y,z);
node->sphere.set(ctr,r);
node->color = (COLOR_T) { .R = R, .G = G, .B = B};
//new.next points to list, and list points to new. then go back to
//beginning of while loop
node->next = *list;
*list = node;
}
}
COLOR_T trace (RAY_T ray, OBJ_T *list, Light light) {
//declare variables, initialize some of them
Vector closest_int_pt,closest_normal,int_pt,normal;
double t,min_t;
min_t = 1000;
COLOR_T color;
color = (COLOR_T) { .R = 0, .G = 0, .B = 0};
OBJ_T *closest_obj;
closest_obj = NULL;
OBJ_T *obj;
//traverse through the object linked list, find the closest object
for (obj = list; obj != NULL;obj = obj->next){
if(obj->sphere.intersect_sphere(ray,t,int_pt,normal)) {
if(t<min_t){
closest_obj = obj;
closest_int_pt = int_pt;
closest_normal = normal;
min_t = t;
}
}
}
//set color according to the light and illumiinate
if(closest_obj != NULL) {
color = light.illuminate(ray,closest_obj->color,closest_int_pt,closest_normal);
}
return color;
}
int main() {
//declare variables, initialize them.
int i,j;
RAY_T ray;
OBJ_T *node;
read_objs(&node);
COLOR_T pixel = (COLOR_T) { .R = 1, .G = 1, .B = 1};
std::cout << ("P6\n 1000 1000\n 255\n");
Light light = Light(5.0,10.0,0);
//
for ( i = 0; i < 1000; i++) {
for ( j = 0; j < 1000; j++ ) {
ray.origin.set(0,0,0);
ray.dir.set(-0.5 + j / 1000.0, 0.5 - i / 1000.0, 1);
ray.dir.normalize();
pixel = trace(ray,node,light);
//cap color
if (pixel.R > 1.0) pixel.R = 1.0;
if (pixel.G > 1.0) pixel.G = 1.0;
if (pixel.B > 1.0) pixel.B = 1.0;
std::cout<<(unsigned char)(pixel.R * 255)
<<(unsigned char)(pixel.G * 255)
<<(unsigned char)(pixel.B * 255);
}
}
}