本文參考了 【1】 Megabyte 的OpenGL教程 http://www.mbsoftworks.sk/index.php?page=tutorials&series=1
HGLRC m_hGLContext; //OpenGL的RC句柄
BOOL bGlewInitialized; //初始化glew的判斷标志
BOOL InitGLEW(HDC); //首先初始化glew
BOOL 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);