Skip to content

Commit be78c9a

Browse files
committed
Implement shadows
1 parent 9dd2ed0 commit be78c9a

File tree

6 files changed

+72
-41
lines changed

6 files changed

+72
-41
lines changed

source/Camera.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,16 @@ namespace dae
5656
if (pKeyboardState[SDL_SCANCODE_A])
5757
{
5858
origin -= right * deltaTime * speed;
59+
//forward = Matrix::CreateRotation(totalPitch * TO_RADIANS, totalYaw * TO_RADIANS, 0.0f).TransformVector(Vector3::UnitZ);
60+
//forward = (Matrix::CreateRotationY(totalYaw * TO_RADIANS) * Matrix::CreateRotationX(totalPitch * TO_RADIANS)).TransformVector(Vector3::UnitZ);
61+
//forward.Normalize();
5962
}
6063
else if (pKeyboardState[SDL_SCANCODE_D])
6164
{
6265
origin += right * deltaTime * speed;
66+
//forward = Matrix::CreateRotation(totalPitch * TO_RADIANS, totalYaw * TO_RADIANS, 0.0f).TransformVector(Vector3::UnitZ);
67+
//forward = (Matrix::CreateRotationY(totalYaw * TO_RADIANS) * Matrix::CreateRotationX(totalPitch * TO_RADIANS)).TransformVector(Vector3::UnitZ);
68+
//forward.Normalize();
6369
}
6470
if (pKeyboardState[SDL_SCANCODE_W])
6571
{
@@ -95,6 +101,7 @@ namespace dae
95101
if (mouseX or mouseY)
96102
{
97103
forward = Matrix::CreateRotation(totalPitch * TO_RADIANS, totalYaw * TO_RADIANS, 0.0f).TransformVector(Vector3::UnitZ);
104+
//forward = (Matrix::CreateRotationY(totalYaw * TO_RADIANS) * Matrix::CreateRotationX(totalPitch * TO_RADIANS)).TransformVector(Vector3::UnitZ);
98105
forward.Normalize();
99106
}
100107
}

source/Renderer.cpp

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,9 @@
1212

1313
using namespace dae;
1414

