-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathmain.cpp
56 lines (48 loc) · 1.36 KB
/
main.cpp
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
#include "Object.h"
#include "Camera.h"
#include "Light.h"
#include "read_json.h"
#include "write_ppm.h"
#include "viewing_ray.h"
#include "raycolor.h"
#include <Eigen/Core>
#include <vector>
#include <iostream>
#include <memory>
#include <limits>
#include <functional>
int main(int argc, char * argv[])
{
Camera camera;
std::vector< std::shared_ptr<Object> > objects;
std::vector< std::shared_ptr<Light> > lights;
// Read a camera and scene description from given .json file
read_json(
argc<=1?"../data/sphere-and-plane.json":argv[1],
camera,
objects,
lights);
int width = 640;
int height = 360;
std::vector<unsigned char> rgb_image(3*width*height);
// For each pixel (i,j)
for(unsigned i=0; i<height; ++i)
{
for(unsigned j=0; j<width; ++j)
{
// Set background color
Eigen::Vector3d rgb(0,0,0);
// Compute viewing ray
Ray ray;
viewing_ray(camera,i,j,width,height,ray);
// Shoot ray and collect color
raycolor(ray,1.0,objects,lights,0,rgb);
// Write double precision color into image
auto clamp = [](double s){ return std::max(std::min(s,1.0),0.0);};
rgb_image[0+3*(j+width*i)] = 255.0*clamp(rgb(0));
rgb_image[1+3*(j+width*i)] = 255.0*clamp(rgb(1));
rgb_image[2+3*(j+width*i)] = 255.0*clamp(rgb(2));
}
}
write_ppm("rgb.ppm",rgb_image,width,height,3);
}