Buffers, Compositing and Blending Week 8
Total Page:16
File Type:pdf, Size:1020Kb
CS 432/680 INTERACTIVE COMPUTER GRAPHICS Buffers, Compositing and Blending Week 8 David Breen Department of Computer Science Drexel University Based on material from Ed Angel, University of New Mexico Han-Wei Shen, Ohio State University David Blythe, Silicon Graphics & Tom McReynolds, Gigapixel Objectives • Introduce additional OpenGL buffers • Learn to read and write buffers • Learn to use blending • Learn depth-of-field and reflection effects Angel: Interactive Computer Graphics 3E 2 © Addison-Wesley 2002 Pixel and Geometry Pipelines • Parallel pipelines (Geometry & Pixel) come together in the Rasterization stage Angel: Interactive Computer Graphics 3E 3 © Addison-Wesley 2002 Buffer Define a buffer by its spatial resolution (n x m) and its depth k, the number of bits/pixel pixel Angel: Interactive Computer Graphics 3E 4 © Addison-Wesley 2002 OpenGL Frame Buffer Angel: Interactive Computer Graphics 3E 5 © Addison-Wesley 2002 OpenGL Buffers • Color buffers can be displayed – Front – Back – Auxiliary – Overlay • Depth • Accumulation – High resolution buffer • Stencil – Holds masks Angel: Interactive Computer Graphics 3E 6 © Addison-Wesley 2002 Writing in Buffers • Conceptually, we can consider all of memory as a large two-dimensional array of pixels • We read and write rectangular block of pixels – Bit block transfer (bitblt) operations • The frame buffer is part of this memory memory source frame buffer (destination) writing into frame buffer Angel: Interactive Computer Graphics 3E 7 © Addison-Wesley 2002 The Pixel Pipeline • OpenGL has a separate pipeline for pixels – Writing pixels involves • Moving pixels from processor memory to the frame buffer • Unpacking (Format conversions, swapping & alignment) • Transfer ops, Map Lookups, Tests – Reading pixels • Packing (Format conversion, swapping & alignment) Angel: Interactive Computer Graphics 3E 8 © Addison-Wesley 2002 Pixel Storage Modes • Deal with byte-ordering conventions • Memory alignment glPixelStore[if](GLenum pname, TYPE param); pname: GL_UNPACK_SWAP_BYTES, GL_PACK_SWAP_BYTES, GL_UNPACK_LSB_FIRST, GL_PACK_LSB_FIRST, GL_UNPACK_ROW_LENGTH, GL_PACK_ROW_LENGTH, GL_UNPACK_SKIP_ROWS, GL_PACK_SKIP_ROWS, GL_UNPACK_ALIGNMENT, GL_PACK_ALIGNMENT, GL_UNPACK_ALIGNMENT Angel: Interactive Computer Graphics 3E 9 © Addison-Wesley 2002 Pixel Transfer Operations • Color Scale & Bias • Color Index Shift & Offset • Convolution • Matrix multiply glPixelTransferf(GL_RED_SCALE, s); glPixelTransferf(GL_BLUE_BIAS, b); Angel: Interactive Computer Graphics 3E 10 © Addison-Wesley 2002 Pixel Mapping Operations • Color components and color & stencil indices can be modified via a table look-up glPixelMap[ui us f]v(Glenum map, Glint size, TYPE *array); map - GL_PIXEL_MAP_*_TO_* I, S, R, G, B, A Angel: Interactive Computer Graphics 3E 11 © Addison-Wesley 2002 Pixel Tests • Scissor glScissor() – Only draw in a rectangular portion of screen • Alpha glAlphaFunc() – Draw based on alpha value • Stencil glStencilFunc(), glStencilOp() – Draw based on stencil value • Depth glDepthFunc(), – Draw based on depth value Angel: Interactive Computer Graphics 3E 12 © Addison-Wesley 2002 Writing Model Read destination pixel before writing source glLogicOp(GLenum op) Angel: Interactive Computer Graphics 3E 13 © Addison-Wesley 2002 Logical Pixel Operations Angel: Interactive Computer Graphics 3E 14 © Addison-Wesley 2002 Writing Modes • Source and destination bits are combined bitwise • 16 possible functions (one per column in table) AND replace XOR OR NOT(s) Angel: Interactive Computer Graphics 3E 15 © Addison-Wesley 2002 XOR mode • Applying twice returns bit back to original value • XOR is especially useful for swapping blocks of memory such as menus that are stored off screen If S represents screen and M represents a menu the sequence S ← S0 M ← S ⊕ M S ← S ⊕ M swaps the S and M Angel: Interactive Computer Graphics 3E 16 © Addison-Wesley 2002 Raster Position • OpenGL maintains a raster position as part of the state • Set by glRasterPos*() – glRasterPos3f(x, y, z); • The raster position is a geometric entity (like a vertex) – Passes through geometric pipeline – Eventually yields a 2D position in screen coordinates – This position in the frame buffer is where the next raster primitive is drawn Angel: Interactive Computer Graphics 3E 17 © Addison-Wesley 2002 Window Position • In OpenGL 1.4 glWindowPos*() was defined • Allows you to specify current raster position in window coordinates Angel: Interactive Computer Graphics 3E 18 © Addison-Wesley 2002 Buffer Selection • OpenGL can draw into or read from any of the color buffers (front, back, auxiliary) • Default to the back buffer • Change with glDrawBuffer and glReadBuffer • Note that format of the pixels in the frame buffer is different from that of processor memory and these two types of memory reside in different places – Need packing and unpacking – Drawing and reading can be slow Angel: Interactive Computer Graphics 3E 19 © Addison-Wesley 2002 Bitmaps • OpenGL treats 1-bit pixels (bitmaps) differently than multi-bit pixels (pixelmaps) • Bitmaps are masks which determine if the corresponding pixel in the frame buffer is drawn with the present raster color – 0 ⇒ color unchanged – 1 ⇒ color changed based on writing mode • Bitmaps are useful for raster text – GLUT_BIT_MAP_8_BY_13 Angel: Interactive Computer Graphics 3E 20 © Addison-Wesley 2002 Raster Color • Same as drawing color set by glColor*() • Fixed by last call to glRasterPos*() glColor3f(1.0, 0.0, 0.0); glRasterPos3f(x, y, z); glColor3f(0.0, 0.0, 1.0); glBitmap(……. glBegin(GL_LINES); glVertex3f(…..) • Geometry drawn in blue • Ones in bitmap use a drawing color of red Angel: Interactive Computer Graphics 3E 21 © Addison-Wesley 2002 Drawing Bitmaps glBitmap(width, height, x0, y0, xi, yi, bitmap) offset from raster position increments in raster position after bitmap drawn first raster position second raster position Angel: Interactive Computer Graphics 3E 22 © Addison-Wesley 2002 Example: Checker Board GLubyte wb[2] = {0 x 00, 0 x ff}; GLubyte check[4096]; int i, j; for(i=0; i<64; i++) for (j=0; j<64, j++) check[i*8+j] = wb[(i/8+j/8)%2]; glBitmap( 64, 64, 0.0, 0.0, 0.0, 0.0, check); Angel: Interactive Computer Graphics 3E 23 © Addison-Wesley 2002 Pixel Maps • OpenGL works with rectangular arrays of pixels called pixel maps or images • Pixels are in one byte chunks – Luminance (gray scale) images 1 byte/pixel – RGB 3 bytes/pixel • Three functions – Draw pixels: processor memory to frame buffer – Read pixels: frame buffer to processor memory – Copy pixels: frame buffer to frame buffer Angel: Interactive Computer Graphics 3E 24 © Addison-Wesley 2002 OpenGL Pixel Functions glReadPixels(x,y,width,height,format,type,myimage) start pixel in frame buffer size type of pixels type of image pointer to processor memory GLubyte myimage[512][512][3]; glReadPixels(0,0, 512, 512, GL_RGB, GL_UNSIGNED_BYTE, myimage); glDrawPixels(width,height,format,type,myimage) starts at current raster position Angel: Interactive Computer Graphics 3E 25 © Addison-Wesley 2002 OpenGL Pixel Functions glReadPixels(x,y,width,height,format,type,myimage) start pixel in frame buffer size type of pixels type of image pointer to processor memory GLubyte myimage[512][512][3]; glReadPixels(0,0, 512, 512, GL_RGB, GL_UNSIGNED_BYTE, myimage); glDrawPixels(width,height,format,type,myimage) starts at current raster position Angel: Interactive Computer Graphics 3E 26 © Addison-Wesley 2002 OpenGL Pixel Functions glReadPixels(x,y,width,height,format,type,myimage) start pixel in frame buffer size type of pixels type of image pointer to processor memory GLubyte myimage[786432]; glReadPixels(0,0, 512, 512, GL_RGB, GL_UNSIGNED_BYTE, myimage); glDrawPixels(width,height,format,type,myimage) starts at current raster position Angel: Interactive Computer Graphics 3E 27 © Addison-Wesley 2002 Formats & Types • GL_RGB • GL_UNSIGNED_BYTE • GL_RGBA • GL_BYTE • GL_RED • GL_BITMAP • GL_GREEN • GL_UNSIGNED_SHORT • GL_BLUE • GL_SHORT • GL_ALPHA • GL_UNSIGNED_INT • GL_DEPTH_COMPONENT • GL_INT • GL_LUMINANCE • GL_FLOAT • GL_LUMINANCE_ALPHA • GL_UNSIGNED_BYTE_3_3_2 • GL_COLOR_INDEX • GL_UNSIGNED_INT_8_8_8_8 • GL_STENCIL_INDEX • etc. Angel: Interactive Computer Graphics 3E 28 © Addison-Wesley 2002 Image Formats • We often work with images in a standard format (JPEG, TIFF, GIF) • How do we read/write such images with OpenGL? • No support in OpenGL – OpenGL knows nothing of image formats – Some code available on Web – Can write readers/writers for some simple formats in OpenGL Angel: Interactive Computer Graphics 3E 29 © Addison-Wesley 2002 Displaying a PPM Image • PPM is a very simple format • Each image file consists of a header followed by all the pixel data • Header P3 # comment 1 # comment 2 . #comment n rows columns maxvalue pixels Angel: Interactive Computer Graphics 3E 30 © Addison-Wesley 2002 Reading the Header FILE *fd; int k, nm; char c; int i; char b[100]; float s; check for “P3” int red, green, blue; in first line printf("enter file name\n"); scanf("%s", b); fd = fopen(b, "r"); fscanf(fd,"%[^\n] ",b); if(b[0]!='P'|| b[1] != '3'){ printf("%s is not a PPM file!\n", b); exit(0); } printf("%s is a PPM file\n",b); Angel: Interactive Computer Graphics 3E 31 © Addison-Wesley 2002 Reading the Header (cont) fscanf(fd, "%c",&c); while(c == '#') { fscanf(fd, "%[^\n] ", b); printf("%s\n",b); fscanf(fd, "%c",&c); } ungetc(c,fd); skip over comments by looking for # in first column Angel: Interactive Computer Graphics 3E 32 © Addison-Wesley 2002 Reading the Data fscanf(fd, "%d %d %d",