-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRenderer.cpp
More file actions
186 lines (151 loc) · 5.24 KB
/
Renderer.cpp
File metadata and controls
186 lines (151 loc) · 5.24 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
//External includes
#include "SDL.h"
#include "SDL_surface.h"
#include <execution>
//Project includes
#include "Renderer.h"
#include "Math.h"
#include "Material.h"
#include "Scene.h"
#include "Utils.h"
using namespace dae;
Renderer::Renderer(SDL_Window* pWindow) :
m_pWindow(pWindow),
m_pBuffer(SDL_GetWindowSurface(pWindow))
{
//Initialize
SDL_GetWindowSize(pWindow, &m_Width, &m_Height);
m_pBufferPixels = static_cast<uint32_t*>(m_pBuffer->pixels);
m_AspectRatio = static_cast<float>(m_Width) / static_cast<float>(m_Height);
m_LightingMode = LightingMode::Combined;
SetupPixelIndices();
}
void Renderer::RenderPixel(Scene* pScene, const Vector2& rayLocation) const
{
auto& materials = pScene->GetMaterials();
const auto rayDirection = GetRayDirection(static_cast<float>(rayLocation.x), static_cast<float>(rayLocation.y), &pScene->GetCamera());
const Ray viewRay = { pScene->GetCamera().origin, rayDirection };
constexpr size_t bounces{ 1 };
ColorRGB finalColor{};
for(size_t i{}; i < bounces; ++i)
{
//Contains information about a potential hit.
HitRecord closestHit{};
//Get the closest hit for the current ray.
pScene->GetClosestHit(viewRay, closestHit);
//if we did hit something
if (closestHit.didHit)
{
const Vector3 offsetPosition{ closestHit.origin + closestHit.normal * m_RayOffset };
//Go over all lights in the scene
for (const auto& light : pScene->GetLights())
{
//Calculate the direction of the light
Vector3 lightDirection{ LightUtils::GetDirectionToLight(light, offsetPosition) };
const auto lightDistance{ lightDirection.Magnitude() };
//Normalize the direction
lightDirection /= lightDistance;
//Calculate the shadows
if (m_ShadowsEnabled)
{
const Ray lightRay{ offsetPosition, lightDirection, FLT_MIN, lightDistance };
//if we hitted something, we are in shadow, so skip the Lighting calculation
if (pScene->DoesHit(lightRay))
{
continue;
}
}
switch (m_LightingMode)
{
case LightingMode::ObservedArea: //LambertCosine
{
const auto lightNormalAngle{ std::max(Vector3::Dot(closestHit.normal, lightDirection), 0.0f) };
finalColor += ColorRGB{ lightNormalAngle, lightNormalAngle, lightNormalAngle };
break;
}
case LightingMode::Radiance:
{
finalColor += LightUtils::GetRadiance(light, closestHit.origin);
break;
}
case LightingMode::BRDF:
{
const ColorRGB BRDF{ materials[closestHit.materialIndex]->Shade(closestHit, lightDirection, -rayDirection) };
finalColor += BRDF;
break;
}
case LightingMode::Combined:
{
const float lightNormalAngle{ std::max(Vector3::Dot(closestHit.normal, lightDirection), 0.0f) };
const ColorRGB radiance{ LightUtils::GetRadiance(light, closestHit.origin) };
const auto material = materials[closestHit.materialIndex];
const ColorRGB BRDF{ material->Shade(closestHit, lightDirection, -rayDirection) };
finalColor += radiance * BRDF * lightNormalAngle;
}
}
}
}
else
{
//if we didn't hit anything, we are looking at the skybox
//finalColor += pScene->GetSkybox().GetColor(rayDirection);
finalColor += ColorRGB{0.2f, 0.3f, 0.5f};
}
//Reflect the ray
//Todo Look how to properly implement reflections
//viewRay = Ray{ closestHit.origin, Vector3::Reflect(rayDirection, closestHit.normal) };
//viewRay.max = materials[closestHit.materialIndex]->GetReflectivity();
}
//Update Color in Buffer
finalColor.MaxToOne();
m_pBufferPixels[static_cast<uint32_t>(rayLocation.x) + (static_cast<uint32_t>(rayLocation.y) * m_Width)] = SDL_MapRGB(m_pBuffer->format,
static_cast<uint8_t>(finalColor.r * 255),
static_cast<uint8_t>(finalColor.g * 255),
static_cast<uint8_t>(finalColor.b * 255));
}
void Renderer::SetupPixelIndices()
{
m_amountOfPixels = m_Width * m_Height;
m_PixelIndices.reserve(m_amountOfPixels);
for (int px{}; px < m_Width; ++px)
{
for (int py = 0; py < m_Height; ++py)
{
m_PixelIndices.emplace_back(static_cast<float>(px), static_cast<float>(py));
}
}
}
void Renderer::Render(Scene* pScene) const
{
std::for_each(std::execution::par, m_PixelIndices.begin(), m_PixelIndices.end(), [&](const Vector2& rayLocation)
{
RenderPixel(pScene, rayLocation);
});
//@END
//Update SDL Surface
SDL_UpdateWindowSurface(m_pWindow);
}
bool Renderer::SaveBufferToImage() const
{
return SDL_SaveBMP(m_pBuffer, "RayTracing_Buffer.bmp");
}
void Renderer::CycleLightingMode()
{
//Increment the lighting mode
m_LightingMode = static_cast<LightingMode>((static_cast<int>(m_LightingMode) + 1) % static_cast<int>(LightingMode::COUNT));
}
Vector3 Renderer::GetRayDirection(float x, float y, Camera* pCamera) const
{
//Get the middle of the pixel
const auto pcx{ x + 0.5f };
const auto pcy{ y + 0.5f };
//From pixel to raster space
const auto cx = (2.f * pcx / static_cast<float>(m_Width) - 1) * m_AspectRatio * pCamera->fovAngle;
const auto cy = 1 - 2.f * pcy / static_cast<float>(m_Height) * pCamera->fovAngle;
//From raster to camera space
const auto ray = Vector3{ cx,cy,1 };
//From camera to world space.
const Matrix cameraToWorld{ pCamera->CalculateCameraToWorld() };
//Normalize and return
return Vector3{ cameraToWorld.TransformVector(ray.Normalized()) };
}