Discrete Techniques Dr Giorgos A Demetriou Dr Stephania

  • Slides: 91
Download presentation
Discrete Techniques Dr. Giorgos A. Demetriou Dr. Stephania Loizidou Himona Computer Science Frederick Institute

Discrete Techniques Dr. Giorgos A. Demetriou Dr. Stephania Loizidou Himona Computer Science Frederick Institute of Technology Demetriou/Loizidou – ACSC 345 – Chapter 7

Buffer Define a buffer by its spatial resolution (n x m) and its depth

Buffer Define a buffer by its spatial resolution (n x m) and its depth k, the number of bits/pixel Demetriou/Loizidou – ACSC 345 – Chapter 7 2

Open. GL Frame Buffer Demetriou/Loizidou – ACSC 345 – Chapter 7 3

Open. GL Frame Buffer Demetriou/Loizidou – ACSC 345 – Chapter 7 3

Open. GL Buffers § Color buffers can be displayed § § Front Back Auxiliary

Open. GL Buffers § Color buffers can be displayed § § Front Back Auxiliary Overlay § Depth § Accumulation § High resolution buffer § Stencil § Holds masks Demetriou/Loizidou – ACSC 345 – Chapter 7 4

Writing in Buffers § Conceptually, we can consider all of memory as a large

Writing in Buffers § Conceptually, we can consider all of memory as a large twodimensional array of pixels § We read and write rectangular block of pixels § Bit block transfer (bitblt) operations § The frame buffer is part of this memory source writing into frame buffer (destination) Demetriou/Loizidou – ACSC 345 – Chapter 7 5

Writing Model Read destination pixel before writing source Demetriou/Loizidou – ACSC 345 – Chapter

Writing Model Read destination pixel before writing source Demetriou/Loizidou – ACSC 345 – Chapter 7 Demetriou - Computer Graphics - Chapter 7 6

Writing Modes § Source and destination bits are combined bitwise § 16 possible functions

Writing Modes § Source and destination bits are combined bitwise § 16 possible functions (one per column in table) replace XOR OR Demetriou/Loizidou – ACSC 345 – Chapter 7 7

XOR mode § Recall from Chapter 3 that we can use XOR by enabling

XOR mode § 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 S M swaps the S and M Demetriou/Loizidou – ACSC 345 – Chapter 7 8

The Pixel Pipeline § Open. GL has a separate pipeline for pixels § Writing

The Pixel Pipeline § Open. GL 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 Demetriou/Loizidou – ACSC 345 – Chapter 7 9

Raster Position § Open. GL maintains a raster position as part of the state

Raster Position § Open. GL maintains a raster position as part of the state § Set by gl. Raster. Pos*() § gl. Raster. Pos 3 f(x, y, z); § The raster position is a geometric entity § Passes through geometric pipeline § Eventually yields a 2 D position in screen coordinates § This position in the frame buffer is where the next raster primitive is drawn Demetriou/Loizidou – ACSC 345 – Chapter 7 10

Buffer Selection § Open. GL can draw into or read from any of the

Buffer Selection § Open. GL can draw into or read from any of the color buffers (front, back, auxiliary) § Default to the back buffer § Change with gl. Draw. Buffer and gl. Read. Buffer § 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 Demetriou/Loizidou – ACSC 345 – Chapter 7 11

Bitmaps § Open. GL treats 1 -bit pixels (bitmaps) differently than multi-bit pixels (pixelmaps)

Bitmaps § Open. GL treats 1 -bit pixels (bitmaps) differently than multi-bit pixels (pixelmaps) § Bitmaps are masks which 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_BIT_MAP_8_BY_13 Demetriou/Loizidou – ACSC 345 – Chapter 7 12

Raster Color § Same as drawing color set by gl. Color*() § Fixed by

Raster Color § Same as drawing color set by gl. Color*() § Fixed by last call to gl. Raster. Pos*() gl. Color 3 f(1. 0, 0. 0); gl. Raster. Pos 3 f(x, y, z); gl. Color 3 f(0. 0, 1. 0); gl. Bitmap(……. gl. Begin(GL_LINES); gl. Vertex 3 f(…. . ) § Geometry drawn in blue § Ones in bitmap use a drawing color of red Demetriou/Loizidou – ACSC 345 – Chapter 7 13

Drawing Bitmaps gl. Bitmap(width, height, x 0, y 0, xi, yi, bitmap) offset from

Drawing Bitmaps gl. Bitmap(width, height, x 0, y 0, xi, yi, bitmap) offset from raster position increments in raster position after bitmap drawn first raster position second raster position Demetriou/Loizidou – ACSC 345 – Chapter 7 14

Example: Checker Board GLubyte wb[2] = {0 x 00, 0 x ff}; GLubyte check[512];

Example: Checker Board GLubyte wb[2] = {0 x 00, 0 x ff}; GLubyte check[512]; int i, j; for(j=0; i<64; i++) for (j=0; j<64, j++) check[i*8+] = wb[(i/8+j)%2]; gl. Bitmap( 64, 0. 0, check); Demetriou/Loizidou – ACSC 345 – Chapter 7 15

Pixel Maps § Open. GL works with rectangular arrays of pixels called pixel maps

Pixel Maps § Open. GL 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 Demetriou/Loizidou – ACSC 345 – Chapter 7 16

Open. GL Pixel Functions gl. Read. Pixels(x, y, width, height, format, type, myimage) start

Open. GL Pixel Functions gl. Read. Pixels(x, y, width, height, format, type, myimage) start pixel in frame buffer size type of pixels pointer to processor type of image memory GLubyte myimage[512][3]; gl. Read. Pixels(0, 0, 512, GL_RGB, GL_UNSIGNED_BYTE, myimage); gl. Draw. Pixels(width, height, format, type, myimage) starts at raster position Demetriou/Loizidou – ACSC 345 – Chapter 7 17

Image Formats § We often work with images in a standard format (JPEG, TIFF,

Image Formats § We often work with images in a standard format (JPEG, TIFF, GIF) § How do we read/write such images with Open. GL? § No support in Open. GL § Open. GL knows nothing of image formats § Some code available on Web § Can write readers/writers for some simple formats in Open. GL Demetriou/Loizidou – ACSC 345 – Chapter 7 18

Displaying a PPM Image § PPM is a very simple format § Each image

Displaying a PPM Image § PPM is a very simple format § Each image file consists of a header followed by all the pixel data § Header P 3 # comment 1 # comment 2. #comment n rows columns maxvalue pixels Demetriou/Loizidou – ACSC 345 – Chapter 7 19

Reading the Header FILE *fd; int k, nm; char c; int i; char b[100];

Reading the Header FILE *fd; int k, nm; char c; int i; char b[100]; check for “P 3” float s; int red, green, blue; in first line printf("enter file namen"); 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 filen", b); Demetriou/Loizidou – ACSC 345 – Chapter 7 20

Reading the Header (cont) fscanf(fd, "%c", &c); while(c == '#') { fscanf(fd, "%[^n] ",

Reading the Header (cont) fscanf(fd, "%c", &c); while(c == '#') { fscanf(fd, "%[^n] ", b); printf("%sn", b); fscanf(fd, "%c", &c); } ungetc(c, fd); skip over comments by looking for # in first column Demetriou/Loizidou – ACSC 345 – Chapter 7 21

Reading the Data fscanf(fd, "%d %d %d", &n, &m, &k); printf("%d rows %d columns

Reading the Data fscanf(fd, "%d %d %d", &n, &m, &k); printf("%d rows %d columns max value= %dn", n, m, k); nm = n*m; image=malloc(3*sizeof(GLuint)*nm); s=255. /k; scale factor 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; } Demetriou/Loizidou – ACSC 345 – Chapter 7 22

Scaling the Image Data We can scale the image in the pipeline gl. Pixel.

Scaling the Image Data We can scale the image in the pipeline gl. Pixel. Transferf(GL_RED_SCALE, s); gl. Pixel. Transferf(GL_GREEN_SCALE, s); gl. Pixel. Transferf(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 need can use gl. Pixel. Storei(GL_UNPACK_SWAP_BYTES, GL_TRUE); Demetriou/Loizidou – ACSC 345 – Chapter 7 23

The display callback void display() { gl. Clear(GL_COLOR_BUFFER_BIT); gl. Raster. Pos 2 i(0, 0);

The display callback void display() { gl. Clear(GL_COLOR_BUFFER_BIT); gl. Raster. Pos 2 i(0, 0); gl. Draw. Pixels(n, m, GL_RGB, GL_UNSIGNED_INT, image); gl. Flush(); } Demetriou/Loizidou – ACSC 345 – Chapter 7 24

The Limits of Geometric Modeling § Although graphics cards can render over 10 million

The Limits of Geometric Modeling § Although graphics cards can render over 10 million polygons per second, that number is insufficient for many phenomena § § Clouds Grass Terrain Skin Demetriou/Loizidou – ACSC 345 – Chapter 7 25

Modeling an Orange § Consider the problem of modeling an orange (the fruit) §

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 Demetriou/Loizidou – ACSC 345 – Chapter 7 26

Modeling an Orange (cont. ) § Take a picture of a real orange, scan

Modeling an Orange (cont. ) § Take a picture of a real orange, scan it, and “paste” onto simple geometric model § This process is texture mapping § Still might not be sufficient because resulting surface will be smooth § Need to change local shape § Bump mapping Demetriou/Loizidou – ACSC 345 – Chapter 7 27

Three Types of Mapping § Texture Mapping § Uses images to fill inside of

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 Demetriou/Loizidou – ACSC 345 – Chapter 7 28

Texture Mapping geometric model texture mapped Demetriou/Loizidou – ACSC 345 – Chapter 7 29

Texture Mapping geometric model texture mapped Demetriou/Loizidou – ACSC 345 – Chapter 7 29

Environment Mapping Demetriou/Loizidou – ACSC 345 – Chapter 7 30

Environment Mapping Demetriou/Loizidou – ACSC 345 – Chapter 7 30

Bump Mapping Demetriou/Loizidou – ACSC 345 – Chapter 7 31

Bump Mapping Demetriou/Loizidou – ACSC 345 – Chapter 7 31

Where does mapping take place? § Mapping techniques are implemented at the end of

Where does mapping take place? § Mapping techniques are implemented at the end of the rendering pipeline § Very efficient because few polygons pass down the geometric pipeline Demetriou/Loizidou – ACSC 345 – Chapter 7 32

Is it simple? § Although the idea is simple---map an image to a surface---there

Is it simple? § Although the idea is simple---map an image to a surface---there are 3 or 4 coordinate systems involved 2 D image 3 D surface Demetriou/Loizidou – ACSC 345 – Chapter 7 33

Coordinate Systems § Parametric coordinates § May be used to model curved surfaces §

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 Demetriou/Loizidou – ACSC 345 – Chapter 7 34

Texture Mapping parametric coordinates texture coordinates world coordinates screen coordinates Demetriou/Loizidou – ACSC 345

Texture Mapping parametric coordinates texture coordinates world coordinates screen coordinates Demetriou/Loizidou – ACSC 345 – Chapter 7 35

Mapping Functions § Basic problem is how to find the maps § Consider mapping

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 Demetriou/Loizidou – ACSC 345 – Chapter 7 36

Backward Mapping § We really want to go backwards § Given a pixel, we

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 Demetriou/Loizidou – ACSC 345 – Chapter 7 37

Two-part mapping § One solution to the mapping problem is to first map the

Two-part mapping § One solution to the mapping problem is to first map the texture to a simple intermediate surface § Example: map to cylinder Demetriou/Loizidou – ACSC 345 – Chapter 7 38

Cylindrical Mapping parametric cylinder x = r cos 2 p u y = r

Cylindrical Mapping parametric cylinder x = r cos 2 p u y = r sin 2 pu 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 Demetriou/Loizidou – ACSC 345 – Chapter 7 39

Spherical Map § We can use a parametric sphere x = r cos 2

Spherical Map § We can use a parametric sphere x = r cos 2 pu y = r sin 2 pu cos 2 pv z = r sin 2 pu sin 2 pv in a similar manner to the cylinder but have to decide where to put the distortion Spheres are use in environmental maps Demetriou/Loizidou – ACSC 345 – Chapter 7 40

Box Mapping § Easy to use with simple orthographic projection § Also used in

Box Mapping § Easy to use with simple orthographic projection § Also used in environmental maps Demetriou/Loizidou – ACSC 345 – Chapter 7 41

Second Mapping § Map from intermediate object to actual object § Normals from intermediate

Second Mapping § Map from intermediate object to actual object § Normals from intermediate to actual § Normals from actual to intermediate § Vectors from center of intermediate actual intermediate Demetriou/Loizidou – ACSC 345 – Chapter 7 42

Aliasing § Point sampling of the texture can lead to aliasing errors miss blue

Aliasing § Point sampling of the texture can lead to aliasing errors miss blue stripes point samples in u, v (or x, y, z) space point samples in texture space Demetriou/Loizidou – ACSC 345 – Chapter 7 43

Area Averaging § A better but slower option is to use area averaging preimage

Area Averaging § A better but slower option is to use area averaging preimage pixel Note that preimage of pixel is curved Demetriou/Loizidou – ACSC 345 – Chapter 7 44

Open. GL Texture Mapping: Basic Strategy § Three steps to applying a texture 1.

Open. GL Texture Mapping: Basic Strategy § Three steps to applying a texture 1. specify the texture § read or generate image § assign to texture § enable texturing 2. assign texture coordinates to vertices § Proper mapping function is left to application 3. specify texture parameters § wrapping, filtering Demetriou/Loizidou – ACSC 345 – Chapter 7 45

Texture Mapping y z x geometry t screen image s Demetriou/Loizidou – ACSC 345

Texture Mapping y z x geometry t screen image s Demetriou/Loizidou – ACSC 345 – Chapter 7 46

Texture Example § The texture (below) is a 256 x 256 image that has

Texture Example § The texture (below) is a 256 x 256 image that has been mapped to a rectangular polygon which is viewed in perspective Demetriou/Loizidou – ACSC 345 – Chapter 7 47

Texture Mapping and the Open. GL Pipeline § Images and geometry flow through separate

Texture Mapping and the Open. GL Pipeline § Images and geometry flow through separate pipelines that join at the rasterizer § “complex” textures do not affect geometric complexity vertices geometry pipeline rasterizer image pixel pipeline Demetriou/Loizidou – ACSC 345 – Chapter 7 48

Specify Texture Image § Define a texture image from an array of texels (texture

Specify Texture Image § Define a texture image from an array of texels (texture elements) in CPU memory Glubyte my_texels[512]; § Define as any other pixel map § Scan § Via application code § Enable texture mapping § gl. Enable(GL_TEXTURE_2 D) § Open. GL supports 1 -4 dimensional texture maps Demetriou/Loizidou – ACSC 345 – Chapter 7 49

Define Image as a Texture gl. Tex. Image 2 D( target, level, components, w,

Define Image as a Texture gl. Tex. Image 2 D( target, level, components, w, h, border, format, type, texels ); target: type of texture, e. g. GL_TEXTURE_2 D 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: pointer to texel array gl. Tex. Image 2 D(GL_TEXTURE_2 D, 0, 3, 512, 0, GL_RGB, GL_UNSIGNED_BYTE, my_texels); Demetriou/Loizidou – ACSC 345 – Chapter 7 50

Converting A Texture Image § Open. GL requires texture dimensions to be powers of

Converting A Texture Image § Open. GL requires texture dimensions to be powers of 2 § If dimensions of image are not powers of 2 § glu. Scale. Image( 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 Demetriou/Loizidou – ACSC 345 – Chapter 7 51

Mapping a Texture § Based on parametric texture coordinates § gl. Tex. Coord*() specified

Mapping a Texture § Based on parametric texture coordinates § gl. Tex. Coord*() specified at each vertex t 0, 1 Texture Space Object Space 1, 1 (s, t) = (0. 2, 0. 8) A a c (0. 4, 0. 2) b 0, 0 B 1, 0 s C (0. 8, 0. 4) Demetriou/Loizidou – ACSC 345 – Chapter 7 52

Typical Code gl. Begin(GL_POLYGON); gl. Color 3 f(r 0, g 0, b 0); gl.

Typical Code gl. Begin(GL_POLYGON); gl. Color 3 f(r 0, g 0, b 0); gl. Normal 3 f(u 0, v 0, w 0); gl. Tex. Coord 2 f(s 0, t 0); gl. Vertex 3 f(x 0, y 0, z 0); gl. Color 3 f(r 1, g 1, b 1); gl. Normal 3 f(u 1, v 1, w 1); gl. Tex. Coord 2 f(s 1, t 1); gl. Vertex 3 f(x 1, y 1, z 1); . . gl. End(); Note that we can use vertex arrays to increase efficiency Demetriou/Loizidou – ACSC 345 – Chapter 7 53

Interpolation Open. GL uses bilinear interpolation to find proper texels from specified texture coordinates

Interpolation Open. GL uses bilinear interpolation to find proper texels from specified texture coordinates Can be distortions good selection of tex coordinates poor selection of tex coordinates texture stretched over trapezoid showing effects of bilinear interpolation Demetriou/Loizidou – ACSC 345 – Chapter 7 54

Texture Parameters § Open. GL a variety of parameter that determine how texture is

Texture Parameters § Open. GL a variety of parameter 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 Demetriou/Loizidou – ACSC 345 – Chapter 7 55

Wrapping Mode § Clamping: if s, t > 1 use 1, if s, t

Wrapping Mode § Clamping: if s, t > 1 use 1, if s, t <0 use 0 § Wrapping: use s, t modulo 1 gl. Tex. Parameteri( GL_TEXTURE_2 D, GL_TEXTURE_WRAP_S, GL_CLAMP ) gl. Tex. Parameteri( GL_TEXTURE_2 D, GL_TEXTURE_WRAP_T, GL_REPEAT ) t s texture GL_REPEAT wrapping Demetriou/Loizidou – ACSC 345 – Chapter 7 GL_CLAMP wrapping 56

Magnification and Minification § More than one texel can cover a pixel (minification) or

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 Texture Polygon Minification Demetriou/Loizidou – ACSC 345 – Chapter 7 57

Filter Modes § Modes determined by § gl. Tex. Parameteri( target, type, mode )

Filter Modes § Modes determined by § gl. Tex. Parameteri( target, type, mode ) gl. Tex. Parameteri(GL_TEXTURE_2 D, GL_TEXURE_MAG_FILTER, GL_NEAREST); gl. Tex. Parameteri(GL_TEXTURE_2 D, GL_TEXURE_MIN_FILTER, GL_LINEAR); Note that linear filtering requires a border of an extra texel for filtering at edges (border = 1) Demetriou/Loizidou – ACSC 345 – Chapter 7 58

Mipmapped Textures § Mipmapping allows for prefiltered texture maps of decreasing resolutions § Lessens

Mipmapped Textures § Mipmapping allows for prefiltered texture maps of decreasing resolutions § Lessens interpolation errors for smaller textured objects § Declare mipmap level during texture definition gl. Tex. Image 2 D( GL_TEXTURE_*D, level, … ) § GLU mipmap builder routines will build all the textures from a given image glu. Build*DMipmaps( … ) Demetriou/Loizidou – ACSC 345 – Chapter 7 59

Example point sampling mipmapped point sampling linear filtering mipmapped linear filtering Demetriou/Loizidou – ACSC

Example point sampling mipmapped point sampling linear filtering mipmapped linear filtering Demetriou/Loizidou – ACSC 345 – Chapter 7 60

Texture Functions § Controls how texture is applied § gl. Tex. Env{fi}[v]( GL_TEXTURE_ENV, prop,

Texture Functions § Controls how texture is applied § gl. Tex. Env{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 Demetriou/Loizidou – ACSC 345 – Chapter 7 61

Perspective Correction Hint § Texture coordinate and color interpolation § either linearly in screen

Perspective Correction Hint § Texture coordinate and color interpolation § either linearly in screen space § or using depth/perspective values (slower) § Noticeable for polygons “on edge” § gl. Hint( GL_PERSPECTIVE_CORRECTION_HINT, hint ) where hint is one of § GL_DONT_CARE § GL_NICEST § GL_FASTEST Demetriou/Loizidou – ACSC 345 – Chapter 7 62

Generating Texture Coordinates § Open. GL can generate texture coordinates automatically gl. Tex. Gen{ifd}[v]()

Generating Texture Coordinates § Open. GL can generate texture coordinates automatically gl. Tex. Gen{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) Demetriou/Loizidou – ACSC 345 – Chapter 7 63

Texture Objects § Texture is part of the Open. GL state § If we

Texture Objects § Texture is part of the Open. GL state § If we have different textures for different objects, Open. GL will be moving large amounts data from processor memory to texture memory § Recent versions of Open. GL have texture objects § one image per texture object § Texture memory can hold multiple texture obejcts Demetriou/Loizidou – ACSC 345 – Chapter 7 64

Applying Textures II 1. 2. 3. 4. 5. 6. 7. 8. specify textures in

Applying Textures II 1. 2. 3. 4. 5. 6. 7. 8. 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 Demetriou/Loizidou – ACSC 345 – Chapter 7 65

Other Texture Features § Environmental Maps § Start with image of environment through a

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 Open. GL t § Use this texture to generate a spherical map § Use automatic texture coordinate generation § Multitexturing § Apply a sequence of textures through cascaded texture units Demetriou/Loizidou – ACSC 345 – Chapter 7 66

Textures in Open. GL § Create a “texture object” and specify the texture type

Textures in Open. GL § Create a “texture object” and specify the texture type associated with it § e. g. 2 D/3 D, RGB/RGBA/…, etc. § Indicate how the texture is to be applied to each pixel § e. g. nearest/interpolated, repeated/clamped, etc. § Enable texture mapping § Supply the texture coordinates Demetriou/Loizidou – ACSC 345 – Chapter 7 67

Texture Commands in Open. GL § gl. Gen. Textures(GLsizei n, GLuint *tex_names) § Returns

Texture Commands in Open. GL § gl. Gen. Textures(GLsizei n, GLuint *tex_names) § Returns n integers that are to be uniquely associated with texture objects. § gl. Bind. Texture(GLenum target, GLuint tex_name) § Creates a new texture object and assigns it the provided name, or makes a previously created texture object active § Defines the dimensionality of the texture object to be according to the provided target. Demetriou/Loizidou – ACSC 345 – Chapter 7 68

Texture Commands in Open. GL (cont. ) § gl. Tex. Parameter{if} (GLenum target, GLenum

Texture Commands in Open. GL (cont. ) § gl. Tex. Parameter{if} (GLenum target, GLenum pname, TYPE param) § Sets various parameters that control how a texture is applied to a polygon fragment or stored in a texture object. § Options for target: § GL_TEXTURE_1 D § GL_TEXTURE_2 D § GL_TEXTURE_3 D Demetriou/Loizidou – ACSC 345 – Chapter 7 69

Texture Commands in Open. GL (cont. ) § Some options for pname and param:

Texture Commands in Open. GL (cont. ) § Some options for pname and param: § GL_TEXTURE_WRAP_{S, T, R} § GL_CLAMP, GL_REPEAT, GL_CLAMP_TO_EDGE § GL_TEXTURE_MAG_FILTER § GL_NEAREST, GL_LINEAR § GL_TEXTURE_MIN_FILTER § GL_NEAREST, § GL_LINEAR, § GL_NEAREST_MIPMAP_NEAREST, § GL_NEAREST_MIPMAP_LINEAR, § GL_LINEAR_MIPMAP_NEAREST, § GL_LINEAR_MIPMAP_LINEAR Demetriou/Loizidou – ACSC 345 – Chapter 7 70

Texture Commands in Open. GL (cont. ) § gl. Tex. Image 2 D(GLenum target,

Texture Commands in Open. GL (cont. ) § gl. Tex. Image 2 D(GLenum target, GLint level, Glint internal. Format, GLsizei width, GLsizei height, Glint border, GLenum format, GLenum type, const GLvoid *texels) § target is GL_TEXTURE_2 D § (or GL_PROXY_TEXTURE_2 D ) § level specifies the mipmap level that the texture is defined for (if no mipmapping, set level=0) § internal. Format options include: GL_RGB, GL_RGBA, and many more (38+ options) § border specifies width in pixels of texture border (0 or 1) Demetriou/Loizidou – ACSC 345 – Chapter 7 71

Texture Commands in Open. GL (cont. ) § gl. Tex. Env{if}(GLenum target, GLenum pname,

Texture Commands in Open. GL (cont. ) § gl. Tex. Env{if}(GLenum target, GLenum pname, TYPE param) § target must be GL_TEXTURE_ENV § Options for pname, param are: § GL_TEXTURE_ENV_MODE § GL_REPLACE, GL_DECAL, GL_MODULATE, GL_BLEND § GL_TEXTURE_ENV_COLOR § Four values in {0. . 1} representing the R, G, B, A color to blend with the object’s color according to the pattern defined in the texture map Demetriou/Loizidou – ACSC 345 – Chapter 7 72

MIP Mapping § “MIP” = multum in parvo (many things in a small place)

MIP Mapping § “MIP” = multum in parvo (many things in a small place) § an efficient way to store pre-filtered versions of a texture map Demetriou/Loizidou – ACSC 345 – Chapter 7 73

Opacity and Transparency § Opaque surfaces permit no light to pass through § Transparent

Opacity and Transparency § Opaque surfaces permit no light to pass through § Transparent surfaces permit all light to pass § Translucent surfaces pass some light translucency = 1 – opacity (a) opaque surface a =1 Demetriou/Loizidou – ACSC 345 – Chapter 7 74

Physical Models § Dealing with translucency in a physically correct manner is difficult due

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 § Revert to writing model Demetriou/Loizidou – ACSC 345 – Chapter 7 75

Writing Model § Use A component of RGBA (or RGBa) color to store opacity

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 source blending factor blend destination component source component destination blending factor Color Buffer Demetriou/Loizidou – ACSC 345 – Chapter 7 76

Blending Equation § We can define source and destination blending factors for each component

Blending Equation § We can define source and destination blending factors for each component s = [sr, sg, sb, sa] d = [dr, dg, db, da] source and destination colors 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 ] Demetriou/Loizidou – ACSC 345 – Chapter 7 77

Open. GL Blending and Compositing § Must enable blending and pick source and destination

Open. GL Blending and Compositing § Must enable blending and pick source and destination factors gl. Enable(GL_BLEND) gl. Blend. Func(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 Demetriou/Loizidou – ACSC 345 – Chapter 7 78

Example § Suppose that we start with the opaque background color (R 0, G

Example § Suppose that we start with the opaque background color (R 0, G 0, B 0, 1) § This color becomes the initial destination color § We now want to blend in a translucent polygon with color (R 1, G 1, B 1, a 1) § Select GL_SRC_ALPHA and GL_ONE_MINUS_SRC_ALPHA as the source and destination blending factors R’ 1 = a 1 R 1 +(1 - a 1) R 0, …… § Note this formula is correct if polygon is either opaque or transparent Demetriou/Loizidou – ACSC 345 – Chapter 7 79

Clamping and Accuracy § All the components (RGBA) are clamped and stay in the

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 Demetriou/Loizidou – ACSC 345 – Chapter 7 80

Order Dependency § Is this image correct? § Probably not § Polygons are rendered

Order Dependency § Is this image correct? § Probably not § Polygons are rendered in the order they pass down the pipeline § Blending functions are order dependent Demetriou/Loizidou – ACSC 345 – Chapter 7 81

Opaque and Translucent Polygons § Suppose that we have a group of polygons some

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 gl. Depth. Mask(GL_FALSE) which makes depth buffer read-only § Sort polygons first to remove order dependency Demetriou/Loizidou – ACSC 345 – Chapter 7 82

Fog § We can composite with a fixed color and have the blending factors

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) Demetriou/Loizidou – ACSC 345 – Chapter 7 83

Fog Functions Demetriou/Loizidou – ACSC 345 – Chapter 7 84

Fog Functions Demetriou/Loizidou – ACSC 345 – Chapter 7 84

Open. GL Fog Functions GLfloat fcolor[4] = {……}: gl. Enable(GL_FOG); gl. Fogf(GL_FOG_MODE, GL_EXP); gl.

Open. GL Fog Functions GLfloat fcolor[4] = {……}: gl. Enable(GL_FOG); gl. Fogf(GL_FOG_MODE, GL_EXP); gl. Fogf(GL_FOG_DENSITY, 0. 5); gl. FOgv(GL_FOG, fcolor); Demetriou/Loizidou – ACSC 345 – Chapter 7 85

Line Aliasing § Ideal raster line is one pixel wide § All line segments,

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 Demetriou/Loizidou – ACSC 345 – Chapter 7 86

Antialiasing § Can try to color a pixel by adding a fraction of its

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 whethere is overlap no overlap Demetriou/Loizidou – ACSC 345 – Chapter 7 87

Area Averaging § Use average area a 1+a 2 -a 1 a 2 as

Area Averaging § Use average area a 1+a 2 -a 1 a 2 as blending factor Demetriou/Loizidou – ACSC 345 – Chapter 7 88

Open. GL Antialiasing § Can enable separately for points, lines, or polygons gl. Enable(GL_POINT_SMOOTH);

Open. GL Antialiasing § Can enable separately for points, lines, or polygons gl. Enable(GL_POINT_SMOOTH); gl. Enable(GL_LINE_SMOOTH); gl. Enable(GL_POLYGON_SMOOTH); gl. Enable(GL_BLEND); gl. Blend. Func(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); Demetriou/Loizidou – ACSC 345 – Chapter 7 89

Accumulation Buffer § Compositing and blending are limited by resolution of the frame buffer

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 Demetriou/Loizidou – ACSC 345 – Chapter 7 90

Applications § Compositing § Image Filtering (convolution) § Whole scene antialiasing § Motion effects

Applications § Compositing § Image Filtering (convolution) § Whole scene antialiasing § Motion effects Demetriou/Loizidou – ACSC 345 – Chapter 7 91