Upload
silvia-potter
View
225
Download
0
Embed Size (px)
Citation preview
OpenGL Pixel Operations, Bitmaps, Fonts and Images
The Current Raster Position
Current raster position: A position in window coordinates where the next bitmap (or image) is to be drawn.
void glRasterPos2{sifd}(TYPE x, TYPE y)void glRasterPos3{sifd}(TYPE x, TYPE y , TYPE z)
void glRasterPos {234}{sifd}v(TYPE * coords)
void glRasterPos4{sifd}(TYPE x, TYPE y , TYPE z , TYPE h)
Sets the current raster position. (x, y, z, h) is treated as object coordinates and is transformed by current modelview matrix, projection matrix and viewport. The transformed position is stored as current raster position.
Bitmap: An array of bit values (0 or 1).
Bitmaps
void glBitmap(GLsizei width, GLsizei height, GLfloat xo, GLfloat yo, GLfloat xi, GLfloat yi, const GLubyte * bitma
p)Draw a bitmap at current raster position.width, height: Width and height of the bitmap in pixels.xo, yo: Origin of the bitmap relative to its lower-left corner.xi, yi: x and y offsets to be added to the current raster position
after the bitmap is drawn.bitmap: Array that stores the bitmap
Pixel corresponding to “1” in bitmap is written using current raster color. Pixel corresponding to “0” in bitmap is unchanged.Current raster color is set to current color when glRasterPos*() is called.
The bits in a bitmap must be stored in chunks of 8 and start from lower left corner. But the width of the actually bitmap doesn't have to be multiple of 8.
0xff, 0xc0
GLubyte bitmap_F[] = {0xc0, 0x00, 0xc0, 0x00, 0xc0, 0x00, 0xc0, 0x00, 0xc0, 0x00, 0xff, 0x00, 0xff, 0x00, 0xc0, 0x00, 0xc0, 0x00, 0xc0, 0x00, 0xff, 0xc0, 0xff, 0xc0};glBitmap(10, 12, 0.0, 0.0, 11.0, 0.0, bitmap_F);
0xff, 0xc0
0xc0, 0x00
0xc0, 0x00
0xc0, 0x00
0xff, 0x00
0xff, 0x00
0xc0, 0x00
0xc0, 0x00
0xc0, 0x00
0xc0, 0x00
0xc0, 0x00
GLUT Bitmap Font
void glutBitmapCharacter(void *font, int character)
Render character with given font at current raster position. The width of the character is added to the x coordinate of current raster position. Internally GLUT implements this function using glBitmap().
font bitmap font
GLUT_BITMAP_8_BY_13 8 by 13 fixed width font
GLUT_BITMAP_9_BY_15 9 by 15 fixed width font
GLUT_BITMAP_TIMES_ROMAN_10 10-point Times Roman
GLUT_BITMAP_TIMES_ROMAN_24 24-point Times Roman
GLUT_BITMAP_HELVETICA_10 10-point Helvetica
GLUT_BITMAP_HELVETICA_12 12-point Helvetica
GLUT_BITMAP_HELVETICA_18 18-point Helvetica
To render a null-terminated string:
void DrawString(void *font, char *str){
while((*str)!='\0') {glutBitmapCharacter(font,(int)*str);str++;
}}
int glutBitmapWidth(void * font, int character)Return the width, in pixels, of character with given font
Using Fonts in Windows
BOOL wglUseFontBitmaps( HDC hdc, DWORD first, DWORD count, DWORD listBase)
hdc: Specifies the device context with the desired bitmap font. first: Specifies the first glyph. count: Specifies the number of glyphs.listBase: Specifies a starting display list.
This function creates count display lists in the current OpenGL rendering context. Each display list consists of a single call to glBitmap. Display list listBase+i corresponds to glyph first+i of the font currently selected in the device context specified by hdc, i = 0, 1, 2, ... , count 1.
HFONT hfont = CreateFont(......);SelectObject(hdc, hfont);
BOOL wglUseFontOutlines(HDC hdc, DWORD first, DWORD count, DWORD listBase, FLOAT deviation, FLOAT extrusion, int format, LPGLYPHMETRICSFLOAT lpgmf)
hdc: Specifies the device context with the desired outline font. first: Specifies the first glyph.count: Specifies the number of glyphs.listBase: Specifies a starting display list.deviation: Specifies the maximum chordal deviation from the original outlines.extrusion: Specifies how much a font is extruded in the negative z direction.format: WGL_FONT_LINES or WGL_FONT_POLYGONS.lpgmf : Points to an array of GLYPHMETRICSFLOAT structures that is to r
eceive the metrics of the glyphs. When lpgmf is NULL, no glyph metrics are returned.
This function creates count display lists in the current OpenGL rendering context. Display list listBase+i corresponds to glyph first+i of the font currently selected in the device context specified by hdc, i = 0, 1, 2, ... , count 1. The size of the font is 1.0 in x and y direction and extrusion in z direction.
Image Pipeline
Image: An array of pixel values. Pixel values are not limited to color values, they can also be depth values or stencil values.
OpenGL does not support reading images from or saving images to files.
SystemMemory
FrameBuffer
PixelStorageModes
Pixel TransferOperations(Pixel Map)
Unpack Pack
Rasterization(Pixel Zoom)
FragmentOperations
TextureMemory
Reading, Writing and Copying Pixel Data
Reading Pixel Data from Frame Buffer to System Memory
SystemMemory
FrameBuffer
PixelStorageModes
Pixel TransferOperations(Pixel Map)
Unpack Pack
Rasterization(Pixel Zoom)
FragmentOperations
TextureMemory
void glReadPixels( GLint x, GLint y, GLsizei width, GLsizei height,GLenum format, GLenum type, GLvoid *pixels)
Read pixel data from a frame buffer rectangle to system memory.x, y: Lower left corner of the frame buffer rectanglewidth, height: Width and height of the frame buffer rectangleformat: Pixel data elements to be readtype: Data type of each pixel data elementpixels: System memory to receive pixel data
format Pixel data element
GL_RGB Red, Green, Blue components
GL_RGBA Red, Green, Blue, Alpha components
GL_BGR Blue, Green, Red components
GL_BGRA Blue, Green, Red, Alpha components
Pixel data element formats
format Pixel data element
GL_RED Red component
GL_GREEN Green component
GL_BLUE Blue component
GL_ALPHA Alpha component
GL_LUMINANCE Luminance component
GL_LUMINANCE_ALPHA Luminance and Alpha component
GL_STENCIL_INDEX Stencil value
GL_DEPTH_COMPONENT Depth value
Luminance = (Red + Green + Blue) / 3
Pixel data element formats (continued)
GL_BGR and GL_BGRA pixel formats were added in OpenGL 1.2.
type Data type
GL_UNSIGNED_BYTE unsigned 8-bit integer
GL_BYTE 8-bit integer
GL_UNSIGNED_SHORT unsigned 16-bit integer
GL_SHORT 16-bit integer
GL_UNSIGNED_INT unsigned 32-bit integer
GL_INT 32-bit integer
GL_FLOAT single-precision floating point
GL_BITMAP single bits in unsigned 8-bit integer
Normal data types for pixel data element
Each element is scaled to fit the range of the specified data type.
type Data type
GL_UNSIGNED_BYTE_3_3_2 packed unsigned 8-bit integer
GL_UNSIGNED_BYTE_2_3_3_REV packed unsigned 8-bit integer
GL_UNSIGNED_SHORT_5_6_5 packed unsigned 16-bit integer
GL_UNSIGNED_SHORT_5_6_5_REV packed unsigned 16-bit integer
GL_UNSIGNED_SHORT_4_4_4_4 packed unsigned 16-bit integer
GL_UNSIGNED_SHORT_4_4_4_4_REV packed unsigned 16-bit integer
GL_UNSIGNED_SHORT_5_5_5_1 packed unsigned 16-bit integer
GL_UNSIGNED_SHORT_1_5_5_5_REV packed unsigned 16-bit integer
GL_UNSIGNED_INT_8_8_8_8 packed unsigned 32-bit integer
GL_UNSIGNED_INT_8_8_8_8_REV packed unsigned 32-bit integer
GL_UNSIGNED_INT_10_10_10_2 packed unsigned 32-bit integer
GL_UNSIGNED_INT_2_10_10_10_REV packed unsigned 32-bit integer
Packed data types for pixel data element (added in OpenGL 1.2)
type Valid Pixel Element Formats
GL_UNSIGNED_BYTE_3_3_2 GL_RGB
GL_UNSIGNED_BYTE_2_3_3_REV GL_RGB
GL_UNSIGNED_SHORT_5_6_5 GL_RGB
GL_UNSIGNED_SHORT_5_6_5_REV GL_RGB
GL_UNSIGNED_SHORT_4_4_4_4 GL_RGBA, GL_BGRA
GL_UNSIGNED_SHORT_4_4_4_4_REV GL_RGBA, GL_BGRA
GL_UNSIGNED_SHORT_5_5_5_1 GL_RGBA, GL_BGRA
GL_UNSIGNED_SHORT_1_5_5_5_REV GL_RGBA, GL_BGRA
GL_UNSIGNED_INT_8_8_8_8 GL_RGBA, GL_BGRA
GL_UNSIGNED_INT_8_8_8_8_REV GL_RGBA, GL_BGRA
GL_UNSIGNED_INT_10_10_10_2 GL_RGBA, GL_BGRA
GL_UNSIGNED_INT_2_10_10_10_REV GL_RGBA, GL_BGRA
Valid pixel element formats for packed data types
Red Green Blue
01234567
GL_UNSIGNED_BYTE_3_3_2 with GL_RGB
RedGreenBlue
01234567
GL_UNSIGNED_BYTE_2_3_3_REV with GL_RGB
Example of packed data types:
RedGreenBlueAlpha
0123456789101112131415
GL_UNSIGNED_SHORT_4_4_4_4_REV with GL_RGBA
Red Green BlueAlpha
0123456789101112131415
GL_UNSIGNED_SHORT_4_4_4_4_REV with GL_BGRA
Red Green Blue Alpha
0123456789101112131415
GL_UNSIGNED_SHORT_4_4_4_4 with GL_RGBA
RedGreenBlue Alpha
0123456789101112131415
GL_UNSIGNED_SHORT_4_4_4_4 with GL_BGRA
Writing Pixel Data from System Memory to Frame Buffer
SystemMemory
FrameBuffer
PixelStorageModes
Pixel TransferOperations(Pixel Map)
Unpack Pack
Rasterization(Pixel Zoom)
FragmentOperations
TextureMemory
void glDrawPixels( GLsizei width, GLsizei height,GLenum format, GLenum type, GLvoid *pixels)
Draw a rectangle of pixel data from system memory to frame buffer. The lower left corner of the pixel rectangle to be drawn is the current raster position.width, height: Width and height of the pixel rectangleformat: Pixel data elements to be writtentype: Data type of each pixel data elementpixels: System memory that stores the pixel dataformat and type have the same meaning as with glReadPixels().
Copying Pixel Data inside Frame Buffer
SystemMemory
FrameBuffer
PixelStorageModes
Pixel TransferOperations(Pixel Map)
Unpack Pack
Rasterization(Pixel Zoom)
FragmentOperations
TextureMemory
void glCopyPixels( GLint x, GLint y, GLsizei width, GLsizei height,GLenum buffer)
Copy pixel data from one frame buffer rectangle (source) to another frame buffer rectangle (destination). The lower left corner of the destination rectangle is the current raster position.x, y: Lower left corner of the source rectanglewidth, height: Width and height of the pixel rectanglebuffer: GL_COLOR, GL_STENCIL, GL_DEPTH
Note: glCopyPixels() does not need data format or type information and it is not affected by pixel storage modes.
Pixel Storage Modes
Packing: The way in which pixel data is written to system memory.Unpacking: The way in which pixel data is read from system memory.
SystemMemory
FrameBuffer
PixelStorageModes
Pixel TransferOperations(Pixel Map)
Unpack Pack
Rasterization(Pixel Zoom)
FragmentOperations
TextureMemory
void glPixelStorei(GLenum pname, GLint param)void glPixelStoref(GLenum pname, GLfloat param)
Set pixel storage modes. pname: parameter name. param: parameter value.
Parameter name Type Initial value Valid range
GL_UNPACK_ALIGNMENTGL_PACK_ALIGNMENT
GLint 4 1, 2, 4, 8
GL_UNPACK_SWAP_BYTESGL_PACK_SWAP_BYTES
GLboolean GL_FALSE GL_TRUE,GL_FALSE
GL_UNPACK_LSB_FIRSTGL_PACK_LSB_FIRST
GLboolean GL_FALSE GL_TRUE,GL_FALSE
GL_UNPACK_ROW_LENGTHGL_PACK_ROW_LENGTH
GLint 0 non-negative integer
GL_UNPACK_SKIP_ROWSGL_PACK_SKIP_ROWS
GLint 0 non-negative integer
GL_UNPACK_SKIP_PIXELSGL_PACK_SKIP_PIXELS
GLint 0 non-negative integer
LSB: Least Significant Bit.
*ALIGNMENT
Sets the byte alignment requirement for pixel data in system memory.1: Data is aligned on 8-bit address boundary in system memory2: Data is aligned on 16-bit address boundary in system memory4: Data is aligned on 32-bit address boundary in system memory8: Data is aligned on 64-bit address boundary in system memory
*SWAP_BYTES
Defines if the byte order in multi-byte data element is reversed.
*LSB_FIRST
Defines if the bits within a byte data are ordered from least significant bit to most significant bit.
*ROW_LENGTH, *SKIP_ROWS, *SKIP_PIXELS
These parameters are used when we only want to draw or read a sub-image of an entire image data in system memory.
Sub-Image
Image
*ROW_LENGTH
*SKIP_ROWS
*SKIP_PIXELS
Pixel Transfer Operations
Pixel transfer operations perform conversions on pixel data.
void glPixelTransferi(GLenum pname, GLint param)void glPixelTransferf(GLenum pname, GLfloat param)
void glPixelMapuiv(GLenum map, GLint size, const GLuint *values)void glPixelMapusv(GLenum map, GLint size, const GLushort *values)void glPixelMapfv(GLenum map, GLint size, const GLfloat *values)
Pixel Zoom
void glPixelZoom(GLfloat zx, GLfloat zy)
Set pixel zoom factors.zx: Pixel zoom factor in x direction. Default value is 1.0.zy: Pixel zoom factor in y direction. Default value is 1.0.
Zoom factors with magnitude greater than one magnify the image.Zoom factors with magnitude less than one minify the image.Negative zoom factors reflect the image about the current raster position.