Lect17. From Geometry to Pixels-1 DoHoon Lee Ph.D Visual & Biomedical Computing(VisBiC) Lab. School of Computer Science & Engineering Pusan National University http://visbic.pusan.ac.kr/ This slide is modified by DHLee based on Angel s lecture 이강의자료는다른강의자료를참고하여작성된것으로무단배포나공개는원저작자의저작권을침해할수있음을유의하기바랍니다.
Rendering Overview Introduce basic implementation strategies Clipping Rendering lines polygons Give a sample algorithm for each 2
Overview At end of the geometric pipeline, vertices have been assembled into primitives Must clip out primitives that are outside the view frustum Algorithms based on representing primitives by lists of vertices Must find which pixels can be affected by each primitive Fragment generation Rasterization or scan conversion 3
Required Tasks Clipping Rasterization or scan conversion Transformations Some tasks deferred until fragment processing Hidden surface removal Antialiasing 4
Rasterization Meta Algorithms Any rendering method process every object and must assign a color to every pixel Think of rendering algorithms as two loops over objects over pixels The order of these loops defines two strategies image oriented object oriented 5
Object Space Approach For every object, determine which pixels it covers and shade these pixels Pipeline approach Must keep track of depths for HSR(Hidden Surface Removal) Cannot handle most global lighting calculations Need entire framebuffer available at all times 6
Image Space Approach For every pixel, determine which object that projects on the pixel is closest to the viewer and compute the shade of this pixel Ray tracing paradigm Need all objects available Patch Renderers Divide framebuffer into small patches Determine which objects affect each patch Used in limited power devices such as cell phones 7
Clipping Clipping lines First of implementation algorithms Clipping polygons (next) Focus on pipeline plus a few classic algorithms 8
Clipping 2D against clipping window 3D against clipping volume Easy for line segments polygons Hard for curves and text Convert to lines and polygons first 9
Clipping 2D Line Segments Brute force approach: compute intersections with all sides of clipping window Inefficient: one division per intersection 10
Cohen-Sutherland Algorithm Danny Cohen and Ivan Sutherland(1967, for flight simulator work) Idea: eliminate as many cases as possible without computing intersections Start with four lines that determine the sides of the clipping window y = y max x = x min x = x max y = y min 11
The Cases Case 1: both endpoints of line segment inside all four lines Draw (accept) line segment as is y = y max x = x min x = x max y = y min Case 2: both endpoints outside all lines and on same side of a line Discard (reject) the line segment 12
The Cases Case 3: One endpoint inside, one outside Must do at least one intersection Case 4: Both outside May have part inside Must do at least one intersection y = y max x = x min x = x max 13
Defining Outcodes For each endpoint, define an outcode b 0 b 1 b 2 b 3 b 0 = 1 if y > y max, 0 otherwise b 1 = 1 if y < y min, 0 otherwise b 2 = 1 if x > x max, 0 otherwise b 3 = 1 if x < x min, 0 otherwise Outcodes divide space into 9 regions Computation of outcode requires at most 4 subtractions 14
Using Outcodes Consider the 5 cases below AB: outcode(a) = outcode(b) = 0 Accept line segment 15
Using Outcodes CD: outcode (C) = 0, outcode(d) 0 Compute intersection Location of 1 in outcode(d) determines which edge to intersect with Note if there were a segment from A to a point in a region with 2 ones in outcode, we might have to do two interesections 16
Using Outcodes EF: outcode(e) logically ANDed with outcode(f) (bitwise) 0 Both outcodes have a 1 bit in the same place Line segment is outside of corresponding side of clipping window reject 17
Using Outcodes GH and IJ: same outcodes, neither zero but logical AND yields zero Shorten line segment by intersecting with one of sides of window Compute outcode of intersection (new endpoint of shortened line segment) Reexecute algorithm 18
Efficiency In many applications, the clipping window is small relative to the size of the entire data base Most line segments are outside one or more side of the window and can be eliminated based on their outcodes Inefficiency when code has to be reexecuted for line segments that must be shortened in more than one step 19
Cohen Sutherland in 3D Use 6-bit outcodes When needed, clip line segment against planes 20
Plane-Line Intersections 1 0 21
Liang-Barsky Algorithm 교차점의상대적인위치에의한절단알고리즘 : 어느위치가클리핑될지한번에판단 절단사각형의모든변을대상으로교차점계산. 다음과같이교차점번호부여 아래 x 축 = 1, 왼쪽 y 축 = 2, 윗쪽 x 축 = 3, 오른쪽 y 축 = 4 교차점에서의파라미터값의순서를기준으로여러가지경우를판단 4 3 3 2 1 4 1 2
Polygon Rendering Introduce clipping algorithms for polygons Survey hidden-surface algorithms 23
Polygon Clipping Not as simple as line segment clipping Clipping a line segment yields at most one line segment Clipping a polygon can yield multiple polygons However, clipping a convex polygon can yield at most one other polygon 24
Tessellation and Convexity One strategy is to replace nonconvex (concave) polygons with a set of triangular polygons (a tessellation) Also makes fill easier Tessellation through tesselllation shaders 25
Sutherland-Hodgeman Algorithm 절단다각형을기준으로순서대로절단 순서 : 위 -> 우변 -> 밑 -> 좌변기준으로절단 [ 그림 ] 서더런드 - 핫지만알고리즘의처리순서 절단규칙 내부-> 추가 / 내부-> 외부 : 교차점추가 / 외부점 : 무시 / 외부-> 내부 : 교차점, 내부점추가 추가된정점만연결 : 클리핑된다각형 [ 그림 ] 서더런드 - 핫지먼알고리즘의절단규칙
선분을연장한직선을기준으로절단 Ex. 좌변기준의절단 Sutherland-Hodgeman Algorithm [ 그림 ] 삼각형의절단 3 차원절단 상, 하, 좌, 우, 전, 후의 6 개의면을기준으로절단 면을기준으로내외부판정 in Hardware 구현에적합 Silicon Graphics 의 Geometry Engine 에사용 [ 그림 8-26] 3 차원서더런드 - 핫지먼알고리즘
Weiler-Atherton Algorithm : convex/concave Sutherland-Hodgeman 볼록다각형에만적용 하나의다각형으로취급 오목다각형처리결과 : 오류 (b) [ 그림 ] 오목다각형처리결과 해법 1: 다각형분할 (Tessellation) 오목 -> 볼록 [ 그림 ] 다각형분할 해법 2: 웨일러 - 애서톤 (Weiler-Atherton) 알고리즘
Weiler-Atherton Algorithm Sutherland-Hodgeman 알고리즘일반화 내부에서외부로가는교차점이추가되면즉시그교차점으로부터절단사각형을따라서반시계방향으로간다. 즉, 가장최근에외부에서내부로들어온교차점을만날때까지간다. 1-C-D-2 로구성되는하나의다각형이완성 분리된여러개의다각형을생성함 [ 그림 ] 웨일러 - 애서톤알고리즘
Inside and Outside of vertex Clipping algorithm 어떤점이주어진절단선또는절단면의내부 / 외부판정하는것에서출발 [ 그림 ] 정점의내외부판정 예각 : 코사인값양. 내적 : 양둔각 : 코사인값 음. 내적 : 음 90; : 0
Inside and Outside using Homogenous 점 (P) 과평면 (H) 간의거리 (d) P = (x, y, z, 1) H : Ax + By + Cz + D = 0 법선벡터방향이면의외부로정의됨 [ 그림 ] 점과평면의거리
Intersection [ 그림 ] 교차점계산 선분과면의교차점계산에필요한식. 이를전개 선분양끝점 : S, R, 법선벡터 : N, 절단면상의임의점 : Q=> 교차점 t 를구할수있음. 위식에대입하면 x, y, z
Example of Intersection 선분 RS 와육면체가만나는점 (1,.5, -5) 절단면상의점 Q(1, 0, 0) 에서 (Q-R).N = (1-.5, 0-.5, 0-(-5)).(1,0,0) = 0.5 (S-R).N=(2-.5,.5-.5, (-.5)-(-.5)).(1,0,0)=1.5 t = (Q-R).N / (S-R).N = 0.5/1.5 = 1/3 z y Q(1, 0,0) R(.5,.5, -.5) S(2,.5, -.5) 1 x t 값을 x(t),y(t),z(t) 값에적용하면 (1,.5, -.5) 다른방식으로 t 값을구하는방법 평면의식Ax+By+Cz+D=0 에 x,y,z대신 x(t),y(t),z(t) 값을대입 => t에대한식으로전환 예. 우측면의식 x=1, 1x+0y+0z-1=0 인평면. x,y,z대신위식을대입. 다음등식으로 t를구함. R과 S점값을대입 1x+0y+0z-1= 1((1-t)(.5)+t(2)) + 0.((1-t)(.5)+t(.5)) + 0.((1-t)(-.5) + t(-.5))-1 = 0 에서 t=1/3 구함.
Clipping as a Black Box Can consider line segment clipping as a process that takes in two vertices and produces either no vertices or the vertices of a clipped line segment 34
Pipeline Clipping of Line Segments Clipping against each side of window is independent of other sides Can use four independent clippers in a pipeline 35
Pipeline Clipping of Polygons Three dimensions: add front and back clippers Strategy used in SGI Geometry Engine Small increase in latency 36
Bounding Boxes Rather than doing clipping on a complex polygon, we can use an axis-aligned bounding box or extent Smallest rectangle aligned with axes that encloses the polygon Simple to compute: max and min of x and y 37
Bounding boxes Can usually determine accept/reject based only on bounding box reject accept requires detailed clipping 38
Clipping and Visibility Clipping has much in common with hiddensurface removal In both cases, we are trying to remove objects that are not visible to the camera Often we can use visibility or occlusion testing early in the process to eliminate as many polygons as possible before going through the entire pipeline 39
Hidden Surface Removal Approaches Object space method 공간상객체의앞뒤위치관계를이용하여은면결정 객체수가많지않거나서로분산되어있을때효율적방법. 면의수가많아지면처리시간문제. O( n 2 ) n : 면의수 은선제거알고리즘 깊이정렬알고리즘 (Depth sorting / painter s algorithm) 부동소수점계산 Image space method 투영된픽셀평면에서객체가보이는지여부를검사 처음 Hit( 관통 ) 하는면. O(m.n) m : # of pixel, n: # of objects Z-buffer algorithm Ray Casting method 화소 - 정수표현 : 정수계산
Hidden Surface Removal Object-space approach: use pairwise testing between polygons (objects) partially obscuring can draw independently Worst case complexity O(n 2 ) for n polygons 41
Painter s Algorithm Render polygons a back to front order so that polygons behind others are simply painted over B behind A as seen by viewer Fill B then A 42
Depth Sort Requires ordering of polygons first O(n log n) calculation for ordering Not every polygon is either in front or behind all other polygons Order polygons and deal with easy cases first, harder later Polygons sorted by distance from COP 43
Easy Cases A lies behind all other polygons Can render Polygons overlap in z but not in either x or y Can render independently 44
Hard Cases Overlap in all directions but can one is fully on one side of the other cyclic overlap penetration 45
Back-Face Removal (Culling) face is visible iff 90-90 equivalently cos 0 or v n 0 plane of face has form ax + by +cz +d =0 but after normalization n = ( 0 0 1 0) T need only test the sign of c In OpenGL we can simply enable culling but may not work correctly if we have nonconvex objects 46
Image Space Approach Look at each projector (nm for an nxm frame buffer) and find closest of k polygons Complexity O(nmk) Ray tracing z-buffer 47
z-buffer Algorithm Use a buffer called the z or depth buffer to store the depth of the closest object at each pixel found so far As we render each polygon, compare the depth of each pixel to depth in z buffer If less, place shade of pixel in color buffer and update z buffer 48
Efficiency If we work scan line by scan line as we move across a scan line, the depth changes satisfy a x+b y+c z=0 Along scan line In screen space x = 1 49
Scan-Line Algorithm Can combine shading and hsr through scan line algorithm scan line i: no need for depth information, can only be in no or one polygon scan line j: need depth information only when in more than one polygon 50
Implementation Need a data structure to store Flag for each polygon (inside/outside) Incremental structure for scan lines that stores which edges are encountered Parameters for planes 51
Visibility Testing In many realtime applications, such as games, we want to eliminate as many objects as possible within the application Reduce burden on pipeline Reduce traffic on bus Partition space with Binary Spatial Partition (BSP) Tree 52
Simple Example consider 6 parallel polygons top view The plane of A separates B and C from D, E and F 53
BSP Tree Can continue recursively Plane of C separates B from A Plane of D separates E and F Can put this information in a BSP tree Use for visibility and occlusion testing 54
Z-buffer Algorithm 화소공간접근방법 (image space algorithm) 결국화소공간으로사상 Depth Buffer( 깊이버퍼 ) 알고리즘이라고도함 화소공간해상도로은면을판단하면됨 Z 값이가장작은평면의값을그림. Z 값을저장하는깊이버 (z-buffer) 와색상을저장하는프레임버퍼필요 Z-buffer 의시선 [ 그림 8-57] z-buffer 퍼알고리즘의시선
Z-buffer algorithm 특징 일반곡면에대해서도적용가능 구현이간단. 다각형면에대한정렬이필요없음 추가버퍼가요구되므로이를위한저장공간필요 지-버퍼 (Z-Buffer) 또는깊이버퍼 (Depth Buffer) 지-버퍼알고리즘 Initialize Frame Buffer with Background Color; // Frame(x,y) = 배경색으로설정 Initialize Z Buffer with Infinite Distance; // 모든픽셀 (x,y) 에대해depth(x,y)= - for Each Polygon { for Each Pixel { Calculate z of Intersection if (Calculated z < Current z of Z-Buffer[depth(x,y)]) { Update Z-Buffer with Calculate z; //depth(x,y) = z Update Frame Buffer with the Color of Current Polygon; //Frame(x,y)=intensity(pixel) } } }
Z-buffer algorithm [ 그림 8-59] 지 - 버퍼의초기화 [ 그림 8-60] 갈색삼각형처리결과
Z-buffer algorithm [ 그림 8-58] 지 - 버퍼알고리즘 [ 그림 8-60] 갈색삼각형처리결과 [ 그림 8-61] 녹색삼각형처리결과