Skip to content

MTAISBOSS/ray-tracing

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ray-tracing

Building a simple ray tracing project I followed the book Raytracing in a Weekend by Peter Shirley and here is what I've done to complete the project!!

Result

Starting with a simple image

The code generates a ppm file something like this P3 512 288 255 0 120 214 0 120 214 0 120 214 0 120 214

P3 says that colors are in ASCII format the line 512 288 is the dimension of the 2d image 512px * 288px with aspect of 16:9 the 255 in the 3rd line indicates the limit of the colors and after these initializers, we have the actual colors for each pixel

StepOne

Creating Ray

After creating ray struct (I preferred struct for most of my codes, it's memory friendly) here is the final image, a simple gradient from white to sky blue

StepTwo

Sphere

I implemented a simple sphere, or a circle for now, and it's similar to Japan's flag StepThree

Normal

Now here was the fun part, I implemented the normal vector and normal map and it gave me a beautiful mixture of blue, purple and green, like in uv coordinates

StepFour

Hitable

After implementing normals, it was time to get into the actual work, by defining the hitable and hitable_list structs I could inherit from it for the objects that are hitable, like sphere! I also created another big sphere as ground StepFive

Diffuse

So after implementing all those stuff, it was time for creating diffuse color and light, but not a complex light, a simple one, for this I needed a randomly generated number between 0 and 1, unfortunately I couldn't find a built in cpp or c method for that, so I created mine, that clamps the value between 0 and 1 for demonstration I have two outputs, one with random generated number and one with no random generated number

Without Random Number

StepSix

With Random Number

StepSix

Metal

I love this chapter, here I implemented a metallic material with fuzziness, the basic idea is that when rays coming from all directions hit a certain point on a surface, the randomness in reflecting them indicates the metallic attribute of an object!!

Fuzziness

StepSeven

No Fuzziness

StepSeven

Stupid mistake or genius one?

So here I did something wrong in my code, so some of the rays that have a certain attribute should be reflected from the surface and some of them should refract, well, here I refract the rays by mistake, and I got emission material!!! like a glowing sun or lamp

StepEight

Dielectric

Okay, something like water or glass or even air do not reflect the light or refract like other materials, these objects have a formula n * sin(theta) = n' * sin(theta') which says that when a ray of light enters a dielectric space like water, it changes its direction, and there are some complicated stuff here but I achieved this after several attempts!

StepEight StepEight

and here is the final result for this chapter!

Result

StepEight

Amazing right?

Camera

So the next chapter talked about camera, because before coming to this chapter the camera was handled in the main.cpp file, so here I separated the camera and put it in another struct

StepNine StepNine StepNine

There is something strange right? why the images have a shear effect applied to them? A LITTLE STUPID MISTAKE WHICH TOOK ONE WEEK TO FIGURE OUT!

Depth of field (or Defocus Blur)

So I ignored the shear effect that applied to my output and said to myself: Yeah, this is fine!! So after this I implemented depth of field, in summary it blurs the parts of image we don't focus on (worst explanation of depth of field!)

This is the first attempt

StepTen

A beautiful green image!!! . . . .

after adjusting the aperture value and camera position and rotation, I got this StepTen

The End Chapter, The Cover of the book!

Okay here we should spawn many spheres with random materials, see my beautiful image

StepEleven

yeah, the stupid mistake got me here, remember when I told you I ignored the stupid little mistake?

So after days of refactoring code and searching in my codes, I finally got another result, I call it a Nightmare!!

Nightmare

StepEleven

I was losing my sanity, after I realized that the Cross product in my vector class is causing this bug, so basically here is the formula for vector 3 cross product

C.x = A.yB.z - A.zB.y C.y = A.zB.x - A.xB.z C.z = A.xB.y - A.yB.x

so in first line, instead of what is written, I wrote: C.x = A.y*B.z - A.z*B.x You didn't notice it either?? the last element should be B.y, not B.x!!! So I fixed this and no more shearing!!

here is the final images and results with 3 spheres

StepEleven StepEleven

About

Building a simple ray tracing project

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors