Realtime raytracing

Started by
28 comments, last by tbp 18 years, 1 month ago
I'm following a course called "Computer Graphics". We have to write a raytracer, worth 7 of the 20 points of the exam. Actually we have a choice between two things we can make: a rasterizer (where speed is important) or a raytracer (where speed isn't important but the effects are). So the raytracer doesn't have to be realtime, but I want to amaze the professor by making it realtime. Currently, I can raytrace 4 flat shaded, untextured spheres, at 20fps at 400x300 pixels. Now I still have to add Phong shading and "transformations of objects", and later we will get a second part of the assignment, which will probably add things like reflections and translucency. I'm wondering, if I'd still be able to get it somewhat realtime after all those effects are added. I mean, for example I have to add "transformations of objects", that means I have for every pixel, for every object, to add a multiplication with a 4x4 matrix... Which is maybe more work than everything that's done per pixel right now! Is there a way to do this fast too? Oh yeah, I used to compile my code with "g++ *.cpp -lSDL", and it was 30fps with 1 sphere. Then I compiled it with "g++ *.cpp -lSDL -O3" instead, and it was 65fps with 1 sphere!! Are there other such optimizations that g++ can do, that could make it even faster?
Advertisement
I've saw some time ago a real-time raytracer that raytraced 5 reflecting spheres, so I think there's some room left.

What you should really try is to use the vector units of your processor - i.e. SSE/SSE2. There's a compiler switch for GCC for auto-vectorization but you'll probably want to hand-optimize it anyway. Take a look at http://www.openrt.de/, they have some publications on how they optimized their renderer, might give you some helpful hints.
And, if you are allowed, then use the GPU Shaders for extra math computational power. With a good video card, this more than doubles the available processing power.
Check out the forums on this site.
They have produced some VERY fast raytracers and there's some code floating around there to help you.
Also look up Havran's work, he has done a lot of research on fast raytracing (almost getting realtime global illumination).
RealStorm!!
Implement your intersection code using SIMD and trace four rays at once. That should give you a quick boost.
a few:

-ffast-math
-march=i686

and sometimes

-fexpensive-optimizations

there are others, but I will use the top 2 routinely.
Maybe you want to take a look at this library:
http://www.libsh.org/

Basically it allows to use your GPU as a general vector processor, i.e. add pairwise two arrays of numbers and such. You can use it to calculate intersections of rays and spheres in parallel, for instance.
one obvious thing is that you dont have to transform every ray...just transform the objects at the beginning of each frame (still keep the matrices and the original coords, but do the transformation only per object per frame). this is especially true with spheres, which are invariant under rotation (unless you start adding surface shaders).
Heaven Seven is an old (2000) example of real time ray tracing (in 64k no less).

You'll want some form of space partitioning, to help speed up intersection tests (probably wont help with 4 objects but it becomes very useful very quickly). There where some tricks with using interpolation instead of complete samples, but I not remembering them.

This topic is closed to new replies.

Advertisement