diff --git a/example02_threaded_raytracing/src/rayTracer.cpp b/example02_threaded_raytracing/src/rayTracer.cpp index 00eb9e1..e5de0e5 100644 --- a/example02_threaded_raytracing/src/rayTracer.cpp +++ b/example02_threaded_raytracing/src/rayTracer.cpp @@ -106,17 +106,20 @@ std::vector Ex02::RT::renderGraySphere( std::vector grayscalePixels; grayscalePixels.resize(outputWidth * outputHeight); glm::vec3 rayPos{0.0f, 0.0f, 0.0f}; + float lightFalloffStart = 4.5f; + float lightFalloffEnd = 7.0f; if(threadCount == 1) { for(unsigned int j = 0; j < outputHeight; ++j) { float offsetY = ((float)j + 0.5f - ((float)outputHeight / 2.0f)); for(unsigned int i = 0; i < outputWidth; ++i) { float offsetX = ((float)i + 0.5f - ((float)outputWidth / 2.0f)); glm::vec3 rayDir = glm::vec3{ - offsetX, offsetY, -EX02_RAY_TRACER_DRAW_PLANE}; + offsetX, offsetY, -(float)outputHeight * EX02_RAY_TRACER_VIEW_RATIO}; auto rayResult = Internal::rayToSphere( rayPos, rayDir, spherePos, sphereRadius); if(rayResult) { glm::vec3 toLight = lightPos - *rayResult; + glm::vec3 toLightCached = toLight; toLight /= std::sqrt( toLight.x * toLight.x + toLight.y * toLight.y @@ -128,13 +131,17 @@ std::vector Ex02::RT::renderGraySphere( continue; } - glm::vec3 resultToOrigin = rayPos - *rayResult; - float angle = Internal::angleBetweenRays( - toLight, - resultToOrigin); - if(angle <= PI && angle >= 0.0f) { + float dist = std::sqrt( + toLightCached.x * toLightCached.x + + toLightCached.y * toLightCached.y + + toLightCached.z * toLightCached.z); + if(dist < lightFalloffStart) { + grayscalePixels.at(i + j * outputWidth) = 255; + } else if(dist >= lightFalloffStart && dist <= lightFalloffEnd) { grayscalePixels.at(i + j * outputWidth) = - (angle / PI) * 255.0f; + (1.0f - (dist - lightFalloffStart) + / (lightFalloffEnd - lightFalloffStart)) + * 255.0f; } } } diff --git a/example02_threaded_raytracing/src/rayTracer.hpp b/example02_threaded_raytracing/src/rayTracer.hpp index d0dd96c..f42779a 100644 --- a/example02_threaded_raytracing/src/rayTracer.hpp +++ b/example02_threaded_raytracing/src/rayTracer.hpp @@ -1,7 +1,7 @@ #ifndef EX02_RAY_TRACER_HPP #define EX02_RAY_TRACER_HPP -#define EX02_RAY_TRACER_DRAW_PLANE 500.0f +#define EX02_RAY_TRACER_VIEW_RATIO 0.5f #define EX02_RAY_TRACER_DEFAULT_NEAR_PLANE 0.2f #define EX02_RAY_TRACER_DEFAULT_FAR_PLANE 4.0f #define EX02_RAY_TRACER_COLL_INCREMENT 2.0f