270 likes | 277 Views
CS148: Introduction to Computer Graphics and Imaging Triangles. CS148 Lecture 3. How to draw a triangle on the screen. Rasterization. CS148 Lecture 3. Pixel Coordinates. Window edges at integers. y. (4,3). Pixels inside window. (1,1). (0,1). (0,0). (1,0). x. Pixel Coordinates.
E N D
CS148: Introduction to Computer Graphics and Imaging Triangles CS148 Lecture 3
How to draw a triangle on the screen Rasterization CS148 Lecture 3
Pixel Coordinates Window edges at integers y (4,3) Pixels inside window (1,1) (0,1) (0,0) (1,0) x
Pixel Coordinates OpenGL: Pixel centers correspond to half-integer coordinates y (.5, 1.5) (1.5, 1.5) (1.5, .5) (.5, .5) Note: Other graphics packages may use a different convention x
Triangle Rasterization Rule Output fragment if pixel center is inside the triangle CS148 Lecture 3
Triangle Rasterization rasterize( vert v[3] ) { for( int y=0; y<YRES; y++ ) for( int x=0; x<XRES; x++ ) if( inside3(v,x,y) ) fragment(x,y); } v0 l1 l2 v2 l0 v1 CS148 Lecture 3
Normal to the Line CS148 Lecture 3
Line Equation This equation must be true for all point p on the line CS148 Lecture 3
Line Divides Plane into 2 Half-Spaces Normal n points to the right of the line; Inside (negative values) to the left of the line. CS148 Lecture 3
Make a Line from Two Vertices makeline( vert& v0, vert& v1, line& l ) { l.a = v1.y - v0.y; l.b = v0.x - v1.x; l.c = -(l.a * v0.x + l.b * v0.y); } v1 v0 l CS148 Lecture 3
Triangle Orientation v1 v2 v0 v0 v1 v2 CW (Back Facing) CCW (Front Facing) Convention: Inside a CCW polygon is equivalent to inside every lines of the polygon (on their left) CS148 Lecture 3
rasterize( vert v[3] ) { line l0, l1, l2; makeline(v[0],v[1],l2); makeline(v[1],v[2],l0); makeline(v[2],v[0],l1); for( y=0; y<YRES; y++ ) { for( x=0; x<XRES; x++ ) { e0 = l0.a * x + l0.b * y + l0.c; e1 = l1.a * x + l1.b * y + l1.c; e2 = l2.a * x + l2.b * y + l2.c; if( e0<=0 && e1<=0 && e2<=0 ) fragment(x,y); } } } Point Inside Triangle Test v0 l1 l2 v2 l0 v1 CS148 Lecture 3
Singularities • Singularities: Edges that touch pixels (e == 0) • Causes two fragments to be generated • Wasted effort drawing duplicated fragments • Problems with transparency (later lecture) • Not including singularities (e < 0) causes gaps CS148 Lecture 3
int shadow( line l ) { return (l.a>0) || (l.a == 0 && l.b > 0); } int inside( value e, line l ) { return (e == 0) ? !shadow(l) : (e < 0); } Handling Singularities • Create shadowed edges (thick lines) • Don’t draw pixels on shadowed edges • Solid drawn; hollow not drawn CS148 Lecture 3
rasterize( vert v[3] ) { line l0, l1, l2; makeline(v[0],v[1],l2); makeline(v[1],v[2],l0); makeline(v[2],v[0],l1); for( y=0; y<YRES; y++ ) { for( x=0; x<XRES; x++ ) { e0 = l0.a * x + l0.b * y + l0.c; e1 = l1.a * x + l1.b * y + l1.c; e2 = l2.a * x + l2.b * y + l2.c; if( inside(e0,l0)&&inside(e1,l1)&&inside(e2,l2) ) fragment(x,y); } } } Better Point Inside Triangle Test v0 l1 l2 v2 l0 v1 CS148 Lecture 3
Compute Bounding Rectangle (BBox) bound3( vert v[3], bbox& b ) { b.xmin = ceil(min(v[0].x, v[1].x, v[2].x)); b.xmax = ceil(max(v[0].x, v[1].x, v[2].x)); b.ymin = ceil(min(v[0].y, v[1].y, v[2].y)); b.ymax = ceil(max(v[0].y, v[1].y, v[2].y)); } Calculate tight bound around the triangle Round coordinates upward (ceil) to the nearest integer CS148 Lecture 3
Compute Bounding Rectangle (BBox) bound3( vert v[3], bbox& b ) { b.xmin = ceil(min(v[0].x, v[1].x, v[2].x)); b.xmax = ceil(max(v[0].x, v[1].x, v[2].x)); b.ymin = ceil(min(v[0].y, v[1].y, v[2].y)); b.ymax = ceil(max(v[0].y, v[1].y, v[2].y)); } Tested points indicated by filled circles Don’t need to test hollow circles CS148 Lecture 3
rasterize( vert v[3] ) { bbox b; bound3(v, b); line l0, l1, l2; makeline(v[0],v[1],l2); makeline(v[1],v[2],l0); makeline(v[2],v[0],l1); for( y=b.ymin; y<b.ymax, y++ ) { for( x=b.xmin; x<b.xmax, x++ ) { e0 = l0.A * x + l0.B * y + l0.C; e1 = l1.A * x + l1.B * y + l1.C; e2 = l2.A * x + l2.B * y + l2.C; if( inside(e0,l0)&&inside(e1,l1)&&inside(e2,l2) ) fragment(x,y); } } } More Efficient Bounding Box Version CS148 Lecture 3
Interpolation Convert discrete values to a continuous function by filling in “in between” values CS148 Lecture 3
Linear Interpolation 0 1 CS148 Lecture 3
Barycentric Interpolation Edge Triangle CS148 Lecture 3
Barycentric Interpolation on Triangles Can be used to interpolate colors r = a0 r0 + a1 r1 + a2 r2 g = a0 g0 + a1 g1 + a2 g2 b = a0 b0 + a1 b1 + a2 b2 Can be used to interpolate texture coordinates u = a0 u0 + a1 u1 + a2 u2 v = a0 v0 + a1 v1 + a2 v2 Can be used to interpolate z or depth z = a0 z0 + a1 z1 + a2 z2 Can be used to interpolate normals… CS148 Lecture 3
Finding Barycentric Coordinates CS148 Lecture 3
How to Compute Triangle Area C B A CS148 Lecture 3
Barycentric Coordinates: Linear System Determinant of matrix is equal to zero if the points are co-linear CS148 Lecture 3
Barycentric Coordinates: Basis Vectors CS148 Lecture 3
Barycentric Coordinates vs. Basis Vector Coordinates A point p inside triangle p0p1p2 can be expressed using barycentric coordinates as Point p can also be expressed in the basis vector coordinates (of last slide as) Therefore, we get the relations CS148 Lecture 3