OpenGL Programming for Windows MFC


9. Rendering to Bitmap


9.1 PixelFormat for Bitmap (Example)


int SetDCBITMAPPixelFormat (HDC hdc)
{

    static PIXELFORMATDESCRIPTOR pfd = {
        sizeof (PIXELFORMATDESCRIPTOR),             // Size of this structure
        1,                                          // Version number
        PFD_DRAW_TO_BITMAP |                        // Flags
        PFD_DRAW_TO_WINDOW |
        PFD_SUPPORT_OPENGL
        ,
        PFD_TYPE_RGBA,                              // RGBA pixel values
        24,                                         // 24-bit color
        0, 0, 0, 0, 0, 0,                           // Don't care about these
        0, 0,                                       // No alpha buffer
        0, 0, 0, 0, 0,                              // No accumulation buffer
        32,                                         // 32-bit depth buffer
        0,                                          // No stencil buffer
        0,                                          // No auxiliary buffers
        PFD_MAIN_PLANE,                             // Layer type
        0,                                          // Reserved (must be 0)
        0, 0, 0                                     // No layer masks
    };

    int nPixelFormat;
    
    nPixelFormat = ChoosePixelFormat (hdc, &pfd);
    if (SetPixelFormat(hdc, nPixelFormat, &pfd) == FALSE) {
      // SetPixelFormat error
      return FALSE ;
    }

    if (DescribePixelFormat(hdc, nPixelFormat, sizeof(PIXELFORMATDESCRIPTOR),&pfd) == 0) {
      // DescribePixelFormat error
      return FALSE ;
    }

    if (pfd.dwFlags & PFD_NEED_PALETTE) {
        // Need palete !
    }
    return TRUE ;
}


9.2 Rendering to Bitmap function


int CXxxView::RenderingToBitmap(HDC hdc, int w, int h)
{
    HGLRC     rc ;              // OpenGL Rendering context for BITMAP

    SetDCBITMAPPixelFormat(hdc) ;

    rc = wglCreateContext(hdc) ;

    wglMakeCurrent(hdc, rc) ;

    InitGL() ;                                // Initalize OpenGL

    // m_pRGBImage = auxDIBImageLoad("tiger.bmp"); // DIB Image load

    MatGL() ;                                  // Material setup
                                                // Texture setup
    TexGL(m_pRGBImage->sizeX, m_pRGBImage->sizeY, m_pRGBImage->data) ;
    
    Light1GL() ;                               // Light1 Setup

    FogGL() ;                                  // Fog setup
   
    ViewGL(w, h) ;

    DrawGL(m_curquat) ;
   
    glFlush() ;

    // Restore OpenGL status

    wglMakeCurrent(NULL,NULL) ;
    wglDeleteContext(rc) ;

    wglMakeCurrent(m_pDC->m_hDC,m_GLRC) ;

    return 0 ;
}


prev next