vinjn / articles

Everything is possible
8 stars 0 forks source link

egl-initialization #19

Open vinjn opened 11 years ago

vinjn commented 11 years ago

EGL is a common API for GL context management, similar to WGL and GLX, that can be used on many platforms that support OpenGL ES. An abstraction is provided for communicating native window and display handles to EGL, but developers remain responsible for all window management through native APIs.

位于 OpenGl/OpenGl-ES 与 Windows API 之间, 与 WGL 的作用相同。

The first step to EGL setup is to grab a handle to the display, as follows. In Microsoft Windows environments, the native display handle should be NULL. In Linux, the native display handle should be an X11 Display*.

EGLDisplay eglDisplay;
eglDisplay = eglGetDisplay(nativeDisplay);

Next, eglInitialize must be called, and the EGL version numbers supported by the system are returned.Note that the EGL version numbers should not be confused with the OpenGL ES version.

EGLint major = 0;
EGLint minor = 0;
bsuccess = eglInitialize(eglDisplay, &major, &minor); 

The next step is to select a configuration id for the context. This is performed by providing an attribute list of name-value pairs. The attribute list must be terminated by EGL_NONE. In this example, we select the default color format for the device, with a 16 bit depth buffer and no stencil support.

EGLint attrs[] = { EGL_DEPTH_SIZE, 16, EGL_NONE };
EGLint numConfig = 0;
EGLConfig eglConfig = 0;
bsuccess = eglChooseConfig(eglDisplay, attrs, &eglConfig, 1, &numConfig);

Now, an EGLSurface needs to be created and attached to the native window handle. In Microsoft Windows environments, the native window handle will be of type HWND. In Linux , the native window handle should be an X11 Window.

EGLSurface eglSurface;
eglSurface = eglCreateWindowSurface(eglDisplay,eglConfig,nativeWin,NULL);

Additionally, a render context must be created.

EGLContext eglContext;
eglContext = eglCreateContext(eglDisplay,eglConfig,EGL_NO_CONTEXT, NULL);

Finally, the context should be bound to the surface and made active by a call to eglMakeCurrent.

bsuccess = eglMakeCurrent(eglDisplay, eglSurface, eglSurface, eglContext);