-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainmp.cpp
More file actions
88 lines (74 loc) · 2.61 KB
/
Copy pathMainmp.cpp
File metadata and controls
88 lines (74 loc) · 2.61 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 "camera.h"
#include "color.h"
#include "util.h"
#include "bvh.h"
#include "cenas.h"
#include <iostream>
#include <stdlib.h>
color rayColor(const ray& r, const color& background, const objeto& mundo, int depth) {
hitRecord rec;
if (depth <= 0)
return color(0,0,0);
if (!mundo.hit(r, 0.001, infinity, rec))
return background;
ray refletido;
color atenuacao;
color raioEmitido = rec.ptrMat -> emitido(rec.u, rec.v, rec.p);
if(!rec.ptrMat->espalhamento(r, rec, atenuacao, refletido))
return raioEmitido;
return raioEmitido + atenuacao * rayColor(refletido, background, mundo, depth-1);
}
void write_image(uint8_t *image, int nx, int ny) {
std::cout << "P3\n" << nx << " " << ny << "\n255\n";
for (int j = ny-1; j >= 0; j--) {
for (int i = 0; i < nx; ++i) {
std::cout << static_cast<int>(image[3*(j*nx+i)+0]) << " ";
std::cout << static_cast<int>(image[3*(j*nx+i)+1]) << " ";
std::cout << static_cast<int>(image[3*(j*nx+i)+2]) << std::endl;
}
}
}
int main() {
const auto aspectRatio = 16.0/9.0;
const int comp = 1980;
const int alt = static_cast<int>(comp/aspectRatio);
const int samplesPorPixel = 10;
const int profundidade = 100;
const int etaMeio = 1.0;
//auto mundo = random_scene();
//auto mundo = terra();
//auto mundo = two_spheres();
//auto mundo = luzSimples();
//auto mundo = cornellBox();
//listaObjetos world;
//world.add(make_shared<nodeBVH>(mundo, 0, 1));
auto world = nextWeekFinalScene();
point3 lookfrom(478,278,-600);
point3 lookat(278,278,0);
vec3 vup(0,1,0);
auto dist_to_focus = 10.0;
auto aperture = 0.0;
auto vfov = 35.0;
const color background(0,0,0);
camera cam(lookfrom, lookat, vup, vfov, aspectRatio, aperture, dist_to_focus,0.0,1.0);
std::vector<uint8_t> image(alt * comp * 3);
#pragma omp parallel for
for (int j = alt-1; j >= 0; j--) {
for (int i = 0; i < comp; i++) {
color pixelColor(0,0,0);
for(int s = 0; s < samplesPorPixel; s++){
auto u = (i + randDouble()) / (comp);
auto v = (j + randDouble()) / (alt);
ray r = cam.getRay(u, v);
pixelColor += rayColor(r,background, world, profundidade);
}
pixelColor /= double(samplesPorPixel);
pixelColor = vec3( sqrt(pixelColor[0]), sqrt(pixelColor[1]), sqrt(pixelColor[2]) );
image[3*(j*comp+i)+0] = int(256 * clamp(pixelColor[0], 0.0, 0.999));
image[3*(j*comp+i)+1] = int(256 * clamp(pixelColor[1], 0.0, 0.999));
image[3*(j*comp+i)+2] = int(256 * clamp(pixelColor[2], 0.0, 0.999));
}
}
write_image(&image[0], comp, alt);
std::cerr << "\nDone.\n";
}