15-
Renderer::Renderer(SDL_Window * pWindow, uint32_t width, uint32_t height) :
15+
Renderer::Renderer(SDL_Window * pWindow) :
1616
m_pWindow(pWindow),
17-
m_pBuffer(SDL_GetWindowSurface(pWindow)),
18-
m_fWidth{ static_cast<float>(width) },
19-
m_fHeight{static_cast<float>(height)}
17+
m_pBuffer(SDL_GetWindowSurface(pWindow))
2018
{
2119
//Initialize
2220
SDL_GetWindowSize(pWindow, &m_Width, &m_Height);
@@ -30,11 +28,11 @@ void Renderer::Render(Scene* pScene) const
3028
auto& lights = pScene->GetLights();
3129
const Matrix cameraToWorld{camera.CalculateCameraToWorld()};
3230
//std::cout << cameraToWorld << std::endl;
33-
const float aspectRatio{ m_fWidth / m_fHeight };
31+
const float aspectRatio{ static_cast<float>(m_Width) / static_cast<float>(m_Height) };
3432
Vector3 rayDirection;
35-
for (float px{}; px < m_fWidth; ++px)
33+
for (int px{}; px < m_Width; ++px)
3634
{
37-
for (float py{}; py < m_fHeight; ++py)
35+
for (int py{}; py < m_Height; ++py)
3836
{
3937
/*
4038
float gradient = px / m_fWidth;
@@ -44,8 +42,8 @@ void Renderer::Render(Scene* pScene) const
4442

4543
// + 0.5f: we need the middle of a pixel
4644
// rayDirection between almost -1 and 1
47-
const float rayDirectionX { (px + 0.5f) / m_fWidth * 2.0f - 1.0f };
48-
const float rayDirectionY{ 1.0f - (py + 0.5f) / m_fHeight * 2.0f };
45+
const float rayDirectionX { (static_cast<float>(px) + 0.5f) / static_cast<float>(m_Width) * 2.0f - 1.0f };
46+
const float rayDirectionY{ 1.0f - (static_cast<float>(py) + 0.5f) / static_cast<float>(m_Height) * 2.0f };
4947
const float FOV{CalculateFOV(camera.fovAngle)};
5048
rayDirection.x = rayDirectionX * aspectRatio * FOV;
5149
rayDirection.y = rayDirectionY * FOV;
@@ -76,6 +74,17 @@ void Renderer::Render(Scene* pScene) const
7674

7775
//const float scaled_t = closestHit.t / 500.0f;
7876
//finalColor = { scaled_t, scaled_t, scaled_t };
77+
for (const auto& light : lights)
78+
{
79+
closestHit.origin += closestHit.normal * 0.001f;
80+
const Vector3 dirToLight{LightUtils::GetDirectionToLight(light,closestHit.origin)};
81+
const float lightDistance{dirToLight.Magnitude()};
82+
Ray shadowRay{ closestHit.origin, dirToLight / lightDistance, 0.0001f, lightDistance };
83+
if (pScene->DoesHit(shadowRay))
84+
{
85+
finalColor *= 0.5f;
86+
}
87+
}
7988
}
8089

8190
// Test rayDirection's colors

source/Renderer.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace dae
1212
class Renderer final
1313
{
1414
public:
15-
Renderer(SDL_Window* pWindow, uint32_t width, uint32_t height);
15+
Renderer(SDL_Window* pWindow);
1616
~Renderer() = default;
1717

1818
Renderer(const Renderer&) = delete;
@@ -32,7 +32,5 @@ namespace dae
3232

3333
int m_Width{};
3434
int m_Height{};
35-
const float m_fWidth;
36-
const float m_fHeight;
3735
};
3836
}

source/Scene.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ namespace dae
3737
if (hit.t < closestHit.t)
3838
{
3939
closestHit.t = hit.t;
40+
closestHit.origin = hit.origin;
41+
closestHit.normal = hit.normal;
4042
closestHit.materialIndex = sphere.materialIndex;
4143
}
4244
}
@@ -51,6 +53,8 @@ namespace dae
5153
if (hit.t < closestHit.t)
5254
{
5355
closestHit.t = hit.t;
56+
closestHit.origin = hit.origin;
57+
closestHit.normal = hit.normal;
5458
closestHit.materialIndex = plane.materialIndex;
5559
}
5660
}
@@ -59,8 +63,21 @@ namespace dae
5963

6064
bool Scene::DoesHit(const Ray& ray) const
6165
{
62-
//todo W3
63-
assert(false && "No Implemented Yet!");
66+
HitRecord hit;
67+
for (const auto& sphere : m_SphereGeometries)
68+
{
69+
if (dae::GeometryUtils::HitTest_Sphere(sphere, ray, hit, true))
70+
{
71+
return true;
72+
}
73+
}
74+
for (const auto& plane : m_PlaneGeometries)
75+
{
76+
if (dae::GeometryUtils::HitTest_Plane(plane, ray, hit, true))
77+
{
78+
return true;
79+
}
80+
}
6481
return false;
6582
}
6683

source/Utils.h

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,24 @@ inline bool HitTest_Sphere(const Sphere& sphere, const Ray& ray, HitRecord& hitR
2323
const float radius2{ sphere.radius * sphere.radius };
2424
if (d2 > radius2)
2525
return false;
26-
if (not ignoreHitRecord)
26+
const float thc{ sqrt(radius2 - d2) }; // distance from ray to sphere surface
27+
float t0{ tca - thc }; // distance from ray's origin to sphere surface
28+
float t1{ tca + thc }; // distance from ray's origin to sphere surface
29+
//if (t0 > t1)
30+
// std::swap(t0, t1);
31+
if (t0 > ray.min and t0 < ray.max)
2732
{
28-
const float thc{ sqrt(radius2 - d2) }; // distance from ray to sphere surface
29-
float t0{ tca - thc }; // distance from ray's origin to sphere surface
30-
float t1{ tca + thc }; // distance from ray's origin to sphere surface
31-
//if (t0 > t1)
32-
// std::swap(t0, t1);
33-
hitRecord.didHit = true;
34-
hitRecord.t = t0;
35-
//hitRecord.origin = ray.origin + ray.direction * hitRecord.t;
36-
//hitRecord.normal = (hitRecord.origin - sphere.origin) / sphere.radius;
37-
hitRecord.materialIndex = sphere.materialIndex;
33+
if (not ignoreHitRecord)
34+
{
35+
hitRecord.didHit = true;
36+
hitRecord.t = t0;
37+
hitRecord.origin = ray.origin + ray.direction * hitRecord.t;
38+
hitRecord.normal = (hitRecord.origin - sphere.origin) / sphere.radius;
39+
hitRecord.materialIndex = sphere.materialIndex;
40+
}
41+
return true;
3842
}
39-
return true;
43+
return false;
4044
}
4145

4246
inline bool HitTest_Sphere(const Sphere& sphere, const Ray& ray)
@@ -51,20 +55,18 @@ inline bool HitTest_Sphere(const Sphere& sphere, const Ray& ray, HitRecord& hitR
5155
{
5256
const float denom{ Vector3::Dot(plane.normal, ray.direction) };
5357
//if (denom < 0.0f)
54-
if (true)
58+
//if (true)
5559
{
5660
const float t{ Vector3::Dot(plane.origin - ray.origin, plane.normal) / denom };
57-
if (t >= ray.min and t < ray.max)
61+
if (t > ray.max or t < ray.min) return false;
62+
if (not ignoreHitRecord)
5863
{
59-
if (not ignoreHitRecord)
60-
{
61-
hitRecord.didHit = true;
62-
hitRecord.t = t;
63-
hitRecord.origin = ray.origin + ray.direction * hitRecord.t;
64-
hitRecord.normal = plane.normal;
65-
hitRecord.materialIndex = plane.materialIndex;
66-
}
67-
return true;
64+
hitRecord.didHit = true;
65+
hitRecord.t = t;
66+
hitRecord.origin = ray.origin + ray.direction * hitRecord.t;
67+
hitRecord.normal = plane.normal;
68+
hitRecord.materialIndex = plane.materialIndex;
69+
return true;
6870
}
6971
}
7072
}
@@ -111,9 +113,7 @@ inline bool HitTest_Sphere(const Sphere& sphere, const Ray& ray, HitRecord& hitR
111113
//Direction from target to light
112114
inline Vector3 GetDirectionToLight(const Light& light, const Vector3 origin)
113115
{
114-
//todo W3
115-
assert(false && "No Implemented Yet!");
116-
return {};
116+
return light.origin - origin;
117117
}
118118

119119
inline ColorRGB GetRadiance(const Light& light, const Vector3& target)

source/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ int main(int argc, char* args[])
6666

6767
//Initialize "framework"
6868
const auto pTimer = new Timer();
69-
const auto pRenderer = new Renderer(pWindow, width, height);
69+
const auto pRenderer = new Renderer(pWindow);
7070

7171
const auto pScene = new Scene_W2();
7272
pScene->Initialize();

0 commit comments

Comments
 (0)