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.
Lets Talk About Triangle
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
After identifying and showing the triangles correctly, it is necessary to add some color to the work. And the best way to achieve this in computer graphics is to use light, especially if you give it a little bit of bloom. So we need light, and of course, we need to code it. But how is this possible? Different reactions of light take place on different surfaces, some reflect and some do not. How can this be done? If you’ve heard of a concept called shading, that’s where it comes in. It is not possible to add depth perception to an image without shading. The Shading we talked about in this section is not the same as the shading thing meant in 3D engines, if you are using 3d engines, keep this in mind, here we do not play with the positions of the vertexes. I would like to tell you about the light events in ray tracing in detail here, but I have not fully understood it yet. So I’ll put the general formula used below. However, in general, the lights in the environment have parameters. (Position, radiance, etc…) At the same time, the materials of the objects in the environment have properties and we calculate and show the color of the intersection at that point by looking at the interactions of these properties with light. For more information you can look at lightning equation.
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.
Before lighting I was getting the colors right in this scene
Almost 6 min
About 10 minutes
Even though I try to add reflection and refraction, these are the results I get for now. I also think that the spheres appear this way is related to the early exit of the for loop. I’m working on it.
We have been able to do many things well and badly so far, but there is a lot to be done. First of all, even if we manage to intersect the rays with the triangles, the colors are not correct, our lights do not work on these objects. (Although the colors were correct before) We need to figure this out. And we cannot produce the shadows in the environment right now. We use a technique called shadow ray for shadows, I actually wrote the shadow codes but there is nothing I can show as I could not get the correct results. After that, we will try to do reflection and refraction. I have coded for reflection and refraction in the same way as the shadow, but I am far from being close to the desired result, but I will add the images below as I have tried, but I cannot add the render of the dragons now because it takes too long. (It probably goes into an endless loop and doesn’t finish for a very long time, I haven’t gotten any results yet, unfortunately) We will also use the acceleration structure to speed things up because sometimes our renderings can take a long time.
Although we have solved some things, we have a long way to go. First, we need to fix the shadow and speed up the process. (We do not mean multi-threads, we can say various small graphic tricks) But look where did we come from. I hope we can continue this way step by step and see the end. Stay alive until I see you in the next post.