Download presentation
Presentation is loading. Please wait.
1
Discrete Techniques Chapter 8
2
Chapter 8 -- Discrete Techniques
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. CS 480/680 Chapter 8 -- Discrete Techniques
3
Chapter 8 -- Discrete Techniques
Introduction: (cont) We start by looking at the frame buffer in more detail and how we can read and write to it. We learn to work with arrays of pixels that form digital images. We then consider mapping methods. We will then look at some of the other buffers that are supported by the OpenGL API CS 480/680 Chapter 8 -- Discrete Techniques
4
Chapter 8 -- 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. CS 480/680 Chapter 8 -- Discrete Techniques
5
Chapter 8 -- Discrete Techniques
Define a buffer by its spatial resolution (n x m) and its depth k, the number of bits/pixel pixel CS 480/680 Chapter 8 -- Discrete Techniques
6
Chapter 8 -- Discrete Techniques
OpenGL defines the frame buffer as consisting of a variety of buffers CS 480/680 Chapter 8 -- Discrete Techniques
7
Chapter 8 -- 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. CS 480/680 Chapter 8 -- Discrete Techniques
8
Chapter 8 -- 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. CS 480/680 Chapter 8 -- Discrete Techniques
9
Chapter 8 -- 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 CS 480/680 Chapter 8 -- Discrete Techniques
10
Chapter 8 -- 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 CS 480/680 Chapter 8 -- Discrete Techniques
11
Chapter 8 -- 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 CS 480/680 Chapter 8 -- Discrete Techniques
12
Chapter 8 -- Discrete Techniques
Reading the Header (cont) fscanf(fd, "%c",&c); while(c == '#') { fscanf(fd, "%[^\n] ", b); printf("%s\n",b); } ungetc(c,fd); skip over comments by looking for # in first column CS 480/680 Chapter 8 -- Discrete Techniques
13
Chapter 8 -- 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; } CS 480/680 Chapter 8 -- Discrete Techniques
14
Chapter 8 -- 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); CS 480/680 Chapter 8 -- Discrete Techniques
15
Chapter 8 -- 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(); } CS 480/680 Chapter 8 -- Discrete Techniques
16
Chapter 8 -- 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 CS 480/680 Chapter 8 -- Discrete Techniques
17
Chapter 8 -- 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 CS 480/680 Chapter 8 -- Discrete Techniques
18
Chapter 8 -- Discrete Techniques
3.1 Writing Modes Read destination pixel before writing source CS 480/680 Chapter 8 -- Discrete Techniques
19
Chapter 8 -- Discrete Techniques
Writing Modes Source and destination bits are combined bitwise 16 possible functions (one per column in table) XOR replace OR CS 480/680 Chapter 8 -- Discrete Techniques
20
Chapter 8 -- 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 swaps the S and M CS 480/680 Chapter 8 -- Discrete Techniques
21
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. CS 480/680 Chapter 8 -- Discrete Techniques
22
Chapter 8 -- 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 CS 480/680 Chapter 8 -- Discrete Techniques
23
Chapter 8 -- 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 CS 480/680 Chapter 8 -- Discrete Techniques
24
Chapter 8 -- 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 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 CS 480/680 Chapter 8 -- Discrete Techniques
25
Chapter 8 -- 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 CS 480/680 Chapter 8 -- Discrete Techniques
26
Chapter 8 -- 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 CS 480/680 Chapter 8 -- Discrete Techniques
27
Chapter 8 -- 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. CS 480/680 Chapter 8 -- Discrete Techniques
28
Chapter 8 -- Discrete Techniques
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 CS 480/680 Chapter 8 -- Discrete Techniques
29
Chapter 8 -- 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); CS 480/680 Chapter 8 -- Discrete Techniques
30
Chapter 8 -- 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 CS 480/680 Chapter 8 -- Discrete Techniques
31
Chapter 8 -- 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 CS 480/680 Chapter 8 -- Discrete Techniques
32
Chapter 8 -- 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 CS 480/680 Chapter 8 -- Discrete Techniques
33
Chapter 8 -- Discrete Techniques
5. Examples This section considers three examples of writing pixels. Maxwell Triangle – Chromacity of color coordinates Pseudo Color Buffer Picking CS 480/680 Chapter 8 -- Discrete Techniques
34
Chapter 8 -- Discrete Techniques
6. Mapping Methods Although graphics cards can render over 10 million polygons per second, that number is insufficient for many phenomena Clouds Grass Terrain Skin CS 480/680 Chapter 8 -- Discrete Techniques
35
Chapter 8 -- 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 CS 480/680 Chapter 8 -- Discrete Techniques
36
Chapter 8 -- 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 CS 480/680 Chapter 8 -- Discrete Techniques
37
Chapter 8 -- 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 CS 480/680 Chapter 8 -- Discrete Techniques
38
Chapter 8 -- Discrete Techniques
Texture Mapping geometric model texture mapped CS 480/680 Chapter 8 -- Discrete Techniques
39
Chapter 8 -- Discrete Techniques
Environment Mapping CS 480/680 Chapter 8 -- Discrete Techniques
40
Chapter 8 -- Discrete Techniques
Bump Mapping CS 480/680 Chapter 8 -- Discrete Techniques
41
Chapter 8 -- Discrete Techniques
7. 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. CS 480/680 Chapter 8 -- Discrete Techniques
42
Chapter 8 -- 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 CS 480/680 Chapter 8 -- Discrete Techniques
43
Chapter 8 -- 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 CS 480/680 Chapter 8 -- Discrete Techniques
44
Chapter 8 -- 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 CS 480/680 Chapter 8 -- Discrete Techniques
45
Chapter 8 -- Discrete Techniques
6.1 Two-Dimensional Texture Mapping CS 480/680 Chapter 8 -- Discrete Techniques
46
Chapter 8 -- 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 CS 480/680 Chapter 8 -- Discrete Techniques
47
Chapter 8 -- 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 CS 480/680 Chapter 8 -- Discrete Techniques
48
Chapter 8 -- 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 CS 480/680 Chapter 8 -- Discrete Techniques
49
Chapter 8 -- Discrete Techniques
Cylindrical Mapping parametric cylinder 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 CS 480/680 Chapter 8 -- Discrete Techniques
50
Chapter 8 -- Discrete Techniques
Spherical Map We can use a parametric sphere 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 CS 480/680 Chapter 8 -- Discrete Techniques
51
Chapter 8 -- Discrete Techniques
Box Mapping Easy to use with simple orthographic projection Also used in environmental maps CS 480/680 Chapter 8 -- Discrete Techniques
52
Chapter 8 -- Discrete Techniques
Second Mapping Map from intermediate object to actual object Normals from intermediate to actual Normals from actual to intermediate Vectors from center of intermediate CS 480/680 Chapter 8 -- Discrete Techniques
53
Chapter 8 -- Discrete Techniques
Aliasing Point sampling of the texture can lead to aliasing errors point samples in u,v (or x,y,z) space miss blue stripes point samples in texture space CS 480/680 Chapter 8 -- Discrete Techniques
54
Chapter 8 -- Discrete Techniques
Area Averaging A better but slower option is to use area averaging pixel preimage Note that preimage of pixel is curved CS 480/680 Chapter 8 -- Discrete Techniques
55
Chapter 8 -- Discrete Techniques
8. Texture Mapping in OpenGL Three steps to applying a texture specify the texture read or generate image assign to texture enable texturing assign texture coordinates to vertices Proper mapping function is left to application specify texture parameters wrapping, filtering CS 480/680 Chapter 8 -- Discrete Techniques
56
Chapter 8 -- Discrete Techniques
Texture Mapping x y z geometry screen s t image CS 480/680 Chapter 8 -- Discrete Techniques
57
Chapter 8 -- Discrete Techniques
Texture Example The texture (below) is a 256 x 256 image that has been mapped to a rectangular polygon which is viewed in perspective CS 480/680 Chapter 8 -- Discrete Techniques
58
Chapter 8 -- Discrete Techniques
Texture Mapping and the OpenGL Pipeline Images and geometry flow through separate pipelines that join at the rasterizer “complex” textures do not affect geometric complexity geometry pipeline vertices pixel pipeline image rasterizer CS 480/680 Chapter 8 -- Discrete Techniques
59
Chapter 8 -- Discrete Techniques
Specify Texture Image Define a texture image from an array of texels (texture elements) in CPU memory Glubyte my_texels[512][512]; Define as any other pixel map Scanned image Generate by application code Enable texture mapping glEnable(GL_TEXTURE_2D) OpenGL supports 1-4 dimensional texture maps CS 480/680 Chapter 8 -- Discrete Techniques
60
Chapter 8 -- Discrete Techniques
Define Image as a Texture glTexImage2D( target, level, components, w, h, border, format, type, texels ); target: type of texture, e.g. GL_TEXTURE_2D level: used for mipmapping (discussed later) components: elements per texel w, h: width and height of texels in pixels border: used for smoothing (discussed later) format and type: describe texels texels: pointer to texel array glTexImage2D(GL_TEXTURE_2D, 0, 3, 512, 512, 0, GL_RGB, GL_UNSIGNED_BYTE, my_texels); CS 480/680 Chapter 8 -- Discrete Techniques
61
Chapter 8 -- Discrete Techniques
Converting A Texture Image OpenGL requires texture dimensions to be powers of 2 If dimensions of image are not powers of 2 gluScaleImage( format, w_in, h_in, type_in, *data_in, w_out, h_out, type_out, *data_out ); data_in is source image data_out is for destination image Image interpolated and filtered during scaling CS 480/680 Chapter 8 -- Discrete Techniques
62
Chapter 8 -- Discrete Techniques
Mapping a Texture Based on parametric texture coordinates glTexCoord*() specified at each vertex Texture Space Object Space t 1, 1 (s, t) = (0.2, 0.8) 0, 1 A a c (0.4, 0.2) b B C s (0.8, 0.4) 0, 0 1, 0 CS 480/680 Chapter 8 -- Discrete Techniques
63
Chapter 8 -- Discrete Techniques
Typical Code glBegin(GL_POLYGON); glColor3f(r0, g0, b0); glNormal3f(u0, v0, w0); glTexCoord2f(s0, t0); glVertex3f(x0, y0, z0); glColor3f(r1, g1, b1); glNormal3f(u1, v1, w1); glTexCoord2f(s1, t1); glVertex3f(x1, y1, z1); . glEnd(); Note that we can use vertex arrays to increase efficiency CS 480/680 Chapter 8 -- Discrete Techniques
64
Chapter 8 -- Discrete Techniques
Interpolation OpenGL uses bilinear interpolation to find proper texels from specified texture coordinates. There can be distortions texture stretched over trapezoid showing effects of bilinear interpolation good selection of tex coordinates poor selection of tex coordinates CS 480/680 Chapter 8 -- Discrete Techniques
65
Chapter 8 -- Discrete Techniques
Texture Parameters OpenGL has a variety of parameters that determine how texture is applied Wrapping parameters determine what happens of s and t are outside the (0,1) range Filter modes allow us to use area averaging instead of point samples Mipmapping allows us to use textures at multiple resolutions Environment parameters determine how texture mapping interacts with shading CS 480/680 Chapter 8 -- Discrete Techniques
66
Chapter 8 -- Discrete Techniques
Wrapping Mode Clamping: if s,t > 1 use 1, if s,t <0 use 0 Wrapping: use s,t modulo 1 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP ) glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT ) texture s t GL_CLAMP wrapping GL_REPEAT CS 480/680 Chapter 8 -- Discrete Techniques
67
Chapter 8 -- Discrete Techniques
Magnification and Minification More than one texel can cover a pixel (minification) or more than one pixel can cover a texel (magnification) Can use point sampling (nearest texel) or linear filtering ( 2 x 2 filter) to obtain texture values Texture Polygon Magnification Minification CS 480/680 Chapter 8 -- Discrete Techniques
68
Chapter 8 -- Discrete Techniques
Filter Modes Modes determined by glTexParameteri( target, type, mode ) glTexParameteri(GL_TEXTURE_2D, GL_TEXURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXURE_MIN_FILTER, GL_LINEAR); Note that linear filtering requires a border of an extra texel for filtering at edges (border = 1) CS 480/680 Chapter 8 -- Discrete Techniques
69
Chapter 8 -- Discrete Techniques
Mipmapped Textures Mipmapping allows for prefiltered texture maps of decreasing resolutions Lessens interpolation errors for smaller textured objects Declare mipmap level during texture definition glTexImage2D( GL_TEXTURE_*D, level, … ) GLU mipmap builder routines will build all the textures from a given image gluBuild*DMipmaps( … ) CS 480/680 Chapter 8 -- Discrete Techniques
70
Chapter 8 -- Discrete Techniques
Example point sampling linear filtering mipmapped point sampling mipmapped linear filtering CS 480/680 Chapter 8 -- Discrete Techniques
71
Chapter 8 -- Discrete Techniques
Texture Functions Controls how texture is applied glTexEnv{fi}[v]( GL_TEXTURE_ENV, prop, param ) GL_TEXTURE_ENV_MODE modes GL_MODULATE: modulates with computed shade GL_BLEND: blends with an environmental color GL_REPLACE: use only texture color GL(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); Set blend color with GL_TEXTURE_ENV_COLOR CS 480/680 Chapter 8 -- Discrete Techniques
72
Chapter 8 -- Discrete Techniques
Perspective Correction Hint Texture coordinate and color interpolation either linearly in screen space or using depth/perspective values (slower) Noticeable for polygons “on edge” glHint( GL_PERSPECTIVE_CORRECTION_HINT, hint ) where hint is one of GL_DONT_CARE GL_NICEST GL_FASTEST CS 480/680 Chapter 8 -- Discrete Techniques
73
Chapter 8 -- Discrete Techniques
Generating Texture Coordinates OpenGL can generate texture coordinates automatically glTexGen{ifd}[v]() specify a plane generate texture coordinates based upon distance from the plane generation modes GL_OBJECT_LINEAR GL_EYE_LINEAR GL_SPHERE_MAP (used for environmental maps) CS 480/680 Chapter 8 -- Discrete Techniques
74
Chapter 8 -- Discrete Techniques
Texture Objects Texture is part of the OpenGL state If we have different textures for different objects, OpenGL will be moving large amounts data from processor memory to texture memory Recent versions of OpenGL have texture objects one image per texture object Texture memory can hold multiple texture obejcts CS 480/680 Chapter 8 -- Discrete Techniques
75
Chapter 8 -- Discrete Techniques
Applying Textures II specify textures in texture objects set texture filter set texture function set texture wrap mode set optional perspective correction hint bind texture object enable texturing supply texture coordinates for vertex coordinates can also be generated CS 480/680 Chapter 8 -- Discrete Techniques
76
Chapter 8 -- Discrete Techniques
Other Texture Features Environmental Maps Start with image of environment through a wide angle lens Can be either a real scanned image or an image created in OpenGL Use this texture to generate a spherical map Use automatic texture coordinate generation Multitexturing Apply a sequence of textures through cascaded texture units CS 480/680 Chapter 8 -- Discrete Techniques
77
11. Compositing Techniques
OpenGL provides a mechanism, alpha blending, than can (among other effects) create images with transparent objects The objective of this section is to learn to use the A component in RGBA color for Blending for translucent surfaces Compositing images Antialiasing CS 480/680 Chapter 8 -- Discrete Techniques
78
Chapter 8 -- Discrete Techniques
11.1 Opacity and Blending Opaque surfaces permit no light to pass through Transparent surfaces permit all light to pass Translucent surfaces pass some light translucency = 1 – opacity (a) CS 480/680 Chapter 8 -- Discrete Techniques
79
Chapter 8 -- Discrete Techniques
Physical Models Dealing with translucency in a physically correct manner is difficult due to the complexity of the internal interactions of light and matter Using a pipeline renderer CS 480/680 Chapter 8 -- Discrete Techniques
80
Chapter 8 -- Discrete Techniques
Writing Model Use A component of RGBA (or RGBa) color to store opacity During rendering we can expand our writing model to use RGBA values blend source blending factor destination component source component destination blending factor Color Buffer CS 480/680 Chapter 8 -- Discrete Techniques
81
Chapter 8 -- Discrete Techniques
Blending Equation We can define source and destination blending factors for each RGBA component s = [sr, sg, sb, sa] d = [dr, dg, db, da] Suppose that the source and destination colors are b = [br, bg, bb, ba] c = [cr, cg, cb, ca] Blend as c’ = [br sr+ cr dr, bg sg+ cg dg , bb sb+ cb db , ba sa+ ca da ] CS 480/680 Chapter 8 -- Discrete Techniques
82
Chapter 8 -- Discrete Techniques
11.2 Image Compositing The most straightforward use of a blending is to combine and display several images that exist as pixel maps, or equivalently, as sets of data that have been rendered independently CS 480/680 Chapter 8 -- Discrete Techniques
83
Chapter 8 -- Discrete Techniques
11.3 Blending and Compositing in OpenGL Must enable blending and pick source and destination factors glEnable(GL_BLEND) glBlendFunc(source_factor, destination_factor) Only certain factors supported GL_ZERO, GL_ONE GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA GL_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA See Redbook for complete list CS 480/680 Chapter 8 -- Discrete Techniques
84
Chapter 8 -- Discrete Techniques
Example Suppose that we start with the opaque background color (R0,G0,B0,1) This color becomes the initial destination color We now want to blend in a translucent polygon with color (R1,G1,B1,a1) Select GL_SRC_ALPHA and GL_ONE_MINUS_SRC_ALPHA as the source and destination blending factors R’1 = a1 R1 +(1- a1) R0, …… Note this formula is correct if polygon is either opaque or transparent CS 480/680 Chapter 8 -- Discrete Techniques
85
Chapter 8 -- Discrete Techniques
Clamping and Accuracy All the components (RGBA) are clamped and stay in the range (0,1) However, in a typical system, RGBA values are only stored to 8 bits Can easily loose accuracy if we add many components together Example: add together n images Divide all color components by n to avoid clamping Blend with source factor = 1, destination factor = 1 But division by n loses bits CS 480/680 Chapter 8 -- Discrete Techniques
86
Chapter 8 -- Discrete Techniques
11.4 Antialiasing Line Aliasing Ideal raster line is one pixel wide All line segments, other than vertical and horizontal segments, partially cover pixels Simple algorithms color only whole pixels Lead to the “jaggies” or aliasing Similar issue for polygons CS 480/680 Chapter 8 -- Discrete Techniques
87
Chapter 8 -- Discrete Techniques
Antialiasing Can try to color a pixel by adding a fraction of its color to the frame buffer Fraction depends on percentage of pixel covered by fragment Fraction depends on whether there is overlap no overlap overlap CS 480/680 Chapter 8 -- Discrete Techniques
88
Chapter 8 -- Discrete Techniques
Area Averaging Use average area a1+a2-a1a2 as blending factor CS 480/680 Chapter 8 -- Discrete Techniques
89
Chapter 8 -- Discrete Techniques
OpenGL Antialiasing Can enable separately for points, lines, or polygons glEnable(GL_POINT_SMOOTH); glEnable(GL_LINE_SMOOTH); glEnable(GL_POLYGON_SMOOTH); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); CS 480/680 Chapter 8 -- Discrete Techniques
90
Chapter 8 -- Discrete Techniques
11.5 Back-to-Front and Front-to-Back Rendering Is this image correct? Probably not Polygons are rendered in the order they pass down the pipeline Blending functions are order dependent CS 480/680 Chapter 8 -- Discrete Techniques
91
Chapter 8 -- Discrete Techniques
Opaque and Translucent Polygons Suppose that we have a group of polygons some of which are opaque and some translucent How do we use hidden-surface removal? Opaque polygons block all polygons behind them and affect the depth buffer Translucent polygons should not affect depth buffer Render with glDepthMask(GL_FALSE) which makes depth buffer read-only Sort polygons first to remove order dependency CS 480/680 Chapter 8 -- Discrete Techniques
92
Chapter 8 -- Discrete Techniques
11.6 Depth Cueing and Fog We can composite with a fixed color and have the blending factors depend on depth Simulates a fog effect Blend source color Cs and fog color Cf by Cs’=f Cs + (1-f) Cf f is the fog factor Exponential Gaussian Linear (depth cueing) CS 480/680 Chapter 8 -- Discrete Techniques
93
Chapter 8 -- Discrete Techniques
Fog Functions CS 480/680 Chapter 8 -- Discrete Techniques
94
Chapter 8 -- Discrete Techniques
OpenGL Fog Functions GLfloat fcolor[4] = {……}: glEnable(GL_FOG); glFogf(GL_FOG_MODE, GL_EXP); glFogf(GL_FOG_DENSITY, 0.5); glFOgv(GL_FOG, fcolor); CS 480/680 Chapter 8 -- Discrete Techniques
95
12. Multirendering and the Accumulation Buffer
Compositing and blending are limited by resolution of the frame buffer Typically 8 bits per color component The accumulation buffer is a high resolution buffer (16 or more bits per component) that avoids this problem Write into it or read from it with a scale factor Slower than direct compositing into the frame buffer CS 480/680 Chapter 8 -- Discrete Techniques
96
Chapter 8 -- Discrete Techniques
Summary and Notes In the early days of computer graphics, people worked with only three-dimensional geometric objects, whereas those people who were involved with only two-dimensional images were considered to be working in image processing. Advances in hardware have made graphics and image processing systems practically indistinguishable. CS 480/680 Chapter 8 -- Discrete Techniques
97
Chapter 8 -- Discrete Techniques
The idea that a two-dimensional image or texture can be mapped to a three dimensional surface in no more time than it takes to render the surface with constant shading would have been unthinkable a few years ago. Now these techniques are routine. In this chapter we have concentrated on techniques that are supported by recently available hardware and API’s Many of the techniques introduced here are new, many more are just appearing in the literature; even more remain to be discovered. CS 480/680 Chapter 8 -- Discrete Techniques
98
Chapter 8 -- Discrete Techniques
Suggested Readings Bump mapping was first suggested by Blinn (77). Environmental mapping was developed by Blinn and Newell (76) Hardware support for texture mapping came with the SGI Reality Engine Many of the compositing techniques, including the use of the a channel, were suggested by Porter and Duff (84). The OpenGL Programmer’s Guide contains many examples of how buffers can be used. CS 480/680 Chapter 8 -- Discrete Techniques
99
Exercises -- Due next class
CS 480/680 Chapter 8 -- Discrete Techniques
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.