Computer Graphics Zhen Jiang West Chester University Topics

  • Slides: 31
Download presentation
Computer Graphics Zhen Jiang West Chester University

Computer Graphics Zhen Jiang West Chester University

Topics • • • Curves 2 D and 3 D Clipping Projection and shadow

Topics • • • Curves 2 D and 3 D Clipping Projection and shadow Animation

Curves • Parametric cubic polynomial curves • Interpolation (interpolating polynomials) • Bezier Curve

Curves • Parametric cubic polynomial curves • Interpolation (interpolating polynomials) • Bezier Curve

Curves Where is the position for the next point? K=4 K=3 K=2 K=1

Curves Where is the position for the next point? K=4 K=3 K=2 K=1

Curves Where is the position for the next point? K=4 K=3 K=2 K=1

Curves Where is the position for the next point? K=4 K=3 K=2 K=1

Parametric cubic polynomial curves • P(u)=c 0+c 1 u+c 2 u 2+c 3 u

Parametric cubic polynomial curves • P(u)=c 0+c 1 u+c 2 u 2+c 3 u 3 = u. Tc – Where u. T = [1, u, u 2, u 3] and c. T= [c 0, c 1, c 2, c 3 ]

Interpolation Where is the position for any point on this curve? K=4 K=3 K=2

Interpolation Where is the position for any point on this curve? K=4 K=3 K=2 K=1

Interpolation • P 0=p(0) = c 0 P 1=p(1/3) P 2=p(2/3) P 3=p(1) =

Interpolation • P 0=p(0) = c 0 P 1=p(1/3) P 2=p(2/3) P 3=p(1) = c 0+c 1+c 2+c 3 • p. T=[p 0, p 1, p 2, p 3], A = [ • M=A-1= [ 1, 0, 0, 0 1, 1/3, (1/3)2, (1/3)3 1, 2/3, (2/3)2, (2/3)3 1, 1, 1, 1 1, 0, 0, 0 -5. 5, 9, -4. 5, 1 9, -22. 5, 18, -4. 5, 13. 5, -13. 5, 4. 5 ] • c=Mp • P(u)=u. TMp=b(u)p – where b(u) = [b 0(u), b 1(u), b 2(u), b 3(u), ] ]

Interpolation • • B 0(u)=-9/2(u-1/3)(u-2/3)(u-1) B 1(u)=27/2(u-2/3)u(u-1) B 2(u)=-27/2(u-1/3)u(u-1) B 3(u)=9/2(u-1/3)(u-2/3)u

Interpolation • • B 0(u)=-9/2(u-1/3)(u-2/3)(u-1) B 1(u)=27/2(u-2/3)u(u-1) B 2(u)=-27/2(u-1/3)u(u-1) B 3(u)=9/2(u-1/3)(u-2/3)u

Interpolation • Example – p. T=[p 0, p 1, p 2, p 3], where

Interpolation • Example – p. T=[p 0, p 1, p 2, p 3], where p 0= (0, 0), p 1=(1, 1), p 2, =(2, 3), p 3 =(3, 6) – (X, Y) when u=0? • B 0(0)=1, B 1(0)=0, B 2(0)=0, B 3(0)=0 • Since P(u)=b(u)p, X=b(u)Xp, Y=b(u)Yp, where Xp. T= [0, 1, 2, 3] and Yp. T= [0, 1, 3, 6]. 0 • X=[1, 0, 0, 0] [0, 1, 2, 3] T=[1, 0, 0, 0][ 1 ]=0 and Y=0; 2 3

Interpolation • Example – (X, Y) when u=1? • B 0(0)=0, B 1(0)=0, B

Interpolation • Example – (X, Y) when u=1? • B 0(0)=0, B 1(0)=0, B 2(0)=0, B 3(0)=1 • Since P(u)=b(u)p, X=b(u)Xp, Y=b(u)Yp, where Xp. T= [0, 1, 2, 3] and Yp. T= [0, 1, 3, 6]. 0 0 0, 0, 0, 1 3 0, 0, 0, 1 • X=[ ][ ]= and Y= [ ][ 1 ]= 6 ; 1 2 3 3 6

Interpolation • Example – (X, Y) when u=3? • B 0(0)=-56, B 1(0)=189, B

Interpolation • Example – (X, Y) when u=3? • B 0(0)=-56, B 1(0)=189, B 2(0)=-216, B 3(0)=84 • Since P(u)=b(u)p, X=b(u)Xp, Y=b(u)Yp, where Xp. T= [0, 1, 2, 3] and Yp. T= [0, 1, 3, 6]. • X=[ • Y= [ -56, 189, -216, 84 ][ 0 1 2 3 0 1 3 6 ]= 9 ]=45

Bezier Curve • Two endpoints – p 0(x 0, y 0) origin endpoint, source

Bezier Curve • Two endpoints – p 0(x 0, y 0) origin endpoint, source – p 3(x 3, y 3) destination endpoint • Two control points – p 1(x 1, y 1) – p 2(x 2, y 2) • t form 0 to 1 • New point p(x(t), y(t)) • p(t)=c 0+c 1 t+c 2 t 2+c 3 t 3 – Where c 0=p 0, c 1=3(p 1 -p 0), c 2=3(p 2 -p 1)-c 1, c 3=p 3 -p 0 -c 1 -c 2.

/* Three control point Bezier interpolation mu ranges from 0 to 1, start to

/* Three control point Bezier interpolation mu ranges from 0 to 1, start to end of the curve */ XYZ Bezier 3(XYZ p 1, XYZ p 2, XYZ p 3, double mu) { double mum 1, mum 12, mu 2; XYZ p; mu 2 = mu * mu; mum 1 = 1 - mu; mum 12 = mum 1 * mum 1; p. x = p 1. x * mum 12 + 2 * p 2. x * mum 1 * mu + p 3. x * mu 2; p. y = p 1. y * mum 12 + 2 * p 2. y * mum 1 * mu + p 3. y * mu 2; p. z = p 1. z * mum 12 + 2 * p 2. z * mum 1 * mu + p 3. z * mu 2; return(p); }

/* Four control point Bezier interpolation mu ranges from 0 to 1, start to

/* Four control point Bezier interpolation mu ranges from 0 to 1, start to end of curve */ XYZ Bezier 4(XYZ p 1, XYZ p 2, XYZ p 3, XYZ p 4, double mu) { double mum 1, mum 13, mu 3; XYZ p; mum 1 = 1 - mu; mum 13 = mum 1 * mum 1; mu 3 = mu * mu; p. x = mum 13*p 1. x + 3*mu*mum 1*p 2. x + 3*mu*mu*mum 1*p 3. x + mu 3*p 4. x; p. y = mum 13*p 1. y + 3*mu*mum 1*p 2. y + 3*mu*mu*mum 1*p 3. y + mu 3*p 4. y; p. z = mum 13*p 1. z + 3*mu*mum 1*p 2. z + 3*mu*mu*mum 1*p 3. z + mu 3*p 4. z; return(p); }

/* General Bezier curve Number of control points is n+1 0 <= mu <

/* General Bezier curve Number of control points is n+1 0 <= mu < 1 IMPORTANT, the last point is not computed */ XYZ Bezier(XYZ *p, int n, double mu) { int k, kn, nkn; double blend, muk, munk; XYZ b = {0. 0, 0. 0}; muk = 1; munk = pow(1 -mu, (double)n); nn--; if (kn > 1) { blend /= (double)kn; kn--; } if (nkn > 1) { blend /= (double)nkn; nkn--; } } /*end of while*/ b. x += p[k]. x * blend; b. y += p[k]. y * blend; b. z += p[k]. z * blend; } /*end of for*/ for (k=0; k<=n; k++) { return(b); nn = n; } kn = k; nkn = n - k; blend = muk * munk; muk *= mu; munk /= (1 -mu); while (nn >= 1) { blend *= nn;

2 D and 3 D (Transformation) • Coordinate system • Translation (p’=p+d) – (0,

2 D and 3 D (Transformation) • Coordinate system • Translation (p’=p+d) – (0, 0) by d(1, 2) – (1, 2) by d(2, 4) – (3, 6) by d(-3, -6) • Rotation cos Sin -sin cos p’ = [ -(3, 0) by =90˚ ]p=[ P (x’, y’) cos Sin -sin cos x y ][ ] • Scaling p’= r p (r > 1, big one; r < 1, small one) P (x, y)

2 D and 3 D (Transformation) • Mixed d For example, P’= r( P+d),

2 D and 3 D (Transformation) • Mixed d For example, P’= r( P+d), where =[ cos -sin Sin cos ] ! What will it be if P’= r P+d, r (P+d), (r. P+d), r(P+d), or r. P+d? ! Undo if P’= r( P+d), P’’=P= ’(1/r. P’-d), where ’=[ cos sin ] -sin P r cos If P’’’= ’(1/r)P’-d, is P’’’=P? How about ’(1/r)(P’-d), (1/r) ’P’-d, , …, ?

2 D and 3 D (Transformation) • Translation (p’=p+d) – (0, 0, 0) by

2 D and 3 D (Transformation) • Translation (p’=p+d) – (0, 0, 0) by d(1, 2, 3) – (1, 2, 3) by d(2, 4, 6) – (3, 6, 9) by d(-3, -6, -9) • Rotation p’ = R p ( Rz cos 0 1 Ry =[ 0 -sin 0 • Scaling cos -sin 0 =[ sin cos 0 ], 0 0 1 sin 0 ], cos Rx =[ 1 0 0 2 0 cos -sin], 3 0 sin cos p’= r p (r > 1, big one; r < 1, small one) • Mixed

2 D and 3 D (Transformation) Y Y P Rz( ) P Rx( )

2 D and 3 D (Transformation) Y Y P Rz( ) P Rx( ) x x P Ry( ) Z Z

2 D and 3 D (Transformation) • 45 degree about the line through origin

2 D and 3 D (Transformation) • 45 degree about the line through origin (0, 0, 0) and the point(1, 2, 3) with a fixed point of (4, 6, 5): gl. Matrix. Mode (GL_MODELVIEW); gl. Load. Identity(); gl. Translatef(4. 0, 6. 0, 5. 0); gl. Rotatef(45. 0, 1. 0, 2. 0, 3. 0); gl. Translatef(-4. 0, -6. 0, -5. 0);

2 D and 3 D (Transformation) Y’ Y P’ P 1 P X Z

2 D and 3 D (Transformation) Y’ Y P’ P 1 P X Z Z’ X P 1= P+d Z X’

2 D and 3 D (Transformation) P’ 1 Y’ Y’ P’ Y (1, 2,

2 D and 3 D (Transformation) P’ 1 Y’ Y’ P’ Y (1, 2, 3) X’ (0, 0, 0) Y P 2 P 1 Z’ X P 2= Rz P 1= Rz(P+d) Z Z X’

2 D and 3 D (Transformation) P’ 2 P’ 1 Y’ Y’ X Y

2 D and 3 D (Transformation) P’ 2 P’ 1 Y’ Y’ X Y P 2 Z’ X’ Y Z’ X P 3= Rx P 2= Rx. Rz(P+d) Z P 3 Z X’

2 D and 3 D (Transformation) P’ 3 Y Y’ X Y P 3

2 D and 3 D (Transformation) P’ 3 Y Y’ X Y P 3 X Y X’ X P’ 2=? P’ 3 P’ 1=? P’ 2 Z’ Z P’ 3= Ry(45) P 3= Ry(45)Rx. Rz(P+d) P’=? P’ 1 Z Z Need solution? Check “undo” in 2 Ds.

Clipping

Clipping

Clipping

Clipping

Clipping (6, 18) (2, 10) (22, 18) (19, 9) (6, 6) (14, 4) (2,

Clipping (6, 18) (2, 10) (22, 18) (19, 9) (6, 6) (14, 4) (2, 2) (22, 6) (19, 2)

Clipping • Step 1: Start from a vertex – If it is a point

Clipping • Step 1: Start from a vertex – If it is a point inside window, save it as a vertex in the new polygon; status=1; – Otherwise, status=0. • Step 2: Pick the edge starting from this vertex until there is no new vertex of this polygon – If it has an intersection [0, 1) with any edge of window • status=!status, save the intersection as a vertex for the new polygon; • If status==1, save the endpoint of this segment as a vertex for new polygon; • Repeat step 2, start from the endpoint of this segment. – If it has no intersection with any edge of window • If status ==1, save the endpoint of this segment as a vertex for new polygon; • Repeat step 2, start from the endpoint of this segment. • Step 3: Connect all the vertices of the new polygon

Clipping • Does a segment from A(x 1, y 1) to B(x 2, y

Clipping • Does a segment from A(x 1, y 1) to B(x 2, y 2) have an intersection with an edge from P(x 3, y 3) to Q(x 4, y 4)? y up x 3=x 4 I=(1 - )A+ B, 0 ≤ ≤ 1 ylo = (x 3 -x 1)/(x 2 -x 1) y= (1 - )y 1+ y 2 0 ≤ ≤ 1 && ylo ≤ y ≤yup = (y 3 -y 1)/(y 2 -y 1) x= (1 - )x 1+ x 2 xmin y 3=y 4 xmax 0 ≤ ≤ 1 && xmin ≤ x ≤xmax