Shapes
Digital Image Synthesis Yung-Yu Chuang
9/27/2005
with slides by Pat Hanrahan
Shapes
• Shape: raw geometry properties of the primitive
• Primitive=Shape+Material
• Source code in core/shape.* and shapes/*
• pbrt provides the following shape plug-ins:
– quadrics: sphere, cone, cylinder, disk, hyperboloid, paraboloid (surface described by quadratic
polynomials in x, y, z) – triangle mesh
– height field – NURBS
– Loop subdivision surface
Shape
class Shape : public ReferenceCounted { public:
<Shape Interface>
const Transform ObjectToWorld, WorldToObject;
const bool reverseOrientation, transformSwapsHandedness;
}
• All shapes are defined in object coordinate space
Shape interface
• BBox ObjectBound(;
• BBox WorldBound() { (can be overridden)
return ObjectToWorld(ObjectBound());
}
• bool CanIntersect() returns whether this shape can do intersection test; if not, the shape must provide
void Refine(vector<Reference<Shape>>&refined)
• bool Intersect(const Ray &ray,
float *tHit, DifferentialGeometry *dg)
• bool IntersectP(const Ray &ray)
Shape interface
• float Area()
• void GetShadingGeometry(
const Transform &obj2world,
const DifferentialGeometry &dg, DifferentialGeometry *dgShading)
• No back culling for that it doesn’t save much for ray tracing and it is not physically correct
for object instancing
Surfaces
• Implicit: F(x,y,z)=0
you can check
• Explicit: (x(u,v),y(u,v),z(u,v)) you can enumerate
• Quadric
[ ]
01
1 =
⎥⎥
⎥⎥
⎦
⎤
⎢⎢
⎢⎢
⎣
⎡
⎥⎥
⎥⎥
⎦
⎤
⎢⎢
⎢⎢
⎣
⎡
z y x
J I
G D
I H
F C
G F
E B
D C
B A
z y
x
Sphere
• A sphere of radius r at the origin
• Implicit: x2+y2+z2-r2=0
• Parametric: f(θ,ψ) x=rsinθcosψ
y=rsinθsinψ z=rcosθ
mapping f(u,v) over [0,1]2 ψ=uψmax
θ=θmax+v(θmax-θmax)
Sphere
Sphere (construction)
class Sphere: public Shape {
……
private:
float radius;
float phiMax;
float zmin, zmax;
float thetaMin, thetaMax;
}
Sphere(const Transform &o2w, bool ro,
float rad, float zmin, float zmax, float phiMax);
• Bounding box for sphere, only z clipping
Algebraic solution
• Perform in object space, WorldToObject(r, &ray)
• Assume that ray is normalized for a while
2 2
2
2 y z r
x + + =
(
ox + tdx)
2 +(
oy + tdy)
2 +(
oz + tdz)
2 = r22 + Bt + C = 0 At
2 2
2
z y
x d d
d
A = + +
) (
2 dxox dyoy dzoz
B = + +
2 2
2
2 o o r
o
C = x + y + z −
Step 1
Algebraic solution
A
AC B
t B
2
2 4
0
−
−
= −
A
AC B
t B
2
2 4
1
− +
= −
If (B2-4AC<0) then the ray misses the sphere
Step 2
Step 3
Calculate t0 and test if t0<0
Step 4
Calculate t1 and test if t1<0
Geometric solution
1. Origin inside? ox2 + o2y + oz2 > r2
Geometric solution
2. find the closest point, t=-O‧D if t<0 and O outside return false
t
t
Geometric solution
3. find the distance to the origin, d2=O2-t2 if s2=r2-d2<0 return false;
t
r
r d
O
s
Geometric solution
4. calculate intersection distance, if (origin outside) then t-s
else t+s
t
r
d
O
s
t
r d
O s
Sphere intersection
• Use algebraic solution, why?
• Consider numeric stability
Partial sphere
• Have to test sphere intersection against clipping parameters
• Partial derivatives
• Area
) 0 , ,
( max y max x u
p = −φ φ
∂
∂
) sin ,
sin ,
cos )(
(θmax θmin z φ z φ r θ v
p = − −
∂
∂
) ( max min
max r z z
A = φ −
Cylinder φ
maxφ = u
φ cos r
x =
φ sin r
y =
) (
max minmin
v z z
z
z = + −
Cylinder
Cylinder (intersection)
2 2
2 y r
x + =
(
ox + tdx)
2 +(
oy + tdy)
2 = r22 + Bt + C = 0 At
2 2
y
x d
d
A = +
) (
2 dxox dyoy
B = +
2 2
2 o r
o
C = x + y −
Disk φ
maxφ = u
φ cos )
) 1
(( v r vr x = −
i+
h z =
φ sin )
) 1
(( v r vr
y = −
i+
Disk
Disk (intersection)
h-Oz
h t
Dz D z h Oz
t D
D
= −
)
( z
z
O D h
t = D −
z z
D O h D
t'= t = −
Other quadrics
Triangle mesh
The most commonly used shape. In pbrt, it can be supplied by users or tessellated from other shapes.
Triangle mesh
class TriangleMesh : public Shape {
…
int ntris, nverts;
int *vertexIndex;
Point *p;
Normal *n;
Vector *s;
float *uvs;
}
x,y,z x,y,z x,y,z x,y,z … x,y,z vi[3*i]
vi[3*i+1]
vi[3*i+2]
p
Note that p is stored in world space to save transformations. n and s are in object space.
Triangle mesh
• ObjectBound and WorldBound
Void TriangleMesh::Refine(vector<Reference<Shape>>
&refined) {
for (int i = 0; i < ntris; ++i)
refined.push_back(new Triangle(ObjectToWorld, reverseOrientation, (TriangleMesh *)this, i));
}
1. Intersect ray with plane
2. Check if point is inside triangle
Ray triangle intersection
Algebraic Method 0
:
: 0
= +
⋅
+
=
d N
P Plane
tV P
P Ray
Substituting for P, we get:
(
P0 + tV)
⋅ N + d = 0Solution:
( )
(
V NN)
dt P
⋅
+
⋅
= − 0
tV P
P =
0+
Ray plane intersection
Algebraic Method
( )
( )
FALSE return
d N
P if
N P
d
N Normalize
V V
N
P T
V
P T
V
1 0
1 1 0
1
1 1 2
1
2 2
1 1
<
+
⋅
⋅
−
=
×
=
−
=
−
=
For each side of triangle:
end
Ray triangle intersection I
Parametric Method
( ) ( )
( ) ( )
triangle inside
is P then
and if
T T
T T
P
Compute
0 . 1 0
. 0 0
. 1 0
. 0
: ,
1 3
1 2
≤
≤
≤
≤
− +
−
=
β α
β α
β α
Ray triangle intersection II
Ray triangle intersection III
Fast minimum storage intersection
2 1
)
01
( u v V uV vV tD
O + = − − + + 1
and
0
, v ≥ u + v ≤ u
[
1 0 2 0] O V0
v u t V
V V
V
D = −
⎥ ⎥
⎥
⎦
⎤
⎢ ⎢
⎢
⎣
⎡
−
−
−
Fast minimum storage intersection
0 1
1
V V
E = − E
2= V
2− V
0T = O − V
0E
2D
P = × Q = T × E
1⎥ ⎥
⎥
⎦
⎤
⎢ ⎢
⎢
⎣
⎡
⋅
⋅
⋅
= ⋅
⎥ ⎥
⎥
⎦
⎤
⎢ ⎢
⎢
⎣
⎡
D Q
T P
E Q
E v P
u
t
21