OpenGl ES实施
1.get显示器句柄
EGLDisplay* egl_display = eglGetDisplay(EGL_DEFAULT_DISPLAY); // or EGLNativeDisplayType hDisplayType = static_cast<EGLNativeDisplayType>(fbGetDisplayByIndex(0)); EGLDisplay* egl_display = eglGetDisplay(hDisplayType );
2.初始化EGL
eglInitialize(egl_display, &majorVer, &minorVer);
EGL将在初始化时,返回主要和次要版本
2.1 选择配置
EGLint attributes[] = {EGL_RENDERABLE_TYPE, EGL_OPENGL_ES3_BIT_KHR. EGL_NONE}; EGLConfig config; EGLint config_count; bool chosen = eglChooseConfig(display, attributes, config, 1, &config_count);
3.创建窗口
4.获取上下文环境
// 传参获取到的显示器句柄和配置值
EGLint context_attribs[] = { EGL_CONTEXT_CLIENT_VERSION , 3, EGL_NONE };
EGLContext context = eglCreateContext(egl_display, config, NULL, context_attribs);
5.设置上下文环境
eglMakeCurrent(egl_display, windowSurface, windowSurface, context);
6.bind API
eglBindAPI(EGL_OPENGL_ES_API);
实施code
#include <stdio.h> #include <EGL/egl.h> #include <EGL/eglext.h> #include <EGL/eglplatform.h> #include <EGL/eglvivante.h> #include <GLES2/gl2.h> #include <GLES2/gl2ext.h> #include <GLES3/gl31.h> int main(int argc, char **argv) { // 1.get display EGLNativeDisplayType hDisplayType = static_cast<EGLNativeDisplayType>(fbGetDisplayByIndex(0)); EGLDisplay display = eglGetDisplay(hDisplayType); if (EGL_SUCCESS != eglGetError()) { printf("get display error\n"); } // 2.init EGLint major, minor; eglInitialize(display, &major, &minor); // 3.choose config EGLint attribs[] = { EGL_RENDERABLE_TYPE, EGL_OPENGL_ES3_BIT_KHR, EGL_NONE }; EGLConfig configs[100]; EGLint config_size = 0; EGLint num_config; bool res_chose = eglChooseConfig(display, attribs, configs, config_size, &num_config); if (EGL_SUCCESS != eglGetError()) { printf("choose config error\n"); } if (res_chose) { printf("3.choose OK num_config[%d]\n", num_config); } else { printf("3.choose failed\n"); } // 4.create context printf("4.create context begin\n"); EGLContext context; int num = 0; EGLint context_attribs[] = { EGL_CONTEXT_CLIENT_VERSION , 3, EGL_NONE }; for (int i = 0; i < num_config; i++) { context = eglCreateContext(display, configs[i], EGL_NO_CONTEXT, context_attribs); if (EGL_SUCCESS != eglGetError()) { printf("create context error\n"); } else { printf("i[%d]\n", i); num = i; break; } } printf("4.create context end\n"); // create surface EGLint surfAttribs[] = { EGL_COLORSPACE, EGL_COLORSPACE_sRGB, EGL_NONE }; int m_width = 1; int m_height = 1; EGLNativeWindowType win = (EGLNativeWindowType)fbCreateWindow(hDisplayType, 0, 0, m_width, m_height); EGLSurface surface = eglCreateWindowSurface(display, configs[num], win, surfAttribs); if (EGL_SUCCESS != eglGetError()) { printf("create surface error\n"); } // 5.make current printf("5.make current begin\n"); eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, context); // eglMakeCurrent(display, surface, surface, context); if (EGL_SUCCESS != eglGetError()) { printf("make current error\n"); } if (EGL_NO_CONTEXT != context && eglGetCurrentContext() == context) { printf("5.make current OK\n"); } else { printf("5.make current failed\n"); } // 6.bind API printf("6.bind opengl es API begin\n"); bool res_bind = eglBindAPI(EGL_OPENGL_ES_API); if (EGL_SUCCESS != eglGetError()) { printf("bind API error\n"); } if (res_bind) { printf("6.bind OK\n"); } else { printf("6.bind failed\n"); } // 7.test gl api printf("7.test egl version[%s]\n", eglQueryString(display, EGL_VERSION)); printf("7.test version_name[%s]\n", glGetString(GL_VERSION)); GLint major_version = 0; GLint minor_version = 0; glGetIntegerv(GL_MAJOR_VERSION, &major_version); glGetIntegerv(GL_MINOR_VERSION, &minor_version); printf("7.test glGetIntegerv major[%d] minor[%d]\n", major_version, minor_version); if (glGetError() == GL_NO_ERROR) { printf("7.test gl error api OK\n"); } else { printf("7.test gl error api failed\n"); } }
断开连接和释放
eglMakeCurrent(display, EGL_NO_SURFACE , EGL_NO_SURFACE , EGL_NO_CONTEXT);
eglDestroySurface(display, surface);
eglDestroyContext(display, context);
eglTerminate(display);