Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation
Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement.Téléchargez la présentation en cliquant sur ce lien.Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.
Display and OpenGL • 1280x1024 • 75 refreshes / second (from top to bottom in left right order) • Memory elements called pixels
Display and OpenGL • OO programming • Introduced but not required in this class • Application Programmer’s Interfaces (API) • Is shielded from the details of both the hardware and software implementation of the graphics library. • !! Match the conceptual model that we will introduce in this class
Point , line, polygon • Point • Line • Introduction • (0,0) -> (dx,dy) • (0,cy)->(dx,cy+dy) • (cx,cy)->(dx+cx,dy+cy) • Brensenham’s algorithm • Complete Brensenham’s algorithm • Polygon • Vertices and edges • Simple polygon • What’s inside? • fill
Point , line, polygon (line) • (0,0) -> (dx,dy) //int dx, dy int x, Upper_y, Lower_y; float y, d1, d2; for ( x = 0 ; x <= dx ; x++) { y= Upper_y= Lower_y= d1= d2= glBegin(GL_POINTS); if(d1<d2) glVertex2i(x, Lower_y); else glVertex2i(x, Upper_y); glEnd(); } dy y x float (dy*x) / float(dx); int(y)+1; int(y); y-Lower_y; Upper_y-y; (0,0) dx For example, when x=0; y=0; Upper_y=1; Lower_y=0; d1=0; d2=1; d1<d2 glVertex2i(0,0);
Point , line, polygon (line) //int dx, dy int x, Upper_y, Lower_y; float y, d1, d2; for ( x = 0 ; x <= dx ; x++) { y=float (dy*x) / float(dx); Upper_y=int(y)+1; Lower_y=int(y); d1=y-Lower_y; d2=Upper_y-y; glBegin(GL_POINTS); if(d1<d2) glVertex2i(x, Lower_y); else glVertex2i(x, Upper_y); glEnd(); } • Check (0,0) – (7,2)
Point , line, polygon (line) • (0,cy)->(dx,cy+dy) //int dx, dy int x, Upper_y, Lower_y; float y, d1, d2; for ( x = 0 ; x <= dx ; x++) { y=float (dy*x) / float(dx); Upper_y=int(y)+1+cy; Lower_y=int(y)+cy; d1=y+cy-Lower_y; d2=Upper_y-(y+cy); glBegin(GL_POINTS); if(d1<d2) glVertex2i(x, Lower_y); else glVertex2i(x, Upper_y); glEnd(); } dy y x (0,cy) dx
Point , line, polygon (line) //int dx, dy, cy int x, Upper_y, Lower_y; float y, d1, d2; for ( x = 0 ; x <= dx ; x++) { y=float (dy*x) / float(dx); Upper_y=int(y)+1+cy; Lower_y=int(y)+cy; d1=y+cy-Lower_y; d2=Upper_y-(y+cy); glBegin(GL_POINTS); if(d1<d2) glVertex2i(x, Lower_y); else glVertex2i(x, Upper_y); glEnd(); } • Check (0,-2) – (7,1)
Point , line, polygon (line) • (cx,cy)->(dx+cx,dy+cy) //int dx, dy, cx, cy int x, Upper_y, Lower_y; float y, d1, d2; for ( x = cx ; x <= dx+cx ; x++) { y=float (dy*(x-cx)) / float(dx); Upper_y=int(y)+1+cy; Lower_y=int(y)+cy; d1=y+cy-Lower_y; d2=Upper_y-(y+cy); glBegin(GL_POINTS); if(d1<d2) glVertex2i(x, Lower_y); else glVertex2i(x, Upper_y); glEnd(); } dy y x (cx,cy) dx
Point , line, polygon (line) //int dx, dy, cx, cy int x, Upper_y, Lower_y; float y, d1, d2; for ( x = cx ; x <= cx+dx ; x++) { y=float (dy*(x-cx)) / float(dx); Upper_y=int(y)+1+cy; Lower_y=int(y)+cy; d1=y+cy-Lower_y; d2=Upper_y-(y+cy); glBegin(GL_POINTS); if(d1<d2) glVertex2i(x, Lower_y); else glVertex2i(x, Upper_y); glEnd(); } • Check (3,-1) – (10,1)
Point , line, polygon (line) • Brensenham’s Algorithm • Given (x,y), must choose from either (x+1, y+1) or (x+1, y) (x+1,y+1) (x,y) d2 y+1 y d1 x x+1 (x+1,y)
Point , line, polygon (line) • Brensenham’s Algorithm (m=dy/dx) • d1-d2 = 2m(x+1)-2y+2c-1 • For integer calculations, use p = 2dy x-2dx y+2dy+2c dx-dx • d1<d2 =>p negative =>y • d1>d2 =>p positive=>y+1
Point , line, polygon (line) //int dx, dy, cx, cy int x, p,y; y=cy; for ( x = cx ; x <= cx+dx ; x++) { p=2*dy*x -2*dx*y+2*dy+2*c*dx-dx; if(p>=0) y++; glBegin(GL_POINTS); glVertex2i(x, y); glEnd(); } • Check (3,-1) – (10,1)
if(m>1){ if(x2>x1) {cx=x1; cy=y1;} else {cx=x2;cy=y2;dx=-dx; dy=-dy;} c=y2-x2(y1-y2)/(x1-x2); • x=cx; • for ( y = cy ; y >= cy+dy ; y++) { • p=2*dx*y -2*dy*x+2*dx-2*c*dx-dy; • if(p>=0) x++; • glVertex2i(x, y); } } if(m<-1){ if(x2>x1) {cx=x1; cy=y1;} else {cx=x2;cy=y2;dx=-dx; dy=-dy;} c=y2-x2(y1-y2)/(x1-x2); • x=cx; • for ( y = cy ; y <= cy+dy ; y--) { • p=2*dx*y -2*dy*x-2*dx-2*c*dx-dy; • if(p>=0) x++; • glVertex2i(x, y); } } } glEnd(); Point , line, polygon glBegin(GL_POINTS); If (x1==x2) { if(y2>y1) {cy=y1; dy=y2-y1;} else {cy=y2; dy=y1-y2;} for (i=0;i<=dy;i++) glVertex2i(x1, cy+i); } Else if (y1==y2){ // ? Leave for your exercises } Else { dx=x2-x1, dy=y2-y1; m=dy/dx; if(m>-1 && m < 0){ if(y2>y1) {cy=y1; cx=x1;} else {cy=y2;cx=x2;dx=-dx; dy=-dy;} c=y2-x2(y1-y2)/(x1-x2); • y=cy; • for ( x = cx ; x >= cx+dx ; x--) { • p=2*dy*x -2*dx*y-2*dy+2*c*dx-dx; • if(p>=0) y++; • glVertex2i(x, y); } } if(m>0 && m < 1){ if(y2>y1) {cy=y1; cx=x1;} else {cy=y2;cx=x2;dx=-dx; dy=-dy;} c=y2-x2(y1-y2)/(x1-x2); • y=cy; • for ( x = cx ; x <= cx+dx ; x++) { • p=2*dy*x -2*dx*y+2*dy+2*c*dx-dx; • if(p>=0) y++; • glVertex2i(x, y); } }
Point , line, polygon (polygon) • Vertices and edges • Simple polygon • No zero-winding? • No exterior? • Parity? • What’s inside? • Given a pixel, which polygon does it lie in? • Given a pixel, on edge? • Given a pixel, on vertex?
Overall Algorithm: • Line y=py, start from the point (px,py); • Has segment (x1,y1) (x2,y2) an intersection with line y=py? • For each edge in a polygon, see if it has an intersection with y=py. • For each polygon, see if it has odd number of intersections with y=py. • For all polygons in the area, see how many polygons contain this point (px,py).
// detect how many polygons in p[number_of_polygons] contain the pixel point. For (i=0;i<number_of_polygons;i++){ p[ i ]=get_polygon (i); count=0; for(j=0;j<p[i].size_of_edge;j++){ start=get_xy(p[ i ][ j ]); // (x1,y1) end=get_xy(p[ i ][ (j+1) % p[ i ].size_of_edge) // (x2,y2) count+=how_many_intersections (start, end, point); } if(count%2) // it’s inside; else it’s outside; }
or • Has segment (x1,y1) (x2,y2) an intersection with line y=py? • if(x1==x2) { if ((y1-py)*(y2-py)<0) it has one intersection at (x1,py). } • if (y1==y2) { if (py==y1) it has two intersections (x1,py) and (x2,py); one for joining the edge and the other for leaving from this edge. } • X=(py-y1)(x2-x1)/(y2-y1)+x1 if ((x2-x)*(x1-x)<0) it has an intersection at (x,py). or or
(x2,y2) (x1,y1) • Has segment (x1,y1) (x2,y2) an intersection with line y=py? • if(x1==x2) { if ((y1-py)*(y2-py)<=0 && y2!=py) => 1 it has one intersection at (x1,py). } • if (y1==y2) { if (py==y1) => 1 (even) or 0 (odd) it has two intersections (x1,py) and (x2,py). (x2,py) will be counted on the edge leaving from y=py. (x1,py) (where the edges join y=py) is counted to make even when joining edge and leaving edge are on the same side of y=py; otherwise, 0 for odd intersection. } • X=(py-y1)(x2-x1)/(y2-y1)+x1 if ((x2-x)*(x1-x)<=0 && x2!=x) =>1 it has an intersection at (x,py). (x1,y1) (x2,y2) (x2,y2) (x1,y1)
Sweep fill (interior) Xmin, xmax Fill the bottom horizontal span of pixels; move up and keep filling Keep edge information for any change on xmin and xmax AEL (active edge list) (x, 1/m, ymax) Point , line, polygon (polygon)
Overall Algorithm: This algorithm maintains one line based on AEL structure • Get all the structures at the bottom line (filling procedure on this bottom line will be skipped). • Get the structure for the next line (add 1/m easily to x for each old AEL structure, add new one if any, delete the expired one). If no AEL unit, stop! • Find filling segments with xmin and xmax (sorting) • Filling all segments on the current line, the endpoints of each segment are excluded (no painting) • Go to step 2.