1 / 98

1k likes | 1.19k Views

Discrete Techniques. Chapter 7. Introduction: Texture mapping, antialiasing, compositing, and alpha blending are only a few of the techniques that become possible when the API allows us to work with discrete buffers.

Download Presentation
## Discrete Techniques

**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

**Discrete Techniques**Chapter 7**Introduction:**• Texture mapping, antialiasing, compositing, and alpha blending are only a few of the techniques that become possible when the API allows us to work with discrete buffers. • This chapter introduces these techniques, focusing on those that are supported by OpenGL and by similar APIs. Chapter 7 -- Discrete Techniques**1. Buffers**• We have already used two types of buffers: Color buffers and Depth buffers. Later we will introduce others. • What all buffers have in common is that they are inherently discrete. • They have limited resolution, both spatially and in depth. Chapter 7 -- Discrete Techniques**Define a buffer by its spatial resolution (n x m) and its**depth k, the number of bits/pixel pixel Chapter 7 -- Discrete Techniques**OpenGL defines the frame buffer as consisting of a variety**of buffers Chapter 7 -- Discrete Techniques**OpenGL buffers**• Color buffers can be displayed • Front • Back • Auxiliary • Overlay • Depth • Accumulation • High resolution buffer • Stencil • Holds masks • The depth of these buffers combined can exceed a few hundred bits. Chapter 7 -- Discrete Techniques**2. Digital Images**• Before we look at how the graphics system can work with digital images through pixel and bit operations, let’s examine what we mean by a digital image. • Within our programs, we generally work with images that are arrays of pixels. • These images can be of a variety of sizes and data types, depending upon the type of image with which we are working. Chapter 7 -- Discrete Techniques**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 Chapter 7 -- Discrete Techniques**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 Chapter 7 -- Discrete Techniques**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 Chapter 7 -- Discrete Techniques**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 Chapter 7 -- Discrete Techniques**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; } Chapter 7 -- Discrete Techniques**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); Chapter 7 -- Discrete Techniques**The display callback**void display() { glClear(GL_COLOR_BUFFER_BIT); glRasterPos2i(0,0); glDrawPixels(n,m,GL_RGB, GL_UNSIGNED_INT, image); glFlush(); } Chapter 7 -- Discrete Techniques**3. Writing into 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 Chapter 7 -- Discrete Techniques**Note that, from the hardware perspective, the type of**processing involved has none of the characteristics of the processing of geometric objects. • Consequently the hardware that optimizes bitblt operations has a completely different architecture from the pipeline hardware that we used for geometric operations. • Thus, the OpenGL architecture contains both a geometric pipeline and a pixel pipeline, each of which is usually implemented separately Chapter 7 -- Discrete Techniques**3.1 Writing Modes**• Read destination pixel before writing source Chapter 7 -- Discrete Techniques**Writing Modes**• Source and destination bits are combined bitwise • 16 possible functions (one per column in table) XOR replace OR Chapter 7 -- Discrete Techniques**3.2 Writing with XOR**• Recall from Chapter 3 that we can use XOR by enabling logic operations and selecting the XOR write mode • 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 Chapter 7 -- Discrete Techniques**4. Bit and Pixel Operations in OpenGL**• Not only does OpenGL support a separate pixel pipeline and a variety of buffers, but also data can be moved among these buffers and between buffers and the processor memory. • The plethora of formats and types can make writing efficient code for dealing with bits and pixels a difficult task. We shall not discuss what the details are, but instead shall look at what capabilities are supported. Chapter 7 -- Discrete Techniques**4.1 OpenGL Buffers and 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 Chapter 7 -- Discrete Techniques**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 • 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 Chapter 7 -- Discrete Techniques**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 Chapter 7 -- Discrete Techniques**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 • GLUT font:GLUT_BIT_MAP_8_BY_13 Chapter 7 -- Discrete Techniques**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 Chapter 7 -- Discrete Techniques**4.2 Bitmaps**• OpenGL treats arrays of one-bit pixels (bitmaps) differently from multibit pixels (pixelmaps) and provides different programming interfaces for the two cases. • Bitmaps are used for fonts and for displaying the cursor, often making use of the XOR writing mode. Chapter 7 -- Discrete Techniques**offset from raster**position glBitmap(width, height, x0, y0, xi, yi, bitmap) increments in raster position after bitmap drawn first raster position second raster position Chapter 7 -- Discrete Techniques**Example: Checker Board**GLubyte 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); Chapter 7 -- Discrete Techniques**4.4 Pixels and Images**• 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 Chapter 7 -- Discrete Techniques**OpenGL Pixel Functions**glReadPixels(x,y,width,height,format,type,myimage) size type of pixels start pixel in frame buffer type of image 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 Chapter 7 -- Discrete Techniques**4.5 Lookup Tables**• In OpenGL, all colors can be modified by lookup tables as they are placed into buffers. • These maps are essentially the same as the color lookup tables that we introduced in Chapter 2 Chapter 7 -- Discrete Techniques**4.6 Buffers for Picking**• You can use the read and write capabilities of the buffers to allow you to do picking quite easily. • The “colors” in the extra buffer are identifiers of the objects rendered at that pixel location. • If we combine this with z-buffer, then each pixel has the identifier of the closest object to the camera. Chapter 7 -- Discrete Techniques**5. Mapping Methods**• Although graphics cards can render over 10 million polygons per second, that number is insufficient for many phenomena • Clouds • Grass • Terrain • Skin Chapter 7 -- Discrete Techniques**Modeling an Orange**• Consider the problem of modeling an orange (the fruit) • Start with an orange-colored sphere • Too simple • Replace sphere with a more complex shape • Does not capture surface characteristics (small dimples) • Takes too many polygons to model all the dimples Chapter 7 -- Discrete Techniques**Modeling an Orange (2)**• Take a picture of a real orange, scan it, and “paste” onto simple geometric model • This process is known as texture mapping • Still might not be sufficient because resulting surface will be smooth • Need to change local shape • Bump mapping Chapter 7 -- Discrete Techniques**Three Types of Mapping**• Texture Mapping • Uses images to fill inside of polygons • Environmental (reflection mapping) • Uses a picture of the environment for texture maps • Allows simulation of highly specular surfaces • Bump mapping • Emulates altering normal vectors during the rendering process Chapter 7 -- Discrete Techniques**Texture Mapping**geometric model texture mapped Chapter 7 -- Discrete Techniques**Environment Mapping**Chapter 7 -- Discrete Techniques**Bump Mapping**Chapter 7 -- Discrete Techniques**6. Texture Mapping**• Textures are patterns. • They can range from regular patterns, such as stripes and checkerboards, to the complex patterns that characterize natural materials. • Textures can be 1, 2, 3, and 4 dimensional • 1D – used to create a pattern for a curve. • 3D – a block of material used to sculpt an object. • 2D is by far the most common. Chapter 7 -- Discrete Techniques**Where does mapping take place?**• Mapping techniques are implemented at the end of the rendering pipeline • Very efficient because few polygons make it past the clipper Chapter 7 -- Discrete Techniques**Is it simple?**• Although the idea is simple---map an image to a surface---there are 3 or 4 coordinate systems involved 2D image 3D surface Chapter 7 -- Discrete Techniques**Coordinate Systems**• Parametric coordinates • May be used to model curved surfaces • Texture coordinates • Used to identify points in the image to be mapped • World Coordinates • Conceptually, where the mapping takes place • Screen Coordinates • Where the final image is really produced Chapter 7 -- Discrete Techniques**6.1 Two-Dimensional Texture Mapping**Chapter 7 -- Discrete Techniques**Mapping Functions**• Basic problem is how to find the maps • Consider mapping from texture coordinates to a point a surface • Appear to need three functions x = x(s,t) y = y(s,t) z = z(s,t) • But we really want to go the other way (x,y,z) t s Chapter 7 -- Discrete Techniques**Backward Mapping**• We really want to go backwards • Given a pixel, we want to know to which point on an object it corresponds • Given a point on an object, we want to know to which point in the texture it corresponds • Need a map of the form s = s(x,y,z) t = t(x,y,z) • Such functions are difficult to find in general Chapter 7 -- Discrete Techniques**Two-part mapping**• One solution to the mapping problem is to first map the texture to a simple intermediate surface • Example: map to cylinder Chapter 7 -- Discrete Techniques**parametric cylinder**• Cylindrical Mapping x = r cos 2p u y = r sin 2pu z = v/h maps rectangle in u,v space to cylinder of radius r and height h in world coordinates s = u t = v maps from texture space Chapter 7 -- Discrete Techniques**We can use a parametric sphere**• Spherical Map x = r cos 2pu y = r sin 2pu cos 2pv z = r sin 2pu sin 2pv in a similar manner to the cylinder but have to decide where to put the distortion Spheres are used in environmental maps Chapter 7 -- Discrete Techniques**Box Mapping**• Easy to use with simple orthographic projection • Also used in environmental maps Chapter 7 -- Discrete Techniques

More Related