0

I've written my own 3D Game Engine (it took me a year) and I wanted to create a Raytracer that runs on my CPU (not the GPU!!)

For now, the ray tracing process is simplified like this:

  1. Cast a ray for each output pixel.
  2. If the current ray hits an object, set the output pixel color to "white"
  3. Otherwise set it to black

To increase the speed of the ray tracer, I added a spherical bounding box to each Entity. If the current ray intersects the bounding box, it will run the intersection tests with each triangle of the Entity.

I am using the quickest methods on ray-triangle-intersection and ray-point-distance but still each ray has to test every single triangle of each Entity that might be intersected.

As a result, it takes me over 5 minutes to render an object (1920x1080) with around 10000 polygons and I think that's not what I want.

Is there any way of reducing the amount of triangles that I need to check?

Greetings, Finn

Daniel A. Thompson
  • 1,904
  • 1
  • 17
  • 26
Luecx
  • 160
  • 1
  • 12
  • Your question is a broad one, but I can give you some pointers. There have been lots of thesis work, articles, ... about ray tracing (computer vision). You can do some research on this topic. There are multiple approaches each with their pro's and cons. – KarelG Aug 18 '17 at 07:54
  • So you are basically saying that there is a way of reducing the amount of polygons and not the amount of entries. I will give it a try :) thank you – Luecx Aug 18 '17 at 08:09
  • You are now stepping into the are of acceleration data structures mainly Bounding Volume Hierarchy and K-d trees. You can find lots of research papers on this topic particularly related to the traversal of k-d trees on sciencedirect and ieee. – gallickgunner Aug 19 '17 at 07:59
  • You are now stepping into the are of acceleration data structures mainly Bounding Volume Hierarchy and K-d trees. You can find lots of research papers on this topic particularly related to the traversal of k-d trees on sciencedirect and ieee. – gallickgunner Aug 19 '17 at 07:59

1 Answers1

2

Is there any way of reducing the amount of triangles that I need to check?

Yes.

It sounds like your scene consists of a list of triangles, and you're iterating linearly through the list and checking every triangle to find the closest one. This is linear search and has O(n) runtime, n = number of triangles.

You can reduce this to average O(log(n)) time by using volumetric kd-trees or bounding volume hierarchies to store your triangles. Personally, I prefer kd-trees, but either approach works. Note that BVH typically performs better in animated scenes.

Note that acceleration structures can contain subtle bugs in how they are constructed or traversed, so you'll probably want to develop some way to render the same scene using the naive list approach (for a reference image) and a structured approach. In my hobby tracer, I organize it like this:

AbstractScene - base class for all scene types. Most code interacts only with AbstractScene fields and methods.

KDScene - derived class that implements the scene as a kd-tree.

BVHScene - derived class that implements the scene as a BVH.

NaiveScene - derived class that implements the scene as a list of triangles.

There are also other acceleration structures like grids (aka voxels).

Daniel A. Thompson
  • 1,904
  • 1
  • 17
  • 26
  • I've seen KD-Trees but what happens with the triangles that are right on the split between two newly created boxes? – Luecx Aug 22 '17 at 21:42
  • You have to decide how to handle the edge cases :) There are pros and cons, but the most important thing is to be consistent. – Daniel A. Thompson Aug 22 '17 at 21:48
  • I implemented the KD-Tree yesterday and I can reduce my triangle count from 40000-1000. Which is really good but not great. It's because of my edge cases. Now I decided to keep my edge triangles in the node and not to pass them down. This results in my first node having 500 triangles but I am fine with that :) Thanks you very much :) – Luecx Aug 22 '17 at 21:51