OpenGL Bitmaps, Images, and Texts - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

OpenGL Bitmaps, Images, and Texts

Description:

... data for each row of the image begin on a 4-byte boundary ... You can find on-line descriptions and loaders for a lot of image file formats, e.g. imagemagick. ... – PowerPoint PPT presentation

Number of Views:163
Avg rating:3.0/5.0
Slides: 14
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 write a letter A
  • glOrtho(0.0f, 800, 0.0, 600, -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
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)

7
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

8
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

9
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, e.g. glReadPixels
  • Unpacking refers to the way that pixel data is
    read from memory, e.g. glTexImage2D
  • 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

10
Image formats
  • There are hundreds of different image formats
    available. I wont cover any particular format.
  • You can find on-line descriptions and loaders for
    a lot of image file formats, e.g. imagemagick.

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.
  • glutBitmapCharacter(void font, int character)
  • font Bitmap font to use, including
    GLUT_BITMAP_8_BY_13, GLUT_BITMAP_9_BY_15,
    GLUT_BITMAP_TIMES_ROMAN_10, GLUT_BITMAP_TIMES_ROMA
    N_24, GLUT_BITMAP_HELVETICA_10,
    GLUT_BITMAP_HELVETICA_12, GLUT_BITMAP_HELVETICA_18
  • character Character to render
  • Read textbook for displaying fonts with wgl
    extensions.

12
Drawing bitmap fonts
  • char str OpenGL Bitmap Fonts!
  • //Set matrices for ortho
  • //Print text
  • glRasterPos2f(-0.5f, 0.0f)
  • for(unsigned int i0 iltstrlen(str) i)
  • glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18,
    stri)
  • //reset matrices
  • glPopMatrix()
  • glMatrixMode(GL_PROJECTION)
  • glPopMatrix()

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)
  • glPushMatrix()
  • glLoadIdentity()
  • gluOrtho2D(-1.0f, 1.0f, -1.0f, 1.0f)
  • glMatrixMode(GL_MODELVIEW)
  • glPushMatrix
  • glLoadIdentity()
  • glRasterPos2f(-0.5f, 0.0f)
  • A point on the near plane.
Write a Comment
User Comments (0)
About PowerShow.com