Skip to content

matthiasbroske/CPURayTracer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CPURayTracer

Depth of Field         Stanford Bunny

About

A CPU ray tracer built entirely from scratch in C++.

Features

  • Ambient, diffuse and specular shading
  • Directional lights
  • Attenuated point lights
  • Depthcueing
  • Hard shadows
  • Soft shadows
  • Reflection
  • Transparency
  • Refraction
  • Depth of field
  • Texture mapping
  • BVH acceleration

Setup Instructions

Build the ray tracer using the given Makefile with

make

and run using

./raytracer scenefile [outputfile] [softshadows] [dof]
  • scenefile - path to input file containing scene description
  • outputfile - name for final output image file (optional)
  • softshadows - soft shadow toggle, 0 = off, 1 = on (optional)
  • dof - depth of field toggle, 0 = off, 1 = on (optional)

Note that it may take several seconds for the ray tracer to complete rendering the scene.

Example

To render the demo scene included in this repo, navigate to this directory in your terminal of choice and — if you have yet to do so — build the project with

make

and then run the ray tracer on the demo scene:

./raytracer scenes/demo-scene.txt

The output image will appear in a file named demo-scene.ppm located in the scenes folder.

Demo Scene

Scene Description Files

Scenes are defined in simple text files which contain information about the camera, lighting, materials, and the geometry of objects in the scene. For an example of a scene description file, see demo-scene.txt.

The syntax for scene description files is defined as follows, where entries in bold are keywords and entries in italics are variables:

Camera

eye eyex eyey eyez (camera position)

viewdir vdirx vdiry vdirz (camera view direction)

updir upx upy upz (camera up direction)

vfov vfov (vertical field of view, in degrees)

imsize width height (size of the output image in pixel units)

bkgcolor r g b (background color in terms of r, g, b components defined on a scale from 0-1)

viewdist d (depth of field focus distance)

Lighting

light x y z w r g b ((x, y, z, 1) = point light position, (x, y, z, 0) = directional light direction, with rgb light color)

attlight x y z w r g b c1 c2 c3 (attenuated point light)

depthcueing dcr dcg dcb amax amin distmax distmin (depthcueing)

Materials

mtlcolor Odr Odg Odb Osr Osg Osb ka kd ks n α η (material properties, treated as a state variable such that all subsequently-defined objects use the immediately-preceding material properties)

Textures

texture ppm (path to ppm texture file, treated as a state variable in the same way as materials)

Primitives

sphere cx cy cz r (sphere defined by its center point and radius)

Triangles

list of vertex positions

v x1 y1 z1
v x2 y2 z2
v x3 y3 z3
v x4 y4 z4
. . .

list of vertex normals

vn nx1 ny1 nz1
vn nx2 ny2 nz2
vn nx3 ny3 nz3
vn nx4 ny4 nz4
. . .

list of texture coordinates

vt u1 v1
vt u2 v2
vt u3 v3
vt u4 v4
. . .

list of triangle definitions, consisting of appropriate indices into the vertex arrays, starting at 1

f v1/vt1/vn1 v2/vt2/vn2 v3/vt3/vn3
f v1/vt1/vn1 v2/vt2/vn2 v4/vt4/vn4
. . .

Note that triangles are defined more-or-less according to the .obj file format, such that most .obj files should provide geometry combatible with this renderer.

About

A CPU ray tracer built entirely from scratch in C++

Topics

Resources

License

Stars

Watchers

Forks