[csu540-f05-rpf] Some comments on a student's ray tracing code

Robert Futrelle futrelle at ccs.neu.edu
Tue Oct 4 16:27:00 EDT 2005


I was sent some code, and a revised version after some
suggestions by me.

Here are my comments for that student, an more.
Some of them may be useful to everyone.

This is a student who was trying to get TinyRayTrace
modified for spheres, focusing on traceOne, altered
to trace one ray (not one planet).

My suggestions:

Write some pseudocode in English to avoid getting ensnarled
in code that can be obscure and hard to write and figure out.

Choose variable and method names carefully, commenting
them if needed.

If doing more than one planet, do some bookkeeping,
and use iteration over the planets (or just test one
planet for starters).  Don't use an implicit loop
via a counter - crude at best.

Other bookkeeping can use a boolean to keep track of whether
you've hit any planet at all, initially false. Only change
a color if you've later set it true.  I already went over that.

Basically, when looping over planets for each single i,j,
you update as you find the next planet hit value (t value) if any,
and compare it to current t value, if any.  (When the boolean's
false, no comparison needed for the hit, only if true and
there was a previous hit.)

Write an inner product method for two vectors.
Don't embed the element-by-element computation in
the middle of your method.  The inner product should
be a static method as in my geometry code which is
on our website - maybe you didn't notice the link, e.g.,
for vectors, including the inner product,

http://www.ccs.neu.edu/home/futrelle/teaching/csu540f2005/examps/examps/geom/Vec.java

Write a simple test for TinyRayTrace.  It can just be a main() in that
class.  No need to worry about the GUI when testing this.
You can even check a trivial hit you've computed by hand using a JUnit test
versus what your code returns.  You could do various cases, letting
JUnit test for no hit (boolean false), hit nearest (check color), etc.
I don't use JUnit much yet, but I'm learning.  To guarantee no hit,
just put the sphere way off to the side so no hit is possible.

Start off testing the most trivial things, e.g., huge sphere that
you should be guaranteed to hit, two right behind one another, "loop"
over only one single i,j pair, e.g., i=0, i<1, or i=10, i<11,
so you only have a single value that you can then print out.

Bottom line:  You are in charge.  Don't try to write code that does
the entire assignment, any more than you'd try to drive to Alaska starting
the day you got your driver's license.  Walk before you run, crawl before
you walk (especially if you might be near a programming cliff you don't
want to fall off of  ;-)  Fiddle with all sorts of simple things that
build up your understanding, your confidence, and only finally, your
assignment code.

Feel free leave your tests in so I can see what you did.  For the for()
loop I mentioned earlier, don't hardcode the limits, use variables that
for the limits that you can set to various values for various tests.
In your tests of TinyRayTrace you can create planets of various sizes
and positions to trace.

I hope you get the idea now.

  - Prof. Futrelle





More information about the csu540-f05-rpf mailing list