-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
115 lines (96 loc) · 3.88 KB
/
main.cpp
File metadata and controls
115 lines (96 loc) · 3.88 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
#include <iostream>
#include <memory>
#include <limits>
#include <math.h>
#include <algorithm>
#include "include/vector3.hpp"
#include "include/hittables.hpp"
#include "include/ray.hpp"
#include "include/camera.hpp"
#include "include/materials.hpp"
#include "include/light.hpp"
colour image(Ray& ray, Hittables world, Lights light_world, int depth=5)
{
hit_record record;
if (depth <= 0)
// return Gradient(colour(1,1,1), colour(0.5, 0.7, 1.0)).get_colour(ray);
return colour(0,0,0);
if (world.intersect(ray, std::numeric_limits<double>::infinity(), record))
{
Ray scattered = record.material->scatter(ray, record);
return image(scattered, world, light_world, depth-1) * record.col;
}
// return Gradient(colour(1,1,1), colour(0.5, 0.7, 1.0)).get_colour(ray);
return light_world.getLight(ray);
}
void generate_image()
{
//image dimensions
const double aspect_ratio = 6.0/13.0;
const int width = 375; // HD
const int height = width / aspect_ratio;
std::cout << "P3\n" << width << ' ' << height << "\n255\n";
//camera -- at (0,0,0)
Camera camera(point3(0,0,0), 2.0, aspect_ratio, 1.0);
//lights - LIGHT CONSTRUCTION
//set scaling factor to max radius of intensity (all beyond is black)
Lights light_world;
// light_world.add(std::make_shared<Light>(camera.getUpperLeftCorner()+vector3(2,1,0), 10, colour(1.0,1.0,1.0)));
for (int i=0; i<10; i++)
{
light_world.add(std::make_shared<Light>(camera.getUpperLeftCorner()+vector3(0.4*i,0,0), 0.4, randColour()));
}
// WORLD CONSTRUCTION
std::shared_ptr<Diffuse> ground = std::make_shared<Diffuse>(colour(0.8, 0.8, 0.0), "/home/meshva/cpp_projects/RayTracer/textures/im_2.jpg");
std::shared_ptr<Metal> central = std::make_shared<Metal>(colour(0.7, 0.3, 0.3));
std::shared_ptr<Metal> solid = std::make_shared<Metal>(colour(0.2,0.2,0.2));
std::shared_ptr<Glass> glass = std::make_shared<Glass>(2.0);
Hittables world;
world.add(std::make_shared<Sphere>(100,0,-100.5,-1, ground));
// world.add(std::make_shared<Sphere>(0.5,-1,0,-1, central, 0, 2*PI, 0, PI));
// world.add(std::make_shared<Sphere>(0.5,0,0,-1, solid));
// world.add(std::make_shared<Sphere>(0.5,1,0,-1, central));
// world.add(std::make_shared<Rectangle>(point3(-1,0.5,-1), point3(-0.5,-0.5,-1.5), ground, true));
// world.add(std::make_shared<Rectangle>(point3(-0.9,0.4,-1.2), point3(-0.4,-0.4,-1.4), ground, true));
// world.add(std::make_shared<TriPlane>(point3(0,0,-1), point3(0,1,-1), point3(1,0,-2), central, false));
//make many spheres on ground
double r = 102;
for (int i=0; i<100; i++)
{
double z = random_bounds(-3,0.2);
double x = random_bounds(-2,2);
//solve y in bounds
double y = sqrt(r*r - pow(z+1,2) - x*x) - 102.5 + 0.1;
std::cerr << x << " , " << y << " , " << z << '\n' << std::flush;
world.add(std::make_shared<Sphere>(0.1,x,y,z, central));
}
//constants
const int sample_size = 40;
// RENDER IMAGE
for (int r=height-1; r >=0; r--)
{
if (r % 10 == 0)
std::cerr << "line: " << r << '\n' << std::flush;
for(int c=0; c < width; c++)
{
// SMOOTH EDGES -- use multiple u/v to average vectors nearby
colour pixel_col(0,0,0);
for (int i=0; i<sample_size; i++)
{
double u = (double(c) + random_bounds(-1,1)) / (width-1);
double v = (double(r) + random_bounds(-1,1)) / (height-1);
//ray starting from origin (loc of camera) in direction u,v
Ray ray = camera.rayFromCamera(u, v);
pixel_col += image(ray, world, light_world);
}
pixel_col /= sample_size;
pixel_col.print();
}
}
std::cerr << "COMPLETE" << std::flush;
}
int main ()
{
generate_image();
return 0;
}