OpenGL ES (OpenGL for Embedded Systems) 是 OpenGL 三维图形 API 的子集,针对手机、PDA和游戏主机等嵌入式设备而设计。该API由Khronos集团定义推广,Khronos是一个图形软硬件行业协会,该协会主要关注图形和多媒体方面的开放标准。
1. 保存全局变量的数据结构
以下例子程序均基于Linux平台。
1 typedef struct _escontext 2 { 3 void* userData; // Put your user data here... 4 GLint width; // Window width 5 GLint height; // Window height 6 EGLNativeWindowType hWnd; // Window handle 7 EGLDisplay eglDisplay; // EGL display 8 EGLContext eglContext; // EGL context 9 EGLSurface eglSurface; // EGL surface 10 11 // Callbacks 12 void (ESCALLBACK *drawFunc) ( struct _escontext * ); 13 void (ESCALLBACK *keyFunc) ( struct _escontext *, unsigned char, int, int ); 14 void (ESCALLBACK *updateFunc) ( struct _escontext *, float deltaTime ); 15 }ESContext;
1 typedef struct 2 { 3 // Handle to a program object 4 GLuint programObject; 5 6 // Atrribute Location 7 GLint positionLoc; 8 GLint textureLoc; 9 10 // Uniform location 11 GLint matrixModeLoc; 12 GLint matrixViewLoc; 13 GLint matrixPerspectiveLoc; 14 15 // Sampler location 16 GLint samplerLoc; 17 18 // texture 19 GLuint texture; 20 } UserData;
2. 初始化EGL渲染环境和相关元素(第一步曲)
1 int InitEGL(ESContext * esContext) 2 { 3 NativeWindowType eglWindow = NULL; 4 5 EGLDisplay display; 6 EGLContext context; 7 EGLSurface surface; 8 9 EGLConfig configs[2]; 10 EGLBoolean eRetStatus; 11 EGLint majorVer, minorVer; 12 EGLint context_attribs[] = {EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE}; 13 14 EGLint numConfigs; 15 EGLint cfg_attribs[] = {EGL_BUFFER_SIZE, EGL_DONT_CARE, 16 EGL_DEPTH_SIZE, 16, 17 EGL_RED_SIZE, 5, 18 EGL_GREEN_SIZE, 6, 19 EGL_BLUE_SIZE, 5, 20 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, 21 EGL_NONE}; 22 23 // Get default display connection 24 display = eglGetDisplay((EGLNativeDisplayType)EGL_DEFAULT_DISPLAY); 25 if ( display == EGL_NO_DISPLAY ) 26 { 27 return EGL_FALSE; 28 } 29 30 // Initialize EGL display connection 31 eRetStatus = eglInitialize(display, &majorVer, &minorVer); 32 if( eRetStatus != EGL_TRUE ) 33 { 34 return EGL_FALSE; 35 } 36 37 //Get a list of all EGL frame buffer configurations for a display 38 eRetStatus = eglGetConfigs (display, configs, 2, &numConfigs); 39 if( eRetStatus != EGL_TRUE ) 40 { 41 return EGL_FALSE; 42 } 43 44 // Get a list of EGL frame buffer configurations that match specified attributes 45 eRetStatus = eglChooseConfig (display, cfg_attribs, configs, 2, &numConfigs); 46 if( eRetStatus != EGL_TRUE !numConfigs) 47 { 48 return EGL_FALSE; 49 } 50 51 // Create a new EGL window surface 52 surface = eglCreateWindowSurface(display, configs[0], eglWindow, NULL);