Render YUV with OpenGL ES on iOS

Source: Internet
Author: User

1) Create OpenGL context
[[Eaglcontext alloc] initwithapi:keaglrenderingapiopengles2];


2) layer is set to opaque
_eagllayer = (caeagllayer*) Self.layer;
_eagllayer.opaque = YES;


3) Create YUV texture
Glgentextures (1, &m_ntextureplanary);
Glgentextures (1, &m_ntextureplanaru);
Glgentextures (1, &M_NTEXTUREPLANARV);


4) Add shader
Gluint vertexshader = Compileshader (_vertex_shader_y_u_v, Gl_vertex_shader);
Gluint Fragmentshader = Compileshader (_fragment_shader_y_u_v, Gl_fragment_shader);
Glattachshader (M_nprogramhandle, vertexshader);
Glattachshader (M_nprogramhandle, Fragmentshader);


Excerpt a GLSL introduction
OpenGL Coloring Language (Glsl―opengl shading Language) is the language used to color programming in OpenGL, also known as a short customization program written by developers, they are GPU on graphics cards (Graphic Processor Unit graphics processing Unit), instead of a part of a fixed rendering pipeline, to make the different levels in the rendering pipeline programmable. such as: view conversion, projection transformation and so on. The shader code for GLSL (GL shading Language) is divided into 2 parts: Vertex Shader (vertex shader) and fragment (fragment shader), and sometimes geometry Shader (geometry shader). The vertex shader that is responsible for running vertex coloring. It can get the status of the current OpenGL, GLSL the built-in variables for delivery. GLSL uses the C language as the base high-order shading language, avoiding the complexity of using assembly language or hardware specification languages.


5) Create render buffer
Glgenrenderbuffers (1, &ncolorrenderbuffer);
Glbindrenderbuffer (Gl_renderbuffer, Ncolorrenderbuffer);


6) Create frame buffer
Glgenframebuffers (1, &nframebuffer);
Glbindframebuffer (Gl_framebuffer, Nframebuffer);


7) Create viewport
Glgetrenderbufferparameteriv (Gl_renderbuffer, Gl_renderbuffer_width, &nbackingwidth);
Glgetrenderbufferparameteriv (Gl_renderbuffer, Gl_renderbuffer_height, &nbackingheight);
Glviewport (0, 0, nbackingwidth, nbackingheight);
Glclearcolor (0.0, 0.0, 0.0, 1.0);
Glpixelstorei (gl_unpack_alignment, 1);


8) Set four vertices, four vertices are set in different order, can realize the image up and down, flip left and right.

These vertices are used in the final render to screen, as follows

Glvertexattribpointer (M_npositionslot, 2, gl_float, 0, 0, m_fsquarevertices);
Glvertexattribpointer (M_ntexcoordslot, 2, gl_float, 0, 0, m_ftexturevertices);
Gldrawarrays (Gl_triangle_strip, 0, 4);


9) When the YUV data arrives, you can start rendering, the steps are as follows


A. YUV data updated to texture
Glteximage2d (M_ntextureplanary, M_pframedata, M_ntexturewidth, m_ntextureheight);
Glteximage2d (M_ntextureplanaru, pbuffer-(NWIDTHUV * nheightuv), NWIDTHUV, NHEIGHTUV);
Glteximage2d (M_ntextureplanarv, pbuffer, Nwidthuv, NHEIGHTUV);


B. Bind YUV Texture
Glactivetexture (GL_TEXTURE0);
Glbindtexture (gl_texture_2d, m_ntextureplanary);
Gluniform1i (m_ntextureuniformy, 0);

Glactivetexture (Gl_texture1);
Glbindtexture (gl_texture_2d, M_ntextureplanaru);
Gluniform1i (M_NTEXTUREUNIFORMU, 1);

Glactivetexture (Gl_texture2);
Glbindtexture (gl_texture_2d, M_ntextureplanarv);
Gluniform1i (M_NTEXTUREUNIFORMV, 2);


C. Drawing an image
Glvertexattribpointer (M_npositionslot, 2, gl_float, 0, 0, m_fsquarevertices);
Glvertexattribpointer (M_ntexcoordslot, 2, gl_float, 0, 0, m_ftexturevertices);
Gldrawarrays (Gl_triangle_strip, 0, 4);


d. Show it on the screen
[M_pcontext Presentrenderbuffer:gl_renderbuffer];


10) Release all previously allocated resources when the program exits

Render YUV with OpenGL ES on iOS

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.