First, the creation and initialization of the context environment of the Android platform
1. First instantiate the Android context, which is the initialization of the EGL.
BOOLeglcore::init (Eglcontext sharedcontext) {eglint numconfigs; Eglint width; Eglint height; ConstEglint attribs[] = {egl_buffer_size, +, Egl_alpha_size,8, Egl_blue_size,8, Egl_green_size,8, Egl_red_size,8, Egl_renderable_type, Egl_opengl_es2_bit, Egl_surface_type, Egl_window_bit, egl_none}; //Eglgetdisplay to return the target of OpenGL ES rendering, each vendor will return to the default display device if(display = Eglgetdisplay (egl_default_display)) = =Egl_no_display) {LOGE ("Eglgetdisplay () returned error%d", Eglgeterror ()); return false; } //initializing the display device if(!eglinitialize (Display,0,0) {LOGE ("eglinitialize () returned error%d", Eglgeterror ()); return false; } //Get configuration options Information if(!eglchooseconfig (Display, attribs, &config,1, &numconfigs)) {LOGE ("eglchooseconfig () returned error%d", Eglgeterror ()); Release (); return false; } //creating the context for OpenGL EglcontextEglint eglcontextattributes[] = {egl_context_client_version,2, Egl_none}; if(! (Context = Eglcreatecontext (display, config, NULL = = Sharedcontext?)Egl_no_context:sharedcontext, eglcontextattributes))) {LOGE ("Eglcreatecontext () returned error%d", Eglgeterror ()); Release (); return false; } pfneglpresentationtimeandroid= (Pfneglpresentationtimeandroidproc) eglgetprocaddress ("eglpresentationtimeandroid"); if(!pfneglpresentationtimeandroid) {LOGE ("eglpresentationtimeandroid is not available!"); } return true;}
2. Connect the EGL and the device's screen together. Using Eglsurface, the eglcreatewindowsurface provided by the EGL library creates a surface that can be actually displayed, The eglcreatepbuffersurface provided by the EGL library can create a offscreen surface. _window is the Anativewindow type of object created from the surface object of the Java layer, which is the representation of the local device screen . That is, the real surface is created from the Java layer, and OpenGL is just drawn to this target?
Eglsurface Eglcore::createwindowsurface (anativewindow*_window) {Eglsurface surface=NULL; Eglint format; if(!eglgetconfigattrib (Display, config, egl_native_visual_id, &format)) {LOGE ("Eglgetconfigattrib () returned error%d", Eglgeterror ()); Release (); returnsurface; } anativewindow_setbuffersgeometry (_window,0,0, format); if(! (Surface = eglcreatewindowsurface (display, config, _window,0)) {LOGE ("eglcreatewindowsurface () returned error%d", Eglgeterror ()); } returnsurface;}
The object method for creating Anativewindow types from surface objects in the Java layer is as follows:
Jniexportvoidjnicall java_com_phuket_tour_opengl_renderer_pngpreviewcontroller_setsurface (JNIEnv*env, Jobject obj, jobject surface) { if(Surface! =0&& NULL! =Controller) {window=anativewindow_fromsurface (env, surface); Logi ("Got window%p", window); Controller-SetWindow (window); } Else if(Window! =0) {Logi ("Releasing Window"); Anativewindow_release (window); Window=0; }}
3. Developers need to open up a new county to perform rendering operations for OpenGL ES, and must also bind the display device (surface) and context to the county. EGL is a double-buffered mode with two framebuffer inside, and when EGL displays a framebuffer on the screen, another framebuffer waits for OpenGL ES to render output in the background. The framebuffer of the foreground and the framebuffer in the background will not be exchanged until the calling function eglswapbuffers this instruction.
BOOL eglcore::makecurrent (Eglsurface eglsurface) { return eglmakecurrent (display, Eglsurface, Eglsurface, context);}
Second, Texture/shader/program
1. Create a texture (texture)
int picpreviewtexture::inittexture () { glgentextures (1, &texture); Glbindtexture (gl_texture_2d, TEXTURE); Gltexparameteri (gl_texture_2d, Gl_texture_mag_filter, gl_linear); Gltexparameteri (gl_texture_2d, Gl_texture_min_filter, gl_linear); Gltexparameteri (gl_texture_2d, gl_texture_wrap_s, Gl_clamp_to_edge); Gltexparameteri (gl_texture_2d, gl_texture_wrap_t, Gl_clamp_to_edge); return 1 ;}
2. Create/initialize/compile shader
Gluint Picpreviewrender::compileshader (glenum type,Const Char*sources) {Glint status; Gluint Shader=Glcreateshader (type); if(Shader = =0|| Shader = =gl_invalid_enum) {Logi ("Failed to create shader%d", type); return 0; } glshadersource (Shader,1, &sources, NULL); Glcompileshader (shader); Glgetshaderiv (shader, Gl_compile_status,&status); if(Status = =gl_false) {Gldeleteshader (shader); Logi ("Failed to compile shader:%s\n", sources); return 0; } returnshader;}
3. Create and Use Program
intPicpreviewrender::useprogram () { program=Glcreateprogram (); Glattachshader (program, Vertshader); Glattachshader (program, Fragshader); Glbindattriblocation (program, Attribute_vertex,"position"); Glbindattriblocation (program, Attribute_texcoord,"Texcoord"); Gllinkprogram (program); Glint status; GLGETPROGRAMIV (program, Gl_link_status,&status); if(Status = =gl_false) {Logi ("Failed to link program%d", program); return-1; } gluseprogram (program); Uniformsampler= Glgetuniformlocation (Program,"Yuvtexsampler"); return 1;}Third, rendering operation
voidPicpreviewrender::render () {Glviewport (_backingleft, _backingtop, _backingwidth, _backingheight); Glclearcolor (0.0f,0.0f,1.0f,0.0f); Glclear (Gl_color_buffer_bit); Glenable (Gl_blend); Glblendfunc (Gl_src_alpha, Gl_one_minus_src_alpha); Gluseprogram (program); Static ConstGlfloat _vertices[] = {-1.0f,1.0f, -1.0f, -1.0f,1.0f,1.0f,1.0f, -1.0f }; Glvertexattribpointer (Attribute_vertex,2, Gl_float,0,0, _vertices); Glenablevertexattribarray (Attribute_vertex); Static ConstGlfloat texcoords[] = {0.0f,0.0f,0.0f,1.0f,1.0f,0.0f,1.0f,1.0f }; Glvertexattribpointer (Attribute_texcoord,2, Gl_float,0,0, texcoords); Glenablevertexattribarray (Attribute_texcoord); Picpreviewtexture-bindtexture (Uniformsampler); Gldrawarrays (Gl_triangle_strip,0,4);}
After the render operation is performed, the call to Eglswapbuffers is displayed. At this point, one render operation is complete.
OpenGL Learning (v) Shader rendering (Android as an example)