CSG and Raytracing CPSC 407
Mar 26, 2015
CSG and Raytracing
CPSC 407
What is CSG?
Computational Solid Geometry
Volume Representation
Very powerful modeling paradigm
Difficult to render CSG models
Volumes, Not Surfaces
CSG modeling tools allow a user to apply Boolean operations to volumes Union, Intersection, Difference Only works with volumes
Closed surfaces can be interpreted as volumes…
In 3D, a volume is bounded by a surface In 2D, the boundary is a curve
Volumes are Half-Spaces
We can think of volumes as half-spaces Each primitive divides space into a set of points
inside the volume, and a set of points outside The sets of points are infinitely dense
Now our Boolean operations are set operations on these sets of points
Simple 3D Half-Spaces
SphereCylinderConeTorusBox
Plane Plane is tricky - it splits space into two infinite half-spaces
Note that the cylinder and cone are capped. This is not necessary In fact, you can use an infinite cylinder and two planes to make a capped cylinder Infinite cylinders are easier to implement You can also get a box from 6 planes…
Boolean CSG Operations
Union Addition, A B
Intersection A B
Difference Subtraction, A – B, A not B Difference is not commutative
A more complicated example
Difference of: Intersection of Sphere and Cube Union of 3 Cylinders
- =
Raytracing CSG Objects
Need a data structure to describe primitives and operations
Binary CSG Binary tree Leaf nodes are primitives Interior nodes are Boolean operations
Binary Tree Example
Ray Intervals
Define a ray as a set of points (r+td) r is eye point d is the direction of the ray t is the scalar distance along the ray
For now, assume that a primitive is convex A ray (r+td) intersects a convex primitive at most 2 times Let’s say the ray enters the primitive
at distance t1 and leaves at distance t2
Now we can define an interval [t1, t2] along the ray that is inside the primitive
Note: for planes, the interval is either [a,inf] or [inf,b]
CSG Operations on Intervals
Assume we have intervals A = [a1,b1] and B = [a2,b2], and we want to combine them with a CSG operationThere are 5 cases to check for each operation:
No Overlap:
Partial Overlap:
Full Overlap:
CSG Union on 2 Intervals
Remember, Union is A or B
In the no-overlap case, we return the two intervals
The rest of the cases produce one interval, [min(a1,a2), max(b1,b2)]
CSG Intersection on 2 Intervals
Remember, Intersection is A and B
In the no-overlap case, we return no intervals
The rest of the cases produce one interval, [max(a1,a2), min(b1,b2)]
CSG Difference on 2 Intervals
Remember, Difference is A and not B The order is important!
Here we have 5 cases:
Return { [a1,b1] }
Return { [0,0] }
Return { [a1,a2], [b2,b1] }
Return { [a1,a2] }
Return { [b2,b1] }
Lists of intervals
Complex volumes are not convex A ray may intersect the volume more than once Instead of a single interval, we get a set of them
We want to combine interval sets S1 and S2 with a Boolean CSG operation Brute force algorithm:
For each interval Ai in S1, apply the appropriate 2-interval operation with each interval Bj from S2
Return the list of new intervals
This algorithm is O(N2). O(NlogN) algorithms do exist I’m not certain the list of new intervals will be unique. You may have to
check if any overlap, and combine them.
Binary CSG Tree Traversal
Depth-first traversal of the tree At each primitive node, pass back
the list of intervals along the ray that intersect the primitive
At each Boolean node, combine the two child lists and pass back the new set of intervals
The intersection point is at (r+td), where t is the lower value of the first interval in the list returned from the root node of the tree
Materials and Normals for CSG
Need to determine normal at intersection point Need tertiary information with intervals
Either object pointers or normals themselves Note that normals are reversed for subtracted objects
Correct material properties at intersection point are debatable… Some people take material properties at intersection point Others prefer to define material properties for the entire CSG
object as a whole
Additional CSG Bits
It is possible to partially implement CSG without doing a full CSG tree This is how I did difference objects in my raytracer
Blob’s notes have a different CSG algorithm His algorithm looks like it might be more efficient, but
it requires a point inside/outside test Very difficult for arbitrary triangle meshes
General Raytracing Bits
Common bugs
Wrong direction for eye rays Make sure they are going into the scene
Taking the wrong intersection point Ray intersects with a sphere twice, make sure you use the closer
intersection!
Incorrect direction for reflected rays Sending the reflected ray into the object
Total Internal Reflection in refraction Get a sqrt(< 0) in the formula for refracted ray direction In this case, ray reflects instead of refracting
Numerical Error
Self-Intersection due to Numerical Error Produces ‘surface dirt’ – black speckles on otherwise smooth objects
Throw away intersections close to intersection point Bad if you have very thin objects
Throw away any other intersections with current object Assumes objects are convex!!! (not so for CSG, torus)
Avoid by moving a small distance along the normal at the intersection point before casting secondary rays Bad for thin objects again, but probably the simplest method
Transforming Normals
I’m assuming you are implementing object transformations as described in Blob’s Notes
He explains how to transform the intersection point from object space to world space He doesn’t explain how to transform normals
Tutorial on my website: http://www.unknownroad.com/rtfm/graphics/rt_normals.html
Shadow Cache
Assumption: the last object that was hit by a shadow feeler is likely to be the one hit the next time So test it first
For shadow feelers, it doesn’t matter if it’s the closest object (not so for reflection!)
Shadow feelers are usually a large part of the cost of rendering a scene, so this is a big speedup that is relatively simple to implement
WWW Material
http://fuzzyphoton.tripod.com/index.htm
http://www.scs.leeds.ac.uk/cuddles/hyperbks/Raytracer/contents.htmhttp://www.scs.leeds.ac.uk/cuddles/hyperbks/Rendering/introduction.html
http://www.cs.wisc.edu/~schenney/courses/cs559-s2001/lectures/lecture-26-online.ppt