C ++ | MFC(SDI)| opengl:黑屏

时间:2015-06-05 10:56:52

标签: c++ opengl mfc

我想在一个简单的MFC中绘制一些openGl:

流程:

  • 我开始一个新项目MFC / SDI
  • 我将opengl32.lib / glu32.lib链接到我的项目,我也包括gl.h和glu.h。
  • 我实现了opengl的requierts方法(onCreate / precreate / PIXELFORMATDESCRIPTOR)(https://www.microsoft.com/msj/archive/S2085.aspx
  • 我可以毫无错误地构建
  • on on onDraw fonction,如果我什么都不做:什么都没发生! (\ o /)
  • 当我开始写一些opengl代码=>黑屏(我之前测试过此代码,在一个有效的mfc-opengl项目中,它的工作原理)

我的onDraw():

 void CMFCopenGLtestView::OnDraw(CDC* pDC){
        ValidateRect(NULL);
        CRect rect;
        GetClientRect(&rect);
        pDC->SetTextColor(RGB(255, 0, 0));
        wglMakeCurrent(m_hDC, m_hRC);
        //://
        //  HERE i WANT TO DRAW SOME OPENGL
        //://
        SwapBuffers(m_hDC);
        wglMakeCurrent(m_hDC, NULL);
        pDC->DrawText(_T("test"), 16, &rect, 1);
    }

onCreate()

int CMFCopenGLtestView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
    if (CView::OnCreate(lpCreateStruct) == -1)
        return -1;

    // TODO: Add your specialized creation code here
    InitializeOpenGL();
    return 0;
}

BOOL CMFCopenGLtestView::InitializeOpenGL()
{
    m_pDC = new CClientDC(this);
    if (NULL == m_pDC) // failure to get DC
    {

        return FALSE;
    }

    if (!SetupPixelFormat(NULL))
    {
        return FALSE;
    }

    if (0 == (m_hRC =
        ::wglCreateContext(m_pDC->GetSafeHdc())))
    {
        return FALSE;
    }

    if (FALSE ==
        ::wglMakeCurrent(m_pDC->GetSafeHdc(), m_hRC))
    {
        return FALSE;
    }

    // specify black as clear color
    ::glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    // specify the back of the buffer as clear depth
    ::glClearDepth(1.0f);
    // enable depth testing
    ::glEnable(GL_DEPTH_TEST);

    return TRUE;
}

BOOL CMFCopenGLtestView::SetupPixelFormat(
    PIXELFORMATDESCRIPTOR* pPFD)
{
    // default pixel format for a single-buffered,
    // OpenGL-supporting, hardware-accelerated, 
    // RGBA-mode format. Pass in a pointer to a different
    // pixel format if you want something else
    PIXELFORMATDESCRIPTOR pfd =
    {
        sizeof(PIXELFORMATDESCRIPTOR),// size of this pfd
        1,                      // version number
        PFD_DRAW_TO_WINDOW |    // support window
        PFD_SUPPORT_OPENGL,   // support OpenGL
        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
        16,                     // 16-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;
    PIXELFORMATDESCRIPTOR* pPFDtoUse;

    // let the user override the default pixel format
    pPFDtoUse = (0 == pPFD) ? &pfd : pPFD;

    if (0 == (pixelformat =
        ::ChoosePixelFormat(m_pDC->GetSafeHdc(), pPFDtoUse)))
    {
        return FALSE;
    }

    if (FALSE == ::SetPixelFormat(m_pDC->GetSafeHdc(),
        pixelformat, pPFDtoUse))
    {
        return FALSE;
    }

    return TRUE;
}

如果您对我做错了什么有所了解,我会把它们全部拿走!

0 个答案:

没有答案