A raytracer written in OCaml. It can draw planes and spheres with diffuse surface and illuminate them with point & directional light.
$ opam install imagelib
$ make
$ ./main.native
You can set camera position, objects & lights in file scene.json. Feel free to look at an example in the repo.
Order of keys at this and lower levels of JSON matters and must be the same as in this specification.
First level must contain three keys: camera, objects, lights.
A list of seven values:
bottom_left_x : floatbottom_left_y : floatupper_right_x : floatupper_right_y : floatresolution_x : intresolution_y : intfocal_length : float
Camera is a rectangle on a plane Z=0, looking towards vector (0, 0, 1), i.e. looking towards Z+. You can set positon of this rectangle (its bottom left and upper right corners), resolution and focal length. The latter is the distance between camera and focus (point through which all the rays from pixels go).
A list of JSON objects, each has the following keys:
type, eithersphereorplanecolor, one of the following values:pink,blue,green,gold,seashell,tomato,black,white,orchid,oliveshader, as for now onlydiffusedata, a list of floats, depending on object type
Data for sphere is a four element list containing:
position_x : floatposition_y : floatposition_z : floatradius : float
Data for plane is a six element list containing:
position_x : floatposition_y : floatposition_z : floatnormal_x : floatnormal_y : floatnormal_z : float
As plane is given by a point and a normal vector to the plane.
A list of JSON objects, each has the following keys:
type, eitherpointordirectionaldata, a four element list of floats, depending on light type
Data for point is a list containing:
intensity : floatposition_x : floatposition_y : floatposition_z : float
Point light intensity diminishes with distance from the light. It is inversely proportional to the square of the distance from the source.
Data for directional is list containing:
intensity : floatdirection_x : floatdirection_y : floatdirection_z : float
Directional light behaves like the Sun, i.e. it has constant intensity and its source is given by a direction, not by any particular point.