OpenGL ES EGL eglCreateContext

ChatGPT 3.5 国内中文镜像站免费使用啦

零基础 OpenGL ES 学习路线推荐 : OpenGL ES 学习目录 >> OpenGL ES 基础

零基础 OpenGL ES 学习路线推荐 : OpenGL ES 学习目录  >> OpenGL ES 特效

零基础 OpenGL ES 学习路线推荐 : OpenGL ES 学习目录  >> OpenGL ES 转场

零基础 OpenGL ES 学习路线推荐 :  OpenGL ES 学习目录 >> OpenGL ES 函数

零基础 OpenGL ES 学习路线推荐 :  OpenGL ES 学习目录 >> OpenGL ES GPUImage 使用

零基础 OpenGL ES 学习路线推荐 :  OpenGL ES 学习目录 >> OpenGL ES GLSL 编程


一. EGL 前言

EGLNativeDisplayType – 系统显示类型,标识你所开发设备的物理屏幕,DX/OPenGL ES/Metal/Vulkan….

EGLNativeWindowType – 系统窗口,渲染显示的窗口句柄

EGLDisplay – 关联 EGLNativeDisplayType 系统物理屏幕的通用数据类型,是平台上 WGL / GLX / AGL 的等价物

EGLSurface – 渲染区域,系统窗口或 frame buffer 句柄 ,可以理解为一个后端的渲染目标窗口

EGLConfig – 对 EGLSurface的 EGL 配置,可以理解为绘制目标 framebuffer 的配置属性

EGLContextOpenGL ES 图形上下文


二. EGL 绘制流程简介

  1. 获取 EGL Display 对象:eglGetDisplay
  2. 初始化与 EGLDisplay 之间的连接:eglInitialize
  3. 获取 EGLConfig 对象:eglChooseConfig / eglGetConfigs
  4. 创建 EGLContext 实例:eglCreateContext
  5. 创建 EGLSurface 实例:eglCreateWindowSurface / eglCreatePbufferSurface
  6. 连接 EGLContext 和 EGLSurface:eglMakeCurrent
  7. 使用 OpenGL ES API 绘制图形:gl_*
  8. 切换 front buffer 和 back buffer 显示:eglSwapBuffer
  9. 断开并释放与 EGLSurface 关联的 EGLContext 对象:eglRelease
  10. 删除 EGLSurface 对象
  11. 删除 EGLContext 对象
  12. 终止与 EGLDisplay 之间的连接
图片[1]-OpenGL ES EGL  eglCreateContext-猿说编程

三.eglCreateContext 函数简介  

EGLContext 上下文包含了操作所需的所有状态信息,OpenGL ES 必须有一个可用的上下文 EGLContext 才能进行绘图。如果没有 EGLContext ,OpenGL 指令就没有执行的环境。函数声明如下:

/*描述:创建 OpenGL ES 上下文 EGLContext
 *参数:
 *    display:指定显示的连接
 *    config:配置 EGLConfig
 *    share_context:允许其它 EGLContext 共享数据,使用 EGL_NO_CONTEXT 表示不共享
 *    attribList:指定操作的属性列表,只能接受一个属性 EGL_CONTEXT_CLIENT_VERSION(设置 OpenGL ES 版本)
 *
 *返回值:成功时返回新创建的 EGLContext,失败时返回 EGL_NO_CONTEXT
 */

EGLAPI EGLContext EGLAPIENTRY eglCreateContext(
		EGLDisplay display, 
                EGLConfig config,
                EGLContext share_context,
                const EGLint *attribList);

1.关于属性列表 attribList

attribList 属性目前只有 EGL_CONTEXT_CLIENT_VERSION,类型是整数值,用于指定 OpenGL ES 版本,使用方法如下:

/*
EGL_CONTEXT_CLIENT_VERSION, 3, //使用OpenGL ES 3.0 版本 API
EGL_CONTEXT_CLIENT_VERSION, 2, //使用OpenGL ES 2.0版本 API
EGL_CONTEXT_CLIENT_VERSION, 1, //使用OpenGL ES 1.0版本 API
*/
const ELGint attribList[] = {
    EGL_CONTEXT_CLIENT_VERSION, 3, //使用OpenGL ES 3.0 版本 API
    EGL_NONE
};

如果当前设备只支持 OpenGL ES 2.0,那么该值就不能设置为 3 ,否则上下文创建失败;

值得注意的是:如果使用 OpenGL ES 2.0,那么就不能使用 OpenGL ES 3.0相关API, VAO / PBO 等都属于 OpenGL ES 3.0,详细介绍请参考《OpenGL ES 2.0 和 3.0区别》


2.关于返回值

成功时返回新创建的 EGLContext,失败时返回 EGL_NO_CONTEXT,也有可能返回其他错误,可以通过 eglGetError 获取错误号,示例代码:

EGLContext context = eglCreateContext(display, config, EGL_NO_CONTEXT, attribList);

if (context == EGL_NO_CONTEXT) {
    EGLError error = eglGetError();
    if (error == EGL_BAD_CONFIG) {
        // Handle error and recover
    }
}

通过 eglGetError 获取错误类型可能是以下值:

EGL_BAD_MATCH
EGL_BAD_DISPLAY
EGL_NOT_INITIALIZED
EGL_BAD_CONFIG
EGL_BAD_CONTEXT
EGL_BAD_ATTRIBUTE
EGL_BAD_ALLOC

四.eglCreateContext 函数使用 

/******************************************************************************************/
//@Author:猿说编程
//@Blog(个人博客地址): www.codersrc.com
//@File:OpenGL ES EGL eglCreateContext
//@Time:2022/08/04 07:30
//@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!
/******************************************************************************************/

EGLint attrs[3] = { EGL_DEPTH_SIZE, 16, EGL_NONE };
EGLint num_configs;
EGLConfigs *configs_list;
// Get the display device
if ((eglDisplay = eglGetDisplay(EGL_NO_DISPLAY)) == EGL_NO_DISPLAY) {
	return eglGetError();
}
// Initialize the display
if (eglInitialize(eglDisplay, NULL, NULL) == EGL_FALSE) {
	return eglGetError();
}
// Obtain the total number of configurations that match
if (eglChooseConfig(eglDisplay, attrs, NULL, 0, &num_configs) == EGL_FALSE) {
	return eglGetError();
}
configs_list = malloc(num_configs * sizeof(EGLConfig));
if (configs_list == (EGLConfig *)0){
	return eglGetError();
}
// Obtain the first configuration with a depth buffer of 16 bits
if (!eglChooseConfig(eglDisplay, attrs, &configs_list, num_configs, &num_configs)) {
	return eglGetError();
}

const EGLint attribs[] =  
{  
    EGL_CONTEXT_CLIENT_VERSION, 2,  //使用OpenGL ES 2.0 版本 API
    EGL_NONE  
};  
EGLContext context = eglCreateContext(display, configs_list, EGL_NO_CONTEXT, attribs);  

if (context == EGL_NO_CONTEXT)  
{  
    if (EGL_BAD_CONFIG == eglGetError())  
    {  
        ...  
    }  
}  
 

五.猜你喜欢

  1. OpenGL ES 简介
  2. OpenGL ES 版本介绍
  3. OpenGL ES 2.0 和 3.0区别
  4. OpenGL ES 名词解释(一)
  5. OpenGL ES 名词解释(二)
  6. OpenGL ES GLSL 着色器使用过程
  7. OpenGL ES EGL 简介
  8. OpenGL ES EGL 名词解释
  9. OpenGL ES EGL eglGetDisplay
  10. OpenGL ES EGL eglInitialize
  11. OpenGL ES EGL eglGetConfigs
  12. OpenGL ES EGL eglChooseConfig
  13. OpenGL ES EGL eglGetError
  14. OpenGL ES EGL eglCreateContext

ChatGPT 3.5 国内中文镜像站免费使用啦
© 版权声明
THE END
喜欢就支持一下吧
点赞1 分享
评论 共3条

请登录后发表评论

    暂无评论内容