Shapes Digital Image Synthesis Yung-Yu Chuang 9/27/2005 with slides by Pat Hanrahan
Shapes
Digital Image SynthesisYung-Yu Chuang9/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 providevoid 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
[ ] 0
1
1 =
⎥⎥⎥⎥
⎦
⎤
⎢⎢⎢⎢
⎣
⎡
⎥⎥⎥⎥
⎦
⎤
⎢⎢⎢⎢
⎣
⎡
zyx
JIGDIHFCGFEBDCBA
zyx
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
2222 rzyx =++
( ) ( ) ( ) 2222 rtdotdotdo zzyyxx =+++++
02 =++ CBtAt222zyx dddA ++=
)(2 zzyyxx odododB ++=2222 roooC zyx −++=
Step 1
Algebraic solution
AACBBt
242
0−−−
=A
ACBBt2
42
1−+−
=
If (B2-4AC<0) then the ray misses the sphereStep 2
Step 3
Calculate t0 and test if t0<0Step 4
Calculate t1 and test if t1<0
Geometric solution
1. Origin inside? 2222 rooo zyx >++
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
rrd
O
s
Geometric solution
4. calculate intersection distance,if (origin outside) then t-selse t+s
t
rd
O
s
t
rd
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,,( maxmax xyup φφ−=∂∂
)sin,sin,cos)(( minmax θφφθθ rzzvp
−−=∂∂
)( minmaxmax zzrA −= φ
Cylinder
maxφφ u=φcosrx =φsinry =
)( minmaxmin zzvzz −+=
Cylinder
Cylinder (intersection)
222 ryx =+
( ) ( ) 222 rtdotdo yyxx =+++
02 =++ CBtAt22yx ddA +=
)(2 yyxx ododB +=222 rooC yx −+=
Disk
maxφφ u=φcos))1(( vrrvx i +−=
hz =φsin))1(( vrrvy i +−=
Disk
Disk (intersection)
h-Oz
h
t
Dz Dzz Oh
tDD
−=
)( zz
OhDD
t −=
z
z
DOh
Dtt −=='
Other quadrics
Triangle mesh
The most commonly used shape. In pbrt, it can be supplied by users or tessellated from other shapes.
Triangle meshclass 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 plane2. Check if point is inside triangle
Ray triangle intersection
Algebraic Method0:
: 0
=+⋅+=
dNPPlanetVPPRay
Substituting for P, we get:
( ) 00 =+⋅+ dNtVP
Solution:
( )( )NV
dNPt⋅
+⋅−= 0
tVPP += 0
Ray plane intersection
Algebraic Method
( )( )FALSEreturn
dNPifNPd
NNormalizeVVNPTVPTV
011
101
1
121
22
11
<+⋅⋅−=
×=−=−=
For each side of triangle:
end
Ray triangle intersection I
Parametric Method
( ) ( )
( ) ( )triangleinsideisPthen
andif
TTTTP
Compute
0.10.00.10.0
:,
1312
≤≤≤≤
−+−=
βα
βα
βα
Ray triangle intersection II
Ray triangle intersection III
Fast minimum storage intersection
210)1( vVuVVvutDO ++−−=+1 and 0, ≤+≥ vuvu
[ ] 00201 VOvut
VVVVD −=⎥⎥⎥
⎦
⎤
⎢⎢⎢
⎣
⎡−−−
Fast minimum storage intersection
011 VVE −= 022 VVE −= 0VOT −=
2EDP ×= 1ETQ ×=
⎥⎥⎥
⎦
⎤
⎢⎢⎢
⎣
⎡
⋅⋅⋅
⋅=
⎥⎥⎥
⎦
⎤
⎢⎢⎢
⎣
⎡
DQTPEQ
EPvut 2
1
1