Mfc+opengl displaying video streaming pictures

Source: Internet
Author: User

Because want to do an application software, the current own algorithm to apply out, give the researcher to provide a loss of convenience, need to display video stream files in MFC. I tried two methods: OPENCV+MFC;OPENGL+MFC; Both of these methods are feasible. However, when using OPENCV+MFC, it is assumed that there is a problem in setting double buffering, which causes the video stream to flicker seriously. (The actual problem is to read the video, the modification should be feasible, after the use of OpenGL to do, there have been such problems, and then modified well).

1, the first need to load a picture control controls; set ID, etc.
2. Define a double buffer in CTrackingAppDlg.h (XXXDlg.h):

    HDC _HRENDERDC; DC
    HGLRC _HRENDERRC;//rc

Add two initialization functions:

BOOL Ctrackingappdlg::setwindowpixelformat (HDC hdc) {Pixelformatdescriptor pixeldesc;  

    pixeldesc.nsize = sizeof (pixelformatdescriptor);  
    Pixeldesc.nversion = 1; Pixeldesc.dwflags = Pfd_draw_to_window | Pfd_support_opengl | Pfd_doublebuffer |

    Pfd_type_rgba;
    Pixeldesc.ipixeltype = Pfd_type_rgba;
    Pixeldesc.ccolorbits = 32;  
    pixeldesc.credbits = 0;
    Pixeldesc.credshift = 0;
    pixeldesc.cgreenbits = 0;
    Pixeldesc.cgreenshift = 0;
    pixeldesc.cbluebits = 0;  
    Pixeldesc.cblueshift = 0;  
    pixeldesc.calphabits = 0;  
    Pixeldesc.calphashift = 0;  
    pixeldesc.caccumbits = 0;  
    pixeldesc.caccumredbits = 0;  
    pixeldesc.caccumgreenbits = 0;  
    pixeldesc.caccumbluebits = 0;  
    pixeldesc.caccumalphabits = 0;  
    pixeldesc.cdepthbits = 0;  
    Pixeldesc.cstencilbits = 1;  
    pixeldesc.cauxbuffers = 0;  
    Pixeldesc.ilayertype = Pfd_main_plane;  
    pixeldesc.breserved = 0;  
    Pixeldesc.dwlayermask = 0; Pixeldesc.dwvIsiblemask = 0;  

    Pixeldesc.dwdamagemask = 0;  

    _pixelformat = Choosepixelformat (HDC,&PIXELDESC);  
        if (_pixelformat==0)//Choose Default {_pixelformat = 1; if (Describepixelformat (Hdc,_pixelformat, sizeof (Pixelformatdescriptor), &pixeldesc) ==0) {Retu  
        RN false;  
    }} if (Setpixelformat (HDC,_PIXELFORMAT,&PIXELDESC) ==false) {return FALSE;  
} return true;  

    } BOOL Ctrackingappdlg::createviewglcontext (hdc hdc) {_HRENDERRC = Wglcreatecontext (HDC);  

    if (_hrenderrc==null) return FALSE; 

    if (Wglmakecurrent (HDC,_HRENDERRC) ==false) return FALSE;  
return TRUE;   }

3, in the InitDialog function initialization;

    Reads the size of the picture control;
    CRect Picrect;
    CWnd *wnd = GetDlgItem (Idc_pic_raw);
    Wnd->getwindowrect (&picrect);
    Viewport window for setting picture display
    _app->getrawpictureview (i)->setviewport (Picrect.width (), Picrect.height ());

Initialize and set OpenGL display properties, etc.:

_HRENDERDC =:: GetDC (Wnd->m_hwnd); tian///////////////////////////if (Setwindowpixelformat (_HRENDERDC) =  

        =false) return 0;  

        if (Createviewglcontext (_HRENDERDC) ==false) return 0;  
        Glpolygonmode (Gl_front,gl_fill);  
        Glpolygonmode (Gl_back,gl_fill);  
        Glenable (gl_texture_2d);  
        Glshademodel (Gl_smooth);
        It is responsible for how the images captured by the scene are displayed to the screen in terms of height and width.  
        Glviewport (0,0,259,231);  
        Glmatrixmode (gl_projection);  

        Glloadidentity ();  
        /* Gluperspective (45,1,0.1,100.0);  
        Glmatrixmode (Gl_modelview); Glloadidentity ();       */Glshademodel (Gl_smooth);    Enable Smooth Shading Glclearcolor (1.0f, 1.0f, 1.0f, 0.5f);         Black Background glcleardepth (1.0f);       Depth Buffer Setup glenable (gl_depth_test); Enables DePTH testing Gldepthfunc (gl_lequal);  
    The Type of Depth testing to do glenableclientstate (Gl_vertex_array);  
    Glenableclientstate (Gl_texture_coord_array); Glutinitdisplaymode (glut_double |    GLUT_RGB);//cache mode SetTimer (1,10,0);//OnTimer function is executed once every 10ms, reshape in OnTimer can be changed dynamically picture

5, OnTimer class:

void Ctrackingappdlg::ontimer (Uint_ptr nidevent)
{

    //showimage (Picview->getrawimage (), idc_pic_raw);
    ShowImage (Picview->getbitimage (), idc_pic_bit);
    Wglmakecurrent (_HRENDERDC, _HRENDERRC);
    Glclear (Gl_color_buffer_bit | Gl_depth_buffer_bit);
    if (_app)
    {for
        (int i=0;i<2;i++)
            _app->getrawpictureview (i)->reshape ();
    }
    Swapbuffers (_HRENDERDC);
    Cdialogex::ontimer (nidevent);
}

The initialization section is so much, the others are the same as OpenGL use. Do not know how to make up a little more OpenGL-related basis.
The reshape function is as follows:

void Rawpicview::reshape () {glmatrixmode (gl_projection);

    Glloadidentity ();

Glortho (-1.0, 1.0,-1.0, 1.0,-40.0, 40.0);

    Glmatrixmode (Gl_modelview);
    if (_channelid = = 0) glviewport (0,0,_rectwight/2-5,_recthight);

    else Glviewport (_rectwight/2+5,0,_rectwight/2-5,_recthight);
    Glloadidentity ();

    Glpolygonmode (Gl_front_and_back, Gl_fill);

    if (!_initialized) initlize ();
    Gldisable (gl_lighting);
    Glenable (gl_texture_2d);

    Glbindtexture (gl_texture_2d, _textureid);

    if (!_valid) replacetexture ();
    GLCOLOR3F (1.0, 1.0, 1.0); Glbegin (gl_quads); Drawing Using Triangles////////////////////////tian//////////////////////////////////////////////GlTexC
        OORD2F (0.0, 0.0);

        GLVERTEX2F ( -1.0, 1.0f);
        GLTEXCOORD2F (0.0, 1.0);    

        GLVERTEX2F (-1.0,-1.0);
        GLTEXCOORD2F (1.0, 1.0);

        GLVERTEX2F (1.0,-1.0);
        GLTEXCOORD2F (1.0, 0.0); GlvertEX2F (1.0, 1.0);

Glend (); }

Of course, it is better to give a result:
The effect is as follows:

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.