Simple ray tracing

A simple ray tracing toy system

This project is maintained by mpinto70

Simple ray tracing

This system was inspired by these articles

Introduction

The idea of raytracing is to build an image based on a camera, a field of view, and the rays from the camera to each point in the field of view.

Given a field of view with size and an screen of size , it is necessary to send a ray from the camera to each point of the field of view divided by the dimensions of the screen and check the color that be filled in that position in the screen.

A ray is defined by its origin and its direction:

Initially, is the camera position. To decide pixel color, it is necessary to determine if the ray hits an object in the environment.

Ray hitting a sphere

For a sphere, the hit point is determined by the equation:

From eq. 1:

Expanding and applying the properties of dot product:

Solving the quadratic polynomial on will give us the values of that satisfies the equation:

That way, the values for that satisfies eq. 4 are:

If the ray crosses the sphere behind the ray’s origin (). Note also that, if is a unitary vector, .

Ray reflecting from a surface

See vector formulation of reflection article in wikipedia.

If the ray direction vector and the normal are unitary vectors:

Replacing eq. 7 in eq. 8: