Presentation is loading. Please wait.

Presentation is loading. Please wait.

Objectives Introduce OpenGL buffers and read/write Introduce OpenGL buffers and read/write Introduce texture mapping Introduce texture mapping Introduce.

Similar presentations


Presentation on theme: "Objectives Introduce OpenGL buffers and read/write Introduce OpenGL buffers and read/write Introduce texture mapping Introduce texture mapping Introduce."— Presentation transcript:

1 Objectives Introduce OpenGL buffers and read/write Introduce OpenGL buffers and read/write Introduce texture mapping Introduce texture mapping Introduce Blending Introduce Blending CSC461: Lecture 25 Discrete Techniques

2 Buffers  Define a buffer by its spatial resolution (n x m) and its depth k, the number of bits/pixel pixel  OpenGL Buffers

3 Writing in Buffers Conceptually, we can consider all of memory as a large two-dimensional array of pixels Conceptually, we can consider all of memory as a large two-dimensional array of pixels We read and write rectangular block of pixels We read and write rectangular block of pixels Bit block transfer (bitblt) operations Bit block transfer (bitblt) operations The frame buffer is part of this memory The frame buffer is part of this memory Read destination pixel before writing source Read destination pixel before writing source frame buffer (destination) writing into frame buffer sourc e memory

4 Writing Modes Source and destination bits are combined bitwise Source and destination bits are combined bitwise 16 possible functions (one per column in table) 16 possible functions (one per column in table) XOR is especially useful for swapping blocks of memory such as menus that are stored off screen XOR is especially useful for swapping blocks of memory such as menus that are stored off screen replace OR XOR

5 The Pixel Pipeline OpenGL has a separate pipeline for pixels OpenGL has a separate pipeline for pixels Writing pixels involves Writing pixels involves Moving pixels from processor memory to the frame buffer Moving pixels from processor memory to the frame buffer Format conversions Format conversions Mapping, Lookups, Tests Mapping, Lookups, Tests Reading pixels Reading pixels Format conversion Format conversion

6 Raster Position OpenGL maintains a raster position as part of the state OpenGL maintains a raster position as part of the state Set by glRasterPos*(); glRasterPos3f(x,y,z); Set by glRasterPos*(); glRasterPos3f(x,y,z); The raster position is a geometric entity The raster position is a geometric entity Passes through geometric pipeline Passes through geometric pipeline Eventually yields a 2D position in screen coordinates Eventually yields a 2D position in screen coordinates This position in the frame buffer is where the next raster primitive is drawn This position in the frame buffer is where the next raster primitive is drawn Buffer Selection OpenGL can draw into or read from any of the color buffers (front, back, auxiliary) with default to the back buffer OpenGL can draw into or read from any of the color buffers (front, back, auxiliary) with default to the back buffer Change with glDrawBuffer and glReadBuffer 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 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 Need packing and unpacking Drawing and reading can be slow Drawing and reading can be slow

7 Bitmaps OpenGL treats 1-bit pixels (bitmaps) differently than multi-bit pixels (pixelmaps) OpenGL 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 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 0  color unchanged, 1  color changed based on writing mode Bitmaps are useful for raster text: GLUT_BIT_MAP_8_BY_13 Bitmaps are useful for raster text: GLUT_BIT_MAP_8_BY_13 Raster Color Same as drawing color set by glColor*() Same as drawing color set by glColor*() Fixed by last call to glRasterPos*() 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 Geometry drawn in blue Ones in bitmap use a drawing color of red Ones in bitmap use a drawing color of red

8 Drawing Bitmaps 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

9 Pixel Maps OpenGL works with rectangular arrays of pixels called pixel maps or images OpenGL works with rectangular arrays of pixels called pixel maps or images Pixels are in one byte ( 8 bit) chunks Pixels are in one byte ( 8 bit) chunks Luminance (gray scale) images 1 byte/pixel Luminance (gray scale) images 1 byte/pixel RGB 3 bytes/pixel RGB 3 bytes/pixel Three functions Three functions Draw pixels: processor memory to frame buffer Draw pixels: processor memory to frame buffer Read pixels: frame buffer to processor memory Read pixels: frame buffer to processor memory Copy pixels: frame buffer to frame buffer Copy pixels: frame buffer to frame buffer glReadPixels(x,y,width,height,format,type,myimage) start pixel in frame buffer size type of image type of pixels pointer to processor memory GLubyte myimage[512][512][3]; glReadPixels(0,0,512,512,GL_RGB,GL_UNSIGNED_BYTE,myimage); glDrawPixels(width,height,format,type,myimage) starts at raster position

10 Image Formats We often work with images in a standard format (JPEG, TIFF, GIF) We often work with images in a standard format (JPEG, TIFF, GIF) How do we read/write such images with OpenGL? How do we read/write such images with OpenGL? No support in OpenGL No support in OpenGL OpenGL knows nothing of image formats OpenGL knows nothing of image formats Some code available on Web Some code available on Web Can write readers/writers for some simple formats in OpenGL such as PPM image Can write readers/writers for some simple formats in OpenGL such as PPM image  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 need can use glPixelStorei(GL_UNPACK_SWAP_BYTES,GL_TRUE);

11 Texture Mapping The Limits of Geometric Modeling: Although graphics cards can render over 10 million polygons per second, that number is insufficient for many phenomena, such as Clouds, Grass, Terrain, Skin The Limits of Geometric Modeling: Although graphics cards can render over 10 million polygons per second, that number is insufficient for many phenomena, such as Clouds, Grass, Terrain, Skin Modeling an Orange Modeling an Orange Consider the problem of modeling an orange (the fruit) Consider the problem of modeling an orange (the fruit) Start with an orange-colored sphere Start with an orange-colored sphere Too simple Too simple Replace sphere with a more complex shape Replace sphere with a more complex shape Does not capture surface characteristics (small dimples) Does not capture surface characteristics (small dimples) Takes too many polygons to model all the dimples Takes too many polygons to model all the dimples Take a picture of a real orange, scan it, and “paste” onto simple geometric model Take a picture of a real orange, scan it, and “paste” onto simple geometric model This process is texture mapping This process is texture mapping Still might not be sufficient because resulting surface will be smooth Still might not be sufficient because resulting surface will be smooth Need to change local shape Need to change local shape Bump mapping Bump mapping

12 Texture Mapping Texture Mapping Uses images to fill inside of polygons Uses images to fill inside of polygons Environmental (reflection mapping) Environmental (reflection mapping) Uses a picture of the environment for texture maps Uses a picture of the environment for texture maps Allows simulation of highly specular surfaces Allows simulation of highly specular surfaces Bump mapping Bump mapping Emulates altering normal vectors during the rendering process) Emulates altering normal vectors during the rendering process) Three Types of Mapping geometric modelTexture MappingEnvironmentalBump mapping

13 Where and How ? Mapping techniques are implemented at the end of the rendering pipeline Mapping techniques are implemented at the end of the rendering pipeline Very efficient because few polygons pass down the geometric pipeline Very efficient because few polygons pass down the geometric pipeline Although the idea is simple---map an image to a surface---there are 3 or 4 coordinate systems involved Although the idea is simple---map an image to a surface---there are 3 or 4 coordinate systems involved Parametric coordinates -- May be used to model curved surfaces Parametric coordinates -- May be used to model curved surfaces Texture coordinates -- Used to identify points in the image to be mapped Texture coordinates -- Used to identify points in the image to be mapped World Coordinates -- Conceptually, where the mapping takes place World Coordinates -- Conceptually, where the mapping takes place Screen Coordinates -- Where the final image is really produced Screen Coordinates -- Where the final image is really produced

14 Texture Mapping Process parametric coordinates texture coordinates world coordinates screen coordinates

15 Mapping Functions Basic problem is how to find the maps Basic problem is how to find the maps Forward mapping – from object points to pixels Forward mapping – from object points to pixels Backward mapping – from pixels to object points Backward mapping – from pixels to object points Such functions are difficult to find in general Such functions are difficult to find in general One solution -- Two-part mapping One solution -- Two-part mapping First map the texture to a simple intermediate surface, such as First map the texture to a simple intermediate surface, such as Cylindrical Mapping Cylindrical Mapping Spherical Map Spherical Map Box Mapping Box Mapping Second map from intermediate object to actual object Second map from intermediate object to actual object Normals from intermediate to actual Normals from intermediate to actual Normals from actual to intermediate Normals from actual to intermediate Vectors from center of intermediate Vectors from center of intermediate

16 Aliasing Point sampling of the texture can lead to aliasing errors Point sampling of the texture can lead to aliasing errors point samples in texture space point samples in u,v (or x,y,z) space miss blue stripes A better but slower option is to use area averaging A better but slower option is to use area averaging Note that the preimage of pixel is curved pixel preimage

17 Opacity and Transparency Opaque surfaces permit no light to pass through Opaque surfaces permit no light to pass through Transparent surfaces permit all light to pass Transparent surfaces permit all light to pass Translucent surfaces pass some light Translucent surfaces pass some light translucency = 1 – opacity (  ) translucency = 1 – opacity (  ) Physical Models Physical Models Dealing with translucency in a physically correct manner is difficult due to Dealing with translucency in a physically correct manner is difficult due to the complexity of the internal interactions of light and matter the complexity of the internal interactions of light and matter Using a pipeline renderer Using a pipeline renderer Revert to writing model Revert to writing model

18 Writing Model Use A component of RGBA (or RGB  ) color to store opacity Use A component of RGBA (or RGB  ) color to store opacity During rendering we can expand our writing model to use RGBA values During rendering we can expand our writing model to use RGBA values Color Buffer destination component blend destination blending factor source blending factor source component

19 Blending Equation We can define source and destination blending factors for each component We can define source and destination blending factors for each component s = [s r, s g, s b, s  ] s = [s r, s g, s b, s  ] d = [d r, d g, d b, d  ] d = [d r, d g, d b, d  ] source and destination colors b = [b r, b g, b b, b  ] b = [b r, b g, b b, b  ] c = [c r, c g, c b, c  ] c = [c r, c g, c b, c  ] Blend as c’ = [b r s r + c r d r, b g s g + c g d g, b b s b + c b d b, b  s  + c  d  ]

20 OpenGL Blending and Compositing Must enable blending and pick source and destination factors Must enable blending and pick source and destination factors glEnable(GL_BLEND) glEnable(GL_BLEND) glBlendFunc(source_factor, glBlendFunc(source_factor, destination_factor) destination_factor) Only certain factors supported Only certain factors supported GL_ZERO, GL_ONE GL_ZERO, GL_ONE GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA GL_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA GL_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA See Redbook for complete list See Redbook for complete list

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

22 Opaque and Translucent Polygons Suppose that we have a group of polygons some of which are opaque and some translucent Suppose that we have a group of polygons some of which are opaque and some translucent How do we use hidden-surface removal? How do we use hidden-surface removal? Opaque polygons block all polygons behind them and affect the depth buffer Opaque polygons block all polygons behind them and affect the depth buffer Translucent polygons should not affect depth buffer Translucent polygons should not affect depth buffer Render with glDepthMask(GL_FALSE) which makes depth buffer read-only Render with glDepthMask(GL_FALSE) which makes depth buffer read-only Sort polygons first to remove order dependency Sort polygons first to remove order dependency

23 Fog We can composite with a fixed color and have the blending factors depend on depth We can composite with a fixed color and have the blending factors depend on depth Simulates a fog effect Simulates a fog effect Blend source color C s and fog color C f by Blend source color C s and fog color C f by C s ’=f C s + (1-f) C f C s ’=f C s + (1-f) C f f is the fog factor f is the fog factor Exponential Exponential Gaussian Gaussian Linear (depth cueing) Linear (depth cueing) OpenGL Fog Functions 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);

24 Line Aliasing and Antialiasing Ideal raster line is one pixel wide Ideal raster line is one pixel wide All line segments, other than vertical and horizontal segments, partially cover pixels All line segments, other than vertical and horizontal segments, partially cover pixels Simple algorithms color only whole pixels Simple algorithms color only whole pixels Lead to the “jaggies” or aliasing Lead to the “jaggies” or aliasing Similar issue for polygons Similar issue for polygons Can try to color a pixel by adding a fraction of its color to the frame buffer 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 percentage of pixel covered by fragment Fraction depends on whether there is overlap Fraction depends on whether there is overlap Use average area  1 +  2 -  1  2 as blending factor Use average area  1 +  2 -  1  2 as blending factor OpenGL Antialiasing OpenGL Antialiasing glEnable(GL_POINT_SMOOTH); glEnable(GL_LINE_SMOOTH); glEnable(GL_POLYGON_SMOOTH); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );


Download ppt "Objectives Introduce OpenGL buffers and read/write Introduce OpenGL buffers and read/write Introduce texture mapping Introduce texture mapping Introduce."

Similar presentations


Ads by Google