-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
91 lines (82 loc) · 3.66 KB
/
main.cpp
File metadata and controls
91 lines (82 loc) · 3.66 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
#include <iostream>
#include <fstream>
#include <memory>
#include <vector>
#include "src/Utils.h"
#include "src/camera.h"
#include "src/hitable.h"
#include "src/hitablelist.h"
#include "src/material.h"
#include "src/vec3.h"
#include "src/ray.h"
#include "src/sphere.h"
float hit_sphere(const vec3& center, const float radius, const ray& r) {
const vec3 oc = r.origin() - center;
const float a = dot(r.direction(), r.direction());
const float b = 2.f * dot(oc, r.direction());
const float c = dot(oc, oc) - radius * radius;
const float discriminant = b * b - 4 * a * c;
if (discriminant < 0) {
return -1.f;
}
return (-b - std::sqrt(discriminant)) / (2.f * a); // we take the closest point thats why whe just do - sqrt (discriminant) instead of +- sqrt...
}
vec3 color (const ray& r, const std::shared_ptr<hitable>& world, const int depth) {
hit_record record;
if (world->hit(r, 0.001f, std::numeric_limits<float>::max(), record)) {
ray scattered;
vec3 attenuation;
if (depth < 50 && record.mat_ptr->scatter(r, record, attenuation, scattered)) {
return attenuation * color(scattered, world, depth + 1);
}
else {
return {0, 0, 0};
}
}
const vec3 unit_direction = unit_vector(r.direction());
const float t = 0.5f * (unit_direction.y() + 1.f);
return (1.f - t) * vec3(1.f, 1.f, 1.f) + t * vec3(0.5f, 0.7f, 1.f);
}
int main() {
std::ofstream f("../preview.ppm");
if (!f.is_open()) {
std::cerr << "Error opening the file";
}
if (f.is_open()) {
constexpr int ny = 200;
constexpr int nx = 400;
constexpr int ns = 100;
f << "P3\n" << nx << " " << ny << "\n255\n";
std::vector<std::shared_ptr<hitable>> list;
std::shared_ptr<material> c1 = std::make_shared<lambertian>(vec3(0.1f, 0.2f, 0.5f), 0.3f);
std::shared_ptr<material> c2 = std::make_shared<lambertian>(vec3(0.8f, 0.8f, 0.f), 0.2f);
std::shared_ptr<material> c3 = std::make_shared<metal>(vec3(0.8f, 0.6f, 0.2f), 0.1f);
std::shared_ptr<material> c4 = std::make_shared<dielectric>(1.5f);
list.emplace_back(std::make_shared<sphere>(vec3{0, 0, -1}, 0.5f, c1));
list.emplace_back(std::make_shared<sphere>(vec3{0, -100.5f, -1}, 100, c2));
list.emplace_back(std::make_shared<sphere>(vec3{1, 0, -1}, 0.5f, c3));
list.emplace_back(std::make_shared<sphere>(vec3{-1, 0, -1}, 0.5f, c4));
list.emplace_back(std::make_shared<sphere>(vec3{-1, 0, -1}, -0.45f, c4));
const std::shared_ptr<hitable> world = std::make_shared<hitablelist>(list, list.size());
camera cam({-2, 2, 1}, {0, 0, -1}, {0, 1, 0}, 40, static_cast<float>(nx) / static_cast<float>(ny));
for (int j = ny - 1; j >= 0; j--) {
for (int i = 0; i < nx; i++) {
vec3 col(0, 0, 0);
for (int s = 0; s < ns; s++){
const float u = (static_cast<float>(i) + Utils::random(0.f, 0.99f)) / static_cast<float>(nx);
const float v = (static_cast<float>(j) + Utils::random(0.f, 0.99f)) / static_cast<float>(ny);
ray r = cam.get_ray(u, v);
vec3 p = r.point_at_parameter(2.f);
col += color(r, world, 0);
}
col /= static_cast<float>(ns);
col = vec3(std::sqrt(col[0]), std::sqrt(col[1]), std::sqrt(col[2])); // gamma 2
const int ir = int(255.99*col[0]);
const int ig = int(255.99*col[1]);
const int ib = int(255.99*col[2]);
f << ir << " " << ig << " " << ib << "\n";
}
}
}
return 1;
}