# 2-4 Tracing Rays

Next comes figuring out what color the light coming through (viewport_x, viewport_y, viewport_z) is, as seen through the camera's point of view(object_x, object_y, object_z).

In the real world our light usually comes from a source like the sun or a lightbulb, and it bounces off of other objects until it reaches our eyes. The concept of simulating every photon coming from a light source is called photon mapping and photon tracing. The amount of computing power we would need to simulate every photon coming from our lightsource would be far too great and would take too long to render.

So, instead we are going to think of the rays of light in reverse. It will start with a ray originating from the camera and will go through a point in the viewport and we'll trace the path of the ray until it hits an object. The object is what the camera sees throught the viewport. Figure 2-5: Tiny square in the viewport, representing a single pixel in the canvas, painted with the color of the object the camera sees through it

### The Ray Equation

The easiest way to represent a ray is through a parametric equation. The ray passes through origin and we know its direction (from origin to viewport) so we can express any point in the ray as

point = origin + t(viewport - origin)

Where t is any real number. We can simplify (viewport - origin) by calling it direction so now the equation is

point = origin + t(direction)

A way to better understand this equation is to start at the origin and advance along the direction of the ray by some amount (t). Figure 2-6: Some points of the ray origin + t(direction) for different values of t

### The Sphere Equation

We need to have some sort of object in our scene for our rays to hit. It does not matter what shape you use but for what I need I will use speheres. A sphere is a set of points that lie at a fixed distance from a fixed point. That distance is called the radius and the point is called the center. Figure 2-7: A sphere defined by its center and its radius

so according to the figure above C is the center and r is the radius of a sphere, the points on the surface of that sphere must satisfy this equation