Summary of OpenGL in MFC (iii) -- off-screen rendering and openglmfc
Sometimes it is not suitable to directly create an OpenGL window, or it is not allowed to create a window at all. Just like my current project, the created window cannot be displayed and is limited by the main framework, however, I can only do some work in the subclass of ActiveX controls, so I can only use OpenGL's off-screen rendering technology ~ That is, it is not directly drawn to the window, but to a bitmap, and then calls this bitmap again to implement subsequent work.
The following describes how to use the so-called "off-screen rendering ".
Const int WIDTH = 500; const int HEIGHT = 500; // Create a memory DC compatible with the screen HDC hdc = CreateCompatibleDC (0); if (hdc = 0) cout <"cocould not create memory device context"; // Create a bitmap compatible with the DC // must use CreateDIBSection (), and this means all pixel ops must be synchronised // using callto GdiFlush () (see CreateDIBSection () docs) BITMAPINFO bmi = {sizeof (BITMAPINFOHEADER), WIDTH, HEIGHT, 1, 32, BI_RGB, 0, 0, 0, 0}, {0}; unsigned char * pbits; // pointer to bitmap bits HBITMAP hbm = CreateDIBSection (hdc, & bmi, DIB_RGB_COLORS, (void **) & pbits, 0, 0); if (hbm = 0) cout <"cocould not create bitmap"; // HDC hdcScreen = GetDC (0); // HBITMAP hbm = CreateCompatibleBitmap (hdcScreen, WIDTH, HEIGHT ); // Select the bitmap into the dc hgdiobj r = SelectObj Ect (hdc, hbm); if (r = 0) cout <"cocould not select bitmap into DC "; // Choose the pixel format PIXELFORMATDESCRIPTOR pfd = {sizeof (PIXELFORMATDESCRIPTOR), // struct size 1, // Version number required | PFD_SUPPORT_OPENGL, // use OpenGL drawing to BM PFD_TYPE_RGBA, // RGBA pixel values 32, // color bits 0, 0, 0, // RGB bits shift sizes... 0, 0, 0, // Don't care about them 0, 0, // No al Pha buffer info 0, 0, 0, 0, 0, // No accumulation buffer 32, // depth buffer bits 0, // No stencel buffer 0, // No auxiliary buffers PFD_MAIN_PLANE, // Layer type 0, // Reserved (must be 0) 0, // No layer mask 0, // No visible mask 0, // No damage mask}; int pfid = ChoosePixelFormat (hdc, & pfd); if (pfid = 0) cout <"Pixel format selection failed "; // Set the pixel format //-must be done * after * The bitmap is selected into dc bool B = SetPixelFormat (hdc, pfid, & pfd); if (! B) cout <"Pixel format set failed"; // Create the OpenGL resource context (RC) and make it current to the thread HGLRC hglrc = wglCreateContext (hdc ); if (hglrc = 0) cout <"OpenGL resource context creation failed"; wglMakeCurrent (hdc, hglrc); // Draw using GL-remember to sync with GdiFlush () gdiFlush ();/* Specific plotting functions ~~~~~~~~~~~~~~ I will not write * // Clean up wglDeleteContext (hglrc); // Delete RC SelectObject (hdc, r); // Remove bitmap from DC DeleteObject (hbm ); // Delete bitmap DeleteDC (hdc); // Delete DC
Use OpenGL to perform off-screen rendering on an object, and then extract the pixel of the rendered object from the frame cache?
This is the feature of OpenGL dual cache, but it also has a single cache mode.
When OpenGL is used in a single MFC Document, how can we first perform operations to render the image, instead of running the image,
These two questions are not easy to understand. You can manually control rendering control. If you do not execute the display function at the beginning, enable the display function in the message of the menu operation. You don't usually get small. Check your program content, probably because of the lack of glLoadIdentity.