shapes
DESCRIPTION
Shapes. Digital Image Synthesis Yung-Yu Chuang. with slides by Pat Hanrahan. Shapes. One advantages of ray tracing is it can support various kinds of shapes as long as we can find ray-shape intersection. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Shapes](https://reader035.vdocuments.site/reader035/viewer/2022070410/56814689550346895db3abc2/html5/thumbnails/1.jpg)
Shapes
Digital Image SynthesisYung-Yu Chuang
with slides by Pat Hanrahan
![Page 2: Shapes](https://reader035.vdocuments.site/reader035/viewer/2022070410/56814689550346895db3abc2/html5/thumbnails/2.jpg)
Shapes
• One advantages of ray tracing is it can support various kinds of shapes as long as we can find ray-shape intersection.
• Careful abstraction of geometric shapes is a key component for a ray tracer. Ideal candidate for object-oriented design. Scan conversion may not have such a neat interface.
• All shape classes implement the same interface and the other parts of the ray tracer just use this interface without knowing the details about this shape.
![Page 3: Shapes](https://reader035.vdocuments.site/reader035/viewer/2022070410/56814689550346895db3abc2/html5/thumbnails/3.jpg)
Shapes
• Primitive=Shape+Material• Shape: raw geometry properties of the
primitive, implements interface such as surface area and bounding box.
• Source code in core/shape.* and shapes/*
![Page 4: Shapes](https://reader035.vdocuments.site/reader035/viewer/2022070410/56814689550346895db3abc2/html5/thumbnails/4.jpg)
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
• Some possible extensions: other subdivision schemes, fractals, CSG, point-sampled geometry
![Page 5: Shapes](https://reader035.vdocuments.site/reader035/viewer/2022070410/56814689550346895db3abc2/html5/thumbnails/5.jpg)
Shapes
class Shape : public ReferenceCounted {public: <Shape Interface> all are virtual functions const Transform ObjectToWorld, WorldToObject; const bool reverseOrientation, transformSwapsHandedness; const int shapeId; each shape is given an unique id. It can be used in
adaptive image sampling, pixels with more complex geometry often need more samples.
static int nextShapeId; initialized as 1 as 0 is reserved as `no shape’ }
• All shapes are defined in object coordinate space
![Page 6: Shapes](https://reader035.vdocuments.site/reader035/viewer/2022070410/56814689550346895db3abc2/html5/thumbnails/6.jpg)
Shape interface: bounding
• BBox ObjectBound() const=0;pure virtual function• BBox WorldBound() { left to individual
shape default implementation; can be overridden return ObjectToWorld(ObjectBound());
}
![Page 7: Shapes](https://reader035.vdocuments.site/reader035/viewer/2022070410/56814689550346895db3abc2/html5/thumbnails/7.jpg)
Shape interface: intersecting
• bool CanIntersect() returns whether this shape can do intersection test; if not, the shape must provide
void Refine(vector<Reference<Shape>>&refined)
examples include complex surfaces (which need to be tessellated first) or placeholders (which store geometry information in the disk)
• bool Intersect(const Ray &ray,
float *tHit, DifferentialGeometry *dg)• bool IntersectP(const Ray &ray)
not pure virtual functions so that non-intersectable shapes don’t need to implement them; instead, a default implementation which prints error is provided.
in world space
![Page 8: Shapes](https://reader035.vdocuments.site/reader035/viewer/2022070410/56814689550346895db3abc2/html5/thumbnails/8.jpg)
Shape interface• float Area() useful when using as an area light• 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
![Page 9: Shapes](https://reader035.vdocuments.site/reader035/viewer/2022070410/56814689550346895db3abc2/html5/thumbnails/9.jpg)
Differential geometry
• DifferentialGeometry: a self-contained representation for a particular point on a surface so that all the other operations in pbrt can be executed without referring to the original shape. It contains
• Position• Parameterization (u,v)• Parametric derivatives (dp/du, dp/dv)• Surface normal (derived from (dp/du)x(dp/dv))• Derivatives of normals• Pointer to shape
![Page 10: Shapes](https://reader035.vdocuments.site/reader035/viewer/2022070410/56814689550346895db3abc2/html5/thumbnails/10.jpg)
Surfaces
• Implicit: F(x,y,z)=0 you can check• Explicit: (x(u,v),y(u,v),z(u,v)) you can enumerate also called parametric • Quadrics
0
1
1
z
y
x
JIGD
IHFC
GFEB
DCBA
zyx
![Page 11: Shapes](https://reader035.vdocuments.site/reader035/viewer/2022070410/56814689550346895db3abc2/html5/thumbnails/11.jpg)
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
θ=θmin+v(θmax-θmin)
useful for texture mapping
θ
![Page 12: Shapes](https://reader035.vdocuments.site/reader035/viewer/2022070410/56814689550346895db3abc2/html5/thumbnails/12.jpg)
Sphere
![Page 13: Shapes](https://reader035.vdocuments.site/reader035/viewer/2022070410/56814689550346895db3abc2/html5/thumbnails/13.jpg)
Sphere (construction)
class Sphere: public Shape {
……
private:
float radius;
float phiMax;
float zmin, zmax;
float thetaMin, thetaMax;
}
Sphere(Transform *o2w, Transform *w2o, bool ro, float rad,
float z0, float z1, float pm);
• Bounding box for sphere, only z clipping
thetas are derived from z
![Page 14: Shapes](https://reader035.vdocuments.site/reader035/viewer/2022070410/56814689550346895db3abc2/html5/thumbnails/14.jpg)
Intersection (algebraic solution)
• Perform in object space, WorldToObject(r, &ray)• Assume that ray is normalized for a while
2222 rzyx
2222 rtdotdotdo zzyyxx
02 CBtAt
222zyx dddA
)(2 zzyyxx odododB 2222 roooC zyx
Step 1
![Page 15: Shapes](https://reader035.vdocuments.site/reader035/viewer/2022070410/56814689550346895db3abc2/html5/thumbnails/15.jpg)
Algebraic solution
A
ACBBt
2
42
0
A
ACBBt
2
42
1
If (B2-4AC<0) then the ray misses the sphereStep 2
Step 3
Calculate t0 and test if t0<0 (actually mint, maxt)Step 4
Calculate t1 and test if t1<0
check the real source code in sphere.cpp
![Page 16: Shapes](https://reader035.vdocuments.site/reader035/viewer/2022070410/56814689550346895db3abc2/html5/thumbnails/16.jpg)
Quadric (in pbrt.h)inline bool Quadratic(float A, float B, float C, float *t0, float *t1) { // Find quadratic discriminant float discrim = B * B - 4.f * A * C; if (discrim < 0.) return false; float rootDiscrim = sqrtf(discrim); // Compute quadratic _t_ values float q; if (B < 0) q = -.5f * (B - rootDiscrim); else q = -.5f * (B + rootDiscrim); *t0 = q / A; *t1 = C / q; if (*t0 > *t1) swap(*t0, *t1); return true;}
![Page 17: Shapes](https://reader035.vdocuments.site/reader035/viewer/2022070410/56814689550346895db3abc2/html5/thumbnails/17.jpg)
Why?
• Cancellation error: devastating loss of precision when small numbers are computed from large numbers by addition or subtraction.
double x1 = 10.000000000000004; double x2 = 10.000000000000000;
double y1 = 10.00000000000004;
double y2 = 10.00000000000000;
double z = (y1 - y2) / (x1 - x2); // 11.5
A
qt 0
q
Ct 1
otherwise 42
1
0 if 42
1
2
2
ACBB
BACBBq
![Page 18: Shapes](https://reader035.vdocuments.site/reader035/viewer/2022070410/56814689550346895db3abc2/html5/thumbnails/18.jpg)
Range checking
if (t0 > ray.maxt || t1 < ray.mint) return false;float thit = t0;if (t0 < ray.mint) {
thit = t1;if (thit > ray.maxt) return false;
} ...
phit = ray(thit);phi = atan2f(phit.y, phit.x);if (phi < 0.) phi += 2.f*M_PI;// Test sphere intersection against clipping parametersif (phit.z < zmin || phit.z > zmax || phi > phiMax) { ... // see if we should check another hit point}
![Page 19: Shapes](https://reader035.vdocuments.site/reader035/viewer/2022070410/56814689550346895db3abc2/html5/thumbnails/19.jpg)
Geometric solution
1. Origin inside? 2222 rooo zyx
![Page 20: Shapes](https://reader035.vdocuments.site/reader035/viewer/2022070410/56814689550346895db3abc2/html5/thumbnails/20.jpg)
Geometric solution
2. find the closest point, t=-O‧D if t<0 and O outside return false
t
t
D is normalized
![Page 21: Shapes](https://reader035.vdocuments.site/reader035/viewer/2022070410/56814689550346895db3abc2/html5/thumbnails/21.jpg)
Geometric solution
3. find the distance to the origin, d2=O2-t2
if s2=r2-d2<0 return false;
t
rrd
O
s
![Page 22: Shapes](https://reader035.vdocuments.site/reader035/viewer/2022070410/56814689550346895db3abc2/html5/thumbnails/22.jpg)
Geometric solution
4. calculate intersection distance, if (origin outside) then t-s else t+s
t
rd
O
s
t
r
d
O
s
![Page 23: Shapes](https://reader035.vdocuments.site/reader035/viewer/2022070410/56814689550346895db3abc2/html5/thumbnails/23.jpg)
Sphere
• Have to test sphere intersection against clipping parameters
• Fill in information for DifferentialGeometry– Position– Parameterization (u,v)– Parametric derivatives– Surface normal– Derivatives of normals– Pointer to shape
![Page 24: Shapes](https://reader035.vdocuments.site/reader035/viewer/2022070410/56814689550346895db3abc2/html5/thumbnails/24.jpg)
Partial sphere
u= / max
v=(θ-θmin)/ (θmax-θmin)
• Partial derivatives (pp121 of textbook)
• Area (pp123)
)0,,( maxmax xyu
p
)sin,sin,cos)(( minmax rzzv
p
)( minmaxmax zzrA
![Page 25: Shapes](https://reader035.vdocuments.site/reader035/viewer/2022070410/56814689550346895db3abc2/html5/thumbnails/25.jpg)
Cylinder
max ucosrx sinry
)( minmaxmin zzvzz
![Page 26: Shapes](https://reader035.vdocuments.site/reader035/viewer/2022070410/56814689550346895db3abc2/html5/thumbnails/26.jpg)
Cylinder
![Page 27: Shapes](https://reader035.vdocuments.site/reader035/viewer/2022070410/56814689550346895db3abc2/html5/thumbnails/27.jpg)
Cylinder (intersection)
222 ryx
222 rtdotdo yyxx
02 CBtAt
22yx ddA
)(2 yyxx ododB 222 rooC yx
![Page 28: Shapes](https://reader035.vdocuments.site/reader035/viewer/2022070410/56814689550346895db3abc2/html5/thumbnails/28.jpg)
Disk
max ucos))1(( vrrvx i
hz
sin))1(( vrrvy i
![Page 29: Shapes](https://reader035.vdocuments.site/reader035/viewer/2022070410/56814689550346895db3abc2/html5/thumbnails/29.jpg)
Disk
![Page 30: Shapes](https://reader035.vdocuments.site/reader035/viewer/2022070410/56814689550346895db3abc2/html5/thumbnails/30.jpg)
Disk (intersection)
h-Oz
h
t
Dz D htDO zz
z
z
D
Oht
![Page 31: Shapes](https://reader035.vdocuments.site/reader035/viewer/2022070410/56814689550346895db3abc2/html5/thumbnails/31.jpg)
Other quadrics
0)( 222
hzr
hy
r
hx
conehyperboloidparaboloid
1222 zyx02
2
2
2
zr
hy
r
hx
![Page 32: Shapes](https://reader035.vdocuments.site/reader035/viewer/2022070410/56814689550346895db3abc2/html5/thumbnails/32.jpg)
Triangle mesh
The most commonly used shape. In pbrt, it can be supplied by users or tessellated from other shapes. Some ray tracers only support triangle meshes.
![Page 33: Shapes](https://reader035.vdocuments.site/reader035/viewer/2022070410/56814689550346895db3abc2/html5/thumbnails/33.jpg)
Triangle meshclass TriangleMesh : public Shape {… int ntris, nverts; int *vertexIndex; Point *p; Normal *n; per vertex Vector *s; tangent float *uvs; parameters Texture<float> atex; mask; useful for modeling leaves}
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]
pNote that p is stored in world space to save transformations. n and s are in object space.
![Page 34: Shapes](https://reader035.vdocuments.site/reader035/viewer/2022070410/56814689550346895db3abc2/html5/thumbnails/34.jpg)
Triangle mesh
Pbrt calls Refine() when it encounters a shape that is not intersectable. (usually, refine is called in acceleration structure creation)
Void TriangleMesh::Refine(vector<Reference<Shape>>
&refined)
{
for (int i = 0; i < ntris; ++i)
refined.push_back(new Triangle(ObjectToWorld,
reverseOrientation, (TriangleMesh *)this, i));
}
Refine breaks a triangle mesh into a list of Triangles.Triangle only stores a pointer to mesh and a pointer to vertexIndex.
![Page 35: Shapes](https://reader035.vdocuments.site/reader035/viewer/2022070410/56814689550346895db3abc2/html5/thumbnails/35.jpg)
1. Intersect ray with plane2. Check if point is inside triangle
Ray triangle intersection
![Page 36: Shapes](https://reader035.vdocuments.site/reader035/viewer/2022070410/56814689550346895db3abc2/html5/thumbnails/36.jpg)
Algebraic Method
0:
: 0
dNPPlane
tVPPRay
Substituting for P, we get:
00 dNtVP
Solution:
NV
dNPt
0
tVPP 0
Ray plane intersection
![Page 37: Shapes](https://reader035.vdocuments.site/reader035/viewer/2022070410/56814689550346895db3abc2/html5/thumbnails/37.jpg)
Algebraic Method
FALSEreturn
dNPif
NPd
NNormalize
VVN
PTV
PTV
011
101
1
211
022
011
For each side of triangle:
end
Ray triangle intersection I
![Page 38: Shapes](https://reader035.vdocuments.site/reader035/viewer/2022070410/56814689550346895db3abc2/html5/thumbnails/38.jpg)
Parametric Method
triangleinsideisPthen
and
andif
TTTTP
Compute
0.1
0.10.00.10.0
:,
1312
Ray triangle intersection II
![Page 39: Shapes](https://reader035.vdocuments.site/reader035/viewer/2022070410/56814689550346895db3abc2/html5/thumbnails/39.jpg)
Ray triangle intersection III
![Page 40: Shapes](https://reader035.vdocuments.site/reader035/viewer/2022070410/56814689550346895db3abc2/html5/thumbnails/40.jpg)
Fast minimum storage intersection
210)1( vVuVVvutDO 1 and 0, vuvu
00201 VO
v
u
t
VVVVD
a point on the ray
a point inside the triangle
![Page 41: Shapes](https://reader035.vdocuments.site/reader035/viewer/2022070410/56814689550346895db3abc2/html5/thumbnails/41.jpg)
Fast minimum storage intersection
00201 VO
v
u
t
VVVVD
O
D
V0
V1
V2O
D
V0
V1
V2 1
1
translation rotation
Geometric interpretation: what is O’s coordinate under the new coordinate system?
![Page 42: Shapes](https://reader035.vdocuments.site/reader035/viewer/2022070410/56814689550346895db3abc2/html5/thumbnails/42.jpg)
Fast minimum storage intersection
011 VVE 022 VVE 0VOT
00201 VO
v
u
t
VVVVD
T
v
u
t
EED
21
![Page 43: Shapes](https://reader035.vdocuments.site/reader035/viewer/2022070410/56814689550346895db3abc2/html5/thumbnails/43.jpg)
Fast minimum storage intersection• Cramer’s rule
TED
ETD
EET
EEDv
u
t
,,
,,
,,
,,
1
1
2
21
21
T
v
u
t
EED
21
ABCBCACBA )()(,,
![Page 44: Shapes](https://reader035.vdocuments.site/reader035/viewer/2022070410/56814689550346895db3abc2/html5/thumbnails/44.jpg)
Fast minimum storage intersection
2EDP 1ETQ
DQ
TP
EQ
EPv
u
t 2
1
1
TED
ETD
EET
EEDv
u
t
,,
,,
,,
,,
1
1
2
21
21
1 division27 multiplies17 adds
![Page 45: Shapes](https://reader035.vdocuments.site/reader035/viewer/2022070410/56814689550346895db3abc2/html5/thumbnails/45.jpg)
Subdivision surfaces
http://www.subdivision.org/demos/demos.html
![Page 46: Shapes](https://reader035.vdocuments.site/reader035/viewer/2022070410/56814689550346895db3abc2/html5/thumbnails/46.jpg)
Subdivision surfaces
• Catmull-Clark (1978)
![Page 47: Shapes](https://reader035.vdocuments.site/reader035/viewer/2022070410/56814689550346895db3abc2/html5/thumbnails/47.jpg)
Loop Subdivision Scheme
• Refine each triangle into 4 triangles by splitting each edge and connecting new vertices
valence=4
valence=6boundary interior
![Page 48: Shapes](https://reader035.vdocuments.site/reader035/viewer/2022070410/56814689550346895db3abc2/html5/thumbnails/48.jpg)
Loop Subdivision Scheme
• Where to place new vertices?– Choose locations for new vertices as weighted
average of original vertices in local neighborhood
even vertices (old)odd vertices (new)
![Page 49: Shapes](https://reader035.vdocuments.site/reader035/viewer/2022070410/56814689550346895db3abc2/html5/thumbnails/49.jpg)
• Where to place new vertices?– Rules for extraordinary vertices and
boundaries:
Loop Subdivision Scheme
![Page 50: Shapes](https://reader035.vdocuments.site/reader035/viewer/2022070410/56814689550346895db3abc2/html5/thumbnails/50.jpg)
Butterfly subdivision
• Interpolating subdivision: larger neighborhood
11//2211//22
-1-1//1616
-1-1//1616
-1-1//1616
-1-1//1616
11//88
11//88
![Page 51: Shapes](https://reader035.vdocuments.site/reader035/viewer/2022070410/56814689550346895db3abc2/html5/thumbnails/51.jpg)
Advantages of subdivision surfaces• Smooth surface• Existing polygon modeling can be
retargeted • Well-suited to describing objects with
complex topology• Easy to control localized shape• Level of details
• Demo
![Page 52: Shapes](https://reader035.vdocuments.site/reader035/viewer/2022070410/56814689550346895db3abc2/html5/thumbnails/52.jpg)
Geri’s game