## Epic (!) Ray Tracing Journey – Part 2

The ray tracer we developed under normal conditions should be given in the image below. (You can see part 1 here) In other words, it has to read from the XML file, create the objects, illuminate them and calculate the shadows. And we should have been integrating the recursive ray tracing and acceleration structure right now. (I will explain why we need these two for later. If you ask why, I have to carry out this study simultaneously with the homework of the course I took (CENG 795 – METU)) Of course, as I said, under normal conditions.

At the end of nearly two weeks of long studies, the output I got is as follows.

Can you spot the difference between the two photos? If you haven’t seen it, let me help: Of course(!) our image so much better but there is some tiny problem. There is no shadow in the photo I created 🙂 You wouldn’t believe it, but even the shadow has to be created with code. Maybe some of you don’t like shade, but we still have to create this. I’ll come to the shadow, but first let’s talk about the changes made after Part-1. The only thing that worked almost right at the end of Part -1 was to create the spheres, since we could not fully develop the codes for the intersect of the rays we sent with the triangles and planes, we only saw the Diffuse colors of the spheres and we only saw color in the background. Of course, progress is progress but when you type Ray Tracing on your favorite search engine you will not satisfy with our current image probably. Because look at this. (On the right)

## We Need More

First I needed to solve the plane, triangle – ray intersection problem. I examined the codes I wrote and looked for errors, but it came to a point where it was difficult to get out of the business by adding without editing the codes. After that point, I tackled the subject from scratch and started to write intersection codes again. And in the meantime, I realized that I had made a mistake in the values of the colors and corrected them. Be sure that when you examine different sources, they process the colors as you do. It’s not that I did it like this, but a general warning 🙂 There is different algorithm to detect intersection between ray and plane-triangles. In my case, I use this equation to detect hit with the plane but after that, I realized something: Plane also consists of triangles, and if I give the triangle points of the planes to the triangle algorithm, I have to get the correct result. I followed the formula below until I realized this, but I managed to extract some of the planes correctly, the reason why not all of them was that the for loop ended where the plane found the first Hit without looking at all the points, and the triangle algorithm came to my rescue when I had to make many radical changes to correct this.

In computer graphics, everything but everything consists of triangles. Yes, spheres also consist of triangles. Since these triangles are very small, we just don’t see these triangles. If you put together very small triangles in a certain order, you will get the shapes you want. (It is not as easy as it is written here, of course) And you can perform all the operations that will take place in a 3-dimensional environment by playing with these triangles. In our example, if you collide triangles with rays and color them depending on the light of the environment and their own colors, you will create shapes. There are many different methods to understand when rays and triangles intersect. However, we will use Barycentric Coordinates. You can understand the basic structure in simple figures below, but do some research for more information. It is not that difficult to understand the subject, but unfortunately, it is not as easy as it is thought to write the code. I wrote the triangle code many times, maybe that’s my problem :(. After I managed to intersect the triangles, the images I got were roughly like this.

## We Need Light

##### With this formula, we can show diffuse + ambient + specular shading.

After trying again and again to add the lighting I mentioned, I started to get close to the correct results. Of course, it’s not quite right but it works. After adding the lighting, I started getting results like the one below. (On the right, which should be on the left, are the results I have achieved now) I also added the images I got along the way to the bottom.