34
Shapes Digital Image Synthesis Yung-Yu Chuang 9/27/2005 with slides by Pat Hanrahan

Shapes - 國立臺灣大學 · 2005. 10. 4. · Shapes • Shape: raw geometry properties of the primitive • Primitive=Shape+Material • Source code in core/shape.* and shapes/*

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Shapes - 國立臺灣大學 · 2005. 10. 4. · Shapes • Shape: raw geometry properties of the primitive • Primitive=Shape+Material • Source code in core/shape.* and shapes/*

Shapes

Digital Image SynthesisYung-Yu Chuang9/27/2005

with slides by Pat Hanrahan

Page 2: Shapes - 國立臺灣大學 · 2005. 10. 4. · Shapes • Shape: raw geometry properties of the primitive • Primitive=Shape+Material • Source code in core/shape.* and shapes/*

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

Page 3: Shapes - 國立臺灣大學 · 2005. 10. 4. · Shapes • Shape: raw geometry properties of the primitive • Primitive=Shape+Material • Source code in core/shape.* and shapes/*

Shape

class Shape : public ReferenceCounted {public:

<Shape Interface>const Transform ObjectToWorld, WorldToObject;const bool reverseOrientation,

transformSwapsHandedness;}

• All shapes are defined in object coordinate space

Page 4: Shapes - 國立臺灣大學 · 2005. 10. 4. · Shapes • Shape: raw geometry properties of the primitive • Primitive=Shape+Material • Source code in core/shape.* and shapes/*

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)

Page 5: Shapes - 國立臺灣大學 · 2005. 10. 4. · Shapes • Shape: raw geometry properties of the primitive • Primitive=Shape+Material • Source code in core/shape.* and shapes/*

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

Page 6: Shapes - 國立臺灣大學 · 2005. 10. 4. · Shapes • Shape: raw geometry properties of the primitive • Primitive=Shape+Material • Source code in core/shape.* and shapes/*

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

Page 7: Shapes - 國立臺灣大學 · 2005. 10. 4. · Shapes • Shape: raw geometry properties of the primitive • Primitive=Shape+Material • Source code in core/shape.* and shapes/*

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)

Page 8: Shapes - 國立臺灣大學 · 2005. 10. 4. · Shapes • Shape: raw geometry properties of the primitive • Primitive=Shape+Material • Source code in core/shape.* and shapes/*

Sphere

Page 9: Shapes - 國立臺灣大學 · 2005. 10. 4. · Shapes • Shape: raw geometry properties of the primitive • Primitive=Shape+Material • Source code in core/shape.* and shapes/*

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

Page 10: Shapes - 國立臺灣大學 · 2005. 10. 4. · Shapes • Shape: raw geometry properties of the primitive • Primitive=Shape+Material • Source code in core/shape.* and shapes/*

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

Page 11: Shapes - 國立臺灣大學 · 2005. 10. 4. · Shapes • Shape: raw geometry properties of the primitive • Primitive=Shape+Material • Source code in core/shape.* and shapes/*

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

Page 12: Shapes - 國立臺灣大學 · 2005. 10. 4. · Shapes • Shape: raw geometry properties of the primitive • Primitive=Shape+Material • Source code in core/shape.* and shapes/*

Geometric solution

1. Origin inside? 2222 rooo zyx >++

Page 13: Shapes - 國立臺灣大學 · 2005. 10. 4. · Shapes • Shape: raw geometry properties of the primitive • Primitive=Shape+Material • Source code in core/shape.* and shapes/*

Geometric solution

2. find the closest point, t=-O‧D if t<0 and O outside return false

t

t

Page 14: Shapes - 國立臺灣大學 · 2005. 10. 4. · Shapes • Shape: raw geometry properties of the primitive • Primitive=Shape+Material • Source code in core/shape.* and shapes/*

Geometric solution

3. find the distance to the origin, d2=O2-t2

if s2=r2-d2<0 return false;

t

rrd

O

s

Page 15: Shapes - 國立臺灣大學 · 2005. 10. 4. · Shapes • Shape: raw geometry properties of the primitive • Primitive=Shape+Material • Source code in core/shape.* and shapes/*

Geometric solution

4. calculate intersection distance,if (origin outside) then t-selse t+s

t

rd

O

s

t

rd

O

s

Page 16: Shapes - 國立臺灣大學 · 2005. 10. 4. · Shapes • Shape: raw geometry properties of the primitive • Primitive=Shape+Material • Source code in core/shape.* and shapes/*

Sphere intersection

• Use algebraic solution, why?• Consider numeric stability

Page 17: Shapes - 國立臺灣大學 · 2005. 10. 4. · Shapes • Shape: raw geometry properties of the primitive • Primitive=Shape+Material • Source code in core/shape.* and shapes/*

Partial sphere

• Have to test sphere intersection against clipping parameters

• Partial derivatives

• Area

)0,,( maxmax xyup φφ−=∂∂

)sin,sin,cos)(( minmax θφφθθ rzzvp

−−=∂∂

)( minmaxmax zzrA −= φ

Page 18: Shapes - 國立臺灣大學 · 2005. 10. 4. · Shapes • Shape: raw geometry properties of the primitive • Primitive=Shape+Material • Source code in core/shape.* and shapes/*

Cylinder

maxφφ u=φcosrx =φsinry =

)( minmaxmin zzvzz −+=

Page 19: Shapes - 國立臺灣大學 · 2005. 10. 4. · Shapes • Shape: raw geometry properties of the primitive • Primitive=Shape+Material • Source code in core/shape.* and shapes/*

Cylinder

Page 20: Shapes - 國立臺灣大學 · 2005. 10. 4. · Shapes • Shape: raw geometry properties of the primitive • Primitive=Shape+Material • Source code in core/shape.* and shapes/*

Cylinder (intersection)

222 ryx =+

( ) ( ) 222 rtdotdo yyxx =+++

02 =++ CBtAt22yx ddA +=

)(2 yyxx ododB +=222 rooC yx −+=

Page 21: Shapes - 國立臺灣大學 · 2005. 10. 4. · Shapes • Shape: raw geometry properties of the primitive • Primitive=Shape+Material • Source code in core/shape.* and shapes/*

Disk

maxφφ u=φcos))1(( vrrvx i +−=

hz =φsin))1(( vrrvy i +−=

Page 22: Shapes - 國立臺灣大學 · 2005. 10. 4. · Shapes • Shape: raw geometry properties of the primitive • Primitive=Shape+Material • Source code in core/shape.* and shapes/*

Disk

Page 23: Shapes - 國立臺灣大學 · 2005. 10. 4. · Shapes • Shape: raw geometry properties of the primitive • Primitive=Shape+Material • Source code in core/shape.* and shapes/*

Disk (intersection)

h-Oz

h

t

Dz Dzz Oh

tDD

−=

)( zz

OhDD

t −=

z

z

DOh

Dtt −=='

Page 24: Shapes - 國立臺灣大學 · 2005. 10. 4. · Shapes • Shape: raw geometry properties of the primitive • Primitive=Shape+Material • Source code in core/shape.* and shapes/*

Other quadrics

Page 25: Shapes - 國立臺灣大學 · 2005. 10. 4. · Shapes • Shape: raw geometry properties of the primitive • Primitive=Shape+Material • Source code in core/shape.* and shapes/*

Triangle mesh

The most commonly used shape. In pbrt, it can be supplied by users or tessellated from other shapes.

Page 26: Shapes - 國立臺灣大學 · 2005. 10. 4. · Shapes • Shape: raw geometry properties of the primitive • Primitive=Shape+Material • Source code in core/shape.* and 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.

Page 27: Shapes - 國立臺灣大學 · 2005. 10. 4. · Shapes • Shape: raw geometry properties of the primitive • Primitive=Shape+Material • Source code in core/shape.* and shapes/*

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));

}

Page 28: Shapes - 國立臺灣大學 · 2005. 10. 4. · Shapes • Shape: raw geometry properties of the primitive • Primitive=Shape+Material • Source code in core/shape.* and shapes/*

1. Intersect ray with plane2. Check if point is inside triangle

Ray triangle intersection

Page 29: Shapes - 國立臺灣大學 · 2005. 10. 4. · Shapes • Shape: raw geometry properties of the primitive • Primitive=Shape+Material • Source code in core/shape.* and shapes/*

Algebraic Method0:

: 0

=+⋅+=

dNPPlanetVPPRay

Substituting for P, we get:

( ) 00 =+⋅+ dNtVP

Solution:

( )( )NV

dNPt⋅

+⋅−= 0

tVPP += 0

Ray plane intersection

Page 30: Shapes - 國立臺灣大學 · 2005. 10. 4. · Shapes • Shape: raw geometry properties of the primitive • Primitive=Shape+Material • Source code in core/shape.* and shapes/*

Algebraic Method

( )( )FALSEreturn

dNPifNPd

NNormalizeVVNPTVPTV

011

101

1

121

22

11

<+⋅⋅−=

×=−=−=

For each side of triangle:

end

Ray triangle intersection I

Page 31: Shapes - 國立臺灣大學 · 2005. 10. 4. · Shapes • Shape: raw geometry properties of the primitive • Primitive=Shape+Material • Source code in core/shape.* and shapes/*

Parametric Method

( ) ( )

( ) ( )triangleinsideisPthen

andif

TTTTP

Compute

0.10.00.10.0

:,

1312

≤≤≤≤

−+−=

βα

βα

βα

Ray triangle intersection II

Page 32: Shapes - 國立臺灣大學 · 2005. 10. 4. · Shapes • Shape: raw geometry properties of the primitive • Primitive=Shape+Material • Source code in core/shape.* and shapes/*

Ray triangle intersection III

Page 33: Shapes - 國立臺灣大學 · 2005. 10. 4. · Shapes • Shape: raw geometry properties of the primitive • Primitive=Shape+Material • Source code in core/shape.* and shapes/*

Fast minimum storage intersection

210)1( vVuVVvutDO ++−−=+1 and 0, ≤+≥ vuvu

[ ] 00201 VOvut

VVVVD −=⎥⎥⎥

⎢⎢⎢

⎡−−−

Page 34: Shapes - 國立臺灣大學 · 2005. 10. 4. · Shapes • Shape: raw geometry properties of the primitive • Primitive=Shape+Material • Source code in core/shape.* and shapes/*

Fast minimum storage intersection

011 VVE −= 022 VVE −= 0VOT −=

2EDP ×= 1ETQ ×=

⎥⎥⎥

⎢⎢⎢

⋅⋅⋅

⋅=

⎥⎥⎥

⎢⎢⎢

DQTPEQ

EPvut 2

1

1