opengl shader nv format conversion

You can refer to:

OpenGL: How to use Shader to convert RGBA to NV21 image format? (Open source for the first time on the whole network) bzdww

nv12

#extension GL_OES_EGL_image_external : require
precision mediump float;
varying vec2 vTextureCoord;
uniform sampler2D sTexture;
void main() {
  float y, u, v;
  // 这里假设你已经将NV12数据传递给了texture
  y = texture2D(sTexture, vTextureCoord).r;
  u = texture2D(sTexture, vTextureCoord).g;
  v = texture2D(sTexture, vTextureCoord + vec2(0.0, 0.5)).g;
  gl_FragColor = vec4(y, u, v, 1.0);
}

Image conversion in YUV format to RGB format

#extension GL_OES_EGL_image_external : require
precision mediump float;
varying vec2 vTextureCoord;
uniform samplerExternalOES sTexture;
void main() {
  float r, g, b, y, u, v;
  // 这里假设你已经将YUV数据传递给了texture
  y = texture2D(sTexture, vTextureCoord).r;
  u = texture2D(sTexture, vTextureCoord).g;
  v = texture2D(sTexture, vTextureCoord).b;
  // 这里是YUV到RGB的转换公式
  r = y + 1.13983*v;
  g = y - 0.39465*u - 0.58060*v;
  b = y + 2.03211*u;
  gl_FragColor = vec4(r, g, b, 1.0);
}

In OpenGL ES, to convert NV12 format to YUV420SemiPlanar format, you need to create a specific fragment shader, and then convert in this shader. Here is a basic step:

  1. Create a new OpenGL program and create a fragment shader.
int program = GLES20.glCreateProgram();
int shader = GLES20.glCreateShader(GLES20.GL_FRAGMENT_SHADER);
  1. Load and compile your shader code. Here is a basic example that may need to be modified for your specific needs.
String shaderCode = 
    "#extension GL_OES_EGL_image_external : require\n" +
    "precision mediump float;\n" +
    "varying vec2 vTextureCoord;\n" +
    "uniform sampler2D sTexture;\n" +
    "void main() {\n" +
    "  float y, u, v;\n" +
    "  y = texture2D(sTexture, vTextureCoord).r;\n" +
    "  u = texture2D(sTexture, vTextureCoord).g;\n" +
    "  v = texture2D(sTexture, vTextureCoord + vec2(0.0, 0.5)).g;\n" +
    "  gl_FragColor = vec4(y, u, v, 1.0);\n" +
    "}";
GLES20.glShaderSource(shader, shaderCode);
GLES20.glCompileShader(shader);
  1. Add your shader to your OpenGL program, then link the program.
GLES20.glAttachShader(program, shader);
GLES20.glLinkProgram(program);
  1. Create a new texture and upload your NV12 data to this texture.
int[] textures = new int[1];
GLES20.glGenTextures(1, textures, 0);
int texture = textures[0];
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texture);
GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_LUMINANCE_ALPHA, width, height, 0, GLES20.GL_LUMINANCE_ALPHA, GLES20.GL_UNSIGNED_BYTE, nv12Data);
  1. Use your OpenGL program, and bind your texture to your shader.
GLES20.glUseProgram(program);
int sTextureLocation = GLES20.glGetUniformLocation(program, "sTexture");
GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texture);
GLES20.glUniform1i(sTextureLocation, 0);
  1. Render your textures. This usually involves setting up some vertex and texture coordinates, then calling GLES20.glDrawArraysor GLES20.glDrawElements.

Note that this is just a basic example and may need to be modified for your specific needs. For example, you may need to adjust texture parameters, or use different vertex and texture coordinates. Also, this example doesn't handle errors, you should add proper error handling in your code.

2023/8/25 15:08:33

smiley

Retry

delete

fixed

copy

Guess you like

Origin blog.csdn.net/jacke121/article/details/132495990