在MFC中使用OpenGL相关配置的整理总结

来源:互联网 发布:网络与新媒体概论考研 编辑:程序博客网 时间:2024/05/21 05:20

本文参考了 【1】 Megabyte 的OpenGL教程 http://www.mbsoftworks.sk/index.php?page=tutorials&series=1

HGLRC m_hGLContext;//OpenGL的RC句柄BOOL bGlewInitialized;//初始化glew的判断标志BOOL InitGLEW(HDC);//首先初始化glewBOOL CreateViewGLContext(HDC);//建立GL的RC</span>

CPP文件中,为函数添加代码,首先是glew的初始化函数。需要注意的是,glew的初始化需要有已经建立的RC,所以我们先设置相关像素格式建立RC,初始化glew后再销毁掉。如果没有销毁的话,此时已经建立起RC可以使用OpenGL1.1了,我们需要使用更高版本的GL特性,则需要销毁掉,重新建立。

BOOL GLWnd::InitGLEW(HDC hDC){   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 iPixelFormat = ChoosePixelFormat(hDC, &pfd);    if (iPixelFormat == 0)return false;    if(!SetPixelFormat(hDC, iPixelFormat, &pfd))return false;    // Create the false, old style context (OpenGL 2.1 and before)   HGLRC hRCFake = wglCreateContext(hDC);    wglMakeCurrent(hDC, hRCFake);    bool bResult = true;    if(!bGlewInitialized)    {       if(glewInit() != GLEW_OK)       {          MessageBox(L"Couldn't initialize GLEW!", L"Fatal Error", MB_ICONERROR);          bResult = false;       }       bGlewInitialized = true;    }    wglMakeCurrent(NULL, NULL);    wglDeleteContext(hRCFake);    return bResult;}

接着,我们为CreateViewGLContext(HDC)创建代码

BOOL GLWnd::CreateViewGLContext(HDC hDC){//首先初始化,glew库。if(!InitGLEW(hDC))return false; bool bError = false;     PIXELFORMATDESCRIPTOR pfd; int GL_Version=4;//要使用的OpenGL版本//设置像素格式,注意OpenGL 2.1以前的设置方法,与更新版本的设置方式是不同的if(GL_Version<=2){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 iPixelFormat = ChoosePixelFormat(hDC, &pfd); if (iPixelFormat == 0)return false; if(!SetPixelFormat(hDC, iPixelFormat, &pfd))return false; printf("Create the old style context (OpenGL 2.1 and before)\n");// Create the old style context (OpenGL 2.1 and before)m_hGLContext = wglCreateContext(hDC); if(m_hGLContext)wglMakeCurrent(hDC, m_hGLContext); else bError = true; }else if(WGLEW_ARB_create_context && WGLEW_ARB_pixel_format){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, 3,         WGL_CONTEXT_MINOR_VERSION_ARB, 1,         WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB,          0 // End of attributes list      };       int iPixelFormat, iNumFormats;       wglChoosePixelFormatARB(hDC, iPixelFormatAttribList, NULL, 1, &iPixelFormat, (UINT*)&iNumFormats);       // PFD seems to be only redundant parameter now      if(!SetPixelFormat(hDC, iPixelFormat, &pfd))return false;   m_hGLContext = wglCreateContextAttribsARB(hDC, 0, iContextAttribs);       // If everything went OK  if(m_hGLContext) {wglMakeCurrent(hDC, m_hGLContext); printf("创建GL渲染环境");}      else bError = true; }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!", 3, 1);       sprintf(sErrorTitle, "OpenGL %d.%d Not Supported", 3, 1);   MessageBoxA(NULL,sErrorMessage, sErrorTitle, MB_ICONINFORMATION);        return false;    }return TRUE;}

这样就可以建立OpenGL的环境了,在窗口的OnCreate()函数中,使用如下代码:

<span style="white-space:pre"></span>CClientDC dc(this);CreateViewGLContext(dc.mhdc);






0 0
原创粉丝点击