Author | Nnoduka Eruchalu |
Date | 03/15/2014 |
- CUDA 4.0
- C
- Would have preferred to use C++ and recursion but CUDA 4.0 doesn't officially support those.
- Had to make do with structs (for objects) and implement binary tree based stack (for recursion)
Ray tracing is 3d graphics by tracing a path of light through pixels in a scene and simulating the effects of its encounters with virtual objects. It is based on the idead that you can model reflection and refraction by recursively following the path that these light rays take as they bounce through the 3d scene.
The problem with ray tracing is that it has a great computational cost so it is typically used in situations where the images can be rednered slowly ahead of time.
However with the power of a CUDA-enabled NVIDIA GPU I've been able to create a ray tracer that actually renders 3d graphic scenes in realtime. This makes it possible to translate, rotate and zoom in/out of the scene.
Read the following for more background information on Ray Tracers:
- ACM Siggraph Ray Tracing
- Paul Rademacher's "Ray Tracing: Graphics for the Masses"
- FuzzyPhoton "What is Ray Tracing?"
Module | Description |
---|---|
Makefile |
Creates executable ray_tracer |
ray_tracer.cu |
Main Loop: Initialize Pixel Buffer, Scene, mouse event handlers and start rendering |
tracer_kernel.cu |
Kernel File: Implement Ray Tracing logic and binary tree stack (used for recursion) |
scene.in |
Sample input scene description file |
screenshots/ |
This folder contains the screenshot images |
This program includes a Makefile
for use in compiling.
To simplify compilation, the kernel file is simply included into the main file.
As a result, make
might not detect changes in the kernel, so you should use
make -B
(rebuild all).
First of all you need to be on a machine with a CUDA-capable NVIDIA GPU To run, go to the folder where executables go and call the ray_tracer program with an input scene description file. More on creating input scene description files later.
./ray_tracer /path/to/scene.in
Note that on my CUDA machine setup, the executables folder is in C/bin/linux/release
The User Interface is the mouse:
Button | Action |
---|---|
right |
zoom |
middle |
translate |
left |
rotate |
Click the esc
key to close the window.
A scene description input file is how the Ray Tracer knows what objects to draw and properties.
I came up with the syntax for this file and it's not very flexible so read the syntax rules that follow:
-
1023 characters maximum per line.
-
Comments must be on separate lines that start with
#
. Yes I did this because I'm a python fan. -
A blank line should truly be blank! i.e. a line with anything other than a carriage return will be considered not-empty.
- This means a line with just one
space character
will fail.
- This means a line with just one
-
Mark the end of the file with a
*
character on a new line. -
Each non-comment line of the file is a unique key-value pair representing an object's properties.
- the key is always a single word
- space(s) follows the key
- the first non-space character following the key marks the beginning of the value.
- The value could be single-value or multi-value
-
Below are example formats of object property key-value pairs:
[property key] [propert value]
c x y z
r x
- The first non-commented line in this file must have the key
type
- See the sample scene description input file
scene.in
for all supported objects and sample property key-value representations
All lights have the following properties:
Key | Description |
---|---|
amb |
ambient color |
diff |
diffuse color |
spec |
specular color |
pos |
position in 3d-coordinates |
Supported object types are:
- Sphere
- Box
- Plane
- Cylinder
- Cone
These objects all have the following properties:
Key | Description |
---|---|
amb |
ambient color |
diff |
diffuse color |
spec |
specular color |
shiny |
shininess |
n |
refraction index |
kr |
reflective coefficient |
kt |
transmittive coefficient |
Key | Description |
---|---|
c |
center |
r |
radius |
Key | Description |
---|---|
min |
minimum vertex |
max |
maximum vertex |
Plane equation is Ax + By + Cx + D = 0
and this is represented by the by the key-value pair:
pl A B C D
Key | Description |
---|---|
pl |
plane equation representation. Value is of format A B C D given equation Ax + By + Cx + D = 0 |
Key | Description |
---|---|
ymin |
y-coordinate of cylinder's bottom |
ymax |
y-coordinate of cylinder's top |
r |
radius of cylinder |
Key | Description |
---|---|
ymin |
y-coordinate of cone's bottom |
ymax |
y-coordinate of cone's top |
r |
base radius of cone |