introduction to opengl part 3 n.
Skip this Video
Loading SlideShow in 5 Seconds..
Introduction to OpenGL (Part 3) PowerPoint Presentation
Download Presentation
Introduction to OpenGL (Part 3)

Introduction to OpenGL (Part 3)

263 Views Download Presentation
Download Presentation

Introduction to OpenGL (Part 3)

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Introduction to OpenGL(Part 3) Ref: OpenGL Programming Guide (The Red Book)

  2. Part 1 Introduction Geometry Viewing Light & Material Part 2 Display List Alpha Channel Polygon Offset Part 3 Image Texture Mapping Part 4 FrameBuffers Selection & Feedback Topics Fall 2009 revised

  3. OpenGL Pixels, Bitmaps, and Images

  4. Bitmaps • A bitmap is a rectangular array of 0s and 1s that serves as a drawing mask for a rectangular portion of the window • Specification: • Specified, in hex codes, in one dimensional array, row by row, starting from lower-left corner • Width need not be multiples of 8 Fall 2009 revised

  5. glBitmap (w, h, xbo, ybo, xbi, ybi, bitmapPtr) xbo, ybo: origin xbi, ybi: increment CRP after display glBitmap(10, 12, 0, 0, 11, 0, bitmapPtr) Bitmaps Fonts: series of bitmaps as display list Fall 2009 revised

  6. Bitmap (cont) • Note: You can't rotate bitmap fonts because the bitmap is always drawn aligned to the x and y framebuffer axes. • Use glColor* to set GL_CURRENT_RASTER_COLOR Fall 2009 revised

  7. Current Raster Position (CRP) • OpenGL maintains raster position, a 3D-position in world coordinate • Modified by: • glRasterPos() • specify the object coordinates (as in glVertex); transformed by MODELVIEW & PROJECTION, passed to the clipping stage. • If the position is not culled, raster position is updated; otherwise, it is not valid. • glBitmap() • glGetFloatv(GL_CURRENT_RASTER_POSITION, fptr) to obtain the CRP • glGetBooleanv (GL_CURRENT_RASTER_POSITION_VALID, &boolvar) to test validity of CRP Fall 2009 revised

  8. drawf.c Fall 2009 revised

  9. Bitmap Editor • This format is called xwindow bitmap, with xbm extension • Bitmaps can be created by the GIMP (GNU Image Manipulation Program) • Or, seek format converters Fall 2009 revised

  10. A simple Tk Program; require Tcl/Tk installed Get tcltk for Windows from ActiveTcl Note that xbm and the opengl xbitmap format is slightly different (details) Another XBM Editor (Here) Fall 2009 revised

  11. Ex: XBM Edit & Display Fall 2009 revised

  12. glReadPixels() Reading pixel data from framebuffer to processor memory. glDrawPixels() Writing pixel data from processor memory to framebuffer glCopyPixels() Copying pixel data within the framebuffer APIs for Images (Pixel Rectangles) Fall 2009 revised

  13. Function Arguments • glReadPixels (x, y, w, h, F, T, ptr) • x,y: window coordinate • F: pixel format • T: is data type • ptr: pointer to image memory • glDrawPixels (w, h, F, T, ptr) • Draw to current raster position • glCopyPixels (x, y, w, h, buffer) • Buffer: GL_COLOR | GL_DEPTH | GL_STENCIL • Equivalent to: Read then Draw Fall 2009 revised

  14. More on glCopyPixels • Note that there's no need for a format or data parameter for glCopyPixels(), since the data is never copied into processor memory. • The read source buffer and the destination buffer of glCopyPixels() are specified by glReadBuffer() and glDrawBuffer() respectively • Default: • single-buffer: GL_FRONT • Double-buffered: GL_BACK Fall 2009 revised

  15. Pixel Format GREY SCALE GREY SCALE with alpha Fall 2009 revised

  16. Data Type Fall 2009 revised

  17. Example 255: 0xFF See also image.c for CopyPixels Fall 2009 revised

  18. Example image.c • Copy the lower left corner to the CRP (where the mouse is) • For single-buffer version, only GL_FRONT is involved • While motion is in action, display is not called • Double-buffer version: [from the API doc] glutSwapBuffers promotes the contents of the back buffer to become the contents of the front buffer. The contents of the back buffer then become undefined. Reality is… two have same content Fall 2009 revised

  19. PixelZoom (xfactor,yfactor) • Enlarge/shrink images • Use negative factors for reflected images Fall 2009 revised

  20. Image Loader (TGA) • simple TGA utility in gluit.rar • only load and save uncompressed images in greyscale, RGB or RGBA mode. • Info in TGA header: • image type [unsigned char] • 1 - colour map image • 2 - RGB(A) uncompressed • 3 - greyscale uncompressed • 9 - greyscale RLE (compressed) • 10 - RGB(A) RLE (compressed) • pixel depth [unsigned char] • 8 – greyscale | 24 – RGB | 32 - RGBA Fall 2009 revised

  21. Offline; Local copy at webhd2:game-lib Image Loader (PNG) Fall 2009 revised

  22. DevIL Fall 2009 revised

  23. int pngLoadRaw (filename,rawinfo) Must be freed manually Fall 2009 revised

  24. Remark • Most graphics formats have image origin at upper/left corner • While OpenGL has the image origin at lower/left corner • Hence, if no correction is done, every image is drawn upside down. • Correction in PNG loader: pngSetStandardOrientation (1); Fall 2009 revised

  25. Imaging Pipeline Fall 2009 revised

  26. Imaging Pipeline(cont.) • glPixelStore() • Controlling Pixel-Storage Modes • glPixelStorei(GL_UNPACK_ALIGNMENT, 1); • Packing and unpacking refer to the way that pixel data is written to and read from processor memory. • tells OpenGL not to skip bytes at the end of a row Fall 2009 revised

  27. PixelStorei(GL_UNPACK_ALIGNMENT, x) • Specifies the alignment requirements for the start of each pixel row in memory. The allowable values are: • 1 (byte-alignment), • 2 (rows aligned to even-numbered bytes), • 4 (word-alignment [default]), and • 8 (rows start on double-word boundaries). Byte:8-bit Word:16-bit Double-word:32-bit Fall 2009 revised

  28. Assuming the RGB image is of size 3x2: (three bytes per pixel) w=3,h=2 Details In client memory, the start of each row of pixels is … Byte aligned (1) 2nd row 1st row Aligned to even bytes (2); Word aligned (4) Double word aligned (8) Fall 2009 revised

  29. For RGBA images, it doesn’t matter (each pixel has 4 bytes: RGBARGBA…) For RGB and luminance images and images with odd width, it should be set to byte-aligned (data are densely packed) Settings glPixelStorei(GL_UNPACK_ALIGNMENT, 1) glPixelStorei(GL_UNPACK_ALIGNMENT, 4) Fall 2009 revised

  30. Improving Pixel Pipeline Performance • A series of fragment operations is applied to pixels as they are drawn into the framebuffer. For optimum performance, disable all fragment operations (depth test, …) • While performing pixel operations, disable other costly states, such as texturing and lighting • It is usually faster to draw a large pixel rectangle than to draw several small ones, since the cost of transferring the pixel data can be amortized (分攤) over many pixels Fall 2009 revised

  31. Get depth buffer content glReadPixels() Reverse and scale! Near (white) far (black) Display it as a luminance image glDrawPixels() Example (depthbuffershow) Fall 2009 revised

  32. Illustrate that CRP is a point in R3 Image displayed is always parallel to projection plane Fake perspective by zooming with distance to camera Example (rasterpos3) Fall 2009 revised

  33. Process sprite image Add alpha layer Load png image Sprite animation “feel of depth” Back-to-front rendering pixelzoom Example (sprite) Fall 2009 revised

  34. OpenGL Texture Mapping (Introduction)

  35. Steps in Texture Mapping • Specify (create) the texture • Set texture parameters: • Indicate how the texture is to be applied to each pixel • Enable texture mapping • Draw the scene, supplying both texture and geometric coordinates • Works only in RGB mode Fall 2009 revised

  36. Specify the Texture • glTexImage1D() • glTexImage2D() • glTexImage3D() Fall 2009 revised

  37. Two Dimensional Texture • glTexImage2D (GLenum target, GLint level, GLint components, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const GLvoid *pixels) • target:GL_TEXTURE_2D • level:LOD number (0: base image) • components:number of color components (1|2|3|4) • Format:pixel data format • Border: 0 or 1 • Width & height • 2m + 2(border bit) • w and h can be different • There are new extensions that removes this restriction Fall 2009 revised

  38. glTexImage*D supplement • In GL version 1.1 or greater, pixels may be a null pointer. • In this case texture memory is allocated to accommodate a texture of width width and height height. You can then download subtextures to initialize this texture memory. • The image is undefined if the user tries to apply an uninitialized portion of the texture image to a primitive. Fall 2009 revised

  39. Setting Texture Environment • glTexEnv{if}{v}(GLenum target, GLenum pname, TYPEparam); • Setting how textures are to be interpreted: • Target:GL_TEXTURE_ENV • Pname: GL_TEXTURE_ENV_MODE • Param: modes (DECAL|REPLACE|MODULATE|BLEND) Fall 2009 revised

  40. Texture Environment Modes • GL_REPLACE • GL_MODULATE (default) • GL_DECAL • GL_BLEND New environment modes: • GL_ADD: Cv = Cf + Ct • GL_COMBINE (ARB, see here) Fall 2009 revised

  41. Color of polygon affects the display of texture GL_MODULATE Tree: (r,g,b,a) acutout = 0 Polygon: (1,0,0) Fall 2009 revised

  42. GL_BLEND Use with: glTexEnvfv (GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, colorPtr) Cc: texture environment color Fall 2009 revised

  43. Appearance solely determined by texture GL_REPLACE Tree: (r,g,b,a) acutout = 0 Polygon: (1,0,0) FOR TEXTURE CUT-OUTS Fall 2009 revised

  44. GL_DECAL Cp: replace Tree: (r,g,b,a) acutout = 0 Polygon: (1,0,0) RGB: DECAL=REPLACE Fall 2009 revised

  45. Texture + Lighting • To show fragment color, use GL_MODULATE • Apply specular color AFTER texture mapping: • glLightModeli (GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR); • See FAQ 21.040 GL_SEPARATE_SPECULAR_COLOR GL_SINGLE_COLOR Fall 2009 revised

  46. Texture Coordinates • Texture coordinate: • Associate texture location (in texture space) with vertices in the polygon • glTexCoord2i (s, t); glVertex2i (x, y); Order cannot be reversed! [Think of TexCoord as state assignment] Fall 2009 revised

  47. Most quadric primitives have default setting for texture coordinates To turn on the default setting: gluQuadricTexture (qobj, GL_TRUE) Texture Coordinates of Quadrics Fall 2009 revised

  48. (1,1) (1,1) (0,.75) (1,0) Polygon (in screen space) and texture coordinates (0,.75) rasterization (1,0) Texture map (4x4) Texture Access and Lookup linear nearest Filters Interpolate (s,t) and lookup (r,g,b,a) in the texture map Fall 2009 revised

  49. 1 Minification Magnification Pixels Texels Pixels Magnification & Minification Nature of problem: • Mismatch between texels and pixels Fall 2009 revised

  50. Magnification GL_NEAREST GL_LINEAR Minification GL_NEAREST, GL_LINEAR, GL_NEAREST_MIPMAP_NEAREST GL_LINEAR_MIPMAP_NEAREST GL_NEAREST_MIPMAP_LINEAR GL_LINEAR_MIPMAP_LINEAR Options Chooses the mipmap that most closely matches the size of the pixel being textured Nearest: in Manhattan distance to the center of the pixel Chooses the two mipmaps that most closely match the size of the pixel being textured Fall 2009 revised