i
i
i
i
i
i
i
i
1.7. Designing and Coding Graphics Programs 9
• vector3. A 3D vector class analogous to vector2.
• hvector. A homogeneous vector with four components (see Chapter 7).
• rgb. An RGB color that stores three components. You should also include
operations for RGB addition, RGB subtraction, RGB multiplication, scalar
multiplication, and scalar division.
• transform.A4 × 4 matrix for transformations. You should include a
matrix multiply and member functions to apply to locations, directions, and
surface normal vectors. As shown in Chapter 6, these are all different.
• image. A 2D array of RGB pixels with an output operation.
You might also consider a
special class for unit-length
vectors, although I have
found them more pain than
they are worth. —P.S.
In addition, you might or might not want to add classes for intervals, orthonormal
bases, and coordinate frames.
1.7.2 Float vs. Double
I suggest using doubles for
geometric computation and
floats for color computation.
For data that occupies a lot
of memory, such as trian-
gle meshes, I suggest stor-
ing float data, but convert-
ing to double when data is
accessed through member
functions. —P.S.
Modern architecture suggests that keeping memory use down and maintaining
coherent memory access are the keys to efficiency. This suggests using single-
precision data. However, avoiding numerical problems suggests using double-
precision arithmetic. The tradeoffs depend on the program, but it is nice to have a
default in your class definitions.
1.7.3 Debugging Graphics Programs
I advocate doing all com-
putations with floats until
you find evidence that dou-
ble precision is needed in a
particular part of the code.
—S.M.
If you ask around, you may find that as programmers become more experienced,
they use traditional debuggers less and less. One reason for this is that using such
debuggers is more awkward for complex programs than for simple programs.
Another reason is that the most difficult errors are conceptual ones where the
wrong thing is being implemented, and it is easy to waste large amounts of time
stepping through variable values without detecting such cases. We have found
several debugging strategies to be particularly useful in graphics.
The Scientific Method
In graphics programs there is an alternative to traditional debugging that is often
very useful. The downside to it is that it is very similar to what computer pro-
grammers are taught not to do early in their careers, so you may feel “naughty”
if you do it: we create an image and observe what is wrong with it. Then, we