zoukankan      html  css  js  c++  java
  • OpenGL于MFC使用汇总(三)——离屏渲染

    有时直接创建OpenGL形式不适合,或者干脆不同意然后创建一个表单,正如我现在这个项目,创建窗体不显示,它仅限于主框架。而我只是ActiveX里做一些相关工作,那仅仅能用到OpenGL的离屏渲染技术了~即不直接绘制到窗体上,而是绘制到一张位图上。然后再次调用这张位图实现兴许的工作。

    以下就总结怎么使用所谓的“离屏渲染”。

        const int WIDTH = 500;
        const int HEIGHT = 500;
    
        // Create a memory DC compatible with the screen
        HDC hdc = CreateCompatibleDC(0);
        if (hdc == 0) cout<<"Could 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 calls to GdiFlush() (see CreateDIBSection() docs)
        BITMAPINFO bmi = {
            { sizeof(BITMAPINFOHEADER), WIDTH, HEIGHT, 1, 32, BI_RGB, 0, 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<<"Could not create bitmap";
    
        //HDC hdcScreen = GetDC(0);
        //HBITMAP hbm = CreateCompatibleBitmap(hdcScreen,WIDTH,HEIGHT);
    
        // Select the bitmap into the DC
        HGDIOBJ r = SelectObject(hdc, hbm);
        if (r == 0) cout<<"Could not select bitmap into DC";
    
        // Choose the pixel format
        PIXELFORMATDESCRIPTOR pfd = {
            sizeof (PIXELFORMATDESCRIPTOR), // struct size
            1, // Version number
            PFD_DRAW_TO_BITMAP | 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 alpha buffer info
            0, 0, 0, 0, 0, // No accumulation buffer
            32, // depth buffer bits
            0, // No stencil 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();
        /*
    	详细的绘制函数~~~~~~~~~~~~~~我就不写了
        */
       
    
        // Clean up
        wglDeleteContext(hglrc); // Delete RC
        SelectObject(hdc, r); // Remove bitmap from DC
        DeleteObject(hbm); // Delete bitmap
        DeleteDC(hdc); // Delete DC


    版权声明:本文博客原创文章。博客,未经同意,不得转载。

  • 相关阅读:
    滚动图片
    Iframe自动适应高度
    我的生活,我的精彩!
    静下心来
    写给关心我的人
    关于考研
    按时间自动刷新页面
    破除网页鼠标右键禁用的十大绝招
    DotNetNuke 皮肤制作白皮书
    WollOp
  • 原文地址:https://www.cnblogs.com/bhlsheji/p/4713200.html
Copyright © 2011-2022 走看看