C++ OpenGL 4核心配置文件、着色器和MFC

C++ OpenGL 4核心配置文件、着色器和MFC,c++,opengl,mfc,C++,Opengl,Mfc,我试图让着色器在带有OpenGL 4核心配置文件的MFC应用程序中工作 我在Win32应用程序中这样做是为了确保它正常工作(确实如此,在窗口的下部绘制了一个三角形): 然后我在MFC MDI程序中也做了同样的操作,尽管背景被清除为与Win32应用程序相同的颜色,但着色器不会绘制任何内容 最后,这里是我的MFC MDI视图类中的相关代码(它与Win32应用程序中的代码不同,因为我一直在试图找出错误所在,但它曾经与Win32应用程序代码相同,因此无法工作): 我通过AMD的GPU Perfstudi

我试图让着色器在带有OpenGL 4核心配置文件的MFC应用程序中工作

我在Win32应用程序中这样做是为了确保它正常工作(确实如此,在窗口的下部绘制了一个三角形):

然后我在MFC MDI程序中也做了同样的操作,尽管背景被清除为与Win32应用程序相同的颜色,但着色器不会绘制任何内容

最后,这里是我的MFC MDI视图类中的相关代码(它与Win32应用程序中的代码不同,因为我一直在试图找出错误所在,但它曾经与Win32应用程序代码相同,因此无法工作):

我通过AMD的GPU Perfstudio运行了这两个可执行文件。Win32框架调试显示一切正常。MFC帧调试显示大小为0的帧缓冲区(win32应用程序的帧缓冲区大小为窗口大小)。两个应用程序的API跟踪是相同的


知道会发生什么吗?

显然,MFC中的OpenGL在使用核心配置文件时,需要通过glViewport设置一个视图端口

处理
WM_SIZE
和执行
glViewport(0,0,(GLsizei)cx,(GLsizei)cy)
解决了问题


值得注意的是,Andon说我不应该两次指定像素格式是正确的。它没有给出任何错误,这很奇怪,但当我删除它时,一切都继续工作。

在同一个HDC上多次调用
SetPixelFormat(…)
是无效的(除非您一直使用相同的像素格式)。这就是为什么大多数时候,当软件执行您在Win32上尝试执行的操作时,它会创建和销毁一个虚拟窗口,然后使用
wglchoospexelformatarb
创建实际窗口。在Win32代码中,我只设置一次像素格式。在MFC代码中,我设置了两次(下面是一个例子:),但是完全像在Win32代码中那样做也不起作用。你能给我指一个MFC样品吗?这些似乎已经从互联网上消失了:/你可能会接受你的答案,因为它是独立的,不依赖于外部引用(可能会腐烂),也许你可以扩展一点。FWIW,我怀疑MFC的区别在于你在窗口实际可见之前进行初始化,而在第一个列表中,您在初始化窗口之前完全创建了它。请参阅
WM_CREATE
…@Shog9的文档,我认为您是对的,但您可能知道,调用CreateWindowEx时会发送WM_CREATE,而使用MFC,在CreateWindowEx完成其工作后调用代码并不是一件小事(如果可能的话)。此外,对于非核心OpenGL概要文件,根本没有问题,MFC和OpenGL在没有glViewport的情况下相处得很好。这个问题只出现在核心OpenGL配置文件和MFC中;IIRC,您也可以通过覆盖MFC的创建方法来解决这个问题,但这还有其他问题-您的解决方案很好,只要您小心-WM_大小在某些情况下可以调用很多,并且您不必总是重置视口。同意。重写MFC的创建方法并不是件小事,这主要是因为窗口消息不是以确定性的方式到达的。创建OnPostCreate处理程序比简单地调用glViewport要困难得多。更改窗口大小时不调整视口的大小是应用程序的决定,即,您希望缩放或剪裁内容。你能举例说明我需要注意的情况吗?
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd;

   hInst = hInstance; // Store instance handle in our global variable

   hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

   if (!hWnd)
   {
      return FALSE;
   }

   m_hDC = ::GetDC(hWnd);

   PIXELFORMATDESCRIPTOR pfd;
   memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
   pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
   pfd.nVersion = 1;
   pfd.dwFlags = PFD_DOUBLEBUFFER | PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW;
   pfd.iPixelType = PFD_TYPE_RGBA;
   pfd.cColorBits = 32;
   pfd.cDepthBits = 32;
   pfd.iLayerType = PFD_MAIN_PLANE;

   int nPixelFormat = ChoosePixelFormat(m_hDC, &pfd);

   if (nPixelFormat == 0) return false;

   BOOL bResult = SetPixelFormat(m_hDC, nPixelFormat, &pfd);

   if (!bResult) return false;

   HGLRC tempContext = wglCreateContext(m_hDC);
   wglMakeCurrent(m_hDC, tempContext);

   GLenum err = glewInit();
   if (GLEW_OK != err)
   {
       MessageBox(hWnd, (LPCWSTR)L"Glew not initialized", (LPCWSTR)L"Error", MB_ICONEXCLAMATION);
   }

   //Get a GL 4,2 context
   int attribs[] =
   {
       WGL_CONTEXT_MAJOR_VERSION_ARB, 4,
       WGL_CONTEXT_MINOR_VERSION_ARB, 2,
       WGL_CONTEXT_FLAGS_ARB, 0,
       0
   };

   if (wglewIsSupported("WGL_ARB_create_context") == 1)
   {
       m_hRC = wglCreateContextAttribsARB(m_hDC, 0, attribs);
       wglMakeCurrent(NULL, NULL);
       wglDeleteContext(tempContext);
       wglMakeCurrent(m_hDC, m_hRC);
   }
   else
   {    //It's not possible to make a GL 4.x context. Use the old style context (GL 2.1 and before)
       m_hRC = tempContext;
   }


   if (!m_hRC) return false;

   static const char * vs_source[] =
   {
       "#version 420 core                                                 \n"
       "                                                                  \n"
       "void main(void)                                                   \n"
       "{                                                                 \n"
       "    const vec4 vertices[] = vec4[](vec4( 2.25, -2.25, 0.5, 1.0),  \n"
       "                                   vec4(-2.25, -2.25, 0.5, 1.0),  \n"
       "                                   vec4( 2.25,  2.25, 0.5, 1.0)); \n"
       "                                                                  \n"
       "    gl_Position = vertices[gl_VertexID];                          \n"
       "}                                                                 \n"
   };

   static const char * fs_source[] =
   {
       "#version 420 core                                                 \n"
       "                                                                  \n"
       "out vec4 color;                                                   \n"
       "                                                                  \n"
       "void main(void)                                                   \n"
       "{                                                                 \n"
       "    color = vec4(1.0, 0.8, 1.0, 1.0);                             \n"
       "}                                                                 \n"
   };

   program = glCreateProgram();
   GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
   glShaderSource(fs, 1, fs_source, NULL);
   glCompileShader(fs);

   GLuint vs = glCreateShader(GL_VERTEX_SHADER);
   glShaderSource(vs, 1, vs_source, NULL);
   glCompileShader(vs);

   glAttachShader(program, vs);
   glAttachShader(program, fs);

   glLinkProgram(program);

   glGenVertexArrays(1, &vao);
   glBindVertexArray(vao);

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}


const GLfloat green[] = { 0.0f, 0.25f, 0.0f, 1.0f };
//
//  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_COMMAND  - process the application menu
//  WM_PAINT    - Paint the main window
//  WM_DESTROY  - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    int wmId, wmEvent;
    PAINTSTRUCT ps;
    HDC hdc;

    switch (message)
    {
    case WM_COMMAND:
        wmId    = LOWORD(wParam);
        wmEvent = HIWORD(wParam);
        // Parse the menu selections:
        switch (wmId)
        {
        case IDM_ABOUT:
            DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
            break;
        case IDM_EXIT:
            DestroyWindow(hWnd);
            break;
        default:
            return DefWindowProc(hWnd, message, wParam, lParam);
        }
        break;
    case WM_PAINT:
        //hdc = BeginPaint(hWnd, &ps);
        // TODO: Add any drawing code here...

        glClearBufferfv(GL_COLOR, 0, green);

        glUseProgram(program);
        glDrawArrays(GL_TRIANGLES, 0, 3);

        SwapBuffers(m_hDC);

        //EndPaint(hWnd, &ps);
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}
int CMFCApplication2View::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
    if (CView::OnCreate(lpCreateStruct) == -1)
        return -1;

    // TODO:  Add your specialized creation code here
    m_hDC = ::GetDC(m_hWnd);

    PIXELFORMATDESCRIPTOR pfd;
    memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
    pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
    pfd.nVersion = 1;
    pfd.dwFlags = PFD_DOUBLEBUFFER | PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW;
    pfd.iPixelType = PFD_TYPE_RGBA;
    pfd.cColorBits = 32;
    pfd.cDepthBits = 24;
    pfd.iLayerType = PFD_MAIN_PLANE;

    int nPixelFormat = ChoosePixelFormat(m_hDC, &pfd);

    if (nPixelFormat == 0) return false;

    BOOL bResult = SetPixelFormat(m_hDC, nPixelFormat, &pfd);

    if (!bResult) return false;

    HGLRC tempContext = wglCreateContext(m_hDC);
    wglMakeCurrent(m_hDC, tempContext);


    glewExperimental = GL_TRUE;
    GLenum err = glewInit();
    if (GLEW_OK != err)
    {
        AfxMessageBox(_T("GLEW is not initialized!"));
    }

    //This is a modern pixel format attribute list.
    //It has an extensible structure. Just add in more argument pairs 
    //befroe the null to request more features.
    const int attribList[] =
    {
        WGL_DRAW_TO_WINDOW_ARB, GL_TRUE,
        WGL_SUPPORT_OPENGL_ARB, GL_TRUE,
        WGL_ACCELERATION_ARB, WGL_FULL_ACCELERATION_ARB,
        WGL_DOUBLE_BUFFER_ARB, GL_TRUE,
        WGL_PIXEL_TYPE_ARB, WGL_TYPE_RGBA_ARB,
        WGL_COLOR_BITS_ARB, 32,
        WGL_DEPTH_BITS_ARB, 24,
        WGL_STENCIL_BITS_ARB, 8,
        0, 0  //End
    };


    unsigned int numFormats;
    int pixelFormat;
    memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));

    //Select a pixel format number
    wglChoosePixelFormatARB(m_hDC, attribList, NULL, 1, &pixelFormat, &numFormats);

    //Optional: Get the pixel format's description. We must provide a 
    //description to SetPixelFormat(), but its contents mean little.
    //According to MSDN: 
    //  The system's metafile component uses this structure to record the logical
    //  pixel format specification. The structure has no other effect upon the
    //  behavior of the SetPixelFormat function.
    //DescribePixelFormat(m_pDC->GetSafeHdc(), pixelFormat, sizeof(PIXELFORMATDESCRIPTOR), &pfd);

    //Set it as the current 
    if (FALSE == SetPixelFormat(m_hDC, pixelFormat, &pfd))
    {

    }



    //Get a GL 4,4 context
    int attribs[] =
    {
        WGL_CONTEXT_MAJOR_VERSION_ARB, 3,
        WGL_CONTEXT_MINOR_VERSION_ARB, 2,
        WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_CORE_PROFILE_BIT_ARB,
        //WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB,
        0, 0  //End
    };

    if (wglewIsSupported("WGL_ARB_create_context") == 1)
    {
        m_hRC = wglCreateContextAttribsARB(m_hDC, 0, attribs);
        wglMakeCurrent(NULL, NULL);
        wglDeleteContext(tempContext);
        wglMakeCurrent(m_hDC, m_hRC);
    }
    else
    {   //It's not possible to make a GL 4.x context. Use the old style context (GL 2.1 and before)
        m_hRC = tempContext;
    }


    if (!m_hRC) return false;

    static const char * vs_source[] =
    {
        "#version 150 core                                                 \n"
        "                                                                  \n"
        "void main(void)                                                   \n"
        "{                                                                 \n"
        "    const vec4 vertices[] = vec4[](vec4( 2.25, -2.25, 0.5, 1.0),  \n"
        "                                   vec4(-2.25, -2.25, 0.5, 1.0),  \n"
        "                                   vec4( 2.25,  2.25, 0.5, 1.0)); \n"
        "                                                                  \n"
        "    gl_Position = vertices[gl_VertexID];                          \n"
        "}                                                                 \n"
    };

    static const char * fs_source[] =
    {
        "#version 150 core                                                 \n"
        "                                                                  \n"
        "out vec4 color;                                                   \n"
        "                                                                  \n"
        "void main(void)                                                   \n"
        "{                                                                 \n"
        "    color = vec4(1.0, 0.8, 1.0, 1.0);                             \n"
        "}                                                                 \n"
    };

    program = glCreateProgram();
    GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fs, 1, fs_source, NULL);
    glCompileShader(fs);

    GLuint vs = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vs, 1, vs_source, NULL);
    glCompileShader(vs);

    glAttachShader(program, vs);
    glAttachShader(program, fs);

    glLinkProgram(program);

    glGenVertexArrays(1, &vao);
    glBindVertexArray(vao);

    wglMakeCurrent(NULL, NULL);

    return 0;
}



void CMFCApplication2View::OnDraw(CDC*/* pDC*/)
{
    CMFCApplication2Doc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);
    if (!pDoc)
        return;

    // TODO: add draw code for native data here
    // Make the rendering context current
    wglMakeCurrent(m_hDC, m_hRC);

    // TODO: add draw code for native data here 
    static const GLfloat green[] = { 1.0f, 0.0f, 0.0f, 1.0f };
    glClearBufferfv(GL_COLOR, 0, green);

    glUseProgram(program);
    glDrawArrays(GL_TRIANGLES, 0, 3);

    SwapBuffers(m_hDC);

    // Allow other rendering contexts to coexist
    wglMakeCurrent(NULL, NULL);

}