370 likes | 607 Views
Computer Graphics. Zhen Jiang West Chester University. Topics. Curves 2D and 3D Clipping Projection and shadow Animation. Curves. Parametric cubic polynomial curves Interpolation (interpolating polynomials) Bezier Curve. Curves. Where is the position for the next point? K=4. K=3.
E N D
Computer Graphics Zhen Jiang West Chester University
Topics • Curves • 2D and 3D • Clipping • Projection and shadow • Animation
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
Parametric cubic polynomial curves • P(u)=c0+c1u+c2u2+c3u3 = uTc • Where uT = [1, u, u2, u3] and cT= [c0, c1, c2, c3 ]
Interpolation Where is the position for any point on this curve? K=4 K=3 K=2 K=1
Interpolation • P0=p(0) = c0 P1=p(1/3) P2=p(2/3) P3=p(1) = c0+c1+c2+c3 • pT=[p0,p1,p2,p3], A = • M=A-1= • c=Mp • P(u)=uTMp=b(u)p • where b(u) = [b0(u), b1(u), b2(u), b3(u), ] 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 -4.5, 13.5, -13.5, 4.5 [ ]
Interpolation • B0(u)=-9/2(u-1/3)(u-2/3)(u-1) • B1(u)=27/2(u-2/3)u(u-1) • B2(u)=-27/2(u-1/3)u(u-1) • B3(u)=9/2(u-1/3)(u-2/3)u
Interpolation • Example • pT=[p0,p1,p2,p3], where p0= (0,0), p1=(1,1), p2,=(2,3), p3 =(3, 6) • (X,Y) when u=0? • B0(0)=1, B1(0)=0, B2(0)=0, B3(0)=0 • Since P(u)=b(u)p, X=b(u)Xp, Y=b(u)Yp, where XpT= [0,1,2,3] and YpT= [0,1,3,6]. • X=[1,0,0,0] [0,1,2,3] T=[1,0,0,0][ ]=0 and Y=0; 0 1 2 3
Interpolation • Example • (X,Y) when u=1? • B0(0)=0, B1(0)=0, B2(0)=0, B3(0)=1 • Since P(u)=b(u)p, X=b(u)Xp, Y=b(u)Yp, where XpT= [0,1,2,3] and YpT= [0,1,3,6]. • X=[ ][ ]= and Y= [ ][ ]= ; 0 1 3 6 0 1 2 3 0,0,0,1 3 0,0,0,1 6
Interpolation • Example • (X,Y) when u=3? • B0(0)=-56, B1(0)=189, B2(0)=-216, B3(0)=84 • Since P(u)=b(u)p, X=b(u)Xp, Y=b(u)Yp, where XpT= [0,1,2,3] and YpT= [0,1,3,6]. • X=[ ][ ]= • Y= [ ][ ]= 0 1 2 3 -56,189,-216,84 9 0 1 3 6 45 -56,189,-216,84
Bezier Curve • Two endpoints • p0(x0,y0) origin endpoint, source • p3(x3,y3) destination endpoint • Two control points • p1(x1,y1) • p2(x2,y2) • t form 0 to 1 • New point p(x(t), y(t)) • p(t)=c0+c1t+c2t2+c3t3 • Where c0=p0, c1=3(p1-p0), c2=3(p2-p1)-c1, c3=p3-p0-c1-c2.
/* Three control point Bezier interpolation mu ranges from 0 to 1, start to end of the curve */ XYZ Bezier3(XYZ p1,XYZ p2,XYZ p3,double mu) { double mum1,mum12,mu2; XYZ p; mu2 = mu * mu; mum1 = 1 - mu; mum12 = mum1 * mum1; p.x = p1.x * mum12 + 2 * p2.x * mum1 * mu + p3.x * mu2; p.y = p1.y * mum12 + 2 * p2.y * mum1 * mu + p3.y * mu2; p.z = p1.z * mum12 + 2 * p2.z * mum1 * mu + p3.z * mu2; return(p); }
/* Four control point Bezier interpolation mu ranges from 0 to 1, start to end of curve */ XYZ Bezier4(XYZ p1,XYZ p2,XYZ p3,XYZ p4,double mu) { double mum1,mum13,mu3; XYZ p; mum1 = 1 - mu; mum13 = mum1 * mum1 * mum1; mu3 = mu * mu * mu; p.x = mum13*p1.x + 3*mu*mum1*mum1*p2.x + 3*mu*mu*mum1*p3.x + mu3*p4.x; p.y = mum13*p1.y + 3*mu*mum1*mum1*p2.y + 3*mu*mu*mum1*p3.y + mu3*p4.y; p.z = mum13*p1.z + 3*mu*mum1*mum1*p2.z + 3*mu*mu*mum1*p3.z + mu3*p4.z; return(p); }
/* 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,nn,nkn; double blend,muk,munk; XYZ b = {0.0, 0.0, 0.0}; muk = 1; munk = pow(1-mu,(double)n); for (k=0;k<=n;k++) { nn = n; kn = k; nkn = n - k; blend = muk * munk; muk *= mu; munk /= (1-mu); while (nn >= 1) { blend *= nn; 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*/ return(b); }
2D and 3D (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 p’ = [ ] p = [ ] [ ] -(3,0) by =90˚ • Scaling p’= r p (r > 1, big one; r < 1, small one) P (x’,y’) P (x,y) cos -sin Sin cos cos -sin Sin cos x y
2D and 3D (Transformation) • Mixed d For example, P’= r(P+d), where =[ ] ! What will it be if P’= rP+d, r(P+d), (rP+d), r(P+d), or rP+d? ! Undo if P’= r(P+d), P’’=P= ’(1/rP’-d), where ’=[ ] If P’’’= ’(1/r)P’-d, is P’’’=P’’=P? How about ’(1/r)(P’-d), (1/r)’P’-d, , …, ? P cos -sin Sin cos r cos sin -sin cos
2D and 3D (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 =[ ], Rx =[ ], Ry =[ ], • Scaling p’= r p (r > 1, big one; r < 1, small one) • Mixed • 0 0 • 0 cos -sin • 0 sin cos cos -sin 0 sin cos 0 0 0 1 cos 0 sin 0 1 0 -sin 0 cos
2D and 3D (Transformation) Y Y P P Rz() Rx() x x P Ry() Z Z
2D and 3D (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): glMatrixMode (GL_MODELVIEW); glLoadIdentity(); glTranslatef(4.0, 6.0, 5.0); glRotatef(45.0, 1.0, 2.0, 3.0); glTranslatef(-4.0, -6.0, -5.0);
2D and 3D (Transformation) Y’ Y P’ Y P’ X’ P1 P X Z’ X P1= P+d Z Z
2D and 3D (Transformation) P’1 Y’ Y’ P’ Y (1,2,3) (0,0,0) Y X’ X’ P2 P1 Z’ Z’ X X P2= Rz P1= Rz(P+d) Z Z
2D and 3D (Transformation) P’2 P’1 Y’ Y’ X P3 Y X’ X’ Y P2 Z’ Z’ X Z P3= Rx P2= RxRz(P+d) Z
2D and 3D (Transformation) P’3 Y Y’ X X Y Y P3 X’ X P’2=?P’3 P’1=?P’2 Z’ Z P’=?P’1 Need solution? Check “undo” in 2Ds. Z Z P’3= Ry(45) P3= Ry(45)RxRz(P+d)
Clipping (6,18) (22,18) (2,10) (19,9) (6,6) (22,6) (14,4) (2,2) (19,2)
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(x1,y1) to B(x2,y2) have an intersection with an edge from P(x3,y3) to Q(x4,y4)? yup • = (x3-x1)/(x2-x1) y= (1-)y1+ y2 x3=x4 0 ≤ ≤1 && ylo ≤ y ≤yup I=(1-)A+ B, 0 ≤ ≤1 ylo • = (y3-y1)/(y2-y1) x= (1-)x1+ x2 xmin xmax y3=y4 0 ≤ ≤1 && xmin ≤ x ≤xmax