-
Notifications
You must be signed in to change notification settings - Fork 0
/
gscene.h
106 lines (94 loc) · 2.51 KB
/
gscene.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
104
105
106
#ifndef GSCENE_H
#define GSCENE_H
#include "ggameobject.h"
#include "gcamera.h"
#include "gmodel.h"
#include "gbuffer.h"
#include "glight.h"
#include "gbvh.h"
class GScene
{
public:
GScene(int filmW, int filmH)
:film(filmW, filmH)
{
}
bool intersect(const GRay& ray, GMath::interval& ray_t, GSurfaceInteraction& isect) const
{
if(bvhTree!=nullptr)
{
if(bvhTree->intersect(ray, ray_t, isect))
{
ray_t.max = isect.t;
return true;
}
return false;
}
else
{
bool hitAny = false;
for(auto light : lights)
{
if(light->intersect(ray, ray_t, isect))
{
ray_t.max = isect.t;
hitAny = true;
}
}
for(auto model : models)
{
if(model->intersect(ray, ray_t, isect))
{
ray_t.max = isect.t;
hitAny = true;
}
}
return hitAny;
}
}
void add(std::shared_ptr<GGameObject> gObj)
{
if(gObj->mtype == GGameObject::kModel)
{
auto model = std::dynamic_pointer_cast<GModel>(gObj);
models.push_back(model);
bbox = aabb(bbox, model->bBox());
hittables.push_back(model);
}
else if(gObj->mtype == GGameObject::kLight)
{
auto light = std::dynamic_pointer_cast<GLight>(gObj);
lights.push_back(std::dynamic_pointer_cast<GLight>(gObj));
if(light->lightType == GLightType::kLTDiffuseArea)
{
auto light = std::dynamic_pointer_cast<GDiffuseAreaLight>(gObj);
bbox = aabb(bbox, light->bBox());
hittables.push_back(light);
}
}
}
void add(std::vector<std::shared_ptr<GGameObject> >& gObjList)
{
for(auto& gobj : gObjList)
{
add(gobj);
}
}
aabb bBox()
{
return bbox;
}
void BuildBVHTree()
{
bvhTree = make_shared<GBVHNode>(hittables, 0, hittables.size());
}
std::shared_ptr<GCamera> camera;
GColorBuffer film;
std::vector<std::shared_ptr<GModel>> models;
std::vector<std::shared_ptr<GLight>> lights;
std::vector<std::shared_ptr<GHittable>> hittables;
private:
std::shared_ptr<GBVHNode> bvhTree;
GMath::aabb bbox;
};
#endif // GSCENE_H