From 2f8726b705c14eed025b18fc3801a998aef4184e Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Sat, 21 Aug 2021 17:32:27 +0900 Subject: [PATCH] EX02 Fixes --- .../src/rayTracer.cpp | 48 +++++++++---------- .../src/rayTracer.hpp | 4 +- 2 files changed, 24 insertions(+), 28 deletions(-) diff --git a/example02_threaded_raytracing/src/rayTracer.cpp b/example02_threaded_raytracing/src/rayTracer.cpp index eebef8c..00eb9e1 100644 --- a/example02_threaded_raytracing/src/rayTracer.cpp +++ b/example02_threaded_raytracing/src/rayTracer.cpp @@ -10,15 +10,14 @@ const float PI = std::acos(-1.0f); glm::vec3 Ex02::RT::Internal::defaultSpherePos() { - // the default model matrix pushes everything back 2 units, - // so it should be ok to define the sphere at location 0 - return glm::vec3{0.0f, 0.0f, 0.0f}; + return glm::vec3{0.0f, 0.0f, -2.5f}; } glm::vec3 Ex02::RT::Internal::defaultLightPos() { - return glm::vec3{1.0f, 1.0f, 0.0f}; + return glm::vec3{4.0f, 4.0f, 0.0f}; } +/* glm::mat4x4 Ex02::RT::Internal::defaultMVP() { glm::mat4x4 mvp = glm::perspective( PI / 2.0f, @@ -38,6 +37,7 @@ glm::mat4x4 Ex02::RT::Internal::defaultMVP() { return mvp; } +*/ std::optional Ex02::RT::Internal::rayToSphere( glm::vec3 rayPos, @@ -73,13 +73,18 @@ std::optional Ex02::RT::Internal::rayToSphere( + rayDir.y * tempVec.y + rayDir.z * tempVec.z; float dist = -temp - std::sqrt(delta); - if(dist < 0.0f) { - dist = -temp + std::sqrt(delta); - if(dist < 0.0f) { + float dist2 = -temp + std::sqrt(delta); + float min = dist > dist2 ? dist2 : dist; + float max = dist > dist2 ? dist : dist2; + if(min < 0.0f) { + if(max < 0.0f) { return {}; + } else { + return {rayPos + rayDir * max}; } + } else { + return {rayPos + rayDir * min}; } - return {rayPos + rayDir * dist}; } } @@ -97,46 +102,39 @@ std::vector Ex02::RT::renderGraySphere( float sphereRadius, int threadCount, glm::vec3 spherePos, - glm::vec3 lightPos, - glm::mat4x4 mvp) { + glm::vec3 lightPos) { std::vector grayscalePixels; grayscalePixels.resize(outputWidth * outputHeight); - -// glm::vec3 tr_spherePos = mvp * glm::vec4{spherePos, 1.0f}; -// glm::vec3 tr_lightPos = mvp * glm::vec4{lightPos, 1.0f}; - glm::vec3 tr_spherePos = glm::vec3{0.0f, 0.0f, -2.0f}; - glm::vec3 tr_lightPos = glm::vec3{1.0f, 1.0f, 0.0f}; glm::vec3 rayPos{0.0f, 0.0f, 0.0f}; if(threadCount == 1) { for(unsigned int j = 0; j < outputHeight; ++j) { - float offsetY = ((float)j - ((float)outputHeight / 2.0f)); + float offsetY = ((float)j + 0.5f - ((float)outputHeight / 2.0f)); for(unsigned int i = 0; i < outputWidth; ++i) { - float offsetX = ((float)i - ((float)outputWidth / 2.0f)); + float offsetX = ((float)i + 0.5f - ((float)outputWidth / 2.0f)); glm::vec3 rayDir = glm::vec3{ offsetX, offsetY, -EX02_RAY_TRACER_DRAW_PLANE}; auto rayResult = Internal::rayToSphere( - rayPos, rayDir, tr_spherePos, sphereRadius); + rayPos, rayDir, spherePos, sphereRadius); if(rayResult) { - glm::vec3 toLight = *rayResult - tr_lightPos; + glm::vec3 toLight = lightPos - *rayResult; toLight /= std::sqrt( toLight.x * toLight.x + toLight.y * toLight.y + toLight.z * toLight.z); - glm::vec3 toLightPos = *rayResult + toLight * 2.4f; + glm::vec3 toLightPos = *rayResult + toLight; auto collResult = Internal::rayToSphere( - toLightPos, toLight, tr_spherePos, sphereRadius); + toLightPos, toLight, spherePos, sphereRadius); if(collResult) { continue; } - glm::vec3 resultToLight = tr_lightPos - *rayResult; glm::vec3 resultToOrigin = rayPos - *rayResult; float angle = Internal::angleBetweenRays( - resultToLight, - resultToOrigin); + toLight, + resultToOrigin); if(angle <= PI && angle >= 0.0f) { grayscalePixels.at(i + j * outputWidth) = - (1.0f - angle / PI) * 255.0f; + (angle / PI) * 255.0f; } } } diff --git a/example02_threaded_raytracing/src/rayTracer.hpp b/example02_threaded_raytracing/src/rayTracer.hpp index fef06b5..d0dd96c 100644 --- a/example02_threaded_raytracing/src/rayTracer.hpp +++ b/example02_threaded_raytracing/src/rayTracer.hpp @@ -20,7 +20,6 @@ namespace RT { namespace Internal { glm::vec3 defaultSpherePos(); glm::vec3 defaultLightPos(); - glm::mat4x4 defaultMVP(); std::optional rayToSphere( glm::vec3 rayPos, @@ -39,8 +38,7 @@ std::vector renderGraySphere( float sphereRadius, int threadCount = 1, glm::vec3 spherePos = Internal::defaultSpherePos(), - glm::vec3 lightPos = Internal::defaultLightPos(), - glm::mat4x4 mvp = Internal::defaultMVP() + glm::vec3 lightPos = Internal::defaultLightPos() ); void writeGrayscaleToFile(