discrete techniques chapter 7. cs 480/680 2chapter 7 -- discrete techniques introduction:...
TRANSCRIPT
Discrete Discrete TechniquesTechniques
Chapter 7Chapter 7
CS 480/680 2Chapter 7 -- Discrete Techniques
Introduction:Introduction: Texture mapping, antialiasing, Texture mapping, antialiasing,
compositing, and alpha blending are compositing, and alpha blending are only a few of the techniques that only a few of the techniques that become possible when the API allows become possible when the API allows us to work with discrete buffers.us to work with discrete buffers.
This chapter introduces these This chapter introduces these techniques, focusing on those that techniques, focusing on those that are supported by OpenGL and by are supported by OpenGL and by similar APIs.similar APIs.
CS 480/680 3Chapter 7 -- Discrete Techniques
1. Buffers1. Buffers
We have already used two types of We have already used two types of buffers: buffers: Color buffers and Depth Color buffers and Depth buffers. Later we will introduce buffers. Later we will introduce others.others.
What all buffers have in common is What all buffers have in common is that they are inherently discrete.that they are inherently discrete. They have limited resolution, both They have limited resolution, both
spatially and in depth.spatially and in depth.
CS 480/680 4Chapter 7 -- Discrete Techniques
Define a buffer by its spatial Define a buffer by its spatial resolution (resolution (n x mn x m) and its depth ) and its depth kk, the , the number of bits/pixelnumber of bits/pixel
pixel
CS 480/680 5Chapter 7 -- Discrete Techniques
OpenGL defines the frame buffer OpenGL defines the frame buffer as consisting of a variety of as consisting of a variety of buffersbuffers
CS 480/680 6Chapter 7 -- Discrete Techniques
OpenGL buffersOpenGL buffers Color buffers can be displayedColor buffers can be displayed
FrontFront BackBack AuxiliaryAuxiliary OverlayOverlay
DepthDepth AccumulationAccumulation
High resolution bufferHigh resolution buffer StencilStencil
Holds masksHolds masks
The depth of these buffers combined The depth of these buffers combined can exceed a few hundred bits.can exceed a few hundred bits.
CS 480/680 7Chapter 7 -- Discrete Techniques
2. Digital Images2. Digital Images
Before we look at how the graphics system Before we look at how the graphics system can work with digital images through pixel can work with digital images through pixel and bit operations, let’s examine what we and bit operations, let’s examine what we mean by a digital image.mean by a digital image.
Within our programs, we generally work Within our programs, we generally work with images that are arrays of pixels. with images that are arrays of pixels.
These images can be of a variety of sizes These images can be of a variety of sizes and data types, depending upon the type of and data types, depending upon the type of image with which we are working.image with which we are working.
CS 480/680 8Chapter 7 -- Discrete Techniques
Image FormatsImage Formats We often work with images in a We often work with images in a
standard format (JPEG, TIFF, GIF)standard format (JPEG, TIFF, GIF) How do we read/write such images How do we read/write such images
with OpenGL?with OpenGL? No support in OpenGLNo support in OpenGL
OpenGL knows nothing of image formatsOpenGL knows nothing of image formats Some code available on WebSome code available on Web Can write readers/writers for some Can write readers/writers for some
simple formats in OpenGLsimple formats in OpenGL
CS 480/680 9Chapter 7 -- Discrete Techniques
Displaying a PPM ImageDisplaying a PPM Image PPM is a very simple formatPPM is a very simple format Each image file consists of a header Each image file consists of a header
followed by all the pixel datafollowed by all the pixel data HeaderHeader
P3P3
# comment 1# comment 1
# comment 2# comment 2
..
#comment n#comment n
rows columns maxvaluerows columns maxvalue
pixelspixels
CS 480/680 10Chapter 7 -- Discrete Techniques
Reading the HeaderReading the HeaderFILE *fd;FILE *fd;
int k, nm;int k, nm;
char c;char c;
int i;int i;
char b[100];char b[100];
float s;float s;
int red, green, blue;int red, green, blue;
printf("enter file name\n");printf("enter file name\n");
scanf("%s", b);scanf("%s", b);
fd = fopen(b, "r");fd = fopen(b, "r");
fscanf(fd,"%[^\n] ",b);fscanf(fd,"%[^\n] ",b);
if(b[0]!='P'|| b[1] != '3'){if(b[0]!='P'|| b[1] != '3'){
printf("%s is not a PPM file!\n", b);printf("%s is not a PPM file!\n", b);
exit(0);exit(0);
}}
printf("%s is a PPM file\n",b);printf("%s is a PPM file\n",b);
check for “P3” in first line
CS 480/680 11Chapter 7 -- Discrete Techniques
Reading the Header (cont)Reading the Header (cont)
fscanf(fd, "%c",&c);fscanf(fd, "%c",&c);
while(c == '#') while(c == '#')
{{
fscanf(fd, "%[^\n] ", b);fscanf(fd, "%[^\n] ", b);
printf("%s\n",b);printf("%s\n",b);
fscanf(fd, "%c",&c);fscanf(fd, "%c",&c);
}}
ungetc(c,fd); ungetc(c,fd);
skip over comments by looking for # in first skip over comments by looking for # in first columncolumn
CS 480/680 12Chapter 7 -- Discrete Techniques
Reading the DataReading the Data
fscanf(fd, "%d %d %d", &n, &m, &k);fscanf(fd, "%d %d %d", &n, &m, &k);
printf("%d rows %d columns max value= %d\printf("%d rows %d columns max value= %d\n",n,m,k);n",n,m,k);
nm = n*m;nm = n*m;
image=malloc(3*sizeof(GLuint)*nm);image=malloc(3*sizeof(GLuint)*nm);
s=255./k;s=255./k;
for(i=0;i<nm;i++)for(i=0;i<nm;i++)
{{
fscanf(fd,"%d %d %d",&red, &green, &blue );fscanf(fd,"%d %d %d",&red, &green, &blue );
image[3*nm-3*i-3]=red;image[3*nm-3*i-3]=red;
image[3*nm-3*i-2]=green;image[3*nm-3*i-2]=green;
image[3*nm-3*i-1]=blue;image[3*nm-3*i-1]=blue;
}}
CS 480/680 13Chapter 7 -- Discrete Techniques
Scaling the Image DataScaling the Image Data We can scale the image in the pipelineWe can scale the image in the pipeline
glPixelTransferf(GL_RED_SCALE, s);glPixelTransferf(GL_RED_SCALE, s);glPixelTransferf(GL_GREEN_SCALE, s);glPixelTransferf(GL_GREEN_SCALE, s);glPixelTransferf(GL_BLUE_SCALE, s);glPixelTransferf(GL_BLUE_SCALE, s);
We may have to swap bytes when we go from We may have to swap bytes when we go from processor memory to the frame buffer processor memory to the frame buffer depending on the processor. If so, we can usedepending on the processor. If so, we can use
glPixelStorei(GL_UNPACK_SWAP_BYTES,GL_TRUE);glPixelStorei(GL_UNPACK_SWAP_BYTES,GL_TRUE);
CS 480/680 14Chapter 7 -- Discrete Techniques
The display callbackThe display callback
void display()void display(){{
glClear(GL_COLOR_BUFFER_BIT);glClear(GL_COLOR_BUFFER_BIT);glRasterPos2i(0,0);glRasterPos2i(0,0);
glDrawPixels(n,m,GL_RGB, glDrawPixels(n,m,GL_RGB, GL_UNSIGNED_INT, image);GL_UNSIGNED_INT, image);
glFlush();glFlush();}}
CS 480/680 15Chapter 7 -- Discrete Techniques
3. Writing into Buffers3. Writing into Buffers Conceptually, we can consider all of memory Conceptually, we can consider all of memory
as a large two-dimensional array of pixelsas a large two-dimensional array of pixels We read and write rectangular block of pixelsWe read and write rectangular block of pixels
Bit block transferBit block transfer ( (bitbltbitblt) ) operationsoperations The frame buffer is part of this memoryThe frame buffer is part of this memory
frame buffer(destination)
writing into frame buffer
sourcememory
CS 480/680 16Chapter 7 -- Discrete Techniques
Note that, from the hardware Note that, from the hardware perspective, the type of processing perspective, the type of processing involved has none of the characteristics involved has none of the characteristics of the processing of geometric objects.of the processing of geometric objects.
Consequently the hardware that Consequently the hardware that optimizes bitblt operations has a optimizes bitblt operations has a completely different architecture from completely different architecture from the pipeline hardware that we used for the pipeline hardware that we used for geometric operations.geometric operations.
Thus, the OpenGL architecture Thus, the OpenGL architecture contains both a geometric pipeline and contains both a geometric pipeline and a pixel pipeline, each of which is a pixel pipeline, each of which is usually implemented separatelyusually implemented separately
CS 480/680 17Chapter 7 -- Discrete Techniques
3.1 Writing Modes 3.1 Writing Modes Read destination pixel before writing Read destination pixel before writing
sourcesource
CS 480/680 18Chapter 7 -- Discrete Techniques
Writing ModesWriting Modes Source and destination bits are Source and destination bits are
combined bitwisecombined bitwise 16 possible functions (one per column in 16 possible functions (one per column in
table)table)replace ORXOR
CS 480/680 19Chapter 7 -- Discrete Techniques
3.2 Writing with XOR3.2 Writing with XOR Recall from Chapter 3 that we can use XOR Recall from Chapter 3 that we can use XOR
by enabling logic operations and selecting by enabling logic operations and selecting the XOR write modethe XOR write mode
XOR is especially useful for swapping XOR is especially useful for swapping blocks of memory such as menus that are blocks of memory such as menus that are stored off screenstored off screen
If If SS represents screen and represents screen and MM represents represents a menua menu
the sequencethe sequence S S S S M M M M S S M M S S S S M M
swaps the S and Mswaps the S and M
CS 480/680 20Chapter 7 -- Discrete Techniques
4. Bit and Pixel Operations 4. Bit and Pixel Operations in OpenGLin OpenGL
Not only does OpenGL support a separate Not only does OpenGL support a separate pixel pipeline and a variety of buffers, but pixel pipeline and a variety of buffers, but also data can be moved among these also data can be moved among these buffers and between buffers and the buffers and between buffers and the processor memory.processor memory.
The plethora of formats and types can The plethora of formats and types can make writing efficient code for dealing make writing efficient code for dealing with bits and pixels a difficult task. We with bits and pixels a difficult task. We shall not discuss what the details are, but shall not discuss what the details are, but instead shall look at what capabilities are instead shall look at what capabilities are supported.supported.
CS 480/680 21Chapter 7 -- Discrete Techniques
4.1 OpenGL Buffers and the Pixel 4.1 OpenGL Buffers and the Pixel PipelinePipeline OpenGL has a separate pipeline for OpenGL has a separate pipeline for
pixelspixels Writing pixels involves Writing pixels involves
Moving pixels from processor memory to the Moving pixels from processor memory to the frame bufferframe buffer
Format conversionsFormat conversions Mapping, Lookups, TestsMapping, Lookups, Tests
Reading pixelsReading pixels Format conversionFormat conversion
CS 480/680 22Chapter 7 -- Discrete Techniques
Raster PositionRaster Position OpenGL maintains a OpenGL maintains a raster positionraster position
as part of the stateas part of the state Set by Set by glRasterPos*()glRasterPos*()
glRasterPos3f(x, y, z);glRasterPos3f(x, y, z);
The raster position is a geometric The raster position is a geometric entityentity Passes through geometric pipelinePasses through geometric pipeline Eventually yields a 2D position in screen Eventually yields a 2D position in screen
coordinatescoordinates This position in the frame buffer is where This position in the frame buffer is where
the next raster primitive is drawnthe next raster primitive is drawn
CS 480/680 23Chapter 7 -- Discrete Techniques
Buffer Selection Buffer Selection OpenGL can draw into or read from any of OpenGL can draw into or read from any of
the color buffers (front, back, auxiliary)the color buffers (front, back, auxiliary) Default to the back bufferDefault to the back buffer Change withChange with glDrawBuffer glDrawBuffer andand glReadBufferglReadBuffer
Note that format of the pixels in the frame Note that format of the pixels in the frame buffer is different from that of processor buffer is different from that of processor memory and these two types of memory memory and these two types of memory reside in different placesreside in different places
Need packing and unpackingNeed packing and unpacking Drawing and reading can be slowDrawing and reading can be slow
CS 480/680 24Chapter 7 -- Discrete Techniques
BitmapsBitmaps OpenGL treats 1-bit pixels (OpenGL treats 1-bit pixels (bitmapsbitmaps) )
differently from multi-bit pixels differently from multi-bit pixels ((pixelmapspixelmaps))
Bitmaps are masks that determine if Bitmaps are masks that determine if the corresponding pixel in the frame the corresponding pixel in the frame buffer is drawn with the buffer is drawn with the present present raster colorraster color
0 0 color unchanged color unchanged 1 1 color changed based on writing mode color changed based on writing mode
Bitmaps are useful for raster textBitmaps are useful for raster text GLUT font:GLUT font: GLUT_BIT_MAP_8_BY_13GLUT_BIT_MAP_8_BY_13
CS 480/680 25Chapter 7 -- Discrete Techniques
Raster ColorRaster Color Same as drawing color set by Same as drawing color set by glColor*()glColor*()
Fixed by last call to Fixed by last call to glRasterPos*()glRasterPos*() glColor3f(1.0, 0.0, 0.0);glColor3f(1.0, 0.0, 0.0); glRasterPos3f(x, y, z);glRasterPos3f(x, y, z); glColor3f(0.0, 0.0, 1.0);glColor3f(0.0, 0.0, 1.0); glBitmap(…….glBitmap(……. glBegin(GL_LINES);glBegin(GL_LINES); glVertex3f(…..)glVertex3f(…..)
Geometry drawn in blueGeometry drawn in blue Ones in bitmap use a drawing color Ones in bitmap use a drawing color
of redof red
CS 480/680 26Chapter 7 -- Discrete Techniques
4.2 Bitmaps 4.2 Bitmaps OpenGL treats arrays of one-bit OpenGL treats arrays of one-bit
pixels (bitmaps) differently from pixels (bitmaps) differently from multibit pixels (pixelmaps) and multibit pixels (pixelmaps) and provides different programming provides different programming interfaces for the two cases.interfaces for the two cases.
Bitmaps are used for fonts and for Bitmaps are used for fonts and for displaying the cursor, often making displaying the cursor, often making use of the XOR writing mode. use of the XOR writing mode.
CS 480/680 27Chapter 7 -- Discrete Techniques
glBitmap(width, height, x0, y0, xi, yi, bitmap)glBitmap(width, height, x0, y0, xi, yi, bitmap)
first raster position
second raster position
offset from raster position
increments in raster position after bitmap drawn
CS 480/680 28Chapter 7 -- Discrete Techniques
Example: Checker BoardExample: Checker BoardGLubyte wb[2] = {0 x 00, 0 x ff};GLubyte wb[2] = {0 x 00, 0 x ff};GLubyte check[512];GLubyte check[512];int i, j;int i, j;for(i=0; i<64; i++) for (j=0; j<64, j++)for(i=0; i<64; i++) for (j=0; j<64, j++) check[i*8+j] = wb[(i/8+j)%2];check[i*8+j] = wb[(i/8+j)%2];
glBitmap( 64, 64, 0.0, 0.0, 0.0, 0.0, check);glBitmap( 64, 64, 0.0, 0.0, 0.0, 0.0, check);
CS 480/680 29Chapter 7 -- Discrete Techniques
4.4 Pixels and Images4.4 Pixels and Images OpenGL works with rectangular arrays OpenGL works with rectangular arrays
of pixels called pixel maps or imagesof pixels called pixel maps or images Pixels are in one byte ( 8 bit) chunksPixels are in one byte ( 8 bit) chunks
Luminance (gray scale) images 1 byte/pixelLuminance (gray scale) images 1 byte/pixel RGB 3 bytes/pixelRGB 3 bytes/pixel
Three functionsThree functions Draw pixels: processor memory to frame Draw pixels: processor memory to frame
bufferbuffer Read pixels: frame buffer to processor Read pixels: frame buffer to processor
memorymemory Copy pixels: frame buffer to frame bufferCopy pixels: frame buffer to frame buffer
CS 480/680 30Chapter 7 -- Discrete Techniques
OpenGL Pixel FunctionsOpenGL Pixel Functions
glReadPixels(x,y,width,height,format,type,myimage)
start pixel in frame buffer size
type of image
type of pixels
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 31Chapter 7 -- Discrete Techniques
4.5 Lookup Tables4.5 Lookup Tables In OpenGL, all colors can be modified In OpenGL, all colors can be modified
by lookup tables as they are placed by lookup tables as they are placed into buffers.into buffers. These maps are essentially the same as These maps are essentially the same as
the color lookup tables that we the color lookup tables that we introduced in Chapter 2introduced in Chapter 2
CS 480/680 32Chapter 7 -- Discrete Techniques
4.6 Buffers for Picking4.6 Buffers for Picking You can use the read and write You can use the read and write
capabilities of the buffers to allow capabilities of the buffers to allow you to do picking quite easily.you to do picking quite easily. The “colors” in the extra buffer are The “colors” in the extra buffer are
identifiers of the objects rendered at that identifiers of the objects rendered at that pixel location.pixel location.
If we combine this with z-buffer, then If we combine this with z-buffer, then each pixel has the identifier of the each pixel has the identifier of the closest object to the camera. closest object to the camera.
CS 480/680 33Chapter 7 -- Discrete Techniques
5. Mapping Methods5. Mapping Methods
Although graphics cards can Although graphics cards can render over 10 million polygons render over 10 million polygons per second, that number is per second, that number is insufficient for many phenomenainsufficient for many phenomena CloudsClouds GrassGrass TerrainTerrain SkinSkin
CS 480/680 34Chapter 7 -- Discrete Techniques
Modeling an OrangeModeling an Orange Consider the problem of modeling an Consider the problem of modeling an
orange (the fruit)orange (the fruit) Start with an orange-colored sphereStart with an orange-colored sphere
Too simpleToo simple Replace sphere with a more complex Replace sphere with a more complex
shapeshape Does not capture surface characteristics Does not capture surface characteristics
(small dimples)(small dimples) Takes too many polygons to model all the Takes too many polygons to model all the
dimplesdimples
CS 480/680 35Chapter 7 -- Discrete Techniques
Modeling an Orange (2)Modeling an Orange (2) Take a picture of a real orange, scan Take a picture of a real orange, scan
it, and “paste” onto simple geometric it, and “paste” onto simple geometric modelmodel This process is known as texture This process is known as texture
mappingmapping Still might not be sufficient because Still might not be sufficient because
resulting surface will be smoothresulting surface will be smooth Need to change local shapeNeed to change local shape Bump mappingBump mapping
CS 480/680 36Chapter 7 -- Discrete Techniques
Three Types of MappingThree Types of Mapping Texture MappingTexture Mapping
Uses images to fill inside of polygonsUses images to fill inside of polygons Environmental (reflection mapping)Environmental (reflection mapping)
Uses a picture of the environment for Uses a picture of the environment for texture mapstexture maps
Allows simulation of highly specular Allows simulation of highly specular surfacessurfaces
Bump mappingBump mapping Emulates altering normal vectors during Emulates altering normal vectors during
the rendering processthe rendering process
CS 480/680 37Chapter 7 -- Discrete Techniques
Texture MappingTexture Mapping
geometric model texture mapped
CS 480/680 38Chapter 7 -- Discrete Techniques
Environment MappingEnvironment Mapping
CS 480/680 39Chapter 7 -- Discrete Techniques
Bump MappingBump Mapping
CS 480/680 40Chapter 7 -- Discrete Techniques
6. Texture Mapping6. Texture Mapping Textures are patterns.Textures are patterns.
They can range from regular patterns, such They can range from regular patterns, such as stripes and checkerboards, to the as stripes and checkerboards, to the complex patterns that characterize natural complex patterns that characterize natural materials.materials.
Textures can be 1, 2, 3, and 4 dimensionalTextures can be 1, 2, 3, and 4 dimensional 1D – used to create a pattern for a curve.1D – used to create a pattern for a curve. 3D – a block of material used to sculpt an object.3D – a block of material used to sculpt an object.
2D is by far the most common.2D is by far the most common.
CS 480/680 41Chapter 7 -- Discrete Techniques
Where does mapping take place?Where does mapping take place? Mapping techniques are implemented at Mapping techniques are implemented at
the end of the rendering pipelinethe end of the rendering pipeline Very efficient because few polygons Very efficient because few polygons
make it past the clipper make it past the clipper
CS 480/680 42Chapter 7 -- Discrete Techniques
Is it simple?Is it simple? Although the idea is simple---map an Although the idea is simple---map an
image to a surface---there are 3 or 4 image to a surface---there are 3 or 4 coordinate systems involvedcoordinate systems involved
2D image
3D surface
CS 480/680 43Chapter 7 -- Discrete Techniques
Coordinate SystemsCoordinate Systems Parametric coordinatesParametric coordinates
May be used to model curved surfacesMay be used to model curved surfaces Texture coordinatesTexture coordinates
Used to identify points in the image to be Used to identify points in the image to be mappedmapped
World CoordinatesWorld Coordinates Conceptually, where the mapping takes Conceptually, where the mapping takes
placeplace Screen CoordinatesScreen Coordinates
Where the final image is really producedWhere the final image is really produced
CS 480/680 44Chapter 7 -- Discrete Techniques
6.1 Two-Dimensional Texture 6.1 Two-Dimensional Texture MappingMapping
CS 480/680 45Chapter 7 -- Discrete Techniques
Mapping FunctionsMapping Functions Basic problem is how to find the Basic problem is how to find the
mapsmaps Consider mapping from texture Consider mapping from texture
coordinates to a point a surface coordinates to a point a surface Appear to need three functionsAppear to need three functions
x = x(s,t)x = x(s,t)y = y(s,t)y = y(s,t)z = z(s,t)z = z(s,t)
But we really want But we really want to go the other wayto go the other way
s
t
(x,y,z)
CS 480/680 46Chapter 7 -- Discrete Techniques
Backward MappingBackward Mapping We really want to go backwardsWe really want to go backwards
Given a pixel, we want to know to which Given a pixel, we want to know to which point on an object it correspondspoint on an object it corresponds
Given a point on an object, we want to Given a point on an object, we want to know to which point in the texture it know to which point in the texture it correspondscorresponds
Need a map of the form Need a map of the form
s = s(x,y,z)s = s(x,y,z)
t = t(x,y,z)t = t(x,y,z) Such functions are difficult to find in Such functions are difficult to find in
generalgeneral
CS 480/680 47Chapter 7 -- Discrete Techniques
Two-part mappingTwo-part mapping One solution to the mapping problem One solution to the mapping problem
is to first map the texture to a simple is to first map the texture to a simple intermediate surfaceintermediate surface
Example: map to cylinderExample: map to cylinder
CS 480/680 48Chapter 7 -- Discrete Techniques
Cylindrical MappingCylindrical Mapping
parametric cylinder
x = r cos 2 uy = r sin 2uz = v/h
maps rectangle in u,v space to cylinderof radius r and height h in world coordinates
s = ut = v
maps from texture space
CS 480/680 49Chapter 7 -- Discrete Techniques
Spherical MapSpherical MapWe can use a parametric sphereWe can use a parametric sphere
x = r cos 2uy = r sin 2u cos 2vz = r sin 2u sin 2v
in a similar manner to the cylinderbut have to decide where to putthe distortion
Spheres are used in environmental maps
CS 480/680 50Chapter 7 -- Discrete Techniques
Box MappingBox Mapping Easy to use with simple orthographic Easy to use with simple orthographic
projectionprojection Also used in environmental mapsAlso used in environmental maps
CS 480/680 51Chapter 7 -- Discrete Techniques
Second MappingSecond Mapping Map from intermediate object to actual Map from intermediate object to actual
objectobject Normals from intermediate to actualNormals from intermediate to actual Normals from actual to intermediateNormals from actual to intermediate Vectors from center of intermediateVectors from center of intermediate
CS 480/680 52Chapter 7 -- Discrete Techniques
AliasingAliasing Point sampling of the texture can Point sampling of the texture can
lead to aliasing errorslead to aliasing errors
point samples in u,v (or x,y,z) space
point samples in texture space
miss blue stripes
CS 480/680 53Chapter 7 -- Discrete Techniques
Area AveragingArea Averaging A better but slower option is to use A better but slower option is to use area area
averagingaveraging
Note that preimage of pixel is curved
pixelpreimage
CS 480/680 54Chapter 7 -- Discrete Techniques
6.2 Texture Mapping in OpenGL 6.2 Texture Mapping in OpenGL Three steps to applying a texture Three steps to applying a texture
specify the texturespecify the texture read or generate imageread or generate image assign to textureassign to texture enable texturing enable texturing
assign texture coordinates to vertices assign texture coordinates to vertices Proper mapping function is left to Proper mapping function is left to
application application specify texture parametersspecify texture parameters
wrapping, filteringwrapping, filtering
CS 480/680 55Chapter 7 -- Discrete Techniques
Texture MappingTexture Mapping
s
t
x
y
z
image
geometry screen
CS 480/680 56Chapter 7 -- Discrete Techniques
Texture ExampleTexture Example The texture (below) is The texture (below) is
a 256 x 256 image a 256 x 256 image that has been mapped that has been mapped to a rectangular to a rectangular polygon which is polygon which is viewed in perspectiveviewed in perspective
CS 480/680 57Chapter 7 -- Discrete Techniques
Texture Mapping and the OpenGL Texture Mapping and the OpenGL PipelinePipeline Images and geometry flow through Images and geometry flow through
separate pipelines that join at the separate pipelines that join at the rasterizerrasterizer ““complex” textures do not affect complex” textures do not affect
geometric complexitygeometric complexitygeometry pipelinevertices
pixel pipelineimage
rasterizer
CS 480/680 58Chapter 7 -- Discrete Techniques
Specify Texture ImageSpecify Texture Image Define a texture image from an array of Define a texture image from an array of
texelstexels (texture elements) in CPU memory (texture elements) in CPU memory Glubyte my_texels[512][512];Glubyte my_texels[512][512];
Define as any other pixel mapDefine as any other pixel map Scanned imageScanned image Generate by application codeGenerate by application code
Enable texture mappingEnable texture mapping glEnable(GL_TEXTURE_2D)glEnable(GL_TEXTURE_2D) OpenGL supports 1-4 dimensional texture mapsOpenGL supports 1-4 dimensional texture maps
CS 480/680 59Chapter 7 -- Discrete Techniques
Define Image as a TextureDefine Image as a TextureglTexImage2D( target, level, components,glTexImage2D( target, level, components,
w, h, border, format, type, texels w, h, border, format, type, texels ); );
target: target: type of texture, e.g.type of texture, e.g. GL_TEXTURE_2D GL_TEXTURE_2D
level: level: used for mipmapping (discussed used for mipmapping (discussed later)later)components: components: elements per texelelements per texelw, h: w, h: width and height ofwidth and height of texels texels in pixelsin pixelsborder: border: used for smoothing (discussed later)used for smoothing (discussed later)format and type: format and type: describe texelsdescribe texelstexels: texels: pointer to texel arraypointer to texel array
glTexImage2D(GL_TEXTURE_2D, 0, 3, 512, 512, glTexImage2D(GL_TEXTURE_2D, 0, 3, 512, 512, 0, GL_RGB, GL_UNSIGNED_BYTE, my_texels);0, GL_RGB, GL_UNSIGNED_BYTE, my_texels);
CS 480/680 60Chapter 7 -- Discrete Techniques
Converting A Texture ImageConverting A Texture Image OpenGL requires texture dimensions to OpenGL requires texture dimensions to
be powers of 2be powers of 2 If dimensions of image are not powers of If dimensions of image are not powers of
22 gluScaleImage( gluScaleImage( format, w_in, h_in,format, w_in, h_in, type_in, *data_in, w_out, h_out, type_in, *data_in, w_out, h_out, type_out, *data_out type_out, *data_out ); );
data_indata_in is source imageis source image data_outdata_out is for destination imageis for destination image
Image interpolated and filtered during Image interpolated and filtered during scalingscaling
CS 480/680 61Chapter 7 -- Discrete Techniques
Mapping a TextureMapping a Texture Based on parametric texture coordinates Based on parametric texture coordinates glTexCoord*()glTexCoord*() specified at each vertex specified at each vertex
s
t1, 1
0, 1
0, 0 1, 0
(s, t) = (0.2, 0.8)
(0.4, 0.2)
(0.8, 0.4)
A
B C
a
bc
Texture Space Object Space
CS 480/680 62Chapter 7 -- Discrete Techniques
Typical CodeTypical Code
glBegin(GL_POLYGON);glBegin(GL_POLYGON);glColor3f(r0, g0, b0);glColor3f(r0, g0, b0);glNormal3f(u0, v0, w0);glNormal3f(u0, v0, w0);glTexCoord2f(s0, t0);glTexCoord2f(s0, t0);glVertex3f(x0, y0, z0);glVertex3f(x0, y0, z0);glColor3f(r1, g1, b1);glColor3f(r1, g1, b1);glNormal3f(u1, v1, w1);glNormal3f(u1, v1, w1);glTexCoord2f(s1, t1);glTexCoord2f(s1, t1);glVertex3f(x1, y1, z1);glVertex3f(x1, y1, z1);
..
..glEnd();glEnd();
Note that we can use vertex arrays to Note that we can use vertex arrays to increase efficiencyincrease efficiency
CS 480/680 63Chapter 7 -- Discrete Techniques
InterpolationInterpolation OpenGL uses bilinear interpolation to find OpenGL uses bilinear interpolation to find
proper texels from specified texture proper texels from specified texture coordinates. There can be distortionscoordinates. There can be distortions
good selectionof tex coordinates
poor selectionof tex coordinates
texture stretchedover trapezoid showing effects of bilinear interpolation
CS 480/680 64Chapter 7 -- Discrete Techniques
Texture ParametersTexture Parameters OpenGL has a variety of parameters OpenGL has a variety of parameters
that determine how texture is appliedthat determine how texture is applied Wrapping parameters determine what Wrapping parameters determine what
happens of s and t are outside the (0,1) happens of s and t are outside the (0,1) rangerange
Filter modes allow us to use area Filter modes allow us to use area averaging instead of point samplesaveraging instead of point samples
Mipmapping allows us to use textures at Mipmapping allows us to use textures at multiple resolutionsmultiple resolutions
Environment parameters determine how Environment parameters determine how texture mapping interacts with shadingtexture mapping interacts with shading
CS 480/680 65Chapter 7 -- Discrete Techniques
Wrapping Mode Wrapping Mode Clamping: if Clamping: if s,t > 1s,t > 1 use 1, if use 1, if s,t <0s,t <0 use 0 use 0 Wrapping: use Wrapping: use s,ts,t modulo 1 modulo 1glTexParameteri( GL_TEXTURE_2D, glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP ) GL_TEXTURE_WRAP_S, GL_CLAMP )
glTexParameteri( GL_TEXTURE_2D, glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT ) GL_TEXTURE_WRAP_T, GL_REPEAT )
texture
s
t
GL_CLAMPwrapping
GL_REPEATwrapping
CS 480/680 66Chapter 7 -- Discrete Techniques
Magnification and MinificationMagnification and Minification More than one texel can cover a pixel More than one texel can cover a pixel
((minificationminification) or more than one pixel can cover ) or more than one pixel can cover a texel (a texel (magnificationmagnification))
Can use point sampling (nearest texel) or linear Can use point sampling (nearest texel) or linear filtering ( 2 x 2 filter) to obtain texture values filtering ( 2 x 2 filter) to obtain texture values
Texture Polygon
Magnification Minification
PolygonTexture
CS 480/680 67Chapter 7 -- Discrete Techniques
Filter ModesFilter Modes
Modes determined byModes determined by glTexParameteri( target, type, mode )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 68Chapter 7 -- Discrete Techniques
Mipmapped TexturesMipmapped Textures MipmappingMipmapping allows for prefiltered texture allows for prefiltered texture
maps of decreasing resolutionsmaps of decreasing resolutions Lessens interpolation errors for smaller Lessens interpolation errors for smaller
textured objectstextured objects Declare mipmap level during texture Declare mipmap level during texture
definitiondefinitionglTexImage2D( GL_TEXTURE_*D, level,glTexImage2D( GL_TEXTURE_*D, level, … )… )
GLU mipmap builder routines will build GLU mipmap builder routines will build all the textures from a given imageall the textures from a given imagegluBuild*DMipmaps( … )gluBuild*DMipmaps( … )
CS 480/680 69Chapter 7 -- Discrete Techniques
ExampleExample
pointpointsamplingsampling
mipmapped pointsampling
mipmapped linear filtering
linear filtering
CS 480/680 70Chapter 7 -- Discrete Techniques
Texture FunctionsTexture Functions Controls how texture is appliedControls how texture is applied glTexEnv{fi}[v](glTexEnv{fi}[v]( GL_TEXTURE_ENV, prop, GL_TEXTURE_ENV, prop, param )param )
GL_TEXTURE_ENV_MODEGL_TEXTURE_ENV_MODE modesmodes GL_MODULATE: GL_MODULATE: modulates with computed shademodulates with computed shade GL_BLEND: GL_BLEND: blends with an environmental colorblends with an environmental color GL_REPLACE: GL_REPLACE: use only texture coloruse only texture color GL(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);GL_MODULATE);
Set blend color withSet blend color with GL_TEXTURE_ENV_COLORGL_TEXTURE_ENV_COLOR
CS 480/680 71Chapter 7 -- Discrete Techniques
Perspective Correction HintPerspective Correction Hint Texture coordinate and color interpolationTexture coordinate and color interpolation
either linearly in screen spaceeither linearly in screen space or using depth/perspective values (slower)or using depth/perspective values (slower)
Noticeable for polygons “on edge”Noticeable for polygons “on edge” glHint(glHint( GL_PERSPECTIVE_CORRECTION_HINT, hintGL_PERSPECTIVE_CORRECTION_HINT, hint ))
where where hinthint is one of is one of GL_DONT_CAREGL_DONT_CARE GL_NICESTGL_NICEST GL_FASTESTGL_FASTEST
CS 480/680 72Chapter 7 -- Discrete Techniques
Generating Texture CoordinatesGenerating Texture Coordinates OpenGL can generate texture coordinates OpenGL can generate texture coordinates
automaticallyautomaticallyglTexGen{ifd}[v]()glTexGen{ifd}[v]()
specify a planespecify a plane generate texture coordinates based upon generate texture coordinates based upon
distance from the planedistance from the plane generation modesgeneration modes
GL_OBJECT_LINEARGL_OBJECT_LINEAR GL_EYE_LINEARGL_EYE_LINEAR GL_SPHERE_MAP GL_SPHERE_MAP (used for environmental maps)(used for environmental maps)
CS 480/680 73Chapter 7 -- Discrete Techniques
6.3 Texture Objects6.3 Texture Objects Texture is part of the OpenGL state Texture is part of the OpenGL state
If we have different textures for different If we have different textures for different objects, OpenGL will be moving large objects, OpenGL will be moving large amounts data from processor memory to amounts data from processor memory to texture memorytexture memory
Recent versions of OpenGL have Recent versions of OpenGL have texture texture objectsobjects
one image per texture objectone image per texture object Texture memory can hold multiple texture Texture memory can hold multiple texture
obejctsobejcts
CS 480/680 74Chapter 7 -- Discrete Techniques
Applying Textures IIApplying Textures II specify textures in texture objectsspecify textures in texture objects set texture filter set texture filter set texture function set texture function set texture wrap modeset texture wrap mode set optional perspective correction hintset optional perspective correction hint bind texture object bind texture object enable texturingenable texturing supply texture coordinates for vertexsupply texture coordinates for vertex
coordinates can also be generatedcoordinates can also be generated
CS 480/680 75Chapter 7 -- Discrete Techniques
Other Texture FeaturesOther Texture Features Environmental MapsEnvironmental Maps
Start with image of environment through Start with image of environment through a wide angle lens a wide angle lens
Can be either a real scanned image or an Can be either a real scanned image or an image created in OpenGLimage created in OpenGL
Use this texture to generate a spherical Use this texture to generate a spherical mapmap
Use automatic texture coordinate Use automatic texture coordinate generationgeneration
MultitexturingMultitexturing Apply a sequence of textures through Apply a sequence of textures through
cascaded texture unitscascaded texture units
CS 480/680 76Chapter 7 -- Discrete Techniques
9. Compositing 9. Compositing TechniquesTechniques
OpenGL provides a mechanism, alpha OpenGL provides a mechanism, alpha blending, than can (among other blending, than can (among other effects) create images with transparent effects) create images with transparent objectsobjects
The objective of this section is to learn The objective of this section is to learn to use the A component in RGBA color to use the A component in RGBA color forfor Blending for translucent surfacesBlending for translucent surfaces Compositing imagesCompositing images AntialiasingAntialiasing
CS 480/680 77Chapter 7 -- Discrete Techniques
9.1 Opacity and Blending9.1 Opacity and Blending Opaque surfaces permit no light to Opaque surfaces permit no light to
pass throughpass through Transparent surfaces permit all Transparent surfaces permit all
light to passlight to pass Translucent surfaces pass some Translucent surfaces pass some
light translucency = 1 – opacity (light translucency = 1 – opacity ())
CS 480/680 78Chapter 7 -- Discrete Techniques
Physical ModelsPhysical Models Dealing with translucency in a physically Dealing with translucency in a physically
correct manner is difficult due tocorrect manner is difficult due to the complexity of the internal the complexity of the internal
interactions of light and matterinteractions of light and matter Using a pipeline rendererUsing a pipeline renderer
CS 480/680 79Chapter 7 -- Discrete Techniques
Writing ModelWriting Model Use A component of RGBA (or Use A component of RGBA (or
RGBRGB) color to store opacity) color to store opacity During rendering we can expand During rendering we can expand
our writing model to use RGBA our writing model to use RGBA values values
Color Buffer
destinationcomponent
blend
destination blending factor
source blending factor sourcecomponent
CS 480/680 80Chapter 7 -- Discrete Techniques
Blending EquationBlending Equation We can define source and destination We can define source and destination
blending factors for each RGBA componentblending factors for each RGBA component
ss = [s = [srr, s, sgg, s, sbb, s, s]]
dd = [d = [drr, d, dgg, d, dbb, d, d]]
Suppose that the source and destination Suppose that the source and destination colors arecolors are
bb = [b = [brr, b, bgg, b, bbb, b, b]]
cc = [c = [crr, c, cgg, c, cbb, c, c]]
Blend asBlend ascc’ = [b’ = [br r ssrr+ c+ cr r ddrr, b, bg g ssgg+ c+ cg g ddg g , b, bb b ssbb+ c+ cb b ddb b , b, b ss+ c+ c dd ]]
CS 480/680 81Chapter 7 -- Discrete Techniques
9.2 Image Compositing9.2 Image Compositing The most straightforward use of The most straightforward use of
blending is to combine and display blending is to combine and display several images that exist as pixel several images that exist as pixel maps, or equivalently, as sets of data maps, or equivalently, as sets of data that have been rendered that have been rendered independentlyindependently
CS 480/680 82Chapter 7 -- Discrete Techniques
9.3 Blending and Compositing in 9.3 Blending and Compositing in OpenGLOpenGL Must enable blending and pick Must enable blending and pick
source and destination factorssource and destination factors glEnable(GL_BLEND)glEnable(GL_BLEND) glBlendFunc(source_factor, glBlendFunc(source_factor, destination_factor)destination_factor) Only certain factors supportedOnly certain factors supported
GL_ZERO, GL_ONEGL_ZERO, GL_ONE GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHAGL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA GL_DST_ALPHA, GL_ONE_MINUS_DST_ALPHAGL_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA See Redbook for complete listSee Redbook for complete list
CS 480/680 83Chapter 7 -- Discrete Techniques
ExampleExample Suppose that we start with the opaque Suppose that we start with the opaque
background color (Rbackground color (R00,G,G00,B,B00,1) ,1) This color becomes the initial destination This color becomes the initial destination
colorcolor We now want to blend in a translucent We now want to blend in a translucent
polygon with color (Rpolygon with color (R11,G,G11,B,B11,,11)) Select Select GL_SRC_ALPHAGL_SRC_ALPHA and and GL_ONE_MINUS_SRC_ALPHAGL_ONE_MINUS_SRC_ALPHA as the source and as the source and destination blending factorsdestination blending factors
RR’’1 1 = = 11 R R1 1 +(1- +(1- 11) R) R0, 0, …… ……
Note this formula is correct if polygon is Note this formula is correct if polygon is either opaque or transparenteither opaque or transparent
CS 480/680 84Chapter 7 -- Discrete Techniques
Clamping and AccuracyClamping and Accuracy All the components (RGBA) are All the components (RGBA) are
clamped and stay in the range (0,1)clamped and stay in the range (0,1) However, in a typical system, RGBA However, in a typical system, RGBA
values are only stored to 8 bitsvalues are only stored to 8 bits Can easily loose accuracy if we add many Can easily loose accuracy if we add many
components togethercomponents together Example: add together n imagesExample: add together n images
Divide all color components by n to avoid Divide all color components by n to avoid clampingclamping
Blend with source factor = 1, destination Blend with source factor = 1, destination factor = 1factor = 1
But division by n loses bitsBut division by n loses bits
CS 480/680 85Chapter 7 -- Discrete Techniques
9.4 Antialiasing9.4 Antialiasing Line AliasingLine Aliasing
Ideal raster line is one pixel wideIdeal raster line is one pixel wide All line segments, other than vertical and All line segments, other than vertical and
horizontal segments, partially cover horizontal segments, partially cover pixelspixels
Simple algorithms colorSimple algorithms color
only whole pixelsonly whole pixels Lead to the “jaggies”Lead to the “jaggies”
or aliasingor aliasing Similar issue for polygonsSimilar issue for polygons
CS 480/680 86Chapter 7 -- Discrete Techniques
AntialiasingAntialiasing Can try to color a pixel by adding a fraction Can try to color a pixel by adding a fraction
of its color to the frame bufferof its color to the frame buffer Fraction depends on percentage of pixel Fraction depends on percentage of pixel
covered by fragment covered by fragment Fraction depends on whether there is Fraction depends on whether there is
overlapoverlap
no overlap overlap
CS 480/680 87Chapter 7 -- Discrete Techniques
Area AveragingArea Averaging Use average area Use average area 11++22--1122 as as
blending factorblending factor
CS 480/680 88Chapter 7 -- Discrete Techniques
OpenGL AntialiasingOpenGL Antialiasing Can enable separately for points, Can enable separately for points,
lines, or polygonslines, or polygonsglEnable(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 89Chapter 7 -- Discrete Techniques
9.5 Back-to-Front and Front-to-9.5 Back-to-Front and Front-to-Back RenderingBack Rendering Is this image correct?Is this image correct?
Probably notProbably not Polygons are renderedPolygons are rendered
in the order they passin the order they pass
down the pipelinedown the pipeline Blending functionsBlending functions
are order dependentare order dependent
CS 480/680 90Chapter 7 -- Discrete Techniques
Opaque and Translucent PolygonsOpaque and Translucent Polygons Suppose that we have a group of Suppose that we have a group of
polygons some of which are opaque and polygons some of which are opaque and some translucentsome translucent
How do we use hidden-surface removal?How do we use hidden-surface removal? Opaque polygons block all polygons Opaque polygons block all polygons
behind them and affect the depth bufferbehind them and affect the depth buffer Translucent polygons should not affect Translucent polygons should not affect
depth bufferdepth buffer Render with Render with glDepthMask(GL_FALSE)glDepthMask(GL_FALSE) which which
makes depth buffer read-onlymakes depth buffer read-only Sort polygons first to remove order Sort polygons first to remove order
dependencydependency
CS 480/680 91Chapter 7 -- Discrete Techniques
9.6 Depth Cueing and Fog9.6 Depth Cueing and Fog We can composite with a fixed color We can composite with a fixed color
and have the blending factors depend and have the blending factors depend on depthon depth Simulates a fog effectSimulates a fog effect
Blend source colorBlend source color C Css and fog colorand fog color C Cf f byby
CCss’=f C’=f Css + (1-f) C + (1-f) Cff
f f is the is the fog factorfog factor ExponentialExponential GaussianGaussian Linear (depth cueing)Linear (depth cueing)
CS 480/680 92Chapter 7 -- Discrete Techniques
Fog FunctionsFog Functions
CS 480/680 93Chapter 7 -- Discrete Techniques
OpenGL Fog FunctionsOpenGL Fog FunctionsGLfloat fcolor[4] = {……}:GLfloat fcolor[4] = {……}:
glEnable(GL_FOG);glEnable(GL_FOG);glFogf(GL_FOG_MODE, GL_EXP);glFogf(GL_FOG_MODE, GL_EXP);glFogf(GL_FOG_DENSITY, 0.5);glFogf(GL_FOG_DENSITY, 0.5);glFOgv(GL_FOG, fcolor);glFOgv(GL_FOG, fcolor);
CS 480/680 94Chapter 7 -- Discrete Techniques
10. Multirendering and the 10. Multirendering and the Accumulation BufferAccumulation Buffer
Compositing and blending are limited by Compositing and blending are limited by resolution of the frame bufferresolution of the frame buffer Typically 8 bits per color componentTypically 8 bits per color component
TheThe accumulation buffer accumulation buffer is a high is a high resolution buffer (16 or more bits per resolution buffer (16 or more bits per component) that avoids this problemcomponent) that avoids this problem
Write into it or read from it with a scale Write into it or read from it with a scale factorfactor
Slower than direct compositing into the Slower than direct compositing into the frame bufferframe buffer
CS 480/680 95Chapter 7 -- Discrete Techniques
12. Summary and Notes12. Summary and Notes
In the early days of computer graphics, In the early days of computer graphics, people worked with only three-people worked with only three-dimensional geometric objects, whereas dimensional geometric objects, whereas those people who were involved with those people who were involved with only two-dimensional images were only two-dimensional images were considered to be working in image considered to be working in image processing. processing.
Advances in hardware have made Advances in hardware have made graphics and image processing systems graphics and image processing systems practically indistinguishable.practically indistinguishable.
CS 480/680 96Chapter 7 -- Discrete Techniques
The idea that a two-dimensional image or The idea that a two-dimensional image or texture can be mapped to a three dimensional texture can be mapped to a three dimensional surface in no more time than it takes to surface in no more time than it takes to render the surface with constant shading render the surface with constant shading would have been unthinkable a few years ago.would have been unthinkable a few years ago.
Now these techniques are routine.Now these techniques are routine.
In this chapter we have concentrated on In this chapter we have concentrated on techniques that are supported by recently techniques that are supported by recently available hardware and API’s available hardware and API’s
Many of the techniques introduced here are Many of the techniques introduced here are new, many more are just appearing in the new, many more are just appearing in the literature; even more remain to be discovered.literature; even more remain to be discovered.
CS 480/680 97Chapter 7 -- Discrete Techniques
13. Suggested Readings13. Suggested Readings
Bump mapping was first suggested by Bump mapping was first suggested by Blinn (77). Environmental mapping was Blinn (77). Environmental mapping was developed by Blinn and Newell (76)developed by Blinn and Newell (76)
Hardware support for texture mapping Hardware support for texture mapping came with the SGI Reality Engine came with the SGI Reality Engine
Many of the compositing techniques, Many of the compositing techniques, including the use of the including the use of the channel, were channel, were suggested by Porter and Duff (84). The suggested by Porter and Duff (84). The OpenGL Programmer’s Guide contains OpenGL Programmer’s Guide contains many examples of how buffers can be many examples of how buffers can be used.used.
CS 480/680 98Chapter 7 -- Discrete Techniques
Exercises -- Due next Exercises -- Due next classclass