OpenGL Bitmaps, Images, and Texts - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

OpenGL Bitmaps, Images, and Texts

Description:

An excellent source of image file formats and code www.imagemagick.org. Game Graphics ... 4 means the data for each row of the image begin on a 4-byte boundary ... – PowerPoint PPT presentation

Number of Views:102
Avg rating:3.0/5.0
Slides: 17
Provided by: IIT89
Category:
Tags: opengl | bitmaps | images | texts

less

Transcript and Presenter's Notes

Title: OpenGL Bitmaps, Images, and Texts


1
OpenGL Bitmaps, Images, and Texts
2
Bitmap
  • OpenGL provides the basic functions of drawing 2D
    raster graphics -- texts and images
  • A bitmap is a rectangular binary array for
    pixels. It is often used as mask or drawing
    texts. When you draw a bitmap,
  • A pixel will be drawn with the current color if
    its bit in the map 1
  • Nothing is drawn if its bit 0

3
Current Raster Position
  • The current raster position is the origin where
    the next bitmap/image is to be drawn. It is set
    by calling
  • glRasterPos234sifd(x, y, z, w)
  • Z 0 and w 1 for glRasterPos2()
  • The raster position is transformed in the same
    way as coordinates in glVertex().
  • If the transformed point is clipped out, the
    current raster position is invalid.
  • Get the current raster position
  • GLfloat pos4
  • glGetFloatv(GL_CURRENT_RASTER_POSITION, pos)

4
Drawing the Bitmap
  • glBitmap() draws a bitmap to the framebuffer
    relative to the current raster position.
  • glBitmap(width, height, x0, y0, xi, yi, bitmap)
  • width, height width and height of the bitmap
  • x0, y0 origin of the bitmap relative to the
    current raster position.
  • xi, yi the x and y increments to the raster
    position after the bitmap is drawn
  • bitmap a byte array storing the bitmap
  • Example
  • glOrtho(0.0f, width, 0.0, height, -1.0, 1.0)
  • glTranslatef(rand()800, rand()600, 0)
  • glRasterPos2i(0, 0)
  • glBitmap(16, 16, 0.0, 0.0, 0.0, 0.0, letterA)

5
Using Images
  • Images have more information for a pixel, e.g.
    RGB values
  • glDrawPixels() writes a rectangular array of
    pixels into the framebuffer at the current raster
    position
  • glDrawPixels(width, height, format, type,
    pixels)
  • format indicates the kind of pixel data elements
  • GL_COLOR_INDEX
  • GL_RGB
  • GL_RGBA
  • GL_DEPTH_COMPONENT
  • type indicates the data type of each element
  • GL_BYTE
  • GL_UNSIGNED_BYTE
  • GL_INT
  • GL_FLOAT
  • pixels an array of pixel data elements

6
Read BMP File
  • BMP is Microsoft image file format. It is
    uncompressed large and easy to use
  • include ltgl/glaux.hgt
  • AUX_RGBImageRec pImg
  • auxDIBImageLoad(Filename)
  • Picture Width pImg-gtsizeX
  • Picture Height pImg-gtsizeY
  • RGB Data pImg-gtdata
  • Many image file formats Targa file, PPM file
    (uncompressed), jpg, gif (compressed)
  • An excellent source of image file formats and
    code www.imagemagick.org

7
Reading Pixels
  • You may read a rectangular array pixels from the
    framebuffer to the main memory
  • glReadPixels(x, y, width, height, format, type,
    pixels)
  • x, y lower left corner of the framebuffer
    rectangle
  • width, height width and height of the rectangle
  • format the same as glDrawPixels()
  • type the same as glDrawPixels()
  • pixels an array of pixel data
  • Reading back from framebuffer is slow!
  • Example
  • GLubyte imgData new GLubytewh3
  • glReadPixels(0, 0, w, h, GL_RGB,
    GL_UNSIGNED_BYTE, imgData)

8
Copying Pixels
  • Copy pixels from one framebuffer rectangle to
    another
  • glCopyPixels(x, y, width, height, buffer)
  • buffer specifying the framebuffer that is used
  • GL_COLOR
  • GL_ DEPTH
  • GL_STENCIL
  • The buffer rectangle specified by (x, y, width,
    height) is copied to a rectangle at the current
    raster position.
  • For all three functions, the exact conversion of
    data going to or from the framebuffer depend on
    the modes in effect at the time

9
Magnifying, Reducing, or Flipping
  • OpenGL allows you to arbitrarily magnify, reduce,
    or even flip (reflect) an image by using
    glPixelZoom()
  • glPixelZoom(xZoom, yZoom)
  • glPixelZoom(-1.0f, -1.0f) // flip image
    horizontally and vertically
  • glPixleZoom(0.5f, 0.5f) // reduce image size
  • glPixelZoom(2.0f, 2.0f) // magnify image size
  • Pixel Zoom is applied to both glDrawPixels() and
    glCopyPixels()
  • Application as a magnifying glass or a sniper gun
    for games

10
Managing Pixel Storage
  • There are a lot of details of how to store pixels
    data in main memory, such as alignments,
    byte-ordering etc.. You can control the pixel
    storage mode using
  • glPixelStoreif(pname, param)
  • pname GL_PACK_ALIGNMENT or GL_UNPACK_ALIGNMENT
  • Packing refers to the way that pixel data is
    written to main memory
  • Unpacking refers to the way that pixel data is
    read from memory
  • param 1, 2, 4, 8
  • Default 4 means the data for each row of the
    image begin on a 4-byte boundary
  • Several other parameters available, check OpenGL
    programming guide

11
Bitmap Fonts
  • A set of bitmaps will generated as display lists,
    and drawn using glCallLists
  • OpenGL doesnt directly support displaying text.
    We need to use platform specific functions.
  • Creating a handle to the Windows font object
  • HFONT hFont CreateFont(size, , ANSI_CHARSET,
    , Courier)
  • Connect to the device context of the window
  • SelectObject(g_HDC, hFont)
  • Generate bitmap display lists
  • base glGenLists(96)
  • wglUseFontBitmaps(g_HDC, 32, 96, base)
  • It generates 96 display lists, starting from
    ASCII char 32

12
Drawing bitmap fonts
  • Drawing a string by calling the display lists
    defined by the characters of the string
  • char str
  • glPushAttrib(GL_LIST_BIT)
  • glListBase(base-32)
  • glCallLists(strlen(str), GL_UNSIGNED_BYTE,
    str)
  • glPopAttrib()
  • For a give character c, c-32base is the name
    (ID) of the display list corresponding to c.
  • Example

13
Setting Raster Position for Text
  • Set the raster position in the 3D space. Remember
    Modelview and Projection transformations are
    applied to the raster position.
  • glMatrixMode(GL_PROJECTION)
  • glLoadIdentity()
  • gluPerspective(54.0f,width/height,1.0f,1000.0f)
  • glMatrixMode(GL_MODELVIEW)
  • glLoadIdentity()
  • glRasterPos3f(-0.35f, 0.0f, -1.0f)
  • A point on the near plane.

14
Outline Fonts
  • Outline fonts are defined in 3D (with depth).
    They can be transformed as other 3D objects.
  • Need to define an array of 256 GLYPHMETRICSFLOAT
    varibles to holds orientation and placement info
    for display lists
  • Typedef struct _GLYPHMETRICSFLOAT
  • FLOAT gmfBlackBoxX
  • FLOAT gmfBlackBoxY
  • POINTFLOAT gmfptGlyphOrigin
  • FLOAT gmfCellIncX
  • FLOAT gmfCellIncY
  • GLYPHMETRICSFLOAT
  • GLYPHMETRICSFLOAT gmf256

15
Create Outline Fonts
  • Generate display lists for outline fonts (A
    wiggle function)
  • base glGenLists(256)
  • GLYPHMETRICSFLOAT gmf256
  • hFont CreateFont()
  • SelectObject(g_HDC, hFont)
  • wglUseFontOutlines(g_HDC, 0, 256, base, 0.0f,
    depth, WGL_FONT_POLYGONS, gmf)
  • depth extrusion in the z direction
  • Format WGL_FONT_POLYGONS or WGL_FONT_LINES

16
Drawing Outline Fonts
  • Drawing a string by calling the display lists
  • char str
  • glPushAttrib(GL_LIST_BIT)
  • glListBase(base)
  • glCallLists(strlen(str), GL_UNSIGNED_BYTE,
    str)
  • glPopAttrib()
  • Example
Write a Comment
User Comments (0)
About PowerShow.com