COMPUTER GRAPHICS, SPRING 2013 Notes from March 6 class General introduction to rendering Different approaches: Painter's algorithm Objects in front are painted later. Problem 1: interpenetrating objects. Problem 2: Objects that can't be ordered back-to-front. Z-buffer Used in just about all computer games, and most CGI films. More on this in a bit! Ray tracing Some cool examples of rendering Reflection and refraction Radiosity Soft shadows and reflections Realism Sub-surface scattering Caustics We will focus for the next few weeks on Z-buffer This week: scan conversion Next week: depth/shading/lighting The rendering pipeline Geometry Polygons Polygons + motion Transformed polygons Transformed polygons + depth Depth sorted polygons Depth sorted polygons + lighting,materials Shaded Image Scan-converting a scene Goal: end up with an image of shaded pixels. Core data structure: the frame buffer The idea comes from television Each displayed "pixel" is actually three dots: R,G,B In software, a frame buffer is a packed array: int pix[] = new int[NROWS * NCOLS]; int getPixel(int col, int row) { return pix[col + NCOLS * row]; } void setPixel(int col, int row, int rgb) { pix[col + NCOLS * row] = rgb; } // RED bits GREEN bits BLUE bits // 23...........16 15...........8 7...........0 int getRed(int rgb) { return rgb >> 16 & 255; } int getGrn(int rgb) { return rgb >> 8 & 255; } int getBlu(int rgb) { return rgb & 255; } int packRGB(int r, int g, int b) { return 255 << 24 | r << 16 | g << 8 | b; } Fun with frame buffers demo!!!! Basic operation: scan converting polygons The slow way The fast way Splitting your triangle into trapezoids: (1) Sort vertices into A,B,C according to their top to bottom order. (2) Compute D: same scan line as B, on opposite edge: t = (yB - yA) / (yC - yA) xD = xA + t * (xC - xA) (3) Split triangle into two trapezoids - one of two cases:
A-A A-A / \ / \ B-------D D-------B or B-------D D-------B \ / \ / C-C C-C
You can always split up polygons into triangles and then each of those triangles can be split into trapezoids: Scan converting a trapezoid Given XLT, XRT, at top scan-line YT, and XLB, XRB, at bottom scan-line YB: YT XLT--------------XRT / \ / \ y /-------x--------------\ / \ YB XLB------------------------XRB Loop over each scanline y between YT and YB: t = (y - yT) / (yB - yT) XL = XLT + t * (XLB - XLT) XR = XRT + t * (XRB - XRT) Loop over pixels x between XL and XR: setPixel(x, y, color); Removing back-facing polygons (1) Compute the triangle's area: sum of (xa-xb)(ya+yb)/2
smallFont = new Font("Sanserif", Font.ITALIC, height / 20); largeFont = new Font("Sanserif", Font.ITALIC, height / 14); draw() { xa = 1.3; ya = -.9; ta = .02; draw.setColor(Color.blue); draw.fillThickLine(-xa ,ya,-xa,ya,ta); draw.fillThickLine(-xa-ta,ya, xa,ya,ta); x1 = 0.8; y1 = 0.2; x2 = 0.1; y2 = 0.7; x3 = -0.8; y3 = -0.3; t = 0.015; draw.setColor(Color.black); draw.fillThickLine(x1,y1,x2,y2,t); draw.fillThickLine(x2,y2,x3,y3,t); draw.fillThickLine(x3,y3,x1,y1,t); if (mode == 0) return; draw.setFont(largeFont); draw.drawText("(", -1.25, .69); draw.drawText(")", -0.72, .69); draw.drawText("x", -1.20, .70); draw.drawText("-", -1.08, .70); draw.drawText("x", -1.19+.2, .70); draw.drawText("(", -1.25 + .6, .69); draw.drawText(")", -0.72 + .6, .69); draw.drawText("y", -1.20 + .6, .70); draw.drawText("+", -1.08 + .6, .70); draw.drawText("y", -1.19+.2 + .6, .70); draw.drawText("2", -.7, .45); draw.fillThickLine(-1.3, .57, -0.1, .57, .01); draw.setFont(smallFont); draw.drawText("i", -1.15, .65); draw.drawText("i", -1.15 + .6, .65); draw.drawText("i+1", -1.15+.3, .65); draw.drawText("i+1", -1.15+.3 + .6, .65); xu = x1; yu = y1; xv = x2; yv = y2; switch (mode) { case 2: xu = x2; yu = y2; xv = x3; yv = y3; break; case 3: xu = x3; yu = y3; xv = x1; yv = y1; break; } draw.setColor(mode < 3 ? Color.green : Color.red); draw.fillThickLine(xu, yu, xv, yv, t); draw.fillThickLine(xu, yu, xu, ya, t); draw.fillThickLine(xv, yv, xv, ya, t); draw.fillThickLine(xu, ya+t, xv, ya+t, t); }
(2) Only keep triangles with positive area
Homework (1) Play around with MISApplet -- make something cool (2) Render your scene as solid color scan-converted triangles, removing back-facing triangles.