1 / 38

Buffers

Buffers. Software College, Shandong University Instructor: Zhou Yuanfeng E-mail: yuanfeng.zhou@gmail.com. Review. Rasterization: Polygon scan conversion algorithm; Hidden surface removal Aliasing. Objectives. Introduce additional OpenGL buffers Learn to read and write buffers

lila-bright
Download Presentation

Buffers

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. Buffers Software College, Shandong University Instructor: Zhou Yuanfeng E-mail: yuanfeng.zhou@gmail.com

  2. Review • Rasterization: Polygon scan conversion algorithm; • Hidden surface removal • Aliasing

  3. Objectives • Introduce additional OpenGL buffers • Learn to read and write buffers • Learn to use blending

  4. Buffer Define a buffer by its spatial resolution (n x m) and its depth (or precision) k, the number of bits/pixel pixel

  5. OpenGL Frame Buffer

  6. OpenGL Buffers • Color buffers can be displayed • Front • Back • Auxiliary • Overlay • Depth • Accumulation • High resolution buffer • Stencil • Holds masks

  7. 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 (Double buffer) • Left and right buffers for Sterero images memory source frame buffer (destination) writing into frame buffer

  8. Bit-block writing • A scan-line writing while polygon filling • Character display • Clear all pixel buffer

  9. BitBlt copy • BOOL BitBlt( int x, int y, int nWidth, int nHeight, CDC* pSrcDC, int xSrc, int ySrc, DWORD dwRop ); //SRCCOPY • Copies a bitmap from the source device context to this current device context. (From a Bitmap in memory to current CDC)

  10. Writing Model Read destination pixel before writing source Source:s Destination:d’ d’ = f(s,d)

  11. Bit Writing Modes (16 modes) • Source and destination bits are combined bitwise • 16 possible functions (one per column in table) XOR replace OR

  12. Mode 3&6 Mode 6 Mode 3

  13. XOR mode • Recall from Chapter 3 that we can use XOR by enabling logic operations and selecting the XOR write mode S  (S  M)  M • 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  S  M M  S  M S  S  M swaps the S and M

  14. The 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 conversion

  15. Raster Position • OpenGL maintains a raster position as part of the state • Set by glRasterPos*() (world coord) • glRasterPos3f(x, y, z); • The raster position is a geometric entity • 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

  16. Buffer Selection • OpenGL can draw into or read from any of the color buffers (front, back, auxiliary) • Default to the back buffer • Change withglDrawBuffer andglReadBuffer • 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

  17. OpenGL code Lib3dsMesh *mesh = f3ds->meshes[i];//由3ds文件中读取出需要数据glGenBuffers(1,&m_Vertex[i]);glGenBuffers(1,&m_Index[i]);glBindBuffer(GL_ARRAY_BUFFER,m_Vertex[i]);glBufferData(GL_ARRAY_BUFFER,(sizeof(float) * 9 * mesh->nfaces) + (sizeof(float) * 3 * mesh->nvertices),TotleV,GL_STATIC_DRAW);glVertexPointer(3,GL_FLOAT,0,BUFFER_OFFSET(0));glNormalPointer(GL_FLOAT,0,BUFFER_OFFSET(V_size)); glBindBuffer(GL_ARRAY_BUFFER,m_Vertex[0]);glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,m_Index[0]);glDrawElements(GL_TRIANGLES,DrawCount[0],GL_UNSIGNED_SHORT,NULL);

  18. Bitmaps • OpenGL treats 1-bit pixels (bitmaps) differently from multi-bit pixels (pixelmaps) • Bitmaps are masks that 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 cursor (XOR) • GLUT font:GLUT_BIT_MAP_8_BY_13

  19. Raster Color • Same as drawing color set by glColor*() • Current raster color is fixed by last call to glRasterPos*() • Geometry drawn in blue • Ones in bitmap use a drawing color of red glColor3f(1.0, 0.0, 0.0); glRasterPos3f(x, y, z); glColor3f(0.0, 0.0, 1.0); glBitmap(……. glBegin(GL_LINES); glVertex3f(…..)

  20. 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

  21. Text definition in OpenGL • Raster text is faster than vector text; • Times, Courier, Computer Modern; • Size: 10pt, 24pt (1 inch=72 pt)

  22. Text in OpenGL • 8x13 text is defined in GLUT_BITMAP_8_BY_13; • The size of text is related with resolution of screen; • Define 8x13 text: • GLubyte my_font[128][13]; • base=glGenLists(128); • for(i=0;i<128;i++){ • glNewList(base+i, GL_COMPILE); • glBitMap(8,13,0.0,0.0,10.0,0.0,my_font[i]); • glEndList(); • }

  23. Example: Checker Board GLubyte wb[2] = {0x00, 0xFF}; GLubyte check[512]; int i, j; for(i=0; i<64; i++) for (j=0; j<8, j++) check[i*8+j] = wb[(i/8+j)%2]; glBitmap( 64, 64, 0.0, 0.0, 0.0, 0.0, check );

  24. Image Formats • We often work with images in a standard format (JPEG, TIFF, GIF, PS, EPS) • 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

  25. PS & EPS • PostScript(PS) image for controlling printer RGB image converts to 7 bits ASCII; so PS image can be identified by printer, but it is large. • Encapsulated PostScript (EPS) is similar to PS image.

  26. GIF image • CompuServe Incorporated (1987) Graphics Interchange Format Color Index Image low-resolution small animations (image slices)

  27. TIFF • Tagged(标签) Image File Format • High quality • First for scanned copy • Lossless compression • Can save vector boundary of image

  28. JPEG • lossy compression • High compression ratio • Discrete cosine transform

  29. Size comparison • Image 1200x1200 • TIFF: 1440198 bytes (about 1.37m) • JPEG: 80109 and 38962 bytes (about 78kb) • EPS: about twice of TIFF • Compressed TIFF: about half size • ZIPcompression: TIFFand EPS are similar JPEG-37 TIFF JPEG-18

  30. Displaying a PPM Image(Linux) • PPM is a very simple format (24 bits) • 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

  31. Reading the Header FILE *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 line

  32. 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

  33. Reading the Data fscanf(fd, "%d %d %d", &n, &m, &k); printf("%d rows %d columns max value= %d\n",n,m,k); nm = n*m; image=malloc(3*sizeof(GLuint)*nm); s=255./k; for(i=0;i<nm;i++) { fscanf(fd,"%d %d %d",&red, &green, &blue ); image[3*nm-3*i-3]=red; image[3*nm-3*i-2]=green; image[3*nm-3*i-1]=blue; } scale factor

  34. Pixel Maps • OpenGL works with rectangular arrays of pixels 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 buffer

  35. Pixel format • Image in memory: RGB, RGBA, Index • Format: int or float • glPixelMap

  36. OpenGL Pixel Functions glReadPixels(x,y,width,height,format,type,myimage) size type of pixels start pixel in frame buffer 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 raster position

  37. Scaling the Image Data We can scale the image in the pipeline glPixelTransferf(GL_RED_SCALE, s); glPixelTransferf(GL_GREEN_SCALE, s); glPixelTransferf(GL_BLUE_SCALE, s); We may have to swap bytes when we go from processor memory to the frame buffer depending on the processor. If so, we can use glPixelStorei(GL_UNPACK_SWAP_BYTES,GL_TRUE);

  38. The display callback void display() { glClear(GL_COLOR_BUFFER_BIT); glRasterPos2i(0,0); glDrawPixels(n,m,GL_RGB, GL_UNSIGNED_INT, image); glFlush(); }

More Related