aashiq shaikh

aashiq's explorations in ray tracing

This is where I will be validating my understanding in ray tracing from the ground up, by walking through my thought process while building a CPU ray tracer with C++.

A ray tracer needs rays. A ray is just an infinite set of points that lie along a line. We can define a ray as

p = o + dt


We need something for the rays to collide with. To start, let's add a sphere. A sphere can be defined as

(x - a)2 + (y - b)2 + (z - c)2 = r2

in 3D space. Thinking in terms of the 3D structures we're working with, it can be rewritten as

(p - cs)2 = r2

So to compute the intersection of a ray and a sphere, we just need to find the points that satisfy both equations. I've written a mathematical derivation to explain this:

If you don't feel like following the derivation, I basically substitute the ray equation into the sphere, and expand and rearrange it to form at2 + bt + c = 0, which means we can apply the quadratic formula to it.

Using the discriminant b2 + 4ac we can find how many solutions there are to this problem:

If you'd like a more visual explanation, here's a diagram depicting the geometry between a sphere and an intersecting ray (with slightly different variable names - sorry!):

In this case, we're using the circle equation instead of the sphere equation. The circle is defined as the circular bisection of the sphere that touches t1, t2, and the center of the circle.

To be continued...