-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorld.cpp
More file actions
147 lines (121 loc) · 4.96 KB
/
Copy pathWorld.cpp
File metadata and controls
147 lines (121 loc) · 4.96 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <G3D/Vector3.h>
#include <G3D/Color3.h>
#include <G3D/Any.h>
#include <G3D/CoordinateFrame.h>
#include <G3D/Stopwatch.h>
#include <G3D/Array.h>
#include <GLG3D/Light.h>
#include <GLG3D/ArticulatedModel.h>
#include "World.h"
World::World() : m_mode(TRACE) {
begin();
lightArray.append(G3D::Light::point("Light1", G3D::Vector3(0, 10, 0), G3D::Color3::white() * 1200));
lightArray.append(G3D::Light::point("Light2", G3D::Vector3(22.6f, 2.9f, 6.6f), G3D::Color3::fromARGB(0xffe5bd) * 1000));
ambient = G3D::Radiance3::fromARGB(0x304855) * 0.3f;
G3D::Any teapot = G3D::PARSE_ANY
( G3D::ArticulatedModel::Specification {
filename = "teapot/teapot.obj";
scale = 0.01;
stripMaterials = true;
preprocess =
( setMaterial(all(),
UniversalMaterial::Specification {
specular = Color3(0.2f);
shininess = mirror();
lambertian = Color3(0.7f, 0.5f, 0.1f);
});
);
} );
insert(G3D::ArticulatedModel::create(teapot), G3D::CFrame::fromXYZYPRDegrees(19.4f, -0.2f, 0.94f, 70));
G3D::Any sphereOutside = G3D::PARSE_ANY
( ArticulatedModel::Specification {
filename = "sphere.ifs";
scale = 0.3;
preprocess =
( setTwoSided(all(), true);
setMaterial(all(),
UniversalMaterial::Specification {
specular = Color3(0.1f);
shininess = mirror();
lambertian = Color3(0.0f);
etaTransmit = 1.3f;
etaReflect = 1.0f;
transmissive = Color3(0.2f, 0.5f, 0.7f);
});
);
});
G3D::Any sphereInside = G3D::PARSE_ANY
( G3D::ArticulatedModel::Specification {
filename = "sphere.ifs";
scale = -0.3;
preprocess =
( setTwoSided(all(), true);
setMaterial(all(),
UniversalMaterial::Specification {
specular = Color3(0.1f);
shininess = mirror();
lambertian = Color3(0.0f);
etaReflect = 1.3f;
etaTransmit = 1.0f;
transmissive = Color3(1.0f);
});
);
});
insert(G3D::ArticulatedModel::create(sphereOutside), G3D::CFrame::fromXYZYPRDegrees(19.7f, 0.2f, -1.1f, 70));
insert(G3D::ArticulatedModel::create(sphereInside), G3D::CFrame::fromXYZYPRDegrees(19.7f, 0.2f, -1.1f, 70));
G3D::Any sponza = G3D::PARSE_ANY
( ArticulatedModel::Specification {
filename = "dabrovic_sponza/sponza.zip/sponza.obj";
} );
G3D::Stopwatch timer;
insert(G3D::ArticulatedModel::create(sponza), G3D::Vector3(8.2f, -6, 0));
timer.after("Sponza load");
end();
}
void World::begin() {
debugAssert(m_mode == TRACE);
m_surfaceArray.clear();
m_triArray.clear();
m_mode = INSERT;
}
void World::insert(const shared_ptr<G3D::ArticulatedModel>& model, const G3D::CFrame& frame) {
G3D::Array<shared_ptr<G3D::Surface> > posed;
model->pose(posed, frame);
for (int i = 0; i < posed.size(); ++i) {
insert(posed[i]);
}
}
void World::insert(const shared_ptr<G3D::Surface>& m) {
debugAssert(m_mode == INSERT);
m_surfaceArray.append(m);
}
void World::end() {
G3D::Surface::getTris(m_surfaceArray, m_cpuVertexArray, m_triArray);
for (int i = 0; i < m_triArray.size(); ++i) {
m_triArray[i].material()->setStorage(G3D::MOVE_TO_CPU);
}
debugAssert(m_mode == INSERT);
m_mode = TRACE;
G3D::TriTree::Settings s;
s.algorithm = G3D::TriTree::MEAN_EXTENT;
G3D::Stopwatch timer;
m_triTree.setContents(m_triArray, m_cpuVertexArray, s);
timer.after("TriTree creation");
m_triArray.clear();
}
bool World::lineOfSight(const G3D::Vector3& v0, const G3D::Vector3& v1) const {
debugAssert(m_mode == TRACE);
G3D::Vector3 d = v1 - v0;
float len = d.length();
G3D::Ray ray = G3D::Ray::fromOriginAndDirection(v0, d / len);
float distance = len;
G3D::Tri::Intersector intersector;
// For shadow rays, try to find intersections as quickly as possible, rather
// than solving for the first intersection
static const bool exitOnAnyHit = true, twoSidedTest = true;
return ! m_triTree.intersectRay(ray, intersector, distance, exitOnAnyHit, twoSidedTest);
}
shared_ptr<G3D::Surfel> World::intersect(const G3D::Ray& ray, float& distance) const {
debugAssert(m_mode == TRACE);
return m_triTree.intersectRay(ray, distance);
}