1 / 19

Triangle Rasterization, Setup

Triangle Rasterization, Setup. Want to understand operation requirements of triangle rasterization, setup Will use algorithms implemented in Mesa-2.6 as a guide. Mesa is a public domain implementation of the OpenGL API Full source available, gives great insights into implementation questions

Download Presentation

Triangle Rasterization, Setup

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Triangle Rasterization, Setup • Want to understand operation requirements of triangle rasterization, setup • Will use algorithms implemented in Mesa-2.6 as a guide. • Mesa is a public domain implementation of the OpenGL API • Full source available, gives great insights into implementation questions • Works under Unix and Win95/NT systems • Will first look at details of pixel rasterization in X,Y followed by Z , RGBA, and S,T

  2. Recall Major Pipeline Sections Triangle Vertices Derivative Processor floating point for triangle slopes slopes (d?/dx,d?/dy) ScanLine Processor End Point generation (integer), per scanline operations y, leftX, rightX Iterators Fragment Generation Fragments Fragment Processing Fragment Operations including texture operations Pixels

  3. Triangle Definitions Vmax(x,y) Etop (top edge) Emaj (major edge) Vmid(x,y) Vmin(x,y) Ebot (bottom edge) Vmin(y) < = Vmid(y) <= Vmax(y) Major edge runs between Vmax and Vmin (highest delta Y) Bottom edge between Vmin & Vmid; Top edge between Vmid and Vmax

  4. Compute Slopes, lines Vmax(x,y) Etop.dxdy Emaj.dxdy top.lines Vmid(x,y) bot.lines Vmin(x,y) Ebot.dxdy Emaj.dxdy = (Vmax(x) - Vmin(x) )/( Vmax(y) - Vmin(y)) Etop.dxdy, Ebot.dxdy calculated similiarly top.lines = Vmax(y) - Vmid(y) bot.lines = Vmid(y) - Vmin(y)

  5. View Triangle as two subtriangles Eleft.top Eleft.top top.lines Top Triangle Eleft.bot Bottom Triangle bot.lines Eright.bot

  6. Rasterization Algorithm /* Start at Vertex VMin */ /*Do Bottom Triangle First, followed by Top Triangle */ Y = VMin(y); Eleft = EMaj; ERight = EBot; LeftX = VMin(x); RightX = VMin(x); lines = Bot.lines; for (I = 0; I < lines; I++) { DoSpan (Y, LeftX, RightX); /* LeftX - RightX = # of fragments in span*/ LeftX = LeftX + Eleft.dxdy; RightX = RightX + Eright.dxdy; Y = Y + 1; } /* Now do Top Triangle */ Eleft = EMaj; Eright = ETop; lines = Top.lines; for (I = 0; I < lines; I++) { DoSpan (Y, LeftX, RightX); LeftX = LeftX + Eleft.dxdy; RightX = RightX + Eright.dxdy; Y = Y + 1; }

  7. What is DoSpan? leftX,Ys rightX,Ys DoSpan is simply the fragment processing. DoSpan (Ys, leftX, rightX) { for (x = leftx; x <=rightX, x++) { process_fragment(x, Ys); }

  8. Major Edge can be on Right Side Eleft.top top.lines Eleft.top EMaj Top Triangle Eright.bot Bottom Triangle bot.lines Eleft.bot Only difference in algorithm is assignment of left, right sides: for bottom triangle : Eleft = Ebot, Eright = EMaj for top triangle: Eleft = Etop, Eright = EMaj Slope is negative for Ebot, positive for Etop

  9. Actual Algorithm Code a bit more complex • When computing Xleft, XRight of span, Mesa maintains inner and outer versions. • Inner X guaranteed to be on the edge or inside it • Outer X guaranteed to be on the edge or outside it • An Error term is used to calculate which X should be used

  10. What about Z? Vmax(x3,y3,z3) Need to interpolate Z values for each fragment xb,ys,zb xa,ys,za zp Vmid(x2,y2,z2) Vmin(x1,y1,z1) Linear Interpolation: za = z3 - (z3-z1)*[ (y3-ys)/y3-y1)] zb = z3 - (z3-z2)*[(y3-ys)/(y3-y2)] zp = zb - (zb - za)*[(xb - xp)/(xb - xa)]

  11. Incremental Method Would like an incremental method similar to that used for X,Y Need to compute dz/dx, dz/dy values Plane Equation: Ax + By +Cz + D = 0 Solve for Z: z = (-D - Ax - By) / (C) Evaluate at (x,y,z) and (x+Dx,y,z) f(x+Dx,y,z) - f(x,y,z) = (-D - A(x+Dx) - By)/(C) - (-D -Ax -By)/(C) dz / dx = -A/C * (Dx) But Dx is just equal to 1, so dz/dx = -A/C. Similiarly, dz/dy = -B/C

  12. What are A, B, C values? Vmax(x3,y3,z3) c Emaj Etop Vmid(x2,y2,z2) Vmin(x1,y1,z1) b Ebot a From Analytic Geometry: Let Vector V(ac) = V((x1,y1,z1)(x3,y3,z3)) = ( (x3-x1), (y3-y1, (z3-z1) ) = (Emaj.dx, Emaj.dy, Emaj.dz) Similiarly, Vector V(ab) = (Ebot.dx, Ebot.dy, Ebot.dz)

  13. What are A, B, C ? (cont) From Analytic Geometry: V(ac) x V(ab) = (A, B, C) (Vector cross-product) if I = (i1,i2,i3) and K = (k1,k2,k3) then I x K = (i2*k3 - i3*k2, i3*k1 - i1*k3, i1*k2 - i2*k1) So V(ac) x V(ab) = (Emaj.dy * Ebot.dz - Emaj.dz * Ebot.dy, Emaj.dz * Ebot.dx - Emaj.dx * Ebot.dz, Emaj.dx * Ebot.dy - Emaj.dy * Ebot.dx )

  14. dz/dx, dz/dy dz/dx = -A/C = Emaj.dz * Ebot.dy - Emaj.dy * Ebot.dz Emaj.dx * Ebot.dy - Emaj.dy * Ebot.dx dz/dy = -B/C = Emaj.dx * Ebot.dz - Emaj.dz * Ebot.dx Emaj.dx * Ebot.dy - Emaj.dy * Ebot.dx To save computation, 1/C computed only one time, then used as multiplication factor to compute dz/dx and dz/dy

  15. New Z value when Scanline++ As we move across a scanline (increment X), the new Z value is simply Z_new = Z_old+ dz/dx However, when we change scanlines, the new Z value will be Z_new = Z_old + dz/dy + (leftX_new - leftX_old)*dz/dx where Z_old is the starting Z of the previous scanline. Note that: Eleft.dxdy = leftX_new - leftX_old so Z_new = Z_old + dz/dy + Eleft.dxdy * dz/dx Finally, let: Zdscan = dz/dy + Eleft.dxdy * dz/dx Then each scanline increment, we compute the new Z as: Z_new = Z_old + Zdscan Note that Zdscan has to be computed only once for each subtriangle, at the same time that dz/dx and dz/dy are computed.

  16. What about R,G,B,A Interpolation? For Gouraud shading, need to interpolate R,G,B,A. Can do this just like Z!!! Vmax(x3,y3,r3) c Emaj Etop Vmin(x1,y1,r1) b Ebot Vmid(x2,y2,r2) a Red Interpolation dr/dx = -A/C = Emaj.dr * Ebot.dy - Emaj.dy * Ebot.dr Emaj.dx * Ebot.dy - Emaj.dy * Ebot.dx dr/dy = -B/C = Emaj.dx * Ebot.dr - Emaj.dr * Ebot.dx Emaj.dx * Ebot.dy - Emaj.dy * Ebot.dx

  17. Derivatives Summary • X, Y • dx/dy • Z (depth) • dz/dx, dz/dy, dz/dscan • R,G,B, A (Gouraud Shading, GL_SMOOTH) • dr/dx,dr/dy, dg/dx,dg/dy, db/dx,db/dy, da/dx,da/dy • dr/dscan, dg/dscan, db/dscan, da/dscan • S,T (Texture coordinates) • ds/dx,ds/dy, ds/dscan, dt/dx,dt/dy, dt/dscan • Each time Y is incremented, we increment by appropriate d?/dscan . Each time X is incremented, we increment by appropriate d?/dx.

  18. 1/C Calculation • Note that the 1/C computation is common for derivatives of Z,R,G,B,A,S,T • Only computed one time • The sign of 1/C is used to determine if major edge is on left or right side • if 1/C < 0 , then major edge on left side • if 1/C >= 0, then major edge on right side

  19. Adjusting to Pixel Centers • All starting points for Z,R,G,B,A are adjusted as if the starting point (x,y) was at a pixel center (X.5, Y.5) • X,Y started at Vmin(X,Y). Compute adjustment values that would place these at these at the next higher pixel center • adjx = ceiling(X+0.5) - (X+0.5) • if frac(X) = 0.5, then adjx = 0 • if frac(X) = 0.2 then adjx = 0.3 • Similar calculation for Y • Actual starting value for Z • Zs = Vmin(Z) + adjy*dz/dx + adjx*dz/dx

More Related