使用Opengl開發時如果沒有配置glee是件頭疼事,或者某些擴展glee裏邊如果也找不到怎麼辦,那就需要自己動手了。
自定義兩個函數用於查詢OpenGL和wgl是否支持指定擴展名:
bool checkExtension(const char *extName,char *extList = nullptr); // 檢查是否支持指定擴展名
bool checkWinExtension(HDC hdc, const char *extName, char *extList = nullptr); // 檢查wgl是否支持指定擴展名
第一個函數 checkExtension(...) 用於查詢Opengl是否支持叫 extName 的擴展,如果支持返回true,否則返回false。extList 是Opengl所有擴展名的字符串兒集合,如果傳入的extList是一個空指針,函數將會使用extList自動返回Opengl支持的擴展名字符串。
第二個函數 checkWinExtension(...) 用於查詢wgl(Opengl的windows擴展)是否支持叫 extName 的擴展,如果支持返回true,否則返回false。extList是wgl所有擴展字符串兒集合,如果傳入的extList爲空,函數將會使用extList自動返回wgl支持的擴展名字符串集合。
還有幾點需要注意:擴展名字符串中每個擴展名以空格間隔;checkWinExtension(...) 只能用於windows平臺;checkExtension(...) 在window平臺下使用時首先要用 wglCreateContext(...) 初始化渲染環境(renderring context),否則checkExtension 內部調用的 Opengl API glGetString(GL_EXTENSIONS) 只能返回空值,言外之意就是win32Console 下不能使用這兩個函數來進行查詢。
下面給出函數定義:
// 功能:檢查是否支持指定擴展名
// 如果extList爲空,extList 將會指向擴展名列表
bool checkExtension(const char *extName,char *extList)
{
char *iteList = extList;
if( !iteList )
iteList = (char*)glGetString(GL_EXTENSIONS);
if( !extName || !iteList )
return false;
// 如果extList爲空, 就返回擴展名
if( !extList )
extList = iteList;
// 查詢擴展名中是否包含指定擴展
const GLubyte extNameLen = strlen(extName);
GLubyte extAnymouseLen = 0; // 依次取得每個擴展名的長度
while( *iteList )
{
extAnymouseLen = strcspn(iteList, " ");
if( (extNameLen == extAnymouseLen) && ( 0 == strncmp(extName, iteList, extAnymouseLen) ) )
return true;
// iteList 指向下一個看擴展名字,每個擴展名字之間以空格隔開
iteList += extAnymouseLen + 1;
}
// 沒有找到
return false;
}
// 功能:檢查windows是否支持指定擴展名
// 如果 extList 爲空,extList 將會指向擴展名列表
// 只適用於windows平臺
bool checkWinExtension(HDC hdc, const char *extName, char *extList)
{
// 獲取wgl擴展名的函數指針
typedef const char* (APIENTRY *pwglGetExtensionStringARB)(HDC hdc);
typedef const char* (APIENTRY *pwglGetExtensionStringEXT)(HDC hdc);
char *iteList = extList;
// 取得wgl擴展
if( !iteList )
{
pwglGetExtensionStringARB pfunc_wglGetExtensionStringARB =
(pwglGetExtensionStringARB)wglGetProcAddress("wglGetExtensionStringArb");
if( !pfunc_wglGetExtensionStringARB )
// 如果pfunc_wglGetExtensionStringARB爲空,wgl可能使用的是其他名字函數來獲取擴展信息
{
pwglGetExtensionStringEXT pfunc_pwglGetExtensionStringEXT =
(pwglGetExtensionStringEXT)wglGetProcAddress("wglGetExtensionStringEXT");
if( !pfunc_pwglGetExtensionStringEXT )
return false;
iteList = (char *)pfunc_pwglGetExtensionStringEXT(hdc);
}
iteList = (char *)pfunc_wglGetExtensionStringARB(hdc);
}
if( !extName || !iteList )
return false;
// 如果extList爲空, 就返回wgl擴展名
if( !extList )
extList = iteList;
// 查詢wgl擴展名是否包含指定擴展extName
const GLubyte extNameLen = strlen(extName);
GLubyte extAnymouseLen = 0; // 依次取得每個擴展名的長度
while( *iteList )
{
extAnymouseLen = strcspn(iteList, " ");
if( (extNameLen == extAnymouseLen) && ( 0 == strncmp(extName, iteList, extAnymouseLen) ) )
return true;
// iteList 指向下一個看擴展名字,每個擴展名字之間以空格隔開
iteList += extAnymouseLen + 1;
}
// 沒有找到
return false;
}
如果有錯請聯繫:
[email protected]
非常感謝!!