/
Scene.cpp
100 lines (82 loc) · 2.92 KB
/
Scene.cpp
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
#include <iostream>
#include "Scene.h"
Scene::Scene (const std::shared_ptr<Binder> binde)
:
binder(binde),
objectsTab(),
lightsTab()
{
}
Scene::Scene(const std::shared_ptr<Binder> binde, const std::vector<std::shared_ptr<Scene_Entity>>& entityTab, const std::vector<std::shared_ptr<LightSource>>& lightTab)
:
binder(binde),
objectsTab(entityTab),
lightsTab(lightTab)
{
}
void Scene::addEntity(std::shared_ptr<Scene_Entity> entity)
{
objectsTab.push_back(entity);
}
void Scene::addLight(std::shared_ptr<LightSource> light)
{
lightsTab.push_back(light);
}
void Scene::addObjectsTabToBinder() const
{
binder->setObjectTab(objectsTab) ;
}
std::vector<std::shared_ptr<LightSource>> Scene::getLight () const
{
return lightsTab ;
}
Color Scene::lanceRayon(const Rayon& ray, int iteration) const
{
this->addObjectsTabToBinder();
Color result(0.0,0.0,0.0);
float minDist(1000.0) ;
float dist (0.0) ;
Hit hit ;
if (binder->intersect(ray,0.0,100.0,hit))
{
//result = hit.getObject()->getOptic()->getColor(hit.getU(),hit.getV());
//result = hit.getObject()->getOptic()->getColor(0.5,0.5);
Color coulObj(hit.getObject()->getOptic()->getColor(hit.getU(),hit.getV()));
for ( std::vector<std::shared_ptr<LightSource>>::const_iterator it = lightsTab.begin(); it != lightsTab.end() ; ++it)
// pour chaque source
{
//d = calcul distance point intersection source
Vector directi(it->get()->getOrigin()-hit.getImpactPoint());
float distInterSource = directi.getNorm() ;
directi.normalize();
//initialiser Ray : point intersect, direction(point intersect, source), couleur = on s'en fout
Color c(0.0,0.0,0.0);
Color resultNorm(0.0,0.0,0.0);
Rayon ray(hit.getImpactPoint(),directi,c);
if (! binder->intersect(ray, 0, distInterSource))
{
Color diff(it->get()->getColor()*coulObj*(dotProduct(hit.getNormal(),ray.getDirect())));
Vector moinsV(-directi.getX(),-directi.getY(),-directi.getZ());
Vector miroirV(moinsV + hit.getNormal()*(2*(dotProduct(directi,hit.getNormal()))));
//Vmir = V symétrique par rapport à N
//spec = coulspec(obj)* (tronquerAZero(RayS.Vmir))^n * coul(source)
Color spec(it->get()->getColor()*coulObj*dotProduct(ray.getDirect(),miroirV));
resultNorm = diff + spec ;
if ( iteration < 2)
{
//Res2 = influence rayon réfléchi
Rayon reflected(hit.getImpactPoint(),miroirV,c);
Color reflectedColor(0.0,0.0,0.0);
reflectedColor = this->lanceRayon(reflected,iteration+1);
//return pourcent1*Res + ourcent2*Res2
result = resultNorm*0.8 + reflectedColor*0.2 ;
}
else
{
result = resultNorm ;
}
}
}
}
return result;
}