创建openGl上下文(新版本)

时间:2017-10-08 16:54:32

标签: windows winapi opengl

我正在尝试创建一个可以处理openGl(新版本)的win32。它似乎不是关于win32 / opengl(新版本,大多数示例都在旧版本中)的变量信息。 我发现的一些信息引导我使用此代码。但它没有显示正方形。 有没有人知道这是否是正确的方法,如果是这样的话它有什么问题(它没有显示方块)。 PS。我知道vbo,vao,shader的部分正在工作,因为我已经用sdl做了。

非常感谢.... :)

以下是代码:

#include <iostream>
#include <windows.h>
#include <stdlib.h>
#include <string.h>
#include <tchar.h>
#include <glew.h>
#include <wglew.h>

using namespace std;

void display(){
// display square
}

bool progRun = false;

//-------------------------------------------------------------------
//#####################################################################
//-------------------------------------------------------------------
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int CALLBACK WinMain(
    HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    LPSTR     lpCmdLine,
    int       nCmdShow
){
    WNDCLASSEX wcex;
    ///////////////////////////LOAD GLEW///////////////////////////////////////////
    //////////////////////////////////////////////////////////////////////
    wcex.cbSize = sizeof(WNDCLASSEX);
    wcex.style          = CS_HREDRAW | CS_VREDRAW | CS_OWNDC | CS_DBLCLKS ;
    wcex.lpfnWndProc    = WndProc;
    wcex.cbClsExtra     = 0;
    wcex.cbWndExtra     = 0;
    wcex.hInstance      = hInstance;
    wcex.hIcon          = LoadIcon(NULL, IDI_APPLICATION);
    wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName   = NULL;
    wcex.lpszClassName  = szWindowClass;
    wcex.hIconSm        = NULL;

    if (!RegisterClassEx(&wcex))
    {
        MessageBox(NULL,"RegisterClassEx","ERROR",MB_OK|MB_ICONSTOP);
        return 0;
    }
    HWND hWnd = CreateWindow(
                    szWindowClass,
                    szTitle,
                    WS_OVERLAPPEDWINDOW,
                    CW_USEDEFAULT, CW_USEDEFAULT,
                    500, 500,
                    NULL,
                    NULL,
                    hInstance,
                    NULL
                    );

    if (!hWnd)
    {
        MessageBox(NULL,"hWnd","ERROR",MB_OK|MB_ICONSTOP);
        return -1;
    }
    HDC 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 pf = ChoosePixelFormat(hdc, &pfd);
    if(pf == 0){
        MessageBox(NULL,"pf == 0","ERROR",MB_OK|MB_ICONSTOP);
        return -1;
    }
    if(SetPixelFormat(hdc, pf, &pfd) == false){
        MessageBox(NULL,"SetPixelFormat","ERROR",MB_OK|MB_ICONSTOP);
        return -1;
    }
    DescribePixelFormat(hdc, pf, sizeof(PIXELFORMATDESCRIPTOR), &pfd);

    HGLRC hrc = wglCreateContext(hdc);
    wglMakeCurrent(hdc, hrc);

    glewExperimental = true;
    if(glewInit() != GLEW_OK){
        MessageBox(NULL, "Couldn't initialize GLEW!", "Fatal Error", MB_ICONERROR);
    }
    wglMakeCurrent(NULL, NULL);
    ReleaseDC(hWnd, hdc);
    wglDeleteContext(hrc);
    DestroyWindow(hWnd);
    ///////////////////////////////TO_CREATE A OPENGL CONTEXT WITH LATER VERSION///////////////////////////////////////
    //////////////////////////////////////////////////////////////////////
    #define iMajorVersion 3
    #define iMinorVersion 1
    hWnd =  CreateWindow(
                    szWindowClass,
                    szTitle,
                    WS_OVERLAPPEDWINDOW,
                    CW_USEDEFAULT, CW_USEDEFAULT,
                    500, 500,
                    NULL,
                    NULL,
                    hInstance,
                    NULL
                    );
    if (!hWnd)
    {
        MessageBox(NULL,"hWnd","ERROR",MB_OK|MB_ICONSTOP);
        return -1;
    }

    hdc = GetDC(hWnd);
    bool bError = false;

    if(iMajorVersion <= 2)
    {
        cout << "version 2" <<endl;
        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;

        pf = ChoosePixelFormat(hdc, &pfd);
        if (pf == 0){
            MessageBox(NULL,"pf == 0","ERROR",MB_OK|MB_ICONSTOP);
            return false;
        }

        if(!SetPixelFormat(hdc, pf, &pfd)){
          MessageBox(NULL,"SetPixelFormat 1","ERROR",MB_OK|MB_ICONSTOP);
          return false;
        }

        // Create the old style context (OpenGL 2.1 and before)
        hrc = wglCreateContext(hdc);
        if(hrc)wglMakeCurrent(hdc, hrc);
        else bError = true;

    }
    else if(WGLEW_ARB_create_context && WGLEW_ARB_pixel_format)
    {
        cout << "version 3" <<endl;
        const int iPixelFormatAttribList[] =
        {
            WGL_DRAW_TO_WINDOW_ARB, GL_TRUE,
            WGL_SUPPORT_OPENGL_ARB, GL_TRUE,
            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 // End of attributes list
        };
        int iContextAttribs[] =
        {
            WGL_CONTEXT_MAJOR_VERSION_ARB, iMajorVersion,
            WGL_CONTEXT_MINOR_VERSION_ARB, iMinorVersion,
            WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB,
            0 // End of attributes list
        };
        int  iNumFormats;
        if(!wglChoosePixelFormatARB(hdc, iPixelFormatAttribList, NULL, 1, &pf, (UINT*)&iNumFormats)){
            MessageBox(NULL,"wglChoosePixelFormatARB","ERROR",MB_OK|MB_ICONSTOP);
            return 0;
        }

        // PFD seems to be only redundant parameter now
        if(!SetPixelFormat(hdc, pf, &pfd)){
            MessageBox(NULL,"SetPixelFormat 2","ERROR",MB_OK|MB_ICONSTOP);
            return false;
        }

        hrc = wglCreateContextAttribsARB(hdc, 0, iContextAttribs);
        // If everything went OK
        if(hrc) wglMakeCurrent(hdc, hrc);
        else bError = true;
        //glewInit();

    }else{
        bError = true;
    }
    if(bError)
    {
        // Generate error messages
        char sErrorMessage[255], sErrorTitle[255];
        sprintf(sErrorMessage, "OpenGL %d.%d is not supported! Please download latest GPU drivers!", iMajorVersion, iMinorVersion);
        sprintf(sErrorTitle, "OpenGL %d.%d Not Supported", iMajorVersion, iMinorVersion);
        MessageBox(hWnd, sErrorMessage, sErrorTitle, MB_ICONINFORMATION);
        return false;
    }
    //////////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////////
    MSG msg;
    progRun = true;

    ShowWindow(hWnd,SW_SHOW);
    UpdateWindow(hWnd);
    //-----------Setup shader and objects  
    setupShader();
    setupObject();
    //-------  
    glClearDepth(1.0f);                         // Depth Buffer Setup
    glEnable(GL_DEPTH_TEST);                        // Enables Depth Testing
    glDepthFunc(GL_LEQUAL);

    glClearColor(1,0,0,1);

    while(progRun){

        if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE)){
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }

        glClear(GL_COLOR_BUFFER_BIT);
        display();

        SwapBuffers(hdc);
    }



    return (int) msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    PAINTSTRUCT ps;
    HDC hdc;
    switch (message){

    case WM_ERASEBKGND:
        display();
        break;
    case WM_PAINT:
        BeginPaint(hWnd, &ps);
        EndPaint(hWnd, &ps);
        break;
    case WM_SIZE: 
        glViewport(0, 0, LOWORD(lParam), HIWORD(lParam));
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        progRun = false;
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
        break;
    }

    return 0;
}

1 个答案:

答案 0 :(得分:0)

如果您使用wglCreateContext,则会获得上下文(&lt; = 2.1)。

您需要wglCreateContextAttribsARB。但首先你需要获得一个指向这个函数的指针,这需要使用一个上下文。

所以:

  1. 创建一个普通的旧上下文,并将其设置为当前。任何像素格式都是 有效的。
  2. 通过使用获取指向wglCreateContextAttribsARB(也许还有wglChoosePixelFormatARB)的指针 wglGetProcAddress。现在您可以删除旧的上下文。
  3. 设置您想要的pixelformat和您希望的上下文属性。
  4. 通过该指针创建上下文。
  5. 来自wiki OpenGL的有用链接是herehere以及here