Programming projects Rendering Algorithms Spring 2010 Matthias Zwicker Universität Bern Description of assignments on class webpage Use programming language and environment of your choice We recommend Java, C++, C# No slow interpreted or scripting languages (Smalltalk, Eiffel, Python, etc.) Turn-in of assignments by demonstration to teaching assistant raytrace() { construct scene representation for each pixel ray = computeprimary() hit = first intersection with scene color = shade( hit ) set pixel color } Topics Generate camera rays Intersection routines for spheres, planes, triangles Meshes, instancing i Phenomenological shading Shadows Reflection Hacker s bonus: refraction Due date: in 3 weeks Start now! 1st week Set up programming environment Camera rays Intersection with planes Image output 2nd week Intersection with spheres, triangles, meshes Phong shading 3rd week Instancing, shadows, reflection Hacker s bonus (refraction, multithreading) A horizontal plane! Shading, shadows Mirrors Meshes, instancing Bonus: refraction 1
Useful Java libraries Matrix and vector math For example javax.vecmath.* Image input/output For example java.awt.image.*, in particular class BufferedImage Useful C++ libraries Boost libraries, http://www.boost.org/ Includes libraries for matrix math, image i/o, and many more Data structures class Ray class Camera class Material class HitRecord interface Intersectable class Sphere e class Triangle class Plane abstract class Aggregate class Mesh class IntersectableList Class Integrator class Film class Tonemapper Data structures class Ray class Camera class Material class HitRecord interface Intersectable class Sphere e class Triangle class Plane abstract class Aggregate class Mesh class IntersectableList Class Integrator class Film class Tonemapper Don t worry about object oriented design too much. Keep it simple! Ray tracing pseudo code Main loop construct scene; reference whole scene by one Intersectable construct lights construct integrator construct camera construct film for all pixels { ray = camera.makeray(pixel) spectrum = integrator.integrate(scene, lights, ray) film.set(pixel, spectrum) } film.writetofile Ray Origin, direction Origin Direction 2
Camera & image specification Parameters: eye position, look-at point, up vector, vertical field of view, aspect ratio, width in pixels, height in pixels Make primary rays given pixel coordinates Hit record Stores information about a ray-surface intersection Position Normal Ray direction Ray parameter at intersection Reference to material Material Stores material properties, information for shading More later Integrator Main methodintegrate Computes color for a ray Implements core ray tracing algorithm Different integrators can use different algorithms Simple integrator for debugging Shading without shadows Recursive ray tracing Path tracing Etc. Binary Integrator For testing, debugging integrate(scene, lights, ray) if(scene.intersect(ray)!= null) return white else return black Integrator with light sources If something is hit, sum over all light sources integrate(scene, lights, ray) hitrecord = scene.intersect(ray) if( hitrecord!= null ) spectrum = black for all light sources spectrum += contribution of light source return spectrum else return black 3
Intersectable Interface implemented by anything that can be intersected with a ray Methods intersect t (get surface area) (get bounding box) Spheres and planes Implement interface Intersectable Sphere: center, radius Plane: distance to origin, normal Intersection methods as discussed in next class Ray-plane intersection http://www.siggraph.org/education/materials/hypergraph/raytrace/rayplane_intersection.htm Ray-sphere intersection http://www.siggraph.org/education/materials/hypergraph/raytrace/rtinter1.htm Aggregate Abstract class that stores a collection of intersectables Intersecting an aggregate returns the closest hit A scene is represented by a single aggregate IntersectableList Simplest implementation of an aggregate A list of Intersectable intersect method traverses list, reports closest hit Mesh Implements Aggregate Stores a collection of triangles Array of vertex positions Array of vertex normals Array of indices, stores indices of three triangle vertices in position, normal arrays Mesh intersection Maintain a bounding volume for the mesh Sphere, axis aligned box If no intersection with bounding volume, return Else, iterate over all triangles Intersect each triangle Retain closest hit, i.e., smallest t parameter of ray Return closest hit 4
Triangle Stores reference to mesh Contains index into index array of mesh Looks up vertex data in arrays of mesh Implement interface Intersectable t Intersection method as discussed in next class Spectrum, Film, ToneMapper Spectrum Stores r,g,b values Film 2D array of spectra ToneMapper Writes Film to an image file Scene description Keep scene description in separate file to keep code clean See example files Next time Ray-surface intersection Shading Reflection, refraction, shadows 5