Pre-Drawing settings: Pixel format--pixelformatdescript, device context, render context

Source: Internet
Author: User
Tags transparent color

pixel format--pixelformatdescript, device context, render context

Before OpenGL renders a window, the window must be configured according to the needs of the rendering.

Do you need hardware rendering or software rendering?

Render using but buffered or double buffering mode?

Do you need a depth buffer?

Do you need a template, target alpha, or cumulative buffer?

After you set these parameters for a window, you cannot modify them. In order to switch from a window with only a depth buffer and a color buffer to a window with a template buffer, the first window must be destroyed and then recreated as necessary.

The pixel format is an important property of the OpenGL window, including whether to use a double buffer, the number of color bits and types, and the number of depth bits. The pixel format can be defined by the so-called pixel format description sub-structure defined by the Windows System (Pixelformatdescriptor), which is defined in the Windows.h header file.

The structure contains 26 attribute information in the form of:

typedefstructtagpixelformatdescriptor{WORD nSize; //The pixel format describes the size of the substructure, and sizeof (Pixelformatdescriptor) sets its valueWORD nversion;//version of the pixelformatdescriptor structure, typically set to 1DWORD DwFlags;//flag bits that indicate the pixel buffering characteristics, such as whether the buffer supports GDI or OpenGL, etc.BYTE Ipixeltype;//Indicates whether the pixel data type is RGBA or a color indexBYTE ccolorbits;//The number of color bit planes in each color buffer, which is the buffer size for the color indexBYTE credbits;//number of red bit planes in each RGBA color bufferBYTE Credshift;//The offset number of the red bit plane in each RGBA color bufferBYTE cgreenbits;//the number of green bit planes in each RGBA color bufferBYTE Cgreenshift;//The offset number of the green bit plane in each RGBA color bufferBYTE cbluebits;//number of blue bit planes in each RGBA color bufferBYTE Cblueshift;//The offset number of the blue bit plane in each RGBA color bufferBYTE calphabits;//number of alpha bit planes in each RGBA color buffer (reserved, not currently supported)BYTE Calphashift;//the number of offsets for the alpha bit plane in each RGBA color buffer (reserved, not currently supported)BYTE caccumbits;//the number of all bit planes in the cumulative bufferBYTE caccumredbits;//number of red bit planes in the accumulate bufferBYTE caccumgreenbits;//number of green bit planes in the accumulate bufferBYTE caccumbluebits;//number of blue bit planes in the accumulate bufferBYTE caccumalphabits;//number of alpha bit planes in the accumulate bufferBYTE cdepthbits;//depth of Z (depth) bufferBYTE cstencilbits;//depth of the template bufferBYTE cauxbuffers;//Number of axial buffers (typically 1.0 versions not supported)BYTE Ilayertype;//to be ignored and included for consistency.BYTE breserved;//number of surface and bottom planes: 0-3 table up to 15 layers of surface plane, bit 4-7 table BottomDWORD Dwlayermask;//to be ignored and included for consistency.DWORD Dwvisiblemask;//is the value of the transparent color (RGBA mode) or an index of the underlying plane (index)DWORD Dwdamagemask;//to be ignored and included for consistency.} Pixelformatdescriptor;

The method for rendering the form pixel format is as follows:

1     //the pixel format tells OpenGL whether to use important information such as dual cache, color mode, number of color bits, depth digits, and so on. 2     //It is described by a description called Pixelformatdescriptor. 3     StaticPixelformatdescriptor pixeldesc={sizeof(Pixelformatdescriptor),//size of the PFD structure4     1,//Version number5pfd_draw_to_window|//supports drawing in Windows6pfd_support_opengl|//support OpenGL7Pfd_doublebuffer,//Dual-cache mode8Pfd_type_rgba,//Rgba Color Mode9      +,//32-bit color depthTen     0,0,0,0,0,0,//Ignore color bits One     0,//No opacity cache A     0,//Ignore shift bit -     0,//No Cumulative cache -     0,0,0,0,//Ignore cumulative bit the      +,//32-bit deep cache -     1,//Template Caching -     0,//No secondary cache -Pfd_main_plane,//Main layer +     0,//reserved -     0,0,0                  //ignore layers, visibility and damage masks +     }; A      at      -  -     //get the most matching pixel format for the device description table -     if(! (Pixelformat=choosepixelformat (Hdc,&pixeldesc))) -     { -MessageBox (_t ("Choosepixelformat failed!")); in         return false; -     } to  +     //sets the most matched pixel format for the current pixel format -     if(! Setpixelformat (Hdc,fixelformat,&pixeldesc)) the     { *MessageBox (_t ("Setpixelformat failed!")); $         return false;Panax Notoginseng     }

It's worth mentioning that here's how to get the device context hdc in the following way (immediately after the previous article " Add OpenGL form to MFC Dialog program ):

1     /* ******************************************************** */ 2     // Get device context 3     CWnd *wnd=getdlgitem (idc_render);   // Idc_render is the ID number of the Picturecontorl control 4     HRENDERDC=::GETDC (wnd->5/     ************************************** ********************/

To continue, after setting the pixel format for the device context, you need to set the render context HGLRC HRENDERRC, as follows:

1 //Render Context HRC2 BOOL Copengltest1dlg::createviewglcontext (hdc hdc)3 { 4     if(! (HRENDERRC =Wglcreatecontext (HDC)); 5     {6MessageBox (_t ("Createcontext failed!"));7         return false;8     }9 Ten     if(!(Wglmakecurrent (HDC,HRENDERRC))) One     { AMessageBox (_t ("makecurrent failed!")); -         return false; -     } the  -     returnTRUE; -}

Once you have done this, you can turn on your drawing work on the OpenGL rendering context form!

Pre-Drawing settings: Pixel format--pixelformatdescript, device context, render context

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.