-
Notifications
You must be signed in to change notification settings - Fork 1
/
scene.cpp
314 lines (254 loc) · 16 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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#include "scene.h"
Scene::Scene( void )
{}
Scene::~Scene( void )
{
if ( bvh_ )
{
delete bvh_;
bvh_ = nullptr;
}
}
/*
bool Scene::intersect( const Ray &ray,
IntersectionRecord &intersection_record ) const
{
bool intersection_result = false;
IntersectionRecord tmp_intersection_record;
std::size_t num_primitives = primitives_.size();
// Loops over the list of primitives, testing the intersection of each primitive against the given ray
for ( std::size_t primitive_id = 0; primitive_id < num_primitives; primitive_id++ )
if ( primitives_[primitive_id]->intersect( ray, tmp_intersection_record ) )
if ( ( tmp_intersection_record.t_ < intersection_record.t_ ) && ( tmp_intersection_record.t_ > 0.0 ) )
{
intersection_record = tmp_intersection_record;
intersection_result = true; // the ray intersects a primitive!
}
return intersection_result;
}
*/
bool Scene::intersect( const Ray &ray,
IntersectionRecord &intersection_record ) const
{
bool intersection_result = false;
IntersectionRecord tmp_intersection_record;
std::size_t num_primitives = primitives_.size();
switch( acceleration_structure_ )
{
case AccelerationStructure::NONE:
for ( std::size_t primitive_id = 0; primitive_id < num_primitives; primitive_id++ )
{
if ( primitives_[primitive_id]->intersect( ray, tmp_intersection_record ) )
{
if ( ( tmp_intersection_record.t_ < intersection_record.t_ ) && ( tmp_intersection_record.t_ > 0.0 ) )
{
intersection_record = tmp_intersection_record;
intersection_result = true;
}
}
}
break;
case AccelerationStructure::BVH_SAH:
intersection_result = bvh_->intersect( ray, intersection_record);
break;
}
return intersection_result;
}
void Scene::buildAccelerationStructure( void )
{
if ( acceleration_structure_ == Scene::AccelerationStructure::BVH_SAH )
{
buildBVH();
std::clog << std::endl;
}
}
void Scene::buildBVH( void )
{
bvh_ = new BVH( primitives_ );
}
void Scene::load( void )
{
//ceiling light 1
primitives_.push_back( Primitive::PrimitiveUniquePtr( new Triangle{
glm::vec3{-1.0f,4.95f,4.5f}, glm::vec3{ -1.0f, 4.95f, 5.5f}, glm::vec3{ 1.0f,4.95f,4.5f}, glm::vec3{ 0.75f,0.75f,0.75f}, glm::vec3{ 10.0f, 15.0f, 10.0f }, false, false, false}));
//ceiling light 1
primitives_.push_back( Primitive::PrimitiveUniquePtr( new Triangle{
glm::vec3{1.0f,4.95f,5.5f}, glm::vec3{ -1.0f, 4.95f, 5.5f}, glm::vec3{ 1.0f,4.95f,4.5f}, glm::vec3{ 0.75f,0.75f,0.75f}, glm::vec3{ 10.0f, 10.0f, 10.0f }, false, false, false}));
//front triangle
// primitives_.push_back( Primitive::PrimitiveUniquePtr( new Triangle{
// glm::vec3{-1.2f,0.0f,1.5f}, glm::vec3{ 1.2f, 0.0f, 1.5f}, glm::vec3{ 0.0f,2.4f,1.5f}, glm::vec3{ 0.2f, 0.2f, 0.2f }, glm::vec3{ 0.0f, 0.0f, 0.0f }, false, false, false}));
//ground1
primitives_.push_back( Primitive::PrimitiveUniquePtr( new Triangle{
glm::vec3{5.0f,0.0f,0.0f}, glm::vec3{-5.0f, 0.0f, 0.0f}, glm::vec3{ -5.0f,0.0f,10.0f}, glm::vec3{ 1.0f, 1.0f, 1.0f }, glm::vec3{ 0.0f, 0.0f, 0.0f }, false, false, false}));
//ground2
primitives_.push_back( Primitive::PrimitiveUniquePtr( new Triangle{
glm::vec3{5.0f,0.0f,0.0f}, glm::vec3{ -5.0f, 0.0f,10.0f}, glm::vec3{ 5.0f,0.0f,10.0f}, glm::vec3{ 1.0f, 1.0f, 1.0f }, glm::vec3{ 0.0f, 0.0f, 0.0f },false, false, false}));
//groundfront
primitives_.push_back( Primitive::PrimitiveUniquePtr( new Triangle{
glm::vec3{5.0f,0.0f,0.0f}, glm::vec3{ -5.0f, 0.0f,0.0f}, glm::vec3{ 0.0f,0.0f,-5.0f}, glm::vec3{ 1.0f, 1.0f, 1.0f }, glm::vec3{ 0.0f, 0.0f, 0.0f },false, false, false}));
//mirror left 1
primitives_.push_back( Primitive::PrimitiveUniquePtr( new Triangle{
glm::vec3{0.0f,0.0f,-5.0f}, glm::vec3{ 0.0f, 5.0f,-5.0f}, glm::vec3{ -5.0f,0.0f,0.0f}, glm::vec3{ 0.75f,0.75f,0.75f }, glm::vec3{ 0.0f, 0.0f, 0.0f },false, false, false}));
//mirror left 2
primitives_.push_back( Primitive::PrimitiveUniquePtr( new Triangle{
glm::vec3{-5.0f,5.0f,0.0f}, glm::vec3{ 0.0f, 5.0f,-5.0f}, glm::vec3{ -5.0f,0.0f,0.0f}, glm::vec3{ 0.75f,0.75f,0.75f }, glm::vec3{ 0.0f, 0.0f, 0.0f },false, false, false}));
//mirror right 1
primitives_.push_back( Primitive::PrimitiveUniquePtr( new Triangle{
glm::vec3{0.0f,0.0f,-5.0f}, glm::vec3{ 0.0f, 5.0f,-5.0f}, glm::vec3{ 5.0f,0.0f,0.0f}, glm::vec3{ 0.75f,0.75f,0.75f }, glm::vec3{ 0.0f, 0.0f, 0.0f },false, false, false}));
//mirror right 2
primitives_.push_back( Primitive::PrimitiveUniquePtr( new Triangle{
glm::vec3{5.0f,5.0f,0.0f}, glm::vec3{ 0.0f, 5.0f,-5.0f}, glm::vec3{ 5.0f,0.0f,0.0f}, glm::vec3{ 0.75f,0.75f,0.75f }, glm::vec3{ 0.0f, 0.0f, 0.0f },false, false, false}));
//ceiling front
primitives_.push_back( Primitive::PrimitiveUniquePtr( new Triangle{
glm::vec3{5.0f,5.0f,0.0f}, glm::vec3{ -5.0f, 5.0f,0.0f}, glm::vec3{ 0.0f,5.0f,-5.0f}, glm::vec3{ 0.968f, 0.086f, 0.086f }, glm::vec3{ 0.0f, 0.0f, 0.0f },false, false, false}));
//ceiling1
primitives_.push_back( Primitive::PrimitiveUniquePtr( new Triangle{
glm::vec3{5.0f,5.0f,0.0f}, glm::vec3{-5.0f, 5.0f, 0.0f}, glm::vec3{ -5.0f,5.0f,10.0f}, glm::vec3{ 0.968f, 0.086f, 0.086f }, glm::vec3{ 0.0f, 0.0f, 0.0f }, false, false, false}));
//ceiling2
primitives_.push_back( Primitive::PrimitiveUniquePtr( new Triangle{
glm::vec3{5.0f,5.0f,0.0f}, glm::vec3{ -5.0f, 5.0f,10.0f}, glm::vec3{ 5.0f,5.0f,10.0f}, glm::vec3{ 0.968f, 0.086f, 0.086f }, glm::vec3{ 0.0f, 0.0f, 0.0f },false, false, false}));
//left wall 1
primitives_.push_back( Primitive::PrimitiveUniquePtr( new Triangle{
glm::vec3{-5.0f,0.0f,0.0f}, glm::vec3{-5.0f, 5.0f, 0.0f}, glm::vec3{ -5.0f,0.0f,10.0f}, glm::vec3{ 0.75f,0.75f,0.75f }, glm::vec3{ 0.0f, 0.0f, 0.0f }, false, false, false}));
//left wall 2
primitives_.push_back( Primitive::PrimitiveUniquePtr( new Triangle{
glm::vec3{-5.0f,5.0f,0.0f}, glm::vec3{-5.0f, 5.0f, 10.0f}, glm::vec3{ -5.0f,0.0f,10.0f}, glm::vec3{ 0.75f,0.75f,0.75f }, glm::vec3{ 0.0f, 0.0f, 0.0f }, false, false, false}));
//right wall 1
primitives_.push_back( Primitive::PrimitiveUniquePtr( new Triangle{
glm::vec3{5.0f,0.0f,0.0f}, glm::vec3{5.0f, 5.0f, 0.0f}, glm::vec3{ 5.0f,0.0f,10.0f}, glm::vec3{ 0.75f,0.75f,0.75f }, glm::vec3{ 0.0f, 0.0f, 0.0f }, false, false, false}));
//right wall 2
primitives_.push_back( Primitive::PrimitiveUniquePtr( new Triangle{
glm::vec3{5.0f,5.0f,0.0f}, glm::vec3{5.0f, 5.0f, 10.0f}, glm::vec3{ 5.0f,0.0f,10.0f}, glm::vec3{ 0.75f,0.75f,0.75f }, glm::vec3{ 0.0f, 0.0f, 0.0f }, false, false, false}));
//back wall 1
primitives_.push_back( Primitive::PrimitiveUniquePtr( new Triangle{
glm::vec3{-5.0f,0.0f,10.0f}, glm::vec3{-5.0f, 5.0f, 10.0f}, glm::vec3{ 5.0f,0.0f,10.0f}, glm::vec3{ 0.75f,0.75f,0.75f }, glm::vec3{ 0.0f, 0.0f, 0.0f }, false, false, false}));
//back wall 2
primitives_.push_back( Primitive::PrimitiveUniquePtr( new Triangle{
glm::vec3{5.0f,5.0f,10.0f}, glm::vec3{-5.0f, 5.0f, 10.0f}, glm::vec3{ 5.0f,0.0f,10.0f}, glm::vec3{ 0.75f,0.75f,0.75f }, glm::vec3{ 0.0f, 0.0f, 0.0f }, false, false, false}));
}
void Scene::loadObject(const char * obj)
{
Assimp::Importer importer;
const aiScene* scene = importer.ReadFile(obj, aiProcess_Triangulate);
unsigned int mesh, face;
float s = 1.0f;
float t_y = 0.0f;
float t_x = 0.0f;
float t_z = 0.0f;
//srand(std::time(0));
for (mesh = 0 ; mesh < scene->mNumMeshes; mesh++)
{
aiColor3D brdf = {0,0,0};
aiColor3D emittance = {0.0f,0.0f,0.0f};
if (scene->mMaterials) {
// Setting up the BRDF and emitance of the mesh
scene->mMaterials[scene->mMeshes[mesh]->mMaterialIndex]->Get(AI_MATKEY_COLOR_DIFFUSE, brdf);
scene->mMaterials[scene->mMeshes[mesh]->mMaterialIndex]->Get(AI_MATKEY_COLOR_EMISSIVE, emittance);
}
for(face = 0; face < scene->mMeshes[mesh]->mNumFaces; face++)
{
//float r = static_cast <float> (rand()) / static_cast <float> (RAND_MAX);
//float g = static_cast <float> (rand()) / static_cast <float> (RAND_MAX);
//float b = static_cast <float> (rand()) / static_cast <float> (RAND_MAX);
glm::vec3 p1 = { (float)scene->mMeshes[mesh]->mVertices[scene->mMeshes[mesh]->mFaces[face].mIndices[0]].x,
(float)scene->mMeshes[mesh]->mVertices[scene->mMeshes[mesh]->mFaces[face].mIndices[0]].y,
(float)scene->mMeshes[mesh]->mVertices[scene->mMeshes[mesh]->mFaces[face].mIndices[0]].z};
p1 = p1*s;
glm::vec3 p2 = { (float)scene->mMeshes[mesh]->mVertices[scene->mMeshes[mesh]->mFaces[face].mIndices[1]].x,
(float)scene->mMeshes[mesh]->mVertices[scene->mMeshes[mesh]->mFaces[face].mIndices[1]].y,
(float)scene->mMeshes[mesh]->mVertices[scene->mMeshes[mesh]->mFaces[face].mIndices[1]].z};
p2 = p2 * s;
glm::vec3 p3 = { (float)scene->mMeshes[mesh]->mVertices[scene->mMeshes[mesh]->mFaces[face].mIndices[2]].x,
(float)scene->mMeshes[mesh]->mVertices[scene->mMeshes[mesh]->mFaces[face].mIndices[2]].y,
(float)scene->mMeshes[mesh]->mVertices[scene->mMeshes[mesh]->mFaces[face].mIndices[2]].z};
p3 = p3 * s;
primitives_.push_back( Primitive::PrimitiveUniquePtr( new Triangle{ p1, p2, p3,
glm::vec3 { 0.80f, 0.498f, 0.196f},//brdf.r, brdf.g, brdf.b },
glm::vec3 { emittance.r, emittance.g, emittance.b },false, false, true}));
}
}
}
void Scene::loadObjectglass(const char * obj)
{
Assimp::Importer importer;
const aiScene* scene2 = importer.ReadFile(obj, aiProcess_Triangulate);
unsigned int mesh, face;
float s = 1.0f;
float t_y = 0.0f;
float t_x = 0.0f;
float t_z = 0.0f;
//srand(std::time(0));
for (mesh = 0 ; mesh < scene2->mNumMeshes; mesh++)
{
aiColor3D brdf = {0,0,0};
aiColor3D emittance = {0.0f,0.0f,0.0f};
if (scene2->mMaterials) {
// Setting up the BRDF and emitance of the mesh
scene2->mMaterials[scene2->mMeshes[mesh]->mMaterialIndex]->Get(AI_MATKEY_COLOR_DIFFUSE, brdf);
scene2->mMaterials[scene2->mMeshes[mesh]->mMaterialIndex]->Get(AI_MATKEY_COLOR_EMISSIVE, emittance);
}
for(face = 0; face < scene2->mMeshes[mesh]->mNumFaces; face++)
{
//float r = static_cast <float> (rand()) / static_cast <float> (RAND_MAX);
//float g = static_cast <float> (rand()) / static_cast <float> (RAND_MAX);
//float b = static_cast <float> (rand()) / static_cast <float> (RAND_MAX);
glm::vec3 p1 = { (float)scene2->mMeshes[mesh]->mVertices[scene2->mMeshes[mesh]->mFaces[face].mIndices[0]].x,
(float)scene2->mMeshes[mesh]->mVertices[scene2->mMeshes[mesh]->mFaces[face].mIndices[0]].y,
(float)scene2->mMeshes[mesh]->mVertices[scene2->mMeshes[mesh]->mFaces[face].mIndices[0]].z};
p1 = p1*s;
glm::vec3 p2 = { (float)scene2->mMeshes[mesh]->mVertices[scene2->mMeshes[mesh]->mFaces[face].mIndices[1]].x,
(float)scene2->mMeshes[mesh]->mVertices[scene2->mMeshes[mesh]->mFaces[face].mIndices[1]].y,
(float)scene2->mMeshes[mesh]->mVertices[scene2->mMeshes[mesh]->mFaces[face].mIndices[1]].z};
p2 = p2 * s;
glm::vec3 p3 = { (float)scene2->mMeshes[mesh]->mVertices[scene2->mMeshes[mesh]->mFaces[face].mIndices[2]].x,
(float)scene2->mMeshes[mesh]->mVertices[scene2->mMeshes[mesh]->mFaces[face].mIndices[2]].y,
(float)scene2->mMeshes[mesh]->mVertices[scene2->mMeshes[mesh]->mFaces[face].mIndices[2]].z};
p3 = p3 * s;
primitives_.push_back( Primitive::PrimitiveUniquePtr( new Triangle{ p1, p2, p3,
glm::vec3 { 1.00f, 0.71f, 0.29f},//brdf.r, brdf.g, brdf.b },
glm::vec3 { emittance.r, emittance.g, emittance.b },false, true, false}));
}
}
}
void Scene::loadObjectdiff(const char * obj)
{
Assimp::Importer importer;
const aiScene* scene3 = importer.ReadFile(obj, aiProcess_Triangulate);
unsigned int mesh, face;
float s = 1.0f;
float t_y = 0.0f;
float t_x = 0.0f;
float t_z = 0.0f;
//srand(std::time(0));
for (mesh = 0 ; mesh < scene3->mNumMeshes; mesh++)
{
aiColor3D brdf = {0,0,0};
aiColor3D emittance = {0.0f,0.0f,0.0f};
if (scene3->mMaterials) {
// Setting up the BRDF and emitance of the mesh
scene3->mMaterials[scene3->mMeshes[mesh]->mMaterialIndex]->Get(AI_MATKEY_COLOR_DIFFUSE, brdf);
scene3->mMaterials[scene3->mMeshes[mesh]->mMaterialIndex]->Get(AI_MATKEY_COLOR_EMISSIVE, emittance);
}
for(face = 0; face < scene3->mMeshes[mesh]->mNumFaces; face++)
{
//float r = static_cast <float> (rand()) / static_cast <float> (RAND_MAX);
//float g = static_cast <float> (rand()) / static_cast <float> (RAND_MAX);
//float b = static_cast <float> (rand()) / static_cast <float> (RAND_MAX);
glm::vec3 p1 = { (float)scene3->mMeshes[mesh]->mVertices[scene3->mMeshes[mesh]->mFaces[face].mIndices[0]].x,
(float)scene3->mMeshes[mesh]->mVertices[scene3->mMeshes[mesh]->mFaces[face].mIndices[0]].y,
(float)scene3->mMeshes[mesh]->mVertices[scene3->mMeshes[mesh]->mFaces[face].mIndices[0]].z};
p1 = p1*s;
glm::vec3 p2 = { (float)scene3->mMeshes[mesh]->mVertices[scene3->mMeshes[mesh]->mFaces[face].mIndices[1]].x,
(float)scene3->mMeshes[mesh]->mVertices[scene3->mMeshes[mesh]->mFaces[face].mIndices[1]].y,
(float)scene3->mMeshes[mesh]->mVertices[scene3->mMeshes[mesh]->mFaces[face].mIndices[1]].z};
p2 = p2 * s;
glm::vec3 p3 = { (float)scene3->mMeshes[mesh]->mVertices[scene3->mMeshes[mesh]->mFaces[face].mIndices[2]].x,
(float)scene3->mMeshes[mesh]->mVertices[scene3->mMeshes[mesh]->mFaces[face].mIndices[2]].y,
(float)scene3->mMeshes[mesh]->mVertices[scene3->mMeshes[mesh]->mFaces[face].mIndices[2]].z};
p3 = p3 * s;
primitives_.push_back( Primitive::PrimitiveUniquePtr( new Triangle{ p1, p2, p3,
glm::vec3 { 0.153f, 0.235f, 0.556f},//brdf.r, brdf.g, brdf.b },
glm::vec3 { emittance.r, emittance.g, emittance.b },false, false, false}));
}
}
}