OpenGL pixel format Management

Source: Internet
Author: User
Tags color representation
1. Windows color palette
OpenGL can use 16, 256, 64 K, and 16 m true colors. The color palette is not required in the true color mode, but it is impossible to get satisfactory results in the 16 color mode. Therefore, for OpenGL, the color palette is meaningful only in the 256 color mode.
We know that Windows divides the color palette into a system color palette and a logical color palette. Each application Program All have their own logical color palette (or use the default color palette ), when the app has a keyboard input focus, you can use up to 256 colors selected from 16 m colors (20 system retain colors and 236 freely selected colors ), however, applications that lose focus may have some colors that do not display properly. The system palette is managed by the Windows Kernel. It consists of 20 colors retained by the system and the colors set by each application after arbitration, and corresponds to the 256 color palette of the hardware. The logical color palette of an application does not have a direct correspondence with the hardware color palette, but is mapped to the system color palette according to the minimum error principle, therefore, even if the application selects 256 different colors to form its own logical palette, some colors may be the same when displayed on the screen.
When the keyboard input focus is received in the application window, Windows sends a wm_querynewpalette message to it, asking it to set its own logical palette, in this case, Windows will add as many colors as possible to the system palette and generate corresponding mappings. Windows then sends a wm_palettechanged message to all the overwrite windows and top-level windows (including windows with keyboard input focus), asking them to set the logical palette and redraw the customer area, in order to make full use of the System palette, windows with keyboard input focus should not process this message to avoid endless loops.
2. OpenGL Color Representation and conversion
OpenGL uses floating-point numbers to represent and process colors. The four components, red, green, and blue, have a maximum value of 1.0 and a minimum value of 0.0. In the 256 color mode, OpenGL converts the internal values of a pixel color to eight bits in a linear relationship to output to the screen, where red occupies three bits of the second bit, green occupies 3 bits in the middle and blue occupies 2 bits in the highest position. Windows regards the 8 bits as the index value of the logical palette. For example, after the color value of OpenGL (1.0, 0.14, 0.6667) is converted, the binary value is 10001111 (111 in red, 001 in green, and 10 in blue), that is, the 143rd color palette, the RGB value of the color specified by the color palette should have the same ratio as (1.0, 0.14, 0.6667), which is (170,). If not, then the displayed color has an error.
3. color palette generation Algorithm
Obviously, the 8-bit value output by OpenGL directly shows the color composition. In order to make the image display normal, we should set the logical palette with a linear relationship, make the index value directly indicate the color composition. Therefore, when the color palette is generated, the index value is divided from low to high into 3-3-2 parts, and each part is mapped to 0-73,109,146,182,219,255, so that the 3-bit ing is {0, 36 }, 2-bit ing is {170,255,}, and the three parts are combined into a color.
After the above processing, the 256 colors are evenly distributed in the color space and do not completely contain the 20 colors (only 7) retained by the system ), this means that several colors will be displayed in the same way, thus affecting the effect. A better solution is to incorporate 13 system colors into the logical palette based on the minimum mean square error principle.
In principle, linear ing is not necessary, but other ing relationships can be used, such as Gamma Correction, to better meet the visual characteristics of the human eye, however, these mappings are not widely used and will not be discussed here.
1.3.4 pixel format settings
Pixel format is an important attribute of OpenGL windows. It includes whether to use double buffering, color digits and types, and depth digits. The pixel format can be defined by the so-called pixel format description sub-structure defined by the Windows System (pixelformatdescriptor). This structure is defined in windows. h.
The structure contains 26 attributes in the form:
Typedef struct tagpixelformatdescriptor
{
Word nsize;
Word nversion;
DWORD dwflags;
Byte ipixeltype;
Byte ccolorbits;
Byte credbits;
Byte credshift;
Byte cgreenbits;
Byte cgreenshift;
Byte cbluebits;
Byte cblueshift;
Byte calphabits;
Byte calphashift;
Byte caccumbits;
Byte caccumredbits;
Byte caccumgreenbits;
Byte caccumbluebits;
Byte caccumalphabits;
Byte cdepthbits;
Byte cstencilbits;
Byte cauxbuffers;
Byte ilayertype;
Byte breserved;
DWORD dwlayermask;
DWORD dwvisiblemask;
DWORD dwdamagemask;
} Pixelformatdescriptor;
The meanings of each variable are as follows:
Nsize: Memory occupied by this structure.
Nversion: version number. The current value is 1.
Dwflags: Specify the pixel format attribute. The optional parameters are shown in Table 1.1.

Table 1.1 pixel format attributes

Identifier explanation
Pfd_draw_to_bitmap supports drawing Bitmap in memory
Pfd_draw_to_window supports Screen Drawing
Pfd_doublebuffer supports double buffering.
Pfd_ceneric_format specifies the pixel format supported by GDI
Pfd_need_palette specifies the logical color palette
Pfd_need_system_palette specifies the hardware palette.
Pfd_stereo nt not supported
Pfd_support_opengl supports OpenGL
Pfd_support_gdi supports GDI. pfd_doublebuffer cannot be used at this time.
Ipixeltype: pixel color mode, which can be pfd_type_rgba or pfd_type_index, which corresponds to the rgba mode and color index mode respectively.
Ccolorbits: specifies the number of digits of the color.
Credbits: the number of digits occupied by the red component when the rgba mode is used.
Credshift: the offset of the red component when the rgba mode is used.
Cgreenbits: the number of green components in the rgba mode.
Cgreenshift: the offset of the green component when the rgba mode is used.
Cbluebits: When rgba is used, the blue component occupies the number of digits.
Cblueshift: the offset of the blue component when the rgba mode is used.
Calphabits: the number of digits occupied by the Alpha component in rgba mode.
Calphashift: the Alpha group offset when the rgba mode is used.
Caccumbits: Specifies the cumulative buffer to indicate the number of digits used by a pixel.
Caccumredbits: Specifies the cumulative buffer to indicate the number of digits occupied by the red component.
Caccumgreenbits: Specify the cumulative buffer to indicate the number of BITs occupied by the Green Group.
Caccumbluebits: Specifies the cumulative buffer to indicate the number of BITs occupied by the blue component.
Caccumalphabits: Specifies the cumulative buffer to indicate the number of digits occupied by the Alpha component.
Cdepthbits: Specify the depth buffer to indicate the number of digits used by a pixel.
Cstencilbits: specifies the number of digits used by a template buffer to indicate a pixel.
Cauxbuffers: Specifies the secondary buffer, which is not supported by Windows9x and NT.
Ilayertype: Windows 9x and NT can only be pfd_main_plane.
Breserved: = 0.
Dwlayermask: Specifies the shielding layer, which is not supported by Windows9x or NT.
Dwvisiblemask: not supported by Windows9x and NT.
Dwdamagemask: not supported by Windows9x and NT.
Windows provides four pixel format management functions, which are described as follows:

(1) int choosepixelformat (HDC,
pixelformatdescriptor * PPDF)
This function compares the pixel format descriptions passed in with the pixel format supported by OpenGL, returns an index with the best matching pixel format. This index value can be passed to setpixelformat to set the pixel format for DC. If the returned value is 0, the operation fails.
when comparing the pixel format, the matching priority order is the following fields in the description sub-structure of the pixel format:
dwflags-> ccolorbits-> calphabits-> caccumbits
-> cdepthbits-> cstencilbits-> cauxbuffers-> ilayertype
hardware-supported pixel formats take precedence over.
(2) int describepixelformat (HDC, int ipixelformat, uint nbytes,
lppixelformatdescriptor * ppfd)
This function uses the format index ipixelformat to specify the pixel format description sub-structure pointed to by ppfd. This function can be used to enumerate pixel formats.
(3) int getpixelformat (HDC)
This function is used to obtain the HDC format index.
(4) bool setpixelformat (HDC, int ipixelformat,
lppixelformatdescriptor * ppfd)
This function uses the format index ipixelformat to set the HDC pixel format. Before using this function, call choosepixelformat to obtain the pixel format index. In addition, the OpenGL window style must contain the ws_clipchildren and ws_clipsiblings types. Otherwise, the setting fails.
note that the choosepixelformat function does not necessarily return an optimal pixel format value. You can use describepixelformat to enumerate all supported pixel formats. OpenGL usually supports 24 different pixel formats. If the system has installed the OpenGL hardware accelerator, it may support other pixel formats.
Step 1.5 shows how to set the DC pixel format.
Figure 1.5 General steps for setting the pixel format

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.