Finish Week 1 (Could be a bug)
This commit is contained in:
@@ -20,22 +20,36 @@ Renderer::Renderer(SDL_Window * pWindow) :
|
|||||||
SDL_GetWindowSize(pWindow, &m_Width, &m_Height);
|
SDL_GetWindowSize(pWindow, &m_Width, &m_Height);
|
||||||
m_pBufferPixels = static_cast<uint32_t*>(m_pBuffer->pixels);
|
m_pBufferPixels = static_cast<uint32_t*>(m_pBuffer->pixels);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Renderer::Render(Scene* pScene) const
|
void Renderer::Render(Scene* pScene) const
|
||||||
{
|
{
|
||||||
Camera& camera = pScene->GetCamera();
|
Camera& camera = pScene->GetCamera();
|
||||||
auto& materials = pScene->GetMaterials();
|
auto& materials = pScene->GetMaterials();
|
||||||
auto& lights = pScene->GetLights();
|
auto& lights = pScene->GetLights();
|
||||||
|
|
||||||
|
float apsectRatio = static_cast<float>(m_Width) / m_Height;
|
||||||
|
|
||||||
for (int px{}; px < m_Width; ++px)
|
for (int px{}; px < m_Width; ++px)
|
||||||
{
|
{
|
||||||
for (int py{}; py < m_Height; ++py)
|
for (int py{}; py < m_Height; ++py)
|
||||||
{
|
{
|
||||||
float gradient = px / static_cast<float>(m_Width);
|
float NDCx = (2 * ((px + 0.5) / m_Width) - 1) * apsectRatio;
|
||||||
gradient += py / static_cast<float>(m_Width);
|
float NDCy = 1 - 2 * ((py + 0.5) / m_Height);
|
||||||
gradient /= 2.0f;
|
|
||||||
|
|
||||||
ColorRGB finalColor{ gradient, gradient, gradient };
|
Vector3 rayDir = Vector3{ NDCx, NDCy, 1 };
|
||||||
|
Ray ray{ Vector3(0, 0, 0), rayDir };
|
||||||
|
|
||||||
|
//set the color based on the ray direction
|
||||||
|
ColorRGB finalColor{ 0.f, 0.f, 0.f };
|
||||||
|
|
||||||
|
HitRecord closestHit{};
|
||||||
|
pScene->GetClosestHit(ray, closestHit);
|
||||||
|
|
||||||
|
if(closestHit.didHit){
|
||||||
|
finalColor = materials[closestHit.materialIndex]->Shade();
|
||||||
|
// finalColor = ColorRGB(closestHit.normal.x, closestHit.normal.y, closestHit.normal.z);
|
||||||
|
// const float scaled_t = (closestHit.t - 50.f) / 40.f;
|
||||||
|
// finalColor = {scaled_t, scaled_t, scaled_t};
|
||||||
|
}
|
||||||
|
|
||||||
//Update Color in Buffer
|
//Update Color in Buffer
|
||||||
finalColor.MaxToOne();
|
finalColor.MaxToOne();
|
||||||
@@ -44,6 +58,7 @@ void Renderer::Render(Scene* pScene) const
|
|||||||
static_cast<uint8_t>(finalColor.r * 255),
|
static_cast<uint8_t>(finalColor.r * 255),
|
||||||
static_cast<uint8_t>(finalColor.g * 255),
|
static_cast<uint8_t>(finalColor.g * 255),
|
||||||
static_cast<uint8_t>(finalColor.b * 255));
|
static_cast<uint8_t>(finalColor.b * 255));
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -28,8 +28,21 @@ namespace dae {
|
|||||||
|
|
||||||
void dae::Scene::GetClosestHit(const Ray& ray, HitRecord& closestHit) const
|
void dae::Scene::GetClosestHit(const Ray& ray, HitRecord& closestHit) const
|
||||||
{
|
{
|
||||||
//todo W1
|
closestHit.t = FLT_MAX;
|
||||||
throw std::runtime_error("Not Implemented Yet");
|
|
||||||
|
for (const Sphere& sphere : m_SphereGeometries){
|
||||||
|
HitRecord hitRecord{};
|
||||||
|
if (GeometryUtils::HitTest_Sphere(sphere, ray, hitRecord) && hitRecord.t < closestHit.t){
|
||||||
|
closestHit = hitRecord;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const Plane& plane : m_PlaneGeometries){
|
||||||
|
HitRecord hitRecord{};
|
||||||
|
if(GeometryUtils::HitTest_Plane(plane, ray, hitRecord) && hitRecord.t < closestHit.t){
|
||||||
|
closestHit = hitRecord;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Scene::DoesHit(const Ray& ray) const
|
bool Scene::DoesHit(const Ray& ray) const
|
||||||
|
|||||||
@@ -11,9 +11,34 @@ namespace dae
|
|||||||
//SPHERE HIT-TESTS
|
//SPHERE HIT-TESTS
|
||||||
inline bool HitTest_Sphere(const Sphere& sphere, const Ray& ray, HitRecord& hitRecord, bool ignoreHitRecord = false)
|
inline bool HitTest_Sphere(const Sphere& sphere, const Ray& ray, HitRecord& hitRecord, bool ignoreHitRecord = false)
|
||||||
{
|
{
|
||||||
//todo W1
|
const float radius2 = sphere.radius * sphere.radius;
|
||||||
throw std::runtime_error("Not Implemented Yet");
|
const Vector3 CameraToOrigin = sphere.origin - ray.origin;
|
||||||
|
const float tInSphere = Vector3::Dot(CameraToOrigin, ray.direction);
|
||||||
|
|
||||||
|
if(tInSphere < 0) return false; //Looking away from sphere
|
||||||
|
|
||||||
|
const float od2 = std::pow(Vector3::Reject(CameraToOrigin, ray.direction).Magnitude(),2);
|
||||||
|
const float thc = std::sqrt(radius2 - od2);
|
||||||
|
|
||||||
|
float t0 = tInSphere - thc;
|
||||||
|
float t1 = tInSphere + thc;
|
||||||
|
|
||||||
|
if(t0 < 0 && t1 < 0) return false; //Both intersections are behind the camera
|
||||||
|
|
||||||
|
if(t0 < 0) t0 = t1; //If t0 is behind the camera, use t1
|
||||||
|
|
||||||
|
if (!ignoreHitRecord && t0 < hitRecord.t){
|
||||||
|
hitRecord.t = t0;
|
||||||
|
hitRecord.origin = ray.origin + ray.direction * t0;
|
||||||
|
hitRecord.normal = (hitRecord.origin - sphere.origin).Normalized();
|
||||||
|
hitRecord.materialIndex = sphere.materialIndex;
|
||||||
|
hitRecord.didHit = true;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool HitTest_Sphere(const Sphere& sphere, const Ray& ray)
|
inline bool HitTest_Sphere(const Sphere& sphere, const Ray& ray)
|
||||||
@@ -26,8 +51,23 @@ namespace dae
|
|||||||
//PLANE HIT-TESTS
|
//PLANE HIT-TESTS
|
||||||
inline bool HitTest_Plane(const Plane& plane, const Ray& ray, HitRecord& hitRecord, bool ignoreHitRecord = false)
|
inline bool HitTest_Plane(const Plane& plane, const Ray& ray, HitRecord& hitRecord, bool ignoreHitRecord = false)
|
||||||
{
|
{
|
||||||
//todo W1
|
float dot = Vector3::Dot(plane.normal, ray.direction);
|
||||||
throw std::runtime_error("Not Implemented Yet");
|
if(dot < 0) {
|
||||||
|
float t = Vector3::Dot(plane.origin - ray.origin, plane.normal) / dot;
|
||||||
|
|
||||||
|
bool hit = t >= 0;
|
||||||
|
|
||||||
|
if(hit && !ignoreHitRecord){
|
||||||
|
hitRecord.t = t;
|
||||||
|
hitRecord.origin = ray.origin + ray.direction * t;
|
||||||
|
hitRecord.normal = plane.normal;
|
||||||
|
hitRecord.materialIndex = plane.materialIndex;
|
||||||
|
hitRecord.didHit = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return hit;
|
||||||
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -49,16 +49,12 @@ namespace dae {
|
|||||||
|
|
||||||
float Vector3::Dot(const Vector3& v1, const Vector3& v2)
|
float Vector3::Dot(const Vector3& v1, const Vector3& v2)
|
||||||
{
|
{
|
||||||
//todo W1
|
return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
|
||||||
throw std::runtime_error("Not Implemented Yet");
|
|
||||||
return {};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Vector3 Vector3::Cross(const Vector3& v1, const Vector3& v2)
|
Vector3 Vector3::Cross(const Vector3& v1, const Vector3& v2)
|
||||||
{
|
{
|
||||||
//todo W1
|
return { v1.y * v2.z - v1.z * v2.y, v1.z * v2.x - v1.x * v2.z, v1.x * v2.y - v1.y * v2.x };
|
||||||
throw std::runtime_error("Not Implemented Yet");
|
|
||||||
return {};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Vector3 Vector3::Project(const Vector3& v1, const Vector3& v2)
|
Vector3 Vector3::Project(const Vector3& v1, const Vector3& v2)
|
||||||
|
|||||||
@@ -42,9 +42,7 @@ namespace dae
|
|||||||
|
|
||||||
float Vector4::Dot(const Vector4& v1, const Vector4& v2)
|
float Vector4::Dot(const Vector4& v1, const Vector4& v2)
|
||||||
{
|
{
|
||||||
//todo W1
|
return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z + v1.w * v2.w;
|
||||||
throw std::runtime_error("Not Implemented Yet");
|
|
||||||
return {};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#pragma region Operator Overloads
|
#pragma region Operator Overloads
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ int main(int argc, char* args[])
|
|||||||
const uint32_t height = 480;
|
const uint32_t height = 480;
|
||||||
|
|
||||||
SDL_Window* pWindow = SDL_CreateWindow(
|
SDL_Window* pWindow = SDL_CreateWindow(
|
||||||
"RayTracer - **Insert Name**",
|
"RayTracer - Bram",
|
||||||
SDL_WINDOWPOS_UNDEFINED,
|
SDL_WINDOWPOS_UNDEFINED,
|
||||||
SDL_WINDOWPOS_UNDEFINED,
|
SDL_WINDOWPOS_UNDEFINED,
|
||||||
width, height, 0);
|
width, height, 0);
|
||||||
|
|||||||
Reference in New Issue
Block a user