Dev Notes: Debugging Techniques for Ray Tracer Project
There are different ways to debug your ray tracer according to the problems. Here we list several possible problems you may have in development.
To accelerate your debugging procedure, the command line UI is recommended,
which has all features required for debugging, although some of them may be
difficult to enable (like those must be enabled through the -j
option).
Segfault
This is a (very) common problem for any C/C++ program. The easiest way to locate the problem is running your program with gdb. The following shows the command you can use.
$ gdb bin/ray
(gdb) run input.ray tmp.png
# ... RUNNING
# ... Segfault Captured
(gdb) bt
# ... Backtrace is shown
Now you can locate where the problem is.
Note: cmake -DCMAKE_BUILD_TYPE=Debug
is required to enable the debugging
symbols, otherwise bt
only shows the memory address rather than the line
number of your source file.
Suspicious Shading Result
It would be very expensive to render the whole scene in order to fix one pixel. The recommended procedure is
- Locate the pixel
- You can do it by clicking the suspicious pixel in GUI. The terminal should print the pixel number after clicking.
- Consequently running the GUI by double clicking should be avoided
- Hack
CommandLineUI.cpp
- Comment out
raytracer->traceImage(width, height);
and several following lines#if 0 .. #endif
can be used to “comment” out multiple lines, which is superior that/* */
because#if #endif
can be nested.
- Add
raytracer->tracePixel(..., ...);
there - Now only one pixel is traced
- Add
print
/std::cout
everywhere you are not sure it is correct.