-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainHeader.h
103 lines (98 loc) · 2.76 KB
/
MainHeader.h
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
#ifndef MAINHEADER_H
#define MAINHEADER_H
#include "MyVector.h"
#include "Sphere.h"
#include "Intersection.h"
#include "Subtraction.h"
#include "Plane.h"
#include "Square.h"
#include "Torus.h"
#include "Color.h"
#include "Eye.h"
#include "Ray.h"
#include "Image.h"
#include "SphereGlass.h"
#include "SphereMoving.h"
#include "Light.h"
#include "Scene.h"
#include <chrono>
#include <algorithm>
#include <iterator>
using namespace std;
double minPositiveTime(vector<double> vec, int n)
{
int aux = -1;
double val = 10000;
for (int i = 0; i < n; i++)
{
if (vec[i] < val && vec[i] > 0)
{
val = vec[i];
aux = i;
}
}
return aux;
}
Color PaintPixel(Scene scene, Ray *ray, int Bounces)
{
if (Bounces <= 0)
{
return Color(0, 0, 0);
}
bool goesToInfinity = true;
// Check if ray hits any object
vector<double> listTimes(scene.size);
for (int s = 0; s < scene.size; s++) // per cada spheres
{
double t = scene.world[s]->hit(ray);
listTimes[s] = t;
if (t > 0.0001) // intersecció!
{
goesToInfinity = false;
}
}
if (!goesToInfinity)
{
double min = minPositiveTime(listTimes, scene.size);
Object *object;
object = scene.world[min];
MyVector impactPos = ray->getPosition(listTimes[min]);
if (dynamic_cast<Light *>(object) == nullptr) // en cas que impacti amb la font d'iluminació
{
SphereGlass *child = dynamic_cast<SphereGlass *>(object);
JuliaSet *julia = dynamic_cast<JuliaSet *>(object);
if (child)
child->Rebound(ray, impactPos);
else if (julia)
{
MyVector N = object->NormalVector(impactPos);
return object->Phong(MyVector(-1, 3, 1), scene.eyePosition, impactPos, N);
// return julia->colorize(listTimes[min]);
}
else
object->Rebound(ray, impactPos);
return (scene.lightAbsortion * object->color) * PaintPixel(scene, ray, Bounces - 1);
}
else
{
return object->color;
}
}
return scene.background;
}
void MainLoop(int i, Scene scene, Eye eye, MyVector pixel, Image *image)
{
for (int j = 0; j < image->width; j++)
{
Color pixelColor(0, 0, 0);
for (int sample = 0; sample < image->SamplesPerPixel; sample++)
{
Ray ray(&eye, pixel, image->blur);
pixelColor += PaintPixel(scene, &ray, scene.maxBouncesOfRay);
}
image->matrix[i][j] = pixelColor;
pixel = pixel + (image->dimPixel * eye.horizontalVector);
}
pixel = eye.TopLeftPlain - (1 * (i + 1) * image->dimPixel * eye.verticalVector);
}
#endif