-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
86 lines (66 loc) · 2.21 KB
/
Copy pathmain.cpp
File metadata and controls
86 lines (66 loc) · 2.21 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
#include <SDL2/SDL.h>
#include <iostream>
#include <cassert>
#include "sdl_canvas.h"
#include "rayt.h"
#include "camera.h"
#include "color.h"
#include "hittable_list.h"
#include "material.h"
#include "sphere.h"
#include "scenes.h"
using std::cout;
using std::endl;
using std::cerr;
Color ray_color(const Ray& r, const hittable& world, int depth){
hit_record rec;
if(depth <= 0){
return Color(0,0,0);
}
if(world.hit(r, 0.001, infinity, rec)){
Ray scattered;
Color attenuation;
if(rec.mat_ptr->scatter(r, rec, attenuation, scattered)){
return attenuation * ray_color(scattered, world, depth-1);
}
return Color(0,0,0);
}
Vec3 unit_dir = unit_vector(r.direction());
auto t = 0.5 * (unit_dir.y() + 1.0);
return (1.0 - t) * Color(1.0,1.0,1.0) + t * Color(0.5, 0.7, 1.0);
}
int main(){
//Canvas
const auto aspect_ratio = 3.0/2.0;
const int image_width = 1200;
const int image_height = static_cast<int>(image_width/aspect_ratio);
const int samples_per_pixel = 100;
const int max_depth = 6;
SDLCanvas Canvas(image_width, image_height);
//World
auto world = random_scene();
Point3 lookfrom(13,2,3);
Point3 lookat(0,0,0);
Vec3 vup(0,1,0);
auto dist_to_focus = 10.0;
auto aperture = 0.1;
camera cam(lookfrom, lookat, vup, 20, aspect_ratio, aperture, dist_to_focus);
//Render
Canvas.putPixel(0, 0, 255,255,255);
for (int j = image_height-1; j >= 0; --j) {
cerr << "\rScanlines remaining: " << j << ' ' << std::flush;
for (int i = 0; i < image_width; ++i) {
Color pixel_color(0,0,0);
for(int s = 0; s < samples_per_pixel; ++s){
auto u = (i + random_double()) / (image_width-1);
auto v = (j + random_double()) / (image_height-1);
Ray r = cam.get_ray(u,v);
pixel_color += ray_color(r, world, max_depth);
}
Color final_color = write_color(pixel_color, samples_per_pixel);
Canvas.appendPixel(final_color);
}
}
Canvas.updateCanvas();
SDL_Delay(10000);
}