Unformatted text preview:

BuffersEd AngelProfessor of Computer Science,Electrical and ComputerEngineering, and Media ArtsUniversity of New Mexico2Angel: Interactive Computer Graphics 4E © Addison-Wesley 2004Objectives• Introduce additional OpenGL buffers• Learn to read and write buffers• Learn to use blending3Angel: Interactive Computer Graphics 4E © Addison-Wesley 2004BufferDefine a buffer by its spatial resolution (n x m) andits depth (or precision) k, the number of bits/pixelpixel4Angel: Interactive Computer Graphics 4E © Addison-Wesley 2004OpenGL Frame Buffer5Angel: Interactive Computer Graphics 4E © Addison-Wesley 2004OpenGL Buffers• Color buffers can be displayed- Front- Back- Auxiliary- Overlay• Depth• Accumulation- High resolution buffer• Stencil- Holds masks6Angel: Interactive Computer Graphics 4E © Addison-Wesley 2004Writing in Buffers• Conceptually, we can consider all of memory as alarge 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 memoryframe buffer(destination)writing into frame buffersourcememory7Angel: Interactive Computer Graphics 4E © Addison-Wesley 2004Writing ModelRead destination pixel before writing source8Angel: Interactive Computer Graphics 4E © Addison-Wesley 2004Bit Writing Modes• Source and destination bits are combined bitwise• 16 possible functions (one per column in table)replaceORXOR9Angel: Interactive Computer Graphics 4E © Addison-Wesley 2004XOR mode• Recall from Chapter 3 that we can use XOR byenabling logic operations and selecting the XORwrite mode• XOR is especially useful for swapping blocks ofmemory such as menus that are stored off screenIf S represents screen and M represents a menuthe sequence S ← S ⊕ M M ← S ⊕ M S ← S ⊕ Mswaps the S and M10Angel: Interactive Computer Graphics 4E © Addison-Wesley 2004The Pixel Pipeline• OpenGL has a separate pipeline for pixels- Writing pixels involves• Moving pixels from processor memory to the frame buffer• Format conversions• Mapping, Lookups, Tests- Reading pixels• Format conversion11Angel: Interactive Computer Graphics 4E © Addison-Wesley 2004Raster Position• OpenGL maintains a raster position aspart of the state• Set by glRasterPos*()- glRasterPos3f(x, y, z);• The raster position is a geometric entity- Passes through geometric pipeline- Eventually yields a 2D position in screencoordinates- This position in the frame buffer is where thenext raster primitive is drawn12Angel: Interactive Computer Graphics 4E © Addison-Wesley 2004Buffer Selection• OpenGL can draw into or read from any of the colorbuffers (front, back, auxiliary)• Default to the back buffer• Change with glDrawBuffer and glReadBuffer• Note that format of the pixels in the frame buffer isdifferent from that of processor memory and thesetwo types of memory reside in different places- Need packing and unpacking- Drawing and reading can be slow13Angel: Interactive Computer Graphics 4E © Addison-Wesley 2004Bitmaps• OpenGL treats 1-bit pixels (bitmaps)differently from multi-bit pixels (pixelmaps)• Bitmaps are masks that determine if thecorresponding pixel in the frame buffer isdrawn with the present raster color- 0 ⇒ color unchanged- 1 ⇒ color changed based on writing mode• Bitmaps are useful for raster text- GLUT font: GLUT_BIT_MAP_8_BY_1314Angel: Interactive Computer Graphics 4E © Addison-Wesley 2004Raster Color• Same as drawing color set by glColor*()• Fixed by last call to glRasterPos*()• Geometry drawn in blue• Ones in bitmap use a drawing color of redglColor3f(1.0, 0.0, 0.0);glRasterPos3f(x, y, z);glColor3f(0.0, 0.0, 1.0);glBitmap(…….glBegin(GL_LINES); glVertex3f(…..)15Angel: Interactive Computer Graphics 4E © Addison-Wesley 2004Drawing BitmapsglBitmap(width, height, x0, y0, xi, yi, bitmap)first raster positionsecond raster positionoffset from raster positionincrements in raster position after bitmap drawn16Angel: Interactive Computer Graphics 4E © Addison-Wesley 2004Example: Checker BoardGLubyte wb[2] = {0 x 00, 0 x ff};GLubyte check[512];int i, j;for(i=0; i<64; i++) for (j=0; j<64, j++) check[i*8+j] = wb[(i/8+j)%2];glBitmap( 64, 64, 0.0, 0.0, 0.0, 0.0, check);17Angel: Interactive Computer Graphics 4E © Addison-Wesley 2004Pixel Maps• OpenGL works with rectangular arrays ofpixels called pixel maps or images• Pixels are in one byte ( 8 bit) 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 buffer18Angel: Interactive Computer Graphics 4E © Addison-Wesley 2004OpenGL Pixel FunctionsglReadPixels(x,y,width,height,format,type,myimage)start pixel in frame buffersizetype of imagetype of pixelspointer to processor memoryGLubyte myimage[512][512][3];glReadPixels(0,0, 512, 512, GL_RGB, GL_UNSIGNED_BYTE, myimage);glDrawPixels(width,height,format,type,myimage)starts at raster position19Angel: Interactive Computer Graphics 4E © Addison-Wesley 2004Image Formats• We often work with images in a standardformat (JPEG, TIFF, GIF)• How do we read/write such images withOpenGL?• No support in OpenGL- OpenGL knows nothing of image formats- Some code available on Web- Can write readers/writers for some simpleformats in OpenGL20Angel: Interactive Computer Graphics 4E © Addison-Wesley 2004Displaying a PPM Image• PPM is a very simple format• Each image file consists of a headerfollowed by all the pixel data• HeaderP3# comment 1# comment 2 .#comment nrows columns maxvaluepixels21Angel: Interactive Computer Graphics 4E © Addison-Wesley 2004Reading the HeaderFILE *fd;int k, nm;char c;int i;char b[100];float s;int red, green, blue;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);check for “P3” in first line22Angel: Interactive Computer Graphics 4E © Addison-Wesley 2004Reading 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 bylooking for # in first column23Angel: Interactive Computer Graphics 4E © Addison-Wesley 2004Reading the Datafscanf(fd, "%d %d %d", &n, &m, &k);printf("%d rows %d columns max value= %d\n",n,m,k);nm =


View Full Document

UNM CS 433 - CS 433 BUFFERS

Download CS 433 BUFFERS
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view CS 433 BUFFERS and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view CS 433 BUFFERS 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?