DGI/SUDOA - 060324 Följande hann jag inte gå igenom: detaljer i clip-algoritmer, Warnocks algortim, bilderna efter småuppgift nr 3. Some fundamental concepts Transformations Hidden surface elimination Två småuppgifter till denna lektion om 2p resp 3p vardera. Det kommer att behövas 25p för att få betyget 3/G på tentan. Diverse grundbegrepp Vektorer, ekvationer för linje och plan Matriser och transformationer Konvexa och konkava objekt Objektrum - bildrum Jag kommer inte lämna annan feedback än att man blir godkänd eller inte på småuppgifterna. Linjens ekvation Linjens ekvation L P P 0 P 0 P n (P - P 0 ) o n = 0 d = (P - P 0 ) o n P = P 0 + k L Gäller också plan d = avståndet till linjen, tecknet avgör vilken sida vi är på Linjens ekvation Translation t=1 x = x + t x y = y + t y P 1 t=0 P 0 P(t) = P 0 + t*(p 1 - P 0 ) 1
x = Sx * x y = Sy * y Skalning x = x*cosθ - y*sinθ y = x*sinθ + y*cosθ Rotation Homogena koordinater P =T*P Man lägger till en dimension (x,y) representeras med (x,y,1) Tredje komponenter ska alltid vara 1, annars normerar man: (x,y,a) <--> (x/a, y/a, 1) - detta är samma punkter Detta gör det möjligt att få de tre grundtransformationerna på samma form P =R*P P =S*P Sammansättning av transformationer Transformationer kan nu sättas samman med multiplikationer Rotation kring punkt Flytta till origo Rotera Flytta tillbaka Exempel: rotation kring en given punkt 2
Flytta till origo Rotera T(x r,y r )*R(θ)*T(-x r,-y r )*P Flytta tillbaka 3D transformationer Man lägger väsentligen bara till en dimension till P =R z (θ)*p P =R x (θ)*p P =T*P P =R y (θ)*p Spegling och skjuvning (shear) P =S*P 3
Småuppgift nr 2 Diskutera med bänkgrannen - Hur kan spegling och skjuvning (shear) uttryckas i de tre grundtransformationerna (det räcker med att behandla 2D) Transformationer i modellering - Skicka din lösning (svara med matriser och förklarande text) till mig, lassekj@nada.kth.se, senast 060330 som word eller pdf-fil - Värde på tentan: 2p Projektioner Ortogonal projektion Ortogonala (parallella) Perspektiviska Perspektivisk projektion z p = d x p = x*d/z 4
Konvexa och konkava objekt Konvexa och konkava objekt Konvexa och konkava objekt Objektrum och bildrum Objektrum Man arbetar den matematiska beskrivningsnoggrannheten Det blir antalet objekt som avgör komplexiteten Bildrum Man arbetar med den noggrannhet som ges bildpunkterna (pixlarna) Det blir upplösningen som avgör komplexiteten Alla vinklar <180 Vinklar kan vara >180 Implementation of graphics systems Chapter 7 in the book by Angel To implement a system like OpenGL we need fundamentals algorithms for things like: line clipping polygon clipping hidden surface removal line drawing polygon filling antialiasing Implementation aspects Steps in the rendering process: Modeling (definition of objects, e.g. a structure with polygons) Geometric processing (coordinate transformations, clipping, hidden surface elimination) Rasterization (transformation of object descriptions to pixels) Display (display of frame buffer on screen, includes e.g. aliasing) We will focus on some common algorithms in this process 5
The clipping problem The clipping problem window window lines lines clipped Pipeline clipping Cohen-Sutherland's clip algorithm To clip a line between A and B: while NOT done { CompOutCode(A); CompOutCode(B); IF A bitwise_or B == 0 THEN {inside; done=true ELSE IF A bitwise_and B =/= 0 THEN {outside; done=true ELSE intersect(a,b,outsideedge); ; 1001 0001 0101 1000 1010 0000 0010 0100 0110 Parameter clipping (Cyrus-Beck, Liang-Barsky) Use p(α) = (1- α)*p1 + α *p2 Clipping is done by first calculating α for lines that you might clip (less work than to calculate both x and y) To clip with the egde x=xmin we get: α =(xmin-x1)/(x2-x1) α1=0; α2=1; for (all four edges of window) { P1 α =calculate(edgenr); if entering(edgenr) α1 =max(α1,α) else α2 =min(α2,α); if α2 > α1 then drawline(α1, α2); P2 Polygon clipping Can we use the line clipping algorithm to clip a polygon? new point With line clipping the new point could not be included 6
Sutherland-Hodgeman's algorithm Sutherland-Hodgeman's algorithm Clip top s p Clip right Clip left Clip bottom Note that in each clipping phase of the polygon you have to keep the order of the vertices for all_window_edges { p=polypoint(1); for n=2 to n+1 { s=p; p=polypoint(n); if inside(s) and inside(p) then save(p); if inside(s) and outside(p) then save(intersect(s,p)); if outside(s) and inside(p) then {save(intersect(s,p)); save(p) update(polypoint); What happens if the clipping give more than one polygon? Hidden surface removal Two main approaches: investigate visibility for every pixel, i.e. resolution dependent (image space) investigate visibility for every object, i.e. calculate intersections etc for all objects, lines etc with the precision of the computer (object space) Main ideas Try to use the properties the objects, such as: convex objects? few objects, perhaps only one object? objects non intersecting? static view? advanced rendering wanted? 7
Back-face removal Image space hidden surface removal The back-facing polygons can be removed through a simple test abs (θ) < 90 or n o v > 0 n! v eye Z-buffer algorithm Use an extra buffer with depth values for each pixel for every polygon { for each pixel in polygom { if newpolygondepth(x,y) < depthval_in_zbuffer(x,y) then {depthval_in_zbuffer(x,y) <- newpolygondepth(x,y); framebuffervalue <- newpolygonvalue framebuffer Z-buffer framebuffer Z-buffer framebuffer Z-buffer Depth sort algorithm 1. Sort all object according to depth 2. Paint them into frame buffer in the sorted order (start with object far away) increasing depth Depth sort algorithm one problem 8
BSP algorithm, sorting in a BSP tree Divide the space using half planes. Basic idea object2 object3 Objects on one side of the plane belongs to one child in tree and the the objects on the other side belongs to the second child. object1 Divide the space once more and repeat the division process with a new set of children nodes. Draw object3 Draw object2 Draw object1 Basic idea object2 object3 object1 Draw object1 Draw object2 Draw object3 James, Day, CGF 17/1 void Draw_BSP_Tree (BSP_tree *tree, point eye) { if (result > 0) { Draw_BSP_Tree (tree->back, eye); tree->polygons.draw_polygon_list (); Draw_BSP_Tree (tree->front, eye); else if (result < 0) { Draw_BSP_Tree (tree->front, eye); tree->polygons.draw_polygon_list (); Draw_BSP_Tree (tree->back, eye); else // result is 0 { // the eye point is on the partition plane... Draw_BSP_Tree (tree->front, eye); Draw_BSP_Tree (tree->back, eye); Warnock algorithm Divide the plane into four squares 9
Småuppgift nr 3 Prata med bänkgrannen - Beskriv kortfattat varsin algoritm för att ta bort skymda ytor för varandra. Hitta på ett eget sätt att beskriva (med ord och figur) Line drawing (x2,y2) - Beskriv tre algoritmer inkl en figur för att ta bort skymda ytor utan att kopiera mina eller någon annans text/figurer. - Skicka din lösning till mig, lassekj@nada.kth.se, senast 060330 som word eller pdf-fil - Värde på tentan: 3p (x1,y1) A line between two points have two be represented as a set of dots (pixels) in the raster Simple solution (x1,y1) Line drawing (x2,y2) line(x1,y1,x2,y2,value) { int x; float dx,dy,y,m; dy=y2-y1; dx=x2-x1; m=dy/dx; y=y1; for (ix=x1; ix<=x2; ix++) { y+=m; write_pixel(ix, Round(y),line_color); This works for m < 1 What can be do if m > 1? Drawback: includes float and division 10
(x p,y p ) Solution with integer arithmetic (alternative to the book, we don t go into the details at the lecture) d1 NE M E d2 Line equation can be written as F(x,y)=dy*x-dx*y+B*dx=0 The sign of F(x M,y M ) decides if we should choose E or NE We denote d1=f(x M,y M )= dy*(x p +1)-dx*(y p +0.5)+B*dx Suppose we get d1<0, choose E d2=f(x M +1,y M )= dy*(x p +2)-dx*(y p +0.5)+B*dx= d1+dy Similar result if d1>=0 New algorithm with integer arithmetic MidpointLine(x0,y0,x1,y1,value) { int x,y,dx,dy,d,incre,incrne; dy=y1-y0; dx=x1-x0; d=2*dy-dx; incre=2*dy; incrne=2*(dy-dx); x=x0; y=y0; WritePixel(x,y,value); while (x<x1) { if (d<=0) { d+=incre; x++; else { d+=incrne; x++; y++; WritePixel(x,y,value); Further improvements Scan conversion of a polygon (Polyfill) Take two points in each step The first and last pattern can not both appear for the same line j x1 x2 x3 x4 Use the symmetry around the middle point of the line: take identical steps (except sign) from startpoint and endpoint j + 1 x5 x6 j + 2 x7 x8 Polyfill setup_edgetables; y=min_yentry(et); AET=empty; while not(aet=empty and ET=empty) { insert_new_edges(y,et,aet); sort(aet); fill_scanline(y); for all_lines_in_aet {if y=ymax then remove(line,aet); y=y+1; for all_lines_in_aet {x=x-1/m; This algorithm can be extended to perform hidden surface removall 11