|
2 | 2 | #include "Utils.h" |
3 | 3 | #include "Material.h" |
4 | 4 |
|
5 | | -namespace dae { |
6 | | - |
| 5 | +namespace dae |
| 6 | +{ |
7 | 7 | #pragma region Base Scene |
8 | | - //Initialize Scene with Default Solid Color Material (RED) |
9 | | - Scene::Scene(): |
10 | | - m_Materials({ new Material_SolidColor({1,0,0})}) |
11 | | - { |
12 | | - m_SphereGeometries.reserve(32); |
13 | | - m_PlaneGeometries.reserve(32); |
14 | | - m_TriangleMeshGeometries.reserve(32); |
15 | | - m_Lights.reserve(32); |
16 | | - } |
17 | | - |
18 | | - Scene::~Scene() |
19 | | - { |
20 | | - for(auto& pMaterial : m_Materials) |
21 | | - { |
22 | | - delete pMaterial; |
23 | | - pMaterial = nullptr; |
24 | | - } |
25 | | - |
26 | | - m_Materials.clear(); |
27 | | - } |
28 | | - |
29 | | - void dae::Scene::GetClosestHit(const Ray& ray, HitRecord& closestHit) const |
30 | | - { |
31 | | - for (const auto& sphere : m_SphereGeometries) |
32 | | - { |
33 | | - HitRecord hit; |
34 | | - if (dae::GeometryUtils::HitTest_Sphere(sphere, ray, hit)) |
35 | | - { |
36 | | - closestHit.didHit = true; |
37 | | - if (hit.t < closestHit.t) |
38 | | - { |
39 | | - closestHit.t = hit.t; |
40 | | - closestHit.materialIndex = sphere.materialIndex; |
41 | | - } |
42 | | - } |
43 | | - } |
44 | | - |
45 | | - for (const auto& plane : m_PlaneGeometries) |
46 | | - { |
47 | | - HitRecord hit; |
48 | | - if (dae::GeometryUtils::HitTest_Plane(plane, ray, hit)) |
49 | | - { |
50 | | - closestHit.didHit = true; |
51 | | - if (hit.t < closestHit.t) |
52 | | - { |
53 | | - closestHit.t = hit.t; |
54 | | - closestHit.materialIndex = plane.materialIndex; |
55 | | - } |
56 | | - } |
57 | | - } |
58 | | - |
59 | | - } |
60 | | - |
61 | | - bool Scene::DoesHit(const Ray& ray) const |
62 | | - { |
63 | | - //todo W3 |
64 | | - assert(false && "No Implemented Yet!"); |
65 | | - return false; |
66 | | - } |
| 8 | + //Initialize Scene with Default Solid Color Material (RED) |
| 9 | + Scene::Scene(): |
| 10 | + m_Materials({new Material_SolidColor({1, 0, 0})}) |
| 11 | + { |
| 12 | + m_SphereGeometries.reserve(32); |
| 13 | + m_PlaneGeometries.reserve(32); |
| 14 | + m_TriangleMeshGeometries.reserve(32); |
| 15 | + m_Lights.reserve(32); |
| 16 | + } |
| 17 | + |
| 18 | + Scene::~Scene() |
| 19 | + { |
| 20 | + for (auto& pMaterial : m_Materials) |
| 21 | + { |
| 22 | + delete pMaterial; |
| 23 | + pMaterial = nullptr; |
| 24 | + } |
| 25 | + |
| 26 | + m_Materials.clear(); |
| 27 | + } |
| 28 | + |
| 29 | + void dae::Scene::GetClosestHit(const Ray& ray, HitRecord& closestHit) const |
| 30 | + { |
| 31 | + for (const auto& sphere : m_SphereGeometries) |
| 32 | + { |
| 33 | + HitRecord hit; |
| 34 | + if (dae::GeometryUtils::HitTest_Sphere(sphere, ray, hit)) |
| 35 | + { |
| 36 | + closestHit.didHit = true; |
| 37 | + if (hit.t < closestHit.t) |
| 38 | + { |
| 39 | + closestHit.t = hit.t; |
| 40 | + closestHit.materialIndex = sphere.materialIndex; |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + for (const auto& plane : m_PlaneGeometries) |
| 46 | + { |
| 47 | + HitRecord hit; |
| 48 | + if (dae::GeometryUtils::HitTest_Plane(plane, ray, hit)) |
| 49 | + { |
| 50 | + closestHit.didHit = true; |
| 51 | + if (hit.t < closestHit.t) |
| 52 | + { |
| 53 | + closestHit.t = hit.t; |
| 54 | + closestHit.materialIndex = plane.materialIndex; |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + bool Scene::DoesHit(const Ray& ray) const |
| 61 | + { |
| 62 | + //todo W3 |
| 63 | + assert(false && "No Implemented Yet!"); |
| 64 | + return false; |
| 65 | + } |
67 | 66 |
|
68 | 67 | #pragma region Scene Helpers |
69 | | - Sphere* Scene::AddSphere(const Vector3& origin, float radius, unsigned char materialIndex) |
70 | | - { |
71 | | - Sphere s; |
72 | | - s.origin = origin; |
73 | | - s.radius = radius; |
74 | | - s.materialIndex = materialIndex; |
75 | | - |
76 | | - m_SphereGeometries.emplace_back(s); |
77 | | - return &m_SphereGeometries.back(); |
78 | | - } |
79 | | - |
80 | | - Plane* Scene::AddPlane(const Vector3& origin, const Vector3& normal, unsigned char materialIndex) |
81 | | - { |
82 | | - Plane p; |
83 | | - p.origin = origin; |
84 | | - p.normal = normal; |
85 | | - p.materialIndex = materialIndex; |
86 | | - |
87 | | - m_PlaneGeometries.emplace_back(p); |
88 | | - return &m_PlaneGeometries.back(); |
89 | | - } |
90 | | - |
91 | | - TriangleMesh* Scene::AddTriangleMesh(TriangleCullMode cullMode, unsigned char materialIndex) |
92 | | - { |
93 | | - TriangleMesh m{}; |
94 | | - m.cullMode = cullMode; |
95 | | - m.materialIndex = materialIndex; |
96 | | - |
97 | | - m_TriangleMeshGeometries.emplace_back(m); |
98 | | - return &m_TriangleMeshGeometries.back(); |
99 | | - } |
100 | | - |
101 | | - Light* Scene::AddPointLight(const Vector3& origin, float intensity, const ColorRGB& color) |
102 | | - { |
103 | | - Light l; |
104 | | - l.origin = origin; |
105 | | - l.intensity = intensity; |
106 | | - l.color = color; |
107 | | - l.type = LightType::Point; |
108 | | - |
109 | | - m_Lights.emplace_back(l); |
110 | | - return &m_Lights.back(); |
111 | | - } |
112 | | - |
113 | | - Light* Scene::AddDirectionalLight(const Vector3& direction, float intensity, const ColorRGB& color) |
114 | | - { |
115 | | - Light l; |
116 | | - l.direction = direction; |
117 | | - l.intensity = intensity; |
118 | | - l.color = color; |
119 | | - l.type = LightType::Directional; |
120 | | - |
121 | | - m_Lights.emplace_back(l); |
122 | | - return &m_Lights.back(); |
123 | | - } |
124 | | - |
125 | | - unsigned char Scene::AddMaterial(Material* pMaterial) |
126 | | - { |
127 | | - m_Materials.push_back(pMaterial); |
128 | | - return static_cast<unsigned char>(m_Materials.size() - 1); |
129 | | - } |
| 68 | + Sphere* Scene::AddSphere(const Vector3& origin, float radius, unsigned char materialIndex) |
| 69 | + { |
| 70 | + Sphere s; |
| 71 | + s.origin = origin; |
| 72 | + s.radius = radius; |
| 73 | + s.materialIndex = materialIndex; |
| 74 | + |
| 75 | + m_SphereGeometries.emplace_back(s); |
| 76 | + return &m_SphereGeometries.back(); |
| 77 | + } |
| 78 | + |
| 79 | + Plane* Scene::AddPlane(const Vector3& origin, const Vector3& normal, unsigned char materialIndex) |
| 80 | + { |
| 81 | + Plane p; |
| 82 | + p.origin = origin; |
| 83 | + p.normal = normal; |
| 84 | + p.materialIndex = materialIndex; |
| 85 | + |
| 86 | + m_PlaneGeometries.emplace_back(p); |
| 87 | + return &m_PlaneGeometries.back(); |
| 88 | + } |
| 89 | + |
| 90 | + TriangleMesh* Scene::AddTriangleMesh(TriangleCullMode cullMode, unsigned char materialIndex) |
| 91 | + { |
| 92 | + TriangleMesh m{}; |
| 93 | + m.cullMode = cullMode; |
| 94 | + m.materialIndex = materialIndex; |
| 95 | + |
| 96 | + m_TriangleMeshGeometries.emplace_back(m); |
| 97 | + return &m_TriangleMeshGeometries.back(); |
| 98 | + } |
| 99 | + |
| 100 | + Light* Scene::AddPointLight(const Vector3& origin, float intensity, const ColorRGB& color) |
| 101 | + { |
| 102 | + Light l; |
| 103 | + l.origin = origin; |
| 104 | + l.intensity = intensity; |
| 105 | + l.color = color; |
| 106 | + l.type = LightType::Point; |
| 107 | + |
| 108 | + m_Lights.emplace_back(l); |
| 109 | + return &m_Lights.back(); |
| 110 | + } |
| 111 | + |
| 112 | + Light* Scene::AddDirectionalLight(const Vector3& direction, float intensity, const ColorRGB& color) |
| 113 | + { |
| 114 | + Light l; |
| 115 | + l.direction = direction; |
| 116 | + l.intensity = intensity; |
| 117 | + l.color = color; |
| 118 | + l.type = LightType::Directional; |
| 119 | + |
| 120 | + m_Lights.emplace_back(l); |
| 121 | + return &m_Lights.back(); |
| 122 | + } |
| 123 | + |
| 124 | + unsigned char Scene::AddMaterial(Material* pMaterial) |
| 125 | + { |
| 126 | + m_Materials.push_back(pMaterial); |
| 127 | + return static_cast<unsigned char>(m_Materials.size() - 1); |
| 128 | + } |
130 | 129 | #pragma endregion |
131 | 130 | #pragma endregion |
132 | 131 |
|
133 | 132 | #pragma region SCENE W1 |
134 | | - void Scene_W1::Initialize() |
135 | | - { |
136 | | - //default: Material id0 >> SolidColor Material (RED) |
137 | | - constexpr unsigned char matId_Solid_Red = 0; |
138 | | - const unsigned char matId_Solid_Blue = AddMaterial(new Material_SolidColor{ colors::Blue }); |
139 | | - const unsigned char matId_Solid_Yellow = AddMaterial(new Material_SolidColor{ colors::Yellow }); |
140 | | - const unsigned char matId_Solid_Green = AddMaterial(new Material_SolidColor{ colors::Green }); |
141 | | - const unsigned char matId_Solid_Magenta = AddMaterial(new Material_SolidColor{ colors::Magenta }); |
142 | | - |
143 | | - //Spheres |
144 | | - AddSphere({ -25.f, 0.f, 100.f }, 50.f, matId_Solid_Red); |
145 | | - AddSphere({ 25.f, 0.f, 100.f }, 50.f, matId_Solid_Blue); |
146 | | - |
147 | | - //Plane |
148 | | - AddPlane({ -75.f, 0.f, 0.f }, { 1.f, 0.f,0.f }, matId_Solid_Green); |
149 | | - AddPlane({ 75.f, 0.f, 0.f }, { -1.f, 0.f,0.f }, matId_Solid_Green); |
150 | | - AddPlane({ 0.f, -75.f, 0.f }, { 0.f, 1.f,0.f }, matId_Solid_Yellow); |
151 | | - AddPlane({ 0.f, 75.f, 0.f }, { 0.f, -1.f,0.f }, matId_Solid_Yellow); |
152 | | - AddPlane({ 0.f, 0.f, 125.f }, { 0.f, 0.f,-1.f }, matId_Solid_Magenta); |
153 | | - } |
| 133 | + void Scene_W1::Initialize() |
| 134 | + { |
| 135 | + //default: Material id0 >> SolidColor Material (RED) |
| 136 | + constexpr unsigned char matId_Solid_Red = 0; |
| 137 | + const unsigned char matId_Solid_Blue = AddMaterial(new Material_SolidColor{colors::Blue}); |
| 138 | + const unsigned char matId_Solid_Yellow = AddMaterial(new Material_SolidColor{colors::Yellow}); |
| 139 | + const unsigned char matId_Solid_Green = AddMaterial(new Material_SolidColor{colors::Green}); |
| 140 | + const unsigned char matId_Solid_Magenta = AddMaterial(new Material_SolidColor{colors::Magenta}); |
| 141 | + |
| 142 | + //Spheres |
| 143 | + AddSphere({-25.f, 0.f, 100.f}, 50.f, matId_Solid_Red); |
| 144 | + AddSphere({25.f, 0.f, 100.f}, 50.f, matId_Solid_Blue); |
| 145 | + |
| 146 | + //Plane |
| 147 | + AddPlane({-75.f, 0.f, 0.f}, {1.f, 0.f, 0.f}, matId_Solid_Green); |
| 148 | + AddPlane({75.f, 0.f, 0.f}, {-1.f, 0.f, 0.f}, matId_Solid_Green); |
| 149 | + AddPlane({0.f, -75.f, 0.f}, {0.f, 1.f, 0.f}, matId_Solid_Yellow); |
| 150 | + AddPlane({0.f, 75.f, 0.f}, {0.f, -1.f, 0.f}, matId_Solid_Yellow); |
| 151 | + AddPlane({0.f, 0.f, 125.f}, {0.f, 0.f, -1.f}, matId_Solid_Magenta); |
| 152 | + } |
| 153 | +#pragma endregion |
| 154 | + void Scene_W2::Initialize() |
| 155 | + { |
| 156 | + m_Camera.origin = {0.f, 3.f, -9.f}; |
| 157 | + m_Camera.fovAngle = 90.f; |
| 158 | + |
| 159 | + //default: Material id0 >> SolidColor Material (RED) |
| 160 | + constexpr unsigned char matId_Solid_Red = 0; |
| 161 | + const unsigned char matId_Solid_Blue = AddMaterial(new Material_SolidColor{colors::Blue}); |
| 162 | + |
| 163 | + const unsigned char matId_Solid_Yellow = AddMaterial(new Material_SolidColor{colors::Yellow}); |
| 164 | + const unsigned char matId_Solid_Green = AddMaterial(new Material_SolidColor{colors::Green}); |
| 165 | + const unsigned char matId_Solid_Magenta = AddMaterial(new Material_SolidColor{colors::Magenta}); |
| 166 | + |
| 167 | + //Plane |
| 168 | + AddPlane({-5.f, 0.f, 0.f}, {1.f, 0.f, 0.f}, matId_Solid_Green); |
| 169 | + AddPlane({5.f, 0.f, 0.f}, {-1.f, 0.f, 0.f}, matId_Solid_Green); |
| 170 | + AddPlane({0.f, 0.f, 0.f}, {0.f, 1.f, 0.f}, matId_Solid_Yellow); |
| 171 | + AddPlane({0.f, 10.f, 0.f}, {0.f, -1.f, 0.f}, matId_Solid_Yellow); |
| 172 | + AddPlane({0.f, 0.f, 10.f}, {0.f, 0.f, -1.f}, matId_Solid_Magenta); |
| 173 | + |
| 174 | + //Spheres |
| 175 | + AddSphere({-1.75f, 1.f, 0.f}, 0.75f, matId_Solid_Red); |
| 176 | + AddSphere({0.f, 1.f, 0.f}, 0.75f, matId_Solid_Blue); |
| 177 | + AddSphere({1.75f, 1.f, 0.f}, 0.75f, matId_Solid_Red); |
| 178 | + AddSphere({-1.75f, 3.f, 0.f}, 0.75f, matId_Solid_Blue); |
| 179 | + AddSphere({0.f, 3.f, 0.f}, 0.75f, matId_Solid_Red); |
| 180 | + AddSphere({1.75f, 3.f, 0.f}, 0.75f, matId_Solid_Blue); |
| 181 | + |
| 182 | + //Light |
| 183 | + AddPointLight({0.f, 5.f, -5.f}, 70.f, colors::White); |
| 184 | + } |
154 | 185 | #pragma endregion |
155 | 186 | } |
0 commit comments