Skip to content

Commit 1d58666

Browse files
committed
Merge branch 'W02'
2 parents ee6d3ad + c069b5d commit 1d58666

File tree

5 files changed

+200
-146
lines changed

5 files changed

+200
-146
lines changed

source/Renderer.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,13 @@ void Renderer::Render(Scene* pScene) const
4545
// rayDirection between almost -1 and 1
4646
const float rayDirectionX { (px + 0.5f) / m_fWidth * 2.0f - 1.0f };
4747
const float rayDirectionY{ 1.0f - (py + 0.5f) / m_fHeight * 2.0f };
48-
rayDirection.x = rayDirectionX * aspectRatio;
49-
rayDirection.y = rayDirectionY;
48+
const float FOV{CalculateFOV(camera.fovAngle)};
49+
rayDirection.x = rayDirectionX * aspectRatio * FOV;
50+
rayDirection.y = rayDirectionY * FOV;
5051
rayDirection.z = 1.0f;
5152
rayDirection.Normalize();
5253

53-
Ray viewRay{ {0.0f, 0.0f, 0.0f}, rayDirection };
54+
Ray viewRay{ camera.origin, rayDirection };
5455
Sphere testSphere{ {0.0f, 0.0f, 100.0f}, 50.0f , 0 };
5556
Plane testPlane{ {0.0f, -50.0f, 0.0f}, {0.0f, 1.0f, 0.0f}, 0 };
5657

@@ -94,3 +95,9 @@ bool Renderer::SaveBufferToImage() const
9495
{
9596
return SDL_SaveBMP(m_pBuffer, "RayTracing_Buffer.bmp");
9697
}
98+
99+
float Renderer::CalculateFOV(float angle) const
100+
{
101+
const float halfAlpha{ (angle / 2.0f) * TO_RADIANS };
102+
return std::tanf(halfAlpha);
103+
}

source/Renderer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ namespace dae
2222

2323
void Render(Scene* pScene) const;
2424
bool SaveBufferToImage() const;
25+
float CalculateFOV(float angle) const;
2526

2627
private:
2728
SDL_Window* m_pWindow{};

source/Scene.cpp

Lines changed: 173 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -2,154 +2,185 @@
22
#include "Utils.h"
33
#include "Material.h"
44

5-
namespace dae {
6-
5+
namespace dae
6+
{
77
#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+
}
6766

6867
#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+
}
130129
#pragma endregion
131130
#pragma endregion
132131

133132
#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+
}
154185
#pragma endregion
155186
}

source/Scene.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,21 @@ namespace dae
7878
Scene_W1& operator=(const Scene_W1&) = delete;
7979
Scene_W1& operator=(Scene_W1&&) noexcept = delete;
8080

81+
void Initialize() override;
82+
};
83+
//+++++++++++++++++++++++++++++++++++++++++
84+
//WEEK 2 Test Scene
85+
class Scene_W2 final : public Scene
86+
{
87+
public:
88+
Scene_W2() = default;
89+
~Scene_W2() override = default;
90+
91+
Scene_W2(const Scene_W2&) = delete;
92+
Scene_W2(Scene_W2&&) noexcept = delete;
93+
Scene_W2& operator=(const Scene_W2&) = delete;
94+
Scene_W2& operator=(Scene_W2&&) noexcept = delete;
95+
8196
void Initialize() override;
8297
};
8398
}

source/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ int main(int argc, char* args[])
6868
const auto pTimer = new Timer();
6969
const auto pRenderer = new Renderer(pWindow, width, height);
7070

71-
const auto pScene = new Scene_W1();
71+
const auto pScene = new Scene_W2();
7272
pScene->Initialize();
7373

7474
//Start loop

0 commit comments

Comments
 (0)