Fix view ratio, fix lighting for gray sphere

This commit is contained in:
Stephen Seo 2021-08-23 14:46:34 +09:00
parent e813d33357
commit 653f0cafcb
2 changed files with 15 additions and 8 deletions

View file

@ -106,17 +106,20 @@ std::vector<unsigned char> Ex02::RT::renderGraySphere(
std::vector<unsigned char> grayscalePixels; std::vector<unsigned char> grayscalePixels;
grayscalePixels.resize(outputWidth * outputHeight); grayscalePixels.resize(outputWidth * outputHeight);
glm::vec3 rayPos{0.0f, 0.0f, 0.0f}; glm::vec3 rayPos{0.0f, 0.0f, 0.0f};
float lightFalloffStart = 4.5f;
float lightFalloffEnd = 7.0f;
if(threadCount == 1) { if(threadCount == 1) {
for(unsigned int j = 0; j < outputHeight; ++j) { for(unsigned int j = 0; j < outputHeight; ++j) {
float offsetY = ((float)j + 0.5f - ((float)outputHeight / 2.0f)); float offsetY = ((float)j + 0.5f - ((float)outputHeight / 2.0f));
for(unsigned int i = 0; i < outputWidth; ++i) { for(unsigned int i = 0; i < outputWidth; ++i) {
float offsetX = ((float)i + 0.5f - ((float)outputWidth / 2.0f)); float offsetX = ((float)i + 0.5f - ((float)outputWidth / 2.0f));
glm::vec3 rayDir = glm::vec3{ 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( auto rayResult = Internal::rayToSphere(
rayPos, rayDir, spherePos, sphereRadius); rayPos, rayDir, spherePos, sphereRadius);
if(rayResult) { if(rayResult) {
glm::vec3 toLight = lightPos - *rayResult; glm::vec3 toLight = lightPos - *rayResult;
glm::vec3 toLightCached = toLight;
toLight /= std::sqrt( toLight /= std::sqrt(
toLight.x * toLight.x toLight.x * toLight.x
+ toLight.y * toLight.y + toLight.y * toLight.y
@ -128,13 +131,17 @@ std::vector<unsigned char> Ex02::RT::renderGraySphere(
continue; continue;
} }
glm::vec3 resultToOrigin = rayPos - *rayResult; float dist = std::sqrt(
float angle = Internal::angleBetweenRays( toLightCached.x * toLightCached.x
toLight, + toLightCached.y * toLightCached.y
resultToOrigin); + toLightCached.z * toLightCached.z);
if(angle <= PI && angle >= 0.0f) { if(dist < lightFalloffStart) {
grayscalePixels.at(i + j * outputWidth) = 255;
} else if(dist >= lightFalloffStart && dist <= lightFalloffEnd) {
grayscalePixels.at(i + j * outputWidth) = grayscalePixels.at(i + j * outputWidth) =
(angle / PI) * 255.0f; (1.0f - (dist - lightFalloffStart)
/ (lightFalloffEnd - lightFalloffStart))
* 255.0f;
} }
} }
} }

View file

@ -1,7 +1,7 @@
#ifndef EX02_RAY_TRACER_HPP #ifndef EX02_RAY_TRACER_HPP
#define 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_NEAR_PLANE 0.2f
#define EX02_RAY_TRACER_DEFAULT_FAR_PLANE 4.0f #define EX02_RAY_TRACER_DEFAULT_FAR_PLANE 4.0f
#define EX02_RAY_TRACER_COLL_INCREMENT 2.0f #define EX02_RAY_TRACER_COLL_INCREMENT 2.0f