Skip to content

Latest commit

 

History

4 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

C++ Ray Tracer

Describtion

A ray tracker based on C++ implementation, supporting intersection operations for multiple geometric prototypes, two projection modes (perspective and parallel), and a robust scene file parsing system.

Features

  • Multiple geometries support:
    • Triangle
    • Sphere
    • Ellipsoid (axis aligned ellipsoid)
    • Cylinder
    • Cone
  • Material and shading support:
    • Material
    • Texture
    • Normal map
  • Camera system:
    • Perspective Projection
    • Parallel Projection
  • Robust parser:
    • Support # inline comments.
    • Material color management based on state machine.
    • Supports out of order command input and has basic error checking capabilities.

Project Structure

.  
├── include/          # header file (.h)
├── src/              # Source Files (.cpp)
├── inputs/           # scene description file
├── outputs/          # generated image (.ppm)
├── texture/          # texture image (.ppm)
├── build/            # build files after compilation
│   ├── obj/          # object files
│   ├── raytracer1a   # executable file
├── Makefile
└── README.md  

Compile and Execute

Compile

Simply run make in the root directory to generate the executable file build/raytracer1a:

make

Run

You need to specify the input file name (which should be placed in the inputs/ folder). The rendered results will be automatically saved to the outputs/ folder and will retain the same name as the input file:

./build/raytracer1a inputs/your_scene_name.txt

Clean up

clean will clear the contents of the entire build/ folder

make clean

Input file format

The scene file is parsed line by line, and supports the following keywords:

Scene settings:

Keyword Parameter Description
imsize width height Image resolution (pixels)
eye x y z Camera position
viewdir vdirx vdiry vdirz The central viewing direction
updir upx upy upz The 'up' direction
bkgcolor r g b eta Background color (0-1) and the Refractive Index (eta) of the environment (e.g., 1.0 for air)
vfov fovv The vertical field of view (in degrees; if parallel is set, do not set this)
parallel frustum_height Height of parallel projection (if vfov is set, do not set this)
mtlcolor Odr Odg Odb Osr Osg Osb ka kd ks n alpha eta A current ‘material’ color (scale from 0-1; must be defined before any Geometric Objects). Od represents the diffuse color; Os represents the specular color; ka(ambient coefficient), kd(diffuse coefficient), ks(specular coefficient), n(shininess exponent) are lighting coefficient. alpha defines opacity (0.0 transparent, 1.0 opaque). eta is the refractive index. Enables refractive transparency and Beer's Law attenuation.
texture ppmfile A current ‘texture’. ppmfile: A PPM file stored in texture/
bump ppmfile A current ‘normal maps’. ppmfile: A PPM file stored in texture/
light x y z w i A point light source is defined by a 3D location in space (x, y, z, 1), and a directional light source is defined by a vector (x, y, z, 0); i is light source intensity (positive less than 1).
attlight x y z w i c1 c2 c3 Useing for light source attenuation, x, y, z, w, i are the same definition as parameters in light. c1, c2, c3 are distance attenuation coefficient.
depthcueing dcr dcg dcb ɑmin ɑmax distmin distmax Using for depth cueing. (dcr dcg dcb) are depth cue color; ɑmin, ɑmax are mix intensity range; distmin, distmax are distance range.
indirect samples Number of secondary rays for Diffuse Interreflection. High values (e.g., 1000+) produce realistic global illumination/color bleeding but increase render time significantly.

Triangles settings:

Keyword Parameter Description
v x y z Vertex positions
vn nx ny nz Vertex normal vectors.
vt u v Vertex texture coordinates.
f v1 v2 v3 Triangle definitions, consisting of appropriate indices into the vertex array, starting at 1 (not 0).
f v1//vn1 v2//vn2 v3//vn3 Smooth shaded triangle.
f v1/vt1 v2/vt2 v3/vt3 Flat-shaded textured triangle.
f v1/vt1/vn1 v2/vt2/vn2 v3/vt3/vn3 Smooth-shaded textured triangle

Geometric objects settings:

Keyword Parameter Description
sphere cx cy cz r center point (cx, cy, cz) and radius r
ellipsoid cx cy cz rx ry rz center point (cx, cy, cz) and radii rx, ry, rz
cylinder cx cy cz dx dy dz radius length (cx, cy, cz) represents the center of the base of the cylinder; (dx, dy, dz) defines the direction of the cylinder; radius defines the cylinder's radius; length defines the total length of the cylinder.
cone cx cy cz dx dy dz angle height (cx, cy, cz) denotes the location of the tip of the cone; (dx, dy, dz) defines the direction of the central axis of the cone; angle defines the angle between the surface of the cone and its central axis; height defines the total height of the cone.

Example Scene Design

Inside inputs/cube_frame.txt:

# cube_frame.txt
# Make a cube frame
imsize 1200 900
eye 7 8 10
viewdir -0.7 -0.8 -1
updir 0 1 0
vfov 45
bkgcolor 0.05 0.05 0.1

# Lighting
light 5 10 5 1 0.8

# Corner soccerball (Vertices)
mtlcolor 1.0 1.0 1.0  1.0 1.0 1.0  0.1 0.6 0.3 50
texture soccerball.ppm

sphere -2 -2 -2 0.4
sphere  2 -2 -2 0.4
sphere  2  2 -2 0.4
sphere -2  2 -2 0.4
sphere -2 -2  2 0.4
sphere  2 -2  2 0.4
sphere  2  2  2 0.4
sphere -2  2  2 0.4

# Silver Frame (Edges)
# No texture for the frame tubes
texture none
mtlcolor 0.8 0.8 0.8  1.0 1.0 1.0  0.1 0.5 0.4 20

# Bottom edges
cylinder -2 -2 -2  1 0 0  0.2 4
cylinder  2 -2 -2  0 1 0  0.2 4
cylinder  2  2 -2 -1 0 0  0.2 4
cylinder -2  2 -2  0 -1 0  0.2 4

# Top edges
cylinder -2 -2  2  1 0 0  0.2 4
cylinder  2 -2  2  0 1 0  0.2 4
cylinder  2  2  2 -1 0 0  0.2 4
cylinder -2  2  2  0 -1 0  0.2 4

# Vertical pillars
cylinder -2 -2 -2  0 0 1  0.2 4
cylinder  2 -2 -2  0 0 1  0.2 4
cylinder  2  2 -2  0 0 1  0.2 4
cylinder -2  2 -2  0 0 1  0.2 4

# Grass Lawn (Triangles)
v  10 -2.2  10
v -10 -2.2  10
v -10 -2.2 -10
v  10 -2.2 -10

# Define texture coordinates for the lawn (tiling)
vt 0 0
vt 1 0
vt 1 1
vt 0 1

# Set grass material and texture
mtlcolor 0.2 0.8 0.2  0.1 0.1 0.1  0.1 0.8 0 5
texture grass.ppm

# Create the square lawn using two triangles
f 1/1 2/2 3/3
f 1/1 3/3 4/4

Coordinate system description:

This system uses a right-handed coordinate system:

  • Forward: -Z-axis direction
  • Backward: +Z-axis direction (pointing towards the observer)
  • Right: +X axis direction
  • Up: +Y axis direction

Author

Name: Guohao (Paul) Liao
Email: liao0238@umn.edu

About

A ray tracker based on C++ implementation, supporting intersection operations for multiple geometric prototypes, two projection modes (perspective and parallel), and a robust scene file parsing system.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages