您的位置:首页 > 运维架构

查询OPENGL和WGL是否支持指定扩展

2013-11-16 13:35 591 查看
使用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;
}


如果有错请联系:

1562165834@qq.com

非常感谢!!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