Mfc + OpenGL
От: milkpot Россия  
Дата: 27.05.25 14:39
Оценка:
Здравствуйте, есть Win32 Mfc приложение, использующее OpenGL.

Стоит задача отображать два изображения (кадра) по 640х480 пикселей (байт).
На данный момент отображаются одно изображение поверх другого
в одном окне OpenGL, а не одно изображение рядом с другим.

В итоге должно быть два окна OpenGL в пределах главного диалогового окна.

На следующем этапе необходимо принимать от устройства по Udp
изображения в 640х480 пикселей со скоростью 50 отображений (кадров) в секунду.

Фрагменты кода приведены ниже.

unsigned char tv_buf[307200] = { 0 };
unsigned char hv_buf[307200] = { 0 };
unsigned char my_array[480] = { 0 };

BOOL CBitScrollDlg::OnInitDialog()
{
    MoveWindow(50, 30, 1300, 580);

    pDC = GetDC();

    CenterWindow();

    Init();

    SetTimer(1,SPEED, NULL);
    return TRUE;
}
void CBitScrollDlg::OnTimer(UINT nIDEvent) 
{
    // TODO: Add your message handler code here and/or call default
    DrawScene();
    CDialog::OnTimer(nIDEvent);
}
void CBitScrollDlg::OnClose() 
{
    // TODO: Add your message handler code here and/or call default
    HGLRC   hrc;

    KillTimer(1);
    hrc = ::wglGetCurrentContext();
    ::wglMakeCurrent(NULL,  NULL);
    if (hrc)
        ::wglDeleteContext(hrc);

    CDialog::OnClose();
}
void CBitScrollDlg::Init()
{
    CRect rect;
    HGLRC       hrc;
    GLenum err;

    if (!bSetupPixelFormat())
        return;

    hrc = wglCreateContext(pDC->GetSafeHdc());
    ASSERT(hrc != NULL);
    wglMakeCurrent(pDC->GetSafeHdc(), hrc);

    GetClientRect(&rect);
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    err= glGetError();

    
    glRasterPos2d((GLdouble)-1, (GLdouble)1);
    glPixelZoom(1, -1);
}

void CBitScrollDlg::DrawScene()
{
    int n = 0;
    GLenum err;

    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

    for (size_t i = 0; i < 1200; i++)
    {
        for (size_t j = 0; j < 256; j++)
        {
            ::tv_buf[n] = j;
            n++;
        }
    }
    glDrawPixels(640, 480, GL_LUMINANCE, GL_UNSIGNED_BYTE, &::tv_buf[0]);

    n = 0;
    for (size_t i = 0; i < 1200; i++)
    {
        for (size_t j = 0; j < 256; j++)
        {
            ::hv_buf[n] = 255 - j;
            n++;
        }
    }
    glViewport(640, 480, 640 //Width
        , 480 //Height
    );

    glRasterPos2i(20, 20);
    ;
    glDrawPixels(700, 500, GL_LUMINANCE, GL_UNSIGNED_BYTE, &::hv_buf[0]);
    SwapBuffers(wglGetCurrentDC());
}

void CBitScrollDlg::OnSize(UINT nType, int cx, int cy) 
{
    CDialog::OnSize(nType, cx, cy);
    glViewport(0, 0,1000 //Width
        , 550 //Height
    );
}

BOOL CBitScrollDlg::bSetupPixelFormat()
{
    static PIXELFORMATDESCRIPTOR pfd =
    {
        sizeof(PIXELFORMATDESCRIPTOR),  // size of this pfd
        1,                              // version number
        PFD_DRAW_TO_WINDOW |            // support window
          PFD_SUPPORT_OPENGL |          // support OpenGL
          PFD_DOUBLEBUFFER,             // double buffered
        PFD_TYPE_RGBA,                  // RGBA type
        24,                             // 24-bit color depth
        0, 0, 0, 0, 0, 0,               // color bits ignored
        0,                              // no alpha buffer
        0,                              // shift bit ignored
        0,                              // no accumulation buffer
        0, 0, 0, 0,                     // accum bits ignored
        32,                             // 32-bit z-buffer
        0,                              // no stencil buffer
        0,                              // no auxiliary buffer
        PFD_MAIN_PLANE,                 // main layer
        0,                              // reserved
        0, 0, 0                         // layer masks ignored
    };
    int pixelformat;

    if ( (pixelformat = ChoosePixelFormat(pDC->GetSafeHdc(), &pfd)) == 0 )
    {
        MessageBox("ChoosePixelFormat failed");
        return FALSE;
    }

    if (SetPixelFormat(pDC->GetSafeHdc(), pixelformat, &pfd) == FALSE)
    {
        MessageBox("SetPixelFormat failed");
        return FALSE;
    }

    return TRUE;
}

BOOL CBitScrollDlg::PreCreateWindow(CREATESTRUCT& cs) 
{
    cs.style |= WS_MINIMIZEBOX | WS_CAPTION | WS_OVERLAPPEDWINDOW;

    return CDialog::PreCreateWindow(cs);
}
void CBitScrollDlg::OnDestroy() 
{
    CDialog::OnDestroy();
    
    // TODO: Add your message handler code here

    HGLRC   hrc;

    KillTimer(1);
    hrc = ::wglGetCurrentContext();
    ::wglMakeCurrent(NULL,  NULL);
    if (hrc)
        ::wglDeleteContext(hrc);
}


class CBitScrollDlg : public CDialog
{
public:
    CBitScrollDlg(CWnd* pParent = NULL);    // standard constructor
protected:
    BOOL bSetupPixelFormat(void);
    void DrawScene(void);
    void OnOK();
    CDC* pDC;
    HICON m_hIcon;
    //{{AFX_MSG(CBitScrollDlg)
    virtual BOOL OnInitDialog();
    afx_msg void OnPaint();
    afx_msg HCURSOR OnQueryDragIcon();
    afx_msg void OnTimer(UINT nIDEvent);
    afx_msg void OnClose();
    afx_msg void OnSize(UINT nType, int cx, int cy);
    afx_msg void OnDestroy();
    //}}AFX_MSG
    DECLARE_MESSAGE_MAP()
private:
    void Init(void);
};
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.