Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS148: Introduction to Computer Graphics and Imaging

Similar presentations


Presentation on theme: "CS148: Introduction to Computer Graphics and Imaging"— Presentation transcript:

1 CS148: Introduction to Computer Graphics and Imaging
Triangles CS148 Lecture 3

2 How to draw a triangle on the screen
Rasterization CS148 Lecture 3

3 Window edges at integers
Pixel Coordinates Window edges at integers y (4,3) Pixels inside window (0,1) (1,1) (0,0) (1,0) x

4 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

5 Triangle Rasterization Rule
Output fragment if pixel center is inside the triangle CS148 Lecture 3

6 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 l2 l1 Get rid of bounds l0 v2 v1 CS148 Lecture 3

7 Normal to the Line CS148 Lecture 3 Break into three slides.
First, just p0, p1, and t Second, define Perp Third, Explain the function Perp(t) Perp((x,y)) = (-y,x) Line orientation is backwards; inside is normally to the left CS148 Lecture 3

8 Line Equation This equation must be true for all point p on the line
Convention that n points to the left as we move from p0 to p1 Half-plane equation Show this derivation This equation must be true for all point p on the line CS148 Lecture 3

9 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

10 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.y * v0.x– v1.x*v0.y If inside all three lines int mkedgev( e, x1, y1, x2, y2 ) struct edge *e; value x1, y1; value x2, y2; { e->a = -(y1 - y2); e->b = (x1 - x2); e->c = -(e->a * x1 + e->b * y1); e->v = 0.; } v0 v1 l CS148 Lecture 3

11 Triangle Orientation v1 v2 v0 v1 v0 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

12 Point Inside Triangle Test
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); } v0 l2 l1 l0 v2 v1 CS148 Lecture 3

13 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

14 Handling Singularities
Create shadowed edges (thick lines) Don’t draw pixels on shadowed edges Solid drawn; hollow not drawn 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); CS148 Lecture 3

15 Better Point Inside Triangle Test
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); } v0 l2 l1 l0 v2 v1 CS148 Lecture 3

16 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

17 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

18 More Efficient Bounding Box Version
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); } CS148 Lecture 3

19 Interpolation Convert discrete values to a continuous function by filling in “in between” values CS148 Lecture 3

20 Linear Interpolation 0 1 CS148 Lecture 3
let’s say we have points y1 and y2 and we want to move smoothly between the two. one way to do this is via the line connecting the two points CS148 Lecture 3

21 Barycentric Interpolation
Edge talk about balance point of the triangle negative mass? Triangle CS148 Lecture 3

22 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

23 Finding Barycentric Coordinates
CS148 Lecture 3

24 How to Compute Triangle Area
B A CS148 Lecture 3

25 Barycentric Coordinates: Linear System
Determinant of matrix is equal to zero if the points are co-linear CS148 Lecture 3

26 Barycentric Coordinates: Basis Vectors
CS148 Lecture 3

27 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


Download ppt "CS148: Introduction to Computer Graphics and Imaging"

Similar presentations


Ads by Google