fix: Vertical flip

Fix the difference in (0,0) assumption of jpg and computer graphics.
This commit is contained in:
Tibo De Peuter 2024-10-05 17:32:28 +02:00
parent 4cf9851fbc
commit 08c5c9e8da
Signed by: tdpeuter
GPG key ID: 38297DE43F75FFE2
2 changed files with 7 additions and 5 deletions

View file

@ -72,7 +72,7 @@ int main(int argc, char * argv[])
printf("\r"); printf("\r");
// For each pixel (i,j) // For each pixel (i,j)
for(int i=0; i<height; ++i) for(int i=0; i<height; ++i)
{ {
for(int j=0; j<width; ++j) for(int j=0; j<width; ++j)
{ {
@ -87,7 +87,9 @@ int main(int argc, char * argv[])
// Compute viewing ray // Compute viewing ray
Ray ray; Ray ray;
viewing_ray(camera,i,j,width,height,ray); /* NOTE I flipped the vertical axis of the pixels, because in computer graphics we assume the bottom left pixel
* is (0,0), while traditional jpg assumes (0,0) to be the upper left pixel. */
viewing_ray(camera,height - i,j,width,height,ray);
// Find first visible object hit by ray and its surface normal n // Find first visible object hit by ray and its surface normal n
double t; double t;

View file

@ -15,9 +15,9 @@ void viewing_ray(
*/ */
/* Determine the direction of the ray, using the camera and pixel location. */ /* Determine the direction of the ray, using the camera and pixel location. */
double u = 0 - camera.width / 2 + camera.width * (j + 0.5)/ width; double u = 0 - camera.width / 2 + camera.width * (j + 0.5) / width;
double v = 0 - camera.height /2 + camera.height * (i + 0.5)/ height; double v = 0 - camera.height / 2 + camera.height * (i + 0.5) / height;
ray.origin = camera.e; ray.origin = camera.e;
ray.direction = (0 - camera.d)* camera.w + u * camera.u + v * camera.v; ray.direction = (0 - camera.d) * camera.w + u * camera.u + v * camera.v;
} }