目錄
- 一. EGL 前言
- 二. EGL 繪制流程簡介
- 三.eglCreateContext 函式簡介
- 1.關于屬性串列 attribList
- 2.關于回傳值
- 四.eglCreateContext 函式使用
- 五.猜你喜歡
零基礎 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 的配置屬性
EGLContext – OpenGL ES 圖形背景關系
二. EGL 繪制流程簡介
- 獲取 EGL Display 物件:eglGetDisplay
- 初始化與 EGLDisplay 之間的連接:eglInitialize
- 獲取 EGLConfig 物件:eglChooseConfig / eglGetConfigs
- 創建 EGLContext 實體:eglCreateContext
- 創建 EGLSurface 實體:eglCreateWindowSurface / eglCreatePbufferSurface
- 連接 EGLContext 和 EGLSurface:eglMakeCurrent
- 使用 OpenGL ES API 繪制圖形:gl_*
- 切換 front buffer 和 back buffer 顯示:eglSwapBuffer
- 斷開并釋放與 EGLSurface 關聯的 EGLContext 物件:eglRelease
- 洗掉 EGLSurface 物件
- 洗掉 EGLContext 物件
- 終止與 EGLDisplay 之間的連接

三.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())
{
...
}
}
五.猜你喜歡
- OpenGL ES 簡介
- OpenGL ES 版本介紹
- OpenGL ES 2.0 和 3.0 區別
- OpenGL ES 名詞解釋(一)
- OpenGL ES 名詞解釋(二)
- OpenGL ES GLSL 著色器使用程序
- OpenGL ES EGL 簡介
- OpenGL ES EGL 名詞解釋
- OpenGL ES EGL eglGetDisplay
- OpenGL ES EGL eglInitialize
- OpenGL ES EGL eglGetConfigs
- OpenGL ES EGL eglChooseConfig
- OpenGL ES EGL eglGetError
- OpenGL ES EGL eglCreateContext
本文由博客 - 猿說編程 猿說編程 發布!
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/524857.html
標籤:C
上一篇:深入理解 virtual 關鍵字
