Syntax of the .ray File Format

For this project we define a rudimentary scene graph language. Each command begins with a #-sign and goes on until the next command or until the end of the file is reached. Commands may extend across new-line characters. All color values are floating point values between 0.0 and 1.0, and all other numeric expressions are assumed to be floating point, also, unless otherwise noted.  Angles are given in radians. When needed, the parser automatically scales a vector to a unit-length vector. You should note that in order to define triangles  you must first define the vertices you will use and then index them in the triangle definition.

Commands:

#camera
    px py pz
    dx dy dz
    ux uy uz
    ha

This defines a perspective camera in the scene. (px,py,pz) is the position of the camera in world coordinates, (dx,dy,dz) is a vector describing the direction of the camera, and (ux,uy,uz) is a vector in the up direction.  The half-height angle of the viewing frustum is given by ha, such that the half-width angle can be found by ha*ar, where ar is the width-over-height aspect ratio of the output image (given on the command line). The last camera found in the file will be used, and subsequent camera definitions are ignored.

#light_point
    r g b
    px py pz
    ca la qa

This defines a point light in the scene.  (r,g,b) gives the color of the light.  (px,py,pz) gives the position of the light in world coordinates.    The attenuation of the light with distance from its position is given by ca, la, and qa which define the constant, linear and quadratic components of the attenuation factor.  If d is the distance from the light to the surface, then the light's color at the surface is given by (r,g,b) *1.0/ (ca + la*d + qa*d*d).  Each coeficient must be positive or equal to zero. Note: in this assignment you should use a (ca,la,qa) of (0.0,0.0,1.0). 

#light_spot
    r g b
    px py pz
    dx dy dz
    ca la qa
    sc sd

This defines a spot point light in the scene.  (r,g,b) gives the color of the light.  (px,py,pz) gives the position of the light in world coordinates. (dx,dy,dz) is a vector giving the direction of the light in the scene. The attenuation of the light with distance from its position is given by ca, la, and qa which define the constant, linear and quadratic components of the attenuation factor.  If d is the distance from the light to the surface, then the light's color at the surface is given by (r,g,b) *1.0/ (ca + la*d + qa*d*d).  Each coeficient must be positive or equal to zero. Note: in this assignment you should use a (ca,la,qa) of (0.0,0.0,1.0). The spot light cutoff is given by cs and defines the half angle of divergence of the light cone.  It can be measured as the angle from the center axis to the edge of the spot cone.  It should be less than pi/2 radians.  The fall off in intensity from the center axis to the cone edge is given by the spot drop-off factor, sd.  It can take values from 0.0 to 1.0, where 0.0 indicated constant intensity across the cone, and 1.0 yields a sharp fall-off.  The cosine of the angle between light direction and the direction of a ray from (px,py,pz) to the point being lit, raised to the power of 128*sd will yield the correct result.

#light_dir
    r g b
    dx dy dz

This defines a directional light in the scene.  (r,g,b) gives the color of the light.  (dx,dy,dz) is a vector giving the direction of the light in the scene. Note that attenuation makes no sense for directional lights, and so is not a parameter of this directive.

#texture filename

Each texture used in the scene is declared by the texture directive using the filename of the texture.  The first texture declared will take the interger identifier 0, and subsequent textures will follow in order (i.e. 1, 2, 3, ...). This integer handle is used in material definitions to indicate that the material includes the respective texture. A texture needs to be declared before it is used in any material.

#material
    ar ag ab
    dr dg db
    sr sg sb
    er eg eb
    n kt ir
    tn
    !string!

Each material used in the scene is declared by the material directive.  The first material declared will take the interger identifier 0, and subsequent materials will follow in order (i.e. 1, 2, 3, ...). This integer handle is used in shape definitions to indicate that the rendering should occur with the appropriate material.  

The colors (ar,ag,ab),  (dr,dg,db),  (sr,sg,sb), and  (er,eg,eb) are the ambient, diffuse, specular, and emmisive colors of the material, respectively.  The ambient material color is used in ambient light calculation with the global ambient light color, the diffuse and specular material colors are used in diffuse and specular lighting computations with the color of each light in the scene, and the emmisive material color acts independently of any light source.

The exponent n defines the specular 'shininess' of the material, and takes values from 0.0 to 1.0.  The cosine of the angle between the ray direction and the specular reflection direction raised to the power of 128*n gives the specular highlight factor. sr, sg, and sb define the reflection coefficient for recursive reflection rays.  kt is the transmission coefficient for recursive transmission rays, and ranges from 0.0 to 1.0.  ks + kt need not total to 1.0. The index of refraction is given by ir and is used in Snell's Law computations for refraction direction.  For non-closed surfaces, such as triangles, it is assumed that ir is the index of refraction on the backside of the surface.  For closed surfaces, such as cones, it is assumed that ir is the index of refraction on the inside of the surface.

To assign a texture to this material, tn should be the integer handle of the appropriate texture declared in the file.  At rendering time, it is assumed that the color of the appropriate texture pixel will modulate the color computed by the lighting equation.  If no texture is to be associated with the material, then a value of -1 must be  indicated.

The final parameter is a string delimited by exclaimation marks.  The string must not contain any control characters, or unpredicatable behaviour could occur.  This field is provided for the users to assign material properties not defined by this file format.  For instance, the numeric constants describing a Perlin Noise 3D solid texture could be put in this field to instruct the rendered to how to texture shapes which are drawn with this material.  If no unsupported parameters are required, empty exclamation marks must conclude the material declaration as follows: !!

#vertex
    px py pz
    nx ny nz
    ts tt

This defines a verex at position (px,py,pz) with normal (nx,ny,nz) and texture coordinate (ts,tt). Each vertex used in the scene is declared by the vertex directive. The first vertex declared will take the interger identifier 0, and subsequent vertices will follow in order (i.e. 1, 2, 3, ...). This integer handle is used in triangle definitions to indicate which vertices make up the triangle. A vertex needs to be declared before it is used in any triangle.

#shape_sphere  m
    cx cy cz
    r

This defines a sphere centered at the point (cx,cy,cz) with a radius given by r. m indicates the integer handle of a material previously declared in the file. 

#shape_box  m
    cx cy cz
    lx ly lz

This defines an axis aligned box, centered at the point (cx,cy,cz).   The length of the x, y, and z axis aligned sides is given by lx, ly, and lz, respectively. The box extends from x=cx-lx/2 to x=cx+lx/2, y=cy-ly/2 to y=cy+ly/2, and z=cz-lz/2 to z=cz+lz/2. m indicates the integer handle of its material, which should have been previously declared in the file. 

#shape_cylinder m
    cx cy cz
    ax ay az
    r h

This defines a cylinder, with a central axis parallel to the vector (ax,ay,az) and centered at the point (cx,cy,cz).  The radius and height are given by r and h, respectively.  The cylinder is a closed surface (i.e. it has end caps.)  The ends lie at p=c-h/2*a and p=c+h/2*a, respectively. m indicates the integer handle of its material, which should have been previously declared in the file.

#shape_cone  m
    cx cy cz
    ax ay az

    r h

This defines a cone, with a central axis parallel to the vector (ax,ay,az) and centered at the point (cx,cy,cz).  The radius and height are given by r and h, respectively.  The cone is a closed surface (i.e. it's base is capped) The base and apex of the cone lie at p=c-h/2*a and p=c+h/2*a, respectively. m indicates the integer handle of its material, which should have been previously declared in the file.

#shape_triangle  m
    i1 i2 i3

This defines a triangle with vertices indexed by i1, i2, and i3. m indicates the integer handle of its material, which should have been previously declared in the file.