Computer Graphics Theory — Ray tracing
The ray tracing
This method generates an image by tracing the path of light through pixels in an image plane.
In real world, the light departs from light source and then arrived at our eye. However, we do this in a reverse way in ray tracing.
Why we use?
A scene rendered by ray tracing is much more realistic than scanline renderings.
It also can simulates a wide variety of optical effects.
- reflection, refraction, shadows, scattering, caustics, etc
Basic idea of ray tracing
Basically it is based on digital simulation of geometric optics.
Ray tracing use backward mapping.
A finite number of rays from viewer to objects, then from objects to light source.
We can take an advantage of ‘finite number of rays’ unlike forward mapping.
Ray tracing algorithm
Assuming that scene has been transformed to image space,
- An observer is assumed to be on the positive z-axis
- generate primary ray
Each ray passes from the pixel to scene. - Find closest object along the ray path.
== Find first intersection between ray and the object. - Compute light sample
Apply illumination model to determine direct contribution from light sources.
5. Recursively generat secondary rays at equal.
Ray generation
Generating ray follows a line equation between two points.
In this manner, we can generate a primary ray from eye.
But there is a problem.
Get coderfromnineteen’s stories in your inbox
Join Medium for free to get updates from this writer.
Therer exists a lot of primitive geometries in our scene generally, It is too expensive to calcualte all reflection on them.
Instead of calculating it agains primitives, we can calculate it against new mesh wrapping the sub-meshes(bounding-volume hierachy)
Compute Intersection points
1. on sphere
We have two equations. One is about ray and the other is about Sphere.
Then, we can get roots by solving to equation linearly.
After we get a intersection point P, we need to find a normal vector on that point.
It is same direction with a vector from sphere origin to Point. We just need to normalize it.
2. On triangle
- Check whether the ray is intersected with a plane that includes target triangle
A plane equation is represented as combination of any two points on the plane and its normal.
2. If condition one is satisfied, Check whether the intersection exists inside of triangle area.
First, calculate normal vector (N1) using two edges (V1, V2).
Then we do dot product between (p-p0) and the normal(N1).
If the result is negative, it means that the intersection is outside of triangle.
Optimization
About intersections, it is too exhaust to calculate all intersections in a naive way.
We can accelerate the process using bounding volume hierachy.
Similar to Triangle intersection, We check whether there is an intersection between ray and bounding volume first.
If it is intersected, we go inside the child nodes of bounding volume -> repeat this!
Let’s see an example.
In this picture, the ray is intersected with bounding volume 2.
Then, we need to go next depth of the volume.
we’ve got to know that it is also intersected with volume B.
Like this, we repeat this algorithm until it hit the last child.







