Ray tracing

george7378

DON'T PANIC
Addon Developer
Donator
Joined
Jun 26, 2009
Messages
1,045
Reaction score
0
Points
36
Wow, I haven't been here for ages! I don't want to look at how long ago it was that I last posted...

Anyway, I thought I'd share a neat little thing I made in C++ - a ray tracer. A ray tracer is a program which creates a picture purely using mathematical algorithms for reflection and refraction. You specify the location and properties of your objects, feed them in, and for each pixel in the picture, the program shoots a ray out into the scene, determines what it hits, and colours the pixel according to that. It takes a while to render a frame, but the results are fantastic. I've attached a pic from my (basic) ray tracer.

It shows a pyramid of 14 spheres, all are metal apart from the top one which is glass with a (low) refractive index of 1.04, and the yellow one behind the top sphere, which is made of plastic. It took 75 seconds to render on my computer.

Because of their mathematical nature, ray tracers can only handle primitive objects like spheres and planes (the only two objects mine can render). However, it's possible to build complex meshes using individual triangles.

Anyway, nice to be back, hope I stick around!!
 

Attachments

  • pyramid.png
    pyramid.png
    208.5 KB · Views: 70

george7378

DON'T PANIC
Addon Developer
Donator
Joined
Jun 26, 2009
Messages
1,045
Reaction score
0
Points
36
It's a lot simpler than POV-ray. There are some critical things that I haven't managed yet; mainly proper shadowing. The glass sphere should create 'caustics' on the floor (areas of focussed light) but it just casts a hard shadow like everything else. However, because of how simple and fundamental ray tracing is, once you've got the bare bones tracer up and running, everything else is just an addition which can be bolted on later.
 

Artlav

Aperiodic traveller
Addon Developer
Beta Tester
Joined
Jan 7, 2008
Messages
5,790
Reaction score
780
Points
203
Location
Earth
Website
orbides.org
Preferred Pronouns
she/her
Nice job!
It's really interesting to get to the bottom layer, and implement such things yourself, or try non-mainstream rendering techniques.

How do you define the scene transformation?
From the 75 seconds per frame it sounds to me that you recompute part or all of the transformation on each pixel.
If so, try googling matrix notation.

There is a lot you can do with ray tracing.
For example, general relativity and wormholes in real time:
http://www.orbiter-forum.com/showthread.php?p=165820&postcount=32
(Honestly, it's only real-time when computing on GPU, and on CPU it's still only 1 FPS).
 

Quick_Nick

Passed the Turing Test
Donator
Joined
Oct 20, 2007
Messages
4,088
Reaction score
204
Points
103
Location
Tucson, AZ
The first program I ever made all on my own was a ray tracer in Java.
 

george7378

DON'T PANIC
Addon Developer
Donator
Joined
Jun 26, 2009
Messages
1,045
Reaction score
0
Points
36
The program parses a text file containing the objects, materials, lights, camera and render properties. For each pixel I shoot 4 rays and average them (anti-aliasing) so that makes it take 4 times longer than just rendering the scene by itself.

The scene transformation is pretty simple - I construct a position and a direction for each pixel in world space and move between pixels by moving along the camera's up and left vectors. So basically everything is done in world space, there aren't any matrix transformations or anything involved.
 

george7378

DON'T PANIC
Addon Developer
Donator
Joined
Jun 26, 2009
Messages
1,045
Reaction score
0
Points
36
Here's another one - exactly the same objects but on a bumpy, partially reflective floor. The beauty of this is that you can simply change a bit of code to give the floor a texture or make it bumpy like this:
 

Attachments

  • pyramid2.png
    pyramid2.png
    432.8 KB · Views: 25

Matias Saibene

Development hell
Joined
Jul 7, 2012
Messages
1,049
Reaction score
629
Points
128
Location
Monte Hermoso - Argentina
Website
de-todo-un-poco-computacion-e-ideas.blogspot.com.ar
What program did you use for modeling? Or you've written the code directly?
I remember using Google SketchUp and Wings3D to convert to POV-Ray format.

Here is one of the attempts I made:
Warning: the following images may be disastrously ugly or cause eye damage:lol:.
86ht99rba7rd1pp6g.jpg
 

Quick_Nick

Passed the Turing Test
Donator
Joined
Oct 20, 2007
Messages
4,088
Reaction score
204
Points
103
Location
Tucson, AZ
What program did you use for modeling? Or you've written the code directly?

In ray tracing, you basically hard code primitives as their mathematical equation (and then solve for intersection of a ray equation). Of course, you can model anything as multiple triangles, as he mentioned earlier. But it's really cool to model things mathematically perfectly rather than using an approximation based on triangles that most other rendering techniques limit you to.

The photorealism possible with ray tracing is incredible. (You just have to take the time to model every phenomenon of light you can think of)
 
Last edited:

Thorsten

Active member
Joined
Dec 7, 2013
Messages
785
Reaction score
56
Points
43
Because of their mathematical nature, ray tracers can only handle primitive objects like spheres and planes (the only two objects mine can render).

Of course, you can model anything as multiple triangles, as he mentioned earlier. But it's really cool to model things mathematically perfectly rather than using an approximation based on triangles that most other rendering techniques limit you to.

That's not true for ray-tracing or any other rendering technique.

You need a way to specify to your rendering code where in space a light scatterer is, regardless of how you render. The most straightforward way to do this is to use a mesh of vertices and triangles, but that's by no means the only way.

A cloud layer can for instance be rendered by adding multiple frequencies of 3-d Perlin noise and computing the ray attenuation as it passes through this distribution. That's a completely mathematical description of an object without well-defined sharp surfaces - a 3-dim distribution of haze.

Even real time rendering (where the time constraints are severe) can deal with mathematical descriptions like Bezier curves or splines to describe where surfaces are or build diffuse objects from Perlin noise.

So you have to distinguish the way you describe where objects are to the renderer from the rendering strategy.

* Real time rendering follows only the ray eye (E) - surface (S) - light (L) for any given pixel, so it can't really do reflections.

* Ray-tracking can take the time to investigate more complicated reflections such as E-S-S-S-L (light bouncing off two surfaces illuminating a spot before it is seen) - ray-tracing is good for sharp reflections because of that

E-S-S-L is actually *much* more expensive than E-S-L, since the second S could be anywhere - so after doing E-S, you have to shoot new rays to any point in the scene again for the second S before going to L, and if you sample with 100 rays, it becomes 100 times more expensive than real time rendering. Real time rendering has to fake reflections etc. by precomputing reflection, light and shadow maps.


* Radiosity is a way to compute light in diffuse lighting environments, where one solves for the steady-state of each surface in the scene receiving and reflecting light. It doesn't cope well with sharp reflections E- few S - L but it effectively handles E - infinite S - L solutions.

Whether ray tracing or radiosity is superior really depends on the scene - muted, diffuse light and rough surfaces come out better in radiosity - admittedly ray tracing usually is more spectacular.

Sometimes you wonder how nature does it all in real time...
 
Last edited:
Top