使用 wglChoosePixelFormatARB和 wglCreateContextAttribsARB初始化opengl环境

使用wglChoosePixelFormatARB代替ChoosePixelFormat、wglCreateContextAttribsARB代替wglCreateContext初始化opengl环境.前提是使用glew库,代码如下:

 
#include "StdAfx.h" #include "wglew.h" HGLRC CGLEnvironment::Init(HDC hDC) { HGLRC m_hRC=0; PIXELFORMATDESCRIPTOR pfd; int nPixelFormat=-1; int nPixCount = 0; float fPixAttribs[]={0,0}; int iPixAttribs[] = { WGL_SUPPORT_OPENGL_ARB, 1, // Must support OGL rendering WGL_DRAW_TO_WINDOW_ARB, 1, // pf that can run a window WGL_ACCELERATION_ARB, WGL_FULL_ACCELERATION_ARB, // must be HW accelerated WGL_COLOR_BITS_ARB, 32, // 8 bits of each R, G and B WGL_DEPTH_BITS_ARB, 24, // 24 bits of depth precision for window, 典型情况下深度缓冲区都是24位的,试了几台机器都不支持32位深度缓冲区 WGL_DOUBLE_BUFFER_ARB,GL_TRUE, // Double buffered context WGL_PIXEL_TYPE_ARB, WGL_TYPE_RGBA_ARB, // pf should be RGBA type WGL_STENCIL_BITS_ARB, 8,//开启模板缓冲区,模板缓冲区位数=8 WGL_SAMPLE_BUFFERS_ARB, GL_TRUE, // MSAA on,开启多重采样 WGL_SAMPLES_ARB, 4, // 4x MSAA ,多重采样样本数量为4 0}; // NULL termination //创建一个临时窗口,只是为了初始化glew库 CWnd* tempWnd = new CWnd; tempWnd->Create(_T("STATIC"), _T("Hi"), WS_CHILD | WS_VISIBLE, CRect(0, 0, 20, 20), AfxGetMainWnd(), NULL); HDC tempHdc=tempWnd->GetDC()->GetSafeHdc(); // Attempt to set the pixel format if(!SetPixelFormat(tempHdc,1,&pfd))//每个窗口只能设置一次 return NULL; HGLRC temphRC=wglCreateContext(tempHdc);//创建一个临时的环境为了初始化glew,初始化后才能够使用wglChoosePixelFormatARB,wglCreateContextAttribsARB函数 wglMakeCurrent(tempHdc, temphRC);//只有设置当前opengl环境后才能够初始化glew库 GLenum err = glewInit();//初始化glew库,一个程序中初始化一次就可以,不需要每个opengl环境都初始化 if (GLEW_OK != err) return NULL; wglMakeCurrent(NULL, NULL); wglDeleteContext(temphRC); tempWnd->DestroyWindow();//销毁临时窗口 delete tempWnd; //新的查询像素格式的函数 wglChoosePixelFormatARB(hDC, iPixAttribs, fPixAttribs, 1, &nPixelFormat, (UINT*)&nPixCount); //多重采样时,如果硬件不支持就使用下面的代码关闭多重采样 if (nPixelFormat==-1) { // Try again without MSAA iPixAttribs[18] = 1; wglChoosePixelFormatARB(hDC, iPixAttribs, fPixAttribs, 1, &nPixelFormat, (UINT*)&nPixCount); } // Got a format, now set it as the current one if (!SetPixelFormat( hDC, nPixelFormat, &pfd )) { return NULL; } GLint attribs[] = {WGL_CONTEXT_MAJOR_VERSION_ARB, 3,//主版本3 WGL_CONTEXT_MINOR_VERSION_ARB, 3,//次版本号3 WGL_CONTEXT_PROFILE_MASK_ARB,WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB,//要求返回兼容模式环境,如果不指定或指定为WGL_CONTEXT_CORE_PROFILE_BIT_ARB会返回只包含核心功能的环境 0 }; m_hRC = wglCreateContextAttribsARB(hDC, 0, attribs); if (m_hRC == NULL) { printf("!!! Could not create an OpenGL 3.3 context.\n"); attribs[3] = 2; m_hRC = wglCreateContextAttribsARB(hDC,0, attribs); if (m_hRC == NULL) { printf("!!! Could not create an OpenGL 3.2 context.\n");//要求opengl3.2以上环境 return NULL; } } wglMakeCurrent(NULL, NULL); return hRC; }
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值