OpenGL: bitmap and Image

Source: Internet
Author: User
Tags 0xc0

11.1 bitmap

11.1.1 bitmap and character (font)
Bitmap is stored in the form of a matrix with an element value of 0 or 1. It is usually used to block the drawing of the corresponding area in the window. For example, if the current color is set to red, the pixels are replaced by red when the matrix element value is 1. Otherwise, the corresponding pixels are not affected when the value is 0. Bitmap is generally used for character display. See the following example:

Example 11-1 bitmap character routine (font. c)

# Include "Glos. H"
# Include <Gl/Gl. h>
# Include <Gl/Glu. h>
# Include <Gl/Glaux. h>

Void myinit (void );
Void callback myreshape (glsizei W, glsizei H );
Void callback display (void );

Glubyte rasters [12] = {
0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xfc,
0xfc, 0xc0, 0xc0, 0xc0, 0xff, 0xff };

Void myinit (void)
{
Glpixelstorei (gl_unpack_alignment, 1 );
Glclearcolor (0.0, 0.0, 0.0, 0.0 );
Glclear (gl_color_buffer_bit );
}

Void callback display (void)
{
Glcolor3f (1.0, 0.0, 1.0 );
Glrasterpos2i (100,200 );
Glbitmap (8, 12, 0.0, 0.0, 20.0, 20.0, rasters );
Glbitmap (8, 12, 0.0, 0.0, 0.0, 0.0, rasters );

Glcolor3f (1.0, 1.0, 0.0 );
Glrasterpos2i (150,200 );
Glbitmap (8, 12, 0.0, 0.0, 0.0, 0.0, rasters );

Glflush ();
}

Void callback myreshape (glsizei W, glsizei H)
{
Glviewport (0, 0, W, H );
Glmatrixmode (gl_projection );
Glloadidentity ();
Glortho (0, W, 0, H,-1.0, 1.0 );
Glmatrixmode (gl_modelview );
}

Void main (void)
{
Auxinitdisplaymode (aux_single | aux_rgba );
Auxinitposition (0, 0,500,500 );
Auxinitwindow ("bitmap ");
Myinit ();
Auxreshapefunc (myreshape );
Auxmainloop (Display );
}

AboveProgramThe running result shows three identical characters, F. The OpenGL function library only provides the underlying operations, that is, using glrasterpos * () and glbitmap () to locate and draw a bitmap on the screen, figure 11-1 shows the bitmap of F and corresponding bitmap data.

 
Figure 11-1 character F bitmap display

 
Figure 11-2 character F bitmap and corresponding data

In the figure, the character size is a 12*8 square matrix, and each row of data is represented in octal hexadecimal notation. Note: bitmap data is always stored in blocks, and the number of digits of each block is always a multiple of 8. However, the actual bitmap width is not necessarily a multiple of 8. The bitmap bit is drawn from the lower left corner of the bitmap: first, draw the bottom line, then the last line of the line, and so on. Several important functions in this program are explained in the following sections. The function glpixelstorei () describes how bitmap data is stored in computer memory.

11.1.2 current Grating Position
The current Grating Position function is:

Void glrasterpos {234} {sifd} [v] (Type X, Type Y, type Z, type W );

Set the source of the current bitmap or image. The X, Y, Z, and W parameters provide the grating coordinate. When switching to screen coordinates (that is, using model transformation and perspective transformation), the positions and coordinates of the grating are the same as those provided by glvertex. That is to say, after the conversion, either an effective point is determined or the current Grating Position of the point located outside the viewport is considered invalid.
In the previous example, the position of the color setting is related to the position called by the current Grating Position function. glcolor * () must be placed before glrasterpos, the bitmap following it inherits the current color, for example, the first two purple F; to change the color of the current bitmap, you need to call glcolor * () again *() and glrasterpos * (), such as the display of the third yellow character F.

11.1.3 bitmap display
After the grating position is set, you can call the glbitmap () function to display the bitmap data. The function form is:

Void glbitmap (glsizei width, glsizei height, glfloat xbo, glfloat YbO,
Glfloat xbi, glfloat YBI, const glubyte * Bitmap );

Displays the bitmap specified by bitmap. bitmap is a pointer to a bitmap. The origin of the bitmap is placed on the recently defined position of the current grating. If the current Grating Position is invalid, this bitmap or part is not displayed, and the current grating position is still invalid. The width and height parameters are measured in pixels, indicating the width and height of the bitmap. The width is not necessarily a multiple of 8. The xbo and YbO parameters define the origin of the bitmap (when a positive value is reached, the origin is moved up; when a negative value is reached, the origin is moved down ). The increment of the parameter xbi and YBI after the bitmap grating. In the previous example:

Glcolor3f (1.0, 0.0, 1.0 );
Glrasterpos2i (100,200 );
Glbitmap (8, 12, 0.0, 0.0, 20.0, 20.0, rasters );
Glbitmap (8, 12, 0.0, 0.0, 0.0, 0.0, rasters );

The spacing between the first character F and the second character F is determined by two incremental parameters of glbitmap, that is, the second character F moves 20 pixels to the X axis and Y axis respectively based on the first character F.

11.2 Images
Generally, OpenGL image operations include pixel read/write, pixel copy, and image scaling.

11.2.1 pixel read/write
OpenGL provides the most basic pixel read and write functions, which are:

Read pixel data:

Void glreadpixels (glint X, glint y, glsizesi width, glsizei height,
Glenum format, glenum type, glvoid * pixel );

Function parameters (x, y) define the coordinates of the points in the lower-left corner of the image area. width and height indicate the height and width of the image respectively. * pixel is a pointer pointing to an array storing image data. The format parameter specifies the format of the read pixel data element (index value or R, G, B, and a value, as shown in table 11-1 ), the parameter type indicates the Data Type of each element (see table 11-2 ).
Write pixel data:

Void gldrawpixels (glsizesi width, glsizei height, glenum format,
Glenum type, glvoid * pixel );

The format and type parameters of the function have the same meaning as glreadpixels (). The array pointed to by Pixel contains the pixel data to be drawn. Note: You must set the current Grating Position before calling this function. If the current Grating Position is invalid, no image is drawn when this function is provided, and the current grating position remains invalid.

Name pixel data type
Gl_index single color index
Gl_rgb is the red component, the green component, and the blue component.
Gl_red single red weight
Gl_green single green component
Gl_blue single blue component
Gl_alpha single alpha Value
Gl_luminance_alpha is the brightness component and then the Alpha value.
Gl_stencil_index single template Index
Gl_depth_component single depth component
Table 11-1 function glreadpixels () and gldrawpixels () pixel formats

Name Data Type
Gl_unsigned_byte unsigned 8-digit integer
Gl_byte 8-digit integer
A single digit in an unsigned 8-bit integer array of gl_bitmap
Gl_unsigned_short unsigned 16-bit integer
Gl_short 16-digit integer
Gl_unsigned_int unsigned 32-bit integer
Gl_int 32-bit integer
Gl_float Single-precision floating point number
Table 11-2 function glreadpixels () and gldrawpixels () pixel data types

Each element of an image is stored according to the Data Type given in Table 11-2. If an element represents a continuous value, such as a red, green, blue, or brightness component, each value is scaled proportionally to make it suitable for available digits. For example, the red component is the floating point value between 0.0 and 1.0. If it needs to be placed in an unsigned single-byte integer, it can only be saved with 8-bit precision. The same applies to other unsigned integer types. For signed data types, the color index must be saved to a signed 8-digit integer, its first digit is shielded by 0xfe (that is, this mask contains 7 1 ). If the type is gl_float, the index value is converted into a single-precision floating point value. For example, Index 17 is converted to 17.0.

11.2.2 pixel copy
The pixel copy function is:

Void glcopypixels (glint X, glint y, glsizesi width, glsizei height, glenum type );

This function is similar to calling the glreadpixels () function before calling gldrawpixels (), but it does not need to write data to the memory because it only writes data to framebuffer. The function is to copy the pixel data in the rectangle area (x, y) with the width and height in the lower left corner of framebuffer. Copy the data to a new position. The position in the lower left corner of the data source is in the current grating. The parameter type can be gl_color, gl_stencer, or gl_depth. During the copy process, the type parameter must be converted to format as follows:
1) if the type is gl_depth or gl_stencel, the format should be gl_depth_component or gl_stencil_index;
2) If the type is gl_color, format uses gl_rgb or gl_color_index. This depends on whether the graphic system is in the rgba mode or in the color table mode.

11.2.3 Image Scaling
Generally, a pixel of an image is also a pixel when it is written to the screen, but sometimes the image needs to be zoomed in or out. OpenGL provides this function:

Void glpixelzoom (glfloat zoomx, glfloat zoomy );

Sets the zoom-in or zoom-out factor for Pixel write operations in the X and Y directions. By default, both zoomx and zoomy are 1.0. If they are all 2.0, each image pixel is painted on four screen pixels. Note: The scaling factor and negative factor in decimal form are acceptable.

11.2.4 image routine
The following is an example of image application:

Example 11-2 image application routine (image. c)

# Include "Glos. H"
# Include <Gl/Gl. h>
# Include <Gl/Glu. h>
# Include <Gl/Glaux. h>

Void myinit (void );
Void triangle (void );
Void sourceimage (void );
Void callback display (void );
Void callback myreshape (glsizei W, glsizei H );

Void myinit (void)
{
Glclear (gl_color_buffer_bit );
}

Void triangle (void)
{
Glbegin (gl_triangles );
Glcolor3f (0.0, 1.0, 0.0 );
Glvertex2f (2.0, 3.0 );
Glcolor3f (0.0, 0.0, 1.0 );
Glvertex2f (12.0, 3.0 );
Glcolor3f (1.0, 0.0, 0.0 );
Glvertex2f (7.0, 12.0 );
Glend ();
}

Void sourceimage (void)
{
Glpushmatrix ();
Glloadidentity ();
Gltranslatef (4.0, 8.0, 0.0 );
Glscalef (0.5, 0.5, 0.5 );
Triangle ();
Glpopmatrix ();
}

Void callback display (void)
{
Int I;

/* Draw the original image */
Sourceimage ();

/* Copy the image */
For (I = 0; I <5; I ++)
{
Glrasterpos2i (1 + I * 2, I );
Glcopypixels (160,310,170,160, gl_color );
}

Glflush ();
}

Void callback myreshape (glsizei W, glsizei H)
{
Glviewport (0, 0, W, H );
Glmatrixmode (gl_projection );
Glloadidentity ();
If (W <= H)
Gluortho2d (0.0, 15.0, 0.0, 15.0 * (glfloat) h/(glfloat) W );
Else
Gluortho2d (0.0, 15.0 * (glfloat) W/(glfloat) h, 0.0, 15.0 );
Glmatrixmode (gl_modelview );
}

Void main (void)
{
Auxinitdisplaymode (aux_single | aux_rgba );
Auxinitposition (0, 0,500,500 );
Auxinitwindow ("pixel processing ");
Myinit ();
Auxreshapefunc (myreshape );
Auxmainloop (Display );
}

The above program runs and displays an initial colorful triangle on the front of the screen, and a copy of the triangle in the lower half. Of course, you can add more images, zoom in and out, and try again. What will happen?

 

image copy from Figure 11 to 3

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.