Opengl draw on bitmap, screenoff mode

SetPixelFormat retrurn false, follow example:

int main(int, char **) {
    HDC hdc = CreateCompatibleDC(NULL);
    std::cout << "HDC -> " << std::hex << hdc << std::endl;
    
    HBITMAP hbitmap = CreateCompatibleBitmap(hdc, 640, 480);
    std::cout << "HBITMAP -> " << std::hex << hbitmap << std::endl;
    SelectObject(hdc, hbitmap);

    PIXELFORMATDESCRIPTOR pfd = {0};
    pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR) ;
    pfd.nVersion = 2 ;
    pfd.dwFlags = PFD_DRAW_TO_BITMAP | 
                  PFD_SUPPORT_OPENGL |
                  PFD_SUPPORT_GDI ;
    pfd.iPixelType = PFD_TYPE_RGBA ;
    pfd.cColorBits = 24 ;
    pfd.cDepthBits = 32 ;
    pfd.iLayerType = PFD_MAIN_PLANE ;

    int pixelFormat =  ChoosePixelFormat(hdc, &pfd);
    std::cout << "Pixels format -> " << std::hex << pixelFormat << std::endl;

    bool result = SetPixelFormat(hdc, pixelFormat, &pfd);

    std::cout << "Result set pixels -> " << std::hex << result << std::endl;
    std::cout << "Last Errors -> " << std::dec << GetLastError() << std::endl;

    return 0;
}

Result:


HDC -> B001181D
HBITMAP -> 14050F41
Pixels format -> 23
Result set pixels -> 0
Last Errors -> 1

It’s possible that your hardware doesn’t support any pixelformats which meet these requirements. You can do a loop over a DescribePixelFormat call (remembering that pixel formats are 1-based) checking if any support (PFD_DRAW_TO_BITMAP | PFD_SUPPORT_OPENGL | PFD_SUPPORT_GDI) and see what comes back.

There are several issues in your code:

  1. You have to select the bitmap into the memory device context. (Do you have any experience with GDI? Objects have to be selected in order to be accessed.)

  2. When you setting up the pixel format descriptor, you have to use parameters of the selected bitmap. It is not a window you can play with and set arbitrarily.

  3. Are you sure you really what to render to a bitmap? Although it is possible, it is completely meaningless! There are several reasons, by I’ll list just the most important: no hardware acceleration (it would execute several orders of magnitude slower than it should - at least 100 times) and confined to OpenGL 1.1 (Microsoft’s default sw renderer).

More details about rendering to a bitmap can be found here:

But, once again, stay away from rendering to a bitmap and please consider using FBO instead.

nVersion is the version of the PIXELFORMATDESCRIPTOR data structure (and there is only version 1), and not the OpenGL version.
(see https://msdn.microsoft.com/en-us/library/windows/desktop/dd368826(v=vs.85).aspx )

Believe it or not, but it doesn’t matter. On my Win7 it works perfectly whatever value is set. It seams the version is completely ignored.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.