Chapter 8 OpenGL programming guide: Drawing pixels, bitmaps, fonts, and images

Source: Internet
Author: User
Tags unpack types of tables

Here, bitmap is a single bit array per pixel. This chapter describes how to draw bitmaps and images to the frame buffer zone and how to read pixel data from the buffer zone.

1. Bitmap bitmap is a rectangle array consisting of 0 and 1. It is generally used as a mask for plotting. If the current color is red, when you use glbitmap to draw a bitmap, the position 1 draws a red pixel, and the position 0 is ignored. A common usage of bitmap is to draw fonts. The bitmap data must be an integer multiple of 8 bits. Of course, the actual width of the bitmap does not have this requirement. The order of the bitmap data is from bottom to bottom, from left to right: first, the bottom line of the bitmap, and then the top line, so that the top line.
API glrasterpos * () sets the bitmap and Coordinate Position of the image to be drawn (corresponding to the position in the lower left corner of the image), which is the same as the vertex position set by glvertex, will undergo various matrix transformations. Another function, glwindowpos *, can be used to specify a position in the window coordinate system. Glbitmap (glsizei width, glsizei height, glfloat xbo, glfloat YbO, glfloat xbi, glfloat YBI, const glubyte * Bitmap) Plotting bitmap: width is the bitmap width and height is the bitmap height, the two values do not require a multiple of 8; xbo and YbO are set relative to raster
An offset of position. After xbi and YBI are drawn, the current raster position is moved to a certain distance. Finally, the bitmap parameter is the pointer of the bitmap data.
When glrasterpos or glwindowpos is called, the previously set glcolor will be treated as the raster color. If you then set another color through glcolor, does not affect the current raster color. You can use getfloatv (gl_current_raster_color) to query this value.
2. Compared with bitmap, an image contains complete pixel data, such as rgba values. Images are from cameras, or other software, or read from framebuffer. Naturally, it may be thought that image data should be read from the color buffer. In fact, both depth buffer and stencel buffer can be read. Images can be displayed directly or used as textures. Different frame buffers have different data formats and local image data may have different formats. In this way, different pixel data formats must be considered for data migration and data conversion.
BasicApiglreadpixels (glint X, glint y, glsizei width, glsizei height, glenum format, glenum type, glvoid * pixels) reads pixel data from the frame buffer to the local memory: X, Y is the Coordinate Position of the window. to read the lower left corner of the block, width, height: the size of the block to read. Format: Actually indicates the target data to be read, for example, gl_rg reads the red and green color components of each pixel in the color buffer, gl_alpha reads only the independent Alpha component, gl_depth_component reads the value of depth buffer, and so on; Type: data of each pixel is stored in local memory. For example, gl_unsigned_int indicates that each component of each pixel occupies 32 bits, and gl_unsigned_int_8_8_8_8 indicates that each element occupies 32 bits, each member component of the Data occupies 8 bits, gl_unsigne D_short_5_6_5 is a 16-bit format. For more information, see the manual. This parameter value should correspond to the format, and cannot be combined at will. Pixels: local memory pointer. Data ing conversion occurs during data reading. If the color data in the frame is floating point and an 8-bit integer is to be read, the data will be mapped. If the members of pixel data are packaged into an integer, the first member is normally placed at the high level, and the last member is placed at the low level. However, if the type parameter is in the form of * _ Rev, this order is the opposite.
Gldrawpixels (glsizei width, glsizei height, glenum format, glenum type, const glvoid * pixels) writes pixel data from the local cache to the frame buffer. For parameter meanings, see glreadpixels. Glcopypixels (glint X, glint y, glsizei width, glsizei height, glenum buffer);: Copies an area from one location in the frame buffer to another location: X, Y: location of the source data; width, height: size of the source data; Buffer: Read the buffer, color, depth .... the target location of the write is specified by glrasterpos.
Note that the data to be written by the last two APIs must go through two stages: Rasterization and fragment processing. Pixel data pack & unpackThe local data conversion process that reads pixel data from the frame buffer is called pack, which in turn is unpack. Local Storage may vary with different hardware platforms. For example, different platforms have different collation and different bytes alignment requirements. Also, you may want to display a part of the image. These functions are completed through glpixelmode. Glpixelstore * (glenum pname, type PARAM) defines some features of image data in memory, affecting gldrawpixels (), glreadpixels (), glbitmap (), glpolygonstipple (), glteximage *() API behavior. Pname is the attribute name, Param is the attribute value, and possible pnames include: Gl _ (un) pack_swap_bytes: affects the Data byte order, of course, only affects the multi-byte element type, if this attribute is not defined, the byte sequence of the element is reversed. GL _ (un) pack_lsb_first: affects the bitmap or image data of a single bit per pixel. If this attribute is set to false, the data storage level is higher than the storage level; otherwise, the data level is higher than the storage level. GL _ (un) pack_row_length: If you only want to display the image with a child rectangle, therefore, the width and height of gldrawpixel should be the width and height to be displayed. This parameter can specify the true width of the image data; otherwise, the data element cannot be correctly located; GL _ (UN) pack_skip_pixels: used with the previous attribute. The first few pixels are ignored for each row of data. GL _ (un) pack_skip_rows: used with the previous attribute. The first few rows are ignored for image data. GL _ (UN) pack_alignment: The bytes of each row of image data are aligned. The default value is 4. Pixel transfer operations (pixel-transfer operations)When pixel data is transmitted from memory to framebuffer, data may need to be processed, for example, the color floating point number is scaled or offset. Use glpixeltransfer * () and glpixelmap * () APIs. Glpixeltransfer {if} (glenum pname, type PARAM): pname, Param operation parameter, and pname may have many values. Here are only a few examples. Gl_map_color: Enable color ing. For more information, see gl_map_stencel: stencel ing; gl_red_scale: color red component scale; gl_red_bias: color red component offset. Glpixelmap {ui us f} V (glenum map, glint mapsize, const type * values). For the ing operation, you must first enable it through the previous API. map is the ing type, mapsize and values are the size and data of the ing table. ZoomIn the Rasterization phase after the preceding operations, you can zoom the pixel rectangle to zoom in, zoom out, and flip. Glpixelzoom (glfloat zoomx, glfloat zoomy): The Reverse zoom parameter of X and Y, which can be a negative number. The pull is relative to the starting point (the position set by glrasterpos. In the Rasterization process, each pixel data corresponds to a (zoomx, zoomy) rectangle, and the fragement in this rectangle is generated based on this pixel data. Use BoUsing Bo can reduce the transmission of image data and improve efficiency. The use of Bo is similar to that described in the previous chapter. Taking drawpixel as an example: Generate Bo name: glgenbuffers (1, & pixelbuffer );
Bind Bo: glbindbuffer (gl_pixel_unpack_buffer, pixelbuffer );
Initialize Bo data: glbufferdata (gl_pixel_unpack_buffer, size, imagedata, gl_static_draw); Use Bo plot: gldrawpixels (width, height, gl_rgb, gl_unsigned_byte, buffer_offset (0 )); // bind the image and image subsetimage subset to be an extension of OpenGL, providing additional pixel processing functions for glpixeltransfer and glpixelmap. When pixel data is passed in/out of OpenGL, it can be processed by image subset: 1. Use color table to replace pixel values; 2. Use convolution to filter images; 3. Use the color matrix for color conversion; 4. Collect the statistical data of the image. Color tables
Color Table is a search table used for color replacement. It can be used for contrast adjustment, filter, and image horizontal. There are three types of tables that work in different stages (see the original book), gl_color_table, gl_post_convolution_color_table, and gl_post_color_matrix_color_table. Create color tableglcolortable (glenum target, glenum internalformat, glsizei width, glenum format, glenum type, const glvoid * Data) Target: Table internalformat: Specify and replace; format and type are consistent with gldrawpixels;

Read the frame buffer and create tablevoid glcopycolortable (glenum target, glenum internalformat, glint X, glint y, glsizei width );

Modify the color table (partial) Data glcolorsubtable (glenum target, glsizei start, glsizei count, glenum format, glenum type, const glvoid * data );
Glcopycolorsubtable (glenum target, glsizei start, glint X, glint y, glsizei count );

Obtain the colortable data glgetcolortable (); Convolution (Convolution)Convolution is an operation that replaces pixel values with the weighted combination of pixels and peripheral pixel values. Blur, sharpen, and other effects. The core of convolution is a matrix. The elements of a matrix are numerical values. The calculation process is to multiply and sum the values in the matrix with the pixels at the corresponding position to obtain new pixel values.

Define Convolution: glconvolutionfilter2d (glenumTarget,
GlenumInternalformat,GlsizeiWidth,
GlsizeiHeight,GlenumFormat,
GlenumType,Const glvoid * image );

Read pixel values from the frame buffer to define Convolution: glcopyconvolutionfilter2d (glenumTarget,GlenumInternalformat,GlintX,
GlintY,GlsizeiWidth,
GlsizeiHeight );


Define convolution parameters, such as edge mode.:Glconvolutionparameter {if} (glenumTarget,
GlenumPname, type PARAM );


Color Matrix

A color matrix is a 4*4 matrix used for linear transformation of pixel colors, such as color space transformation.

Usage: glmatrixmode (gl_color );

Glloadmatrix (matrixdata );

Histogram

Used to calculate the image color. Usage:

Define histogram: glhistogram (glenumTarget,
GlsizeiWidth,GlenumInternalformat,GlbooleanSink );

Collect histogram data: glgethistogram (glenumtarget, glboolean reset, glenum format, glenum
Type, glvoid * values );

MINMAX

Similar to histogram, the maximum and minimum values of image colors are counted.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.