-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScene.cpp
More file actions
379 lines (302 loc) · 13.8 KB
/
Scene.cpp
File metadata and controls
379 lines (302 loc) · 13.8 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
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#include "Scene.h"
#include <iostream>
#include "Utils.h"
#include "Material.h"
namespace dae {
#pragma region Base Scene
//Initialize Scene with Default Solid Color Material (RED)
Scene::Scene() :
m_Materials({ new Material_SolidColor({1,0,0}) })
{
m_SphereGeometries.reserve(32);
m_PlaneGeometries.reserve(32);
m_TriangleMeshGeometries.reserve(32);
m_Lights.reserve(32);
}
Scene::~Scene()
{
for (auto& pMaterial : m_Materials)
{
delete pMaterial;
pMaterial = nullptr;
}
m_Materials.clear();
}
void dae::Scene::GetClosestHit(const Ray& ray, HitRecord& closestHit)
{
for (const auto& sphere : m_SphereGeometries)
{
GeometryUtils::HitTest_Sphere(sphere, ray, closestHit);
}
for (const auto& plane : m_PlaneGeometries)
{
GeometryUtils::HitTest_Plane(plane, ray, closestHit);
}
// for (const auto& triangle : m_TriangleGeometries)
// {
// GeometryUtils::HitTest_Triangle(triangle, ray, closestHit);
// }
//Handles Triangle(meshes) HitTest
m_BVH.IntersectBVH(ray, 0, closestHit);
}
bool Scene::DoesHit(const Ray& ray)
{
for (size_t i{}; i < m_SphereGeometries.size(); ++i)
{
if (GeometryUtils::HitTest_Sphere(m_SphereGeometries[i], ray))
{
return true;
}
}
for (size_t i{}; i < m_PlaneGeometries.size(); ++i)
{
if (GeometryUtils::HitTest_Plane(m_PlaneGeometries[i], ray))
{
return true;
}
}
// for (size_t i{}; i < m_TriangleGeometries.size(); ++i)
// {
// if (GeometryUtils::HitTest_Triangle(m_TriangleGeometries[i], ray))
// {
// return true;
// }
// }
//Handles Triangle(meshes) HitTest
return m_BVH.IntersectBVH(ray, 0);
}
#pragma region Scene Helpers
Sphere* Scene::AddSphere(const Vector3& origin, float radius, unsigned char materialIndex)
{
Sphere s;
s.origin = origin;
s.radius = radius;
s.materialIndex = materialIndex;
m_SphereGeometries.emplace_back(s);
return &m_SphereGeometries.back();
}
Plane* Scene::AddPlane(const Vector3& origin, const Vector3& normal, unsigned char materialIndex)
{
Plane p;
p.origin = origin;
p.normal = normal;
p.materialIndex = materialIndex;
m_PlaneGeometries.emplace_back(p);
return &m_PlaneGeometries.back();
}
TriangleMesh* Scene::AddTriangleMesh(TriangleCullMode cullMode, unsigned char materialIndex)
{
TriangleMesh m{};
m.cullMode = cullMode;
m.materialIndex = materialIndex;
m_TriangleMeshGeometries.emplace_back(m);
return &m_TriangleMeshGeometries.back();
}
Light* Scene::AddPointLight(const Vector3& origin, float intensity, const ColorRGB& color)
{
Light l;
l.origin = origin;
l.intensity = intensity;
l.color = color;
l.type = LightType::Point;
m_Lights.emplace_back(l);
return &m_Lights.back();
}
Light* Scene::AddDirectionalLight(const Vector3& direction, float intensity, const ColorRGB& color)
{
Light l;
l.direction = direction;
l.intensity = intensity;
l.color = color;
l.type = LightType::Directional;
m_Lights.emplace_back(l);
return &m_Lights.back();
}
unsigned char Scene::AddMaterial(Material* pMaterial)
{
m_Materials.push_back(pMaterial);
return static_cast<unsigned char>(m_Materials.size() - 1);
}
#pragma endregion
#pragma endregion
#pragma region SCENE W1
void Scene_W1::Initialize()
{
//default: Material id0 >> SolidColor Material (RED)
constexpr unsigned char matId_Solid_Red = 0;
const unsigned char matId_Solid_Blue = AddMaterial(new Material_SolidColor{ colors::Blue });
const unsigned char matId_Solid_Yellow = AddMaterial(new Material_SolidColor{ colors::Yellow });
const unsigned char matId_Solid_Green = AddMaterial(new Material_SolidColor{ colors::Green });
const unsigned char matId_Solid_Magenta = AddMaterial(new Material_SolidColor{ colors::Magenta });
//Spheres
AddSphere({ -25.f, 0.f, 100.f }, 50.f, matId_Solid_Red);
AddSphere({ 25.f, 0.f, 100.f }, 50.f, matId_Solid_Blue);
//Plane
AddPlane({ -75.f, 0.f, 0.f }, { 1.f, 0.f,0.f }, matId_Solid_Green);
AddPlane({ 75.f, 0.f, 0.f }, { -1.f, 0.f,0.f }, matId_Solid_Green);
AddPlane({ 0.f, -75.f, 0.f }, { 0.f, 1.f,0.f }, matId_Solid_Yellow);
AddPlane({ 0.f, 75.f, 0.f }, { 0.f, -1.f,0.f }, matId_Solid_Yellow);
AddPlane({ 0.f, 0.f, 125.f }, { 0.f, 0.f,-1.f }, matId_Solid_Magenta);
}
void Scene_W2::Initialize()
{
m_Camera.origin = { 0.f, 3.f, -9.f };
m_Camera.fovAngle = tan(45.f / 2.f);
//default: Material id0 >> SolidColor Material (RED)
constexpr unsigned char matId_Solid_Red = 0;
const unsigned char matId_Solid_Blue = AddMaterial(new Material_SolidColor{ colors::Blue });
const unsigned char matId_Solid_Yellow = AddMaterial(new Material_SolidColor{ colors::Yellow });
const unsigned char matId_Solid_Green = AddMaterial(new Material_SolidColor{ colors::Green });
const unsigned char matId_Solid_Magenta = AddMaterial(new Material_SolidColor{ colors::Magenta });
//Spheres
AddSphere({ -1.75f, 1.f, 0.f }, .75f, matId_Solid_Red);
AddSphere({ 0.f, 1.f, 0.f }, .75f, matId_Solid_Blue);
AddSphere({ 1.75f, 1.f, 0.f }, .75f, matId_Solid_Red);
AddSphere({ -1.75f, 3.f, 0.f }, .75f, matId_Solid_Blue);
AddSphere({ 0.f, 3.f, 0.f }, .75f, matId_Solid_Red);
AddSphere({ 1.75f, 3.f, 0.f }, .75f, matId_Solid_Blue);
//Plane
AddPlane({ -5.f, 0.f, 0.f }, { 1.f, 0.f,0.f }, matId_Solid_Green);
AddPlane({ 5.f, 0.f, 0.f }, { -1.f, 0.f,0.f }, matId_Solid_Green);
AddPlane({ 0.f, 0.f, 0.f }, { 0.f, 1.f,0.f }, matId_Solid_Yellow);
AddPlane({ 0.f, 10.f, 0.f }, { 0.f, -1.f,0.f }, matId_Solid_Yellow);
AddPlane({ 0.f, 0.f, 10.f }, { 0.f, 0.f,-1.f }, matId_Solid_Magenta);
AddPointLight({ 0.f, 5.f, -5.f }, 70.f, colors::White);
}
void Scene_W3::Initialize()
{
m_Camera.origin = { 0.f, 3.f, -9.f };
m_Camera.fovAngle = tan(45.f / 2.f);
const auto matCT_GrayRoughMetal = AddMaterial(new Material_CookTorrence({ 0.972f, 0.960f, 0.915f }, 1.f, 1.f));
const auto matCT_GrayMediumMetal = AddMaterial(new Material_CookTorrence({ 0.972f, 0.960f, 0.915f }, 1.f, 0.6f));
const auto matCT_GraySmoothMetal = AddMaterial(new Material_CookTorrence({ 0.972f, 0.960f, 0.915f }, 1.f, 0.1f));
const auto matCT_GrayRoughPlastic = AddMaterial(new Material_CookTorrence({ 0.75f, 0.95f, 0.95f }, 0.f, 1.f));
const auto matCT_GrayMediumPlastic = AddMaterial(new Material_CookTorrence({ 0.75f, 0.95f, 0.95f }, 0.f, 0.6f));
const auto matCT_GraySmoothPlastic = AddMaterial(new Material_CookTorrence({ 0.75f, 0.95f, 0.95f }, 0.f, 0.1f));
const auto matLambert_GrayBlue = AddMaterial(new Material_Lambert({ 0.49f, 0.57f, 0.57f }, 1.f));
const auto matLambertPhong1 = AddMaterial(new Material_CookTorrence(colors::Gray, 1.f, 1.f));
const auto matLambertPhong2 = AddMaterial(new Material_CookTorrence(colors::Gray, 1.f, 0.5f));
const auto matLambertPhong3 = AddMaterial(new Material_CookTorrence(colors::Gray, 1.f, 0.1f));
//Spheres
AddSphere({ -1.75f, 1.f, 0.f }, .75f, matCT_GrayRoughMetal);
AddSphere({ 0.f, 1.f, 0.f }, .75f, matCT_GrayMediumMetal);
AddSphere({ 1.75f, 1.f, 0.f }, .75f, matCT_GraySmoothMetal);
AddSphere({ -1.75f, 1.f, 0.f }, .75f, matLambertPhong1);
AddSphere({ 0.f, 1.f, 0.f }, .75f, matLambertPhong2);
AddSphere({ 1.75f, 1.f, 0.f }, .75f, matLambertPhong3);
AddSphere({ -1.75f, 3.f, 0.f }, .75f, matCT_GrayRoughPlastic);
AddSphere({ 0.f, 3.f, 0.f }, .75f, matCT_GrayMediumPlastic);
AddSphere({ 1.75f, 3.f, 0.f }, .75f, matCT_GraySmoothPlastic);
//Plane
AddPlane({ 0.f, 0.f, 10.f }, { 0.f, 0.f,-1.f }, matLambert_GrayBlue); //Back
AddPlane({ 0.f, 0.f, 0.f }, { 0.f, 1.f,0.f }, matLambert_GrayBlue); //Bottom
AddPlane({ 0.f, 10.f, 0.f }, { 0.f, -1.f,0.f }, matLambert_GrayBlue); //Top
AddPlane({ 5.f, 0.f, 0.f }, { -1.f, 0.f,0.f }, matLambert_GrayBlue); //Right
AddPlane({ -5.f, 0.f, 0.f }, { 1.f, 0.f,0.f }, matLambert_GrayBlue); //Left
AddPointLight({ 0.f, 5.f, 5.f }, 50.f, ColorRGB{ 1.f, 0.61f, 0.45f });
AddPointLight({ -2.5f, 5.f, -5.f }, 70.f, ColorRGB{ 1.f, 0.8f, 0.45f });
AddPointLight({ 2.5f, 2.5f, -5.f }, 50.f, ColorRGB{ .34f, 0.47f, 0.68f });
}
void Scene_W4::Update(dae::Timer* pTimer)
{
Scene::Update(pTimer);
//Get a new angle
const auto yawAngle{(cos(pTimer->GetTotal()) + 1.f) / 2.f * PI_2};
//Rotate each triangle mesh over its up(y) axis
for(auto& mesh : m_TriangleMeshGeometries)
{
mesh.RotateY(yawAngle);
mesh.UpdateTransforms();
}
m_BVH.BuildBVH(m_TriangleMeshGeometries);
}
void Scene_W4::Initialize()
{
sceneName = "Reference Scene";
m_Camera.origin = { 0.f, 0.2f, -9.f };
m_Camera.fovAngle = tan(45.f / 2.f);
const auto matCT_GrayRoughMetal = AddMaterial(new Material_CookTorrence({ .972f, .960f, .915f }, 1.f, 1.f));
const auto matCT_GrayMediumMetal = AddMaterial(new Material_CookTorrence({ .972f, .960f, .915f }, 1.f, .6f));
const auto matCT_GraySmoothMetal = AddMaterial(new Material_CookTorrence({ .972f, .960f, .915f }, 1.f, .1f));
GetMaterials()[matCT_GraySmoothMetal]->SetReflectivity(0.1f);
const auto matCT_GrayRoughPlastic = AddMaterial(new Material_CookTorrence({ .75f, .75f, .75f }, .0f, 1.f));
const auto matCT_GrayMediumPlastic = AddMaterial(new Material_CookTorrence({ .75f, .75f, .75f }, .0f, .6f));
const auto matCT_GraySmoothPlastic = AddMaterial(new Material_CookTorrence({ .75f, .75f, .75f }, .0f, .1f));
const auto matLambert_GrayBlue = AddMaterial(new Material_Lambert({ .49f, 0.57f, 0.57f }, 1.f));
const auto matLambert_GrayBlue_Reflective = AddMaterial(new Material_Lambert({ .49f, 0.57f, 0.57f }, 1.f));
GetMaterials()[matLambert_GrayBlue_Reflective]->SetReflectivity(0.01f);
const auto matLambert_White = AddMaterial(new Material_Lambert(colors::White, 1.f));
AddPlane(Vector3{ 0.f, 0.f, 10.f }, Vector3{ 0.f, 0.f, -1.f }, matLambert_GrayBlue); //BACK
AddPlane(Vector3{ 0.f, 0.f, 0.f }, Vector3{ 0.f, 1.f, 0.f }, matLambert_GrayBlue_Reflective); //BOTTOM
AddPlane(Vector3{ 0.f, 10.f, 0.f }, Vector3{ 0.f, -1.f, 0.f }, matLambert_GrayBlue); //TOP
AddPlane(Vector3{ 5.f, 0.f, 0.f }, Vector3{ -1.f, 0.f, 0.f }, matLambert_GrayBlue); //RIGHT
AddPlane(Vector3{ -5.f, 0.f, 0.f }, Vector3{ 1.f, 0.f, 0.f }, matLambert_GrayBlue); //LEFT
AddSphere(Vector3{ -1.75f, 1.f, 0.f }, .75f, matCT_GrayRoughMetal);
AddSphere(Vector3{ 0.f, 1.f, 0.f }, .75f, matCT_GrayMediumMetal);
AddSphere(Vector3{ 1.75f, 1.f, 0.f }, .75f, matCT_GraySmoothMetal);
AddSphere(Vector3{ -1.75f, 3.f, 0.f }, .75f, matCT_GrayRoughPlastic);
AddSphere(Vector3{ 0.f, 3.f, 0.f }, .75f, matCT_GrayMediumPlastic);
AddSphere(Vector3{ 1.75f, 3.f, 0.f }, .75f, matCT_GraySmoothPlastic);
//CW Winding Order!
Triangle baseTriangle = { Vector3(-.75f, 1.5f, 0.f), Vector3(.75f, 0.f, 0.f), Vector3(-.75f, 0.f, 0.f) };
baseTriangle.cullMode = TriangleCullMode::NoCulling;
baseTriangle.materialIndex = matCT_GrayRoughMetal;
m_Meshes.reserve(3);
m_Meshes.emplace_back(AddTriangleMesh(TriangleCullMode::FrontFaceCulling, matLambert_White));
m_Meshes.back()->AppendTriangle(baseTriangle, true);
m_Meshes.back()->Translate({ -1.75f,4.5f,0.f });
m_Meshes.back()->UpdateTransforms();
m_Meshes.emplace_back(AddTriangleMesh(TriangleCullMode::BackFaceCulling, matLambert_White));
m_Meshes.back()->AppendTriangle(baseTriangle, true);
m_Meshes.back()->Translate({ 0.f,4.5f,0.f });
m_Meshes.back()->UpdateTransforms();
m_Meshes.emplace_back(AddTriangleMesh(TriangleCullMode::NoCulling, matLambert_White));
m_Meshes.back()->AppendTriangle(baseTriangle, true);
m_Meshes.back()->Translate({ 1.75f,4.5f,0.f });
m_Meshes.back()->UpdateTransforms();
m_BVH.BuildBVH(m_TriangleMeshGeometries);
AddPointLight(Vector3{ 0.f, 5.f, 5.f }, 50.f, ColorRGB{ 1.f, .61f, .45f }); //Backlight
AddPointLight(Vector3{ -2.5f, 5.f, -5.f }, 70.f, ColorRGB{ 1.f, .8f, .45f }); //Front Light Left
AddPointLight(Vector3{ 2.5f, 2.5f, -5.f }, 50.f, ColorRGB{ .34f, .47f, .68f });
}
void Scene_W4_Bunny::Update(dae::Timer* pTimer)
{
Scene::Update(pTimer);
//Get a new angle
const auto yawAngle{(cos(pTimer->GetTotal()) + 1.f) / 2.f * PI_2};
//Rotate each triangle mesh over its up(y) axis
for(auto& mesh : m_TriangleMeshGeometries)
{
mesh.RotateY(yawAngle);
mesh.UpdateTransforms();
}
m_BVH.BuildBVH(m_TriangleMeshGeometries);
}
void Scene_W4_Bunny::Initialize()
{
sceneName = "Bunny Scene";
m_Camera.origin = { 0.f, 0.2f, -9.f };
m_Camera.fovAngle = tan(45.f / 2.f);
const auto matLambert_White = AddMaterial(new Material_Lambert(colors::White, 1.f));
const auto matLambert_GrayBlue = AddMaterial(new Material_Lambert({ .49f, 0.57f, 0.57f }, 1.f));
AddPointLight(Vector3{ 0.f, 5.f, 5.f }, 50.f, ColorRGB{ 1.f, .61f, .45f }); //Backlight
AddPointLight(Vector3{ -2.5f, 5.f, -5.f }, 70.f, ColorRGB{ 1.f, .8f, .45f }); //Front Light Left
AddPointLight(Vector3{ 2.5f, 2.5f, -5.f }, 50.f, ColorRGB{ .34f, .47f, .68f });
AddPlane(Vector3{ 0.f, 0.f, 10.f }, Vector3{ 0.f, 0.f, -1.f }, matLambert_GrayBlue); //BACK
AddPlane(Vector3{ 0.f, 0.f, 0.f }, Vector3{ 0.f, 1.f, 0.f }, matLambert_GrayBlue); //BOTTOM
AddPlane(Vector3{ 0.f, 10.f, 0.f }, Vector3{ 0.f, -1.f, 0.f }, matLambert_GrayBlue); //TOP
AddPlane(Vector3{ 5.f, 0.f, 0.f }, Vector3{ -1.f, 0.f, 0.f }, matLambert_GrayBlue); //RIGHT
AddPlane(Vector3{ -5.f, 0.f, 0.f }, Vector3{ 1.f, 0.f, 0.f }, matLambert_GrayBlue); //LEFT
//Bunny
m_Meshes.reserve(1);
m_Meshes.emplace_back(AddTriangleMesh(TriangleCullMode::BackFaceCulling, matLambert_White));
Utils::ParseOBJ("Resources/lowpoly_bunny.obj", m_Meshes[0]->positionsX, m_Meshes[0]->positionsY, m_Meshes[0]->positionsZ, m_Meshes[0]->normalsX, m_Meshes[0]->normalsY, m_Meshes[0]->normalsZ, m_Meshes[0]->indices);
m_Meshes.back()->Scale({1 ,1,1});
m_Meshes.back()->UpdateTransforms();
// m_Meshes.emplace_back(AddTriangleMesh(TriangleCullMode::BackFaceCulling, matLambert_White));
// Utils::ParseOBJ("Resources/simple_object.obj", m_Meshes[1]->positions, m_Meshes[1]->normals, m_Meshes[1]->indices);
// m_Meshes[1]->UpdateTransforms();
m_BVH.BuildBVH(m_TriangleMeshGeometries);
}
#pragma endregion
}