基于C语言和opengl es3.2版本加载一张rgba图片和一张NV12并进行blend渲染
- User
基于C语言和opengl es3.2版本加载一张rgba图片和一张NV12并进行blend渲染
- Assistant
由于RGBA图片和NV12图片格式有所不同,需要使用不同的函数进行加载和渲染。以下是基于C语言和opengl es3.2版本加载一张RGBA图片和一张NV12图片,并进行blend渲染的代码示例:
- 加载RGBA图片:
GLuint texture_rgba; // Load the RGBA texture int w, h; unsigned char* image = SOIL_load_image("image_rgba.png", &w, &h, 0, SOIL_LOAD_RGBA); // Generate the texture glGenTextures(1, &texture_rgba); glBindTexture(GL_TEXTURE_2D, texture_rgba); // Set texture properties glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_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); // Load texture data glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image); // Free the image data SOIL_free_image_data(image);- 加载NV12图片:
GLuint texture_nv12_y, texture_nv12_uv; GLuint program_nv12; GLuint vertex_shader_nv12, fragment_shader_nv12; // Load the NV12 texture int w, h; unsigned char* image = SOIL_load_image("image_nv12.yuv", &w, &h, 0, SOIL_LOAD_AUTO); // Split the texture into Y and UV components unsigned char* y_data = image; unsigned char* uv_data = image + (w * h); // Generate the Y texture glGenTextures(1, &texture_nv12_y); glBindTexture(GL_TEXTURE_2D, texture_nv12_y); // Set texture properties glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_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); // Load Y texture data glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, w, h, 0, GL_RED, GL_UNSIGNED_BYTE, y_data); // Generate the UV texture glGenTextures(1, &texture_nv12_uv); glBindTexture(GL_TEXTURE_2D, texture_nv12_uv); // Set texture properties glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_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); // Load UV texture data glTexImage2D(GL_TEXTURE_2D, 0, GL_RG8, w / 2, h / 2, 0, GL_RG, GL_UNSIGNED_BYTE, uv_data); // Free the image data SOIL_free_image_data(image); // Compile the shaders vertex_shader_nv12 = compile_shader(GL_VERTEX_SHADER, vertex_shader_nv12_source); fragment_shader_nv12 = compile_shader(GL_FRAGMENT_SHADER, fragment_shader_nv12_source); // Create the shader program program_nv12 = glCreateProgram(); // Attach the shaders glAttachShader(program_nv12, vertex_shader_nv12); glAttachShader(program_nv12, fragment_shader_nv12); // Bind attribute locations glBindAttribLocation(program_nv12, 0, "position"); glBindAttribLocation(program_nv12, 1, "texcoord"); // Link the program glLinkProgram(program_nv12); // Get uniform locations GLint uniforms[] = { glGetUniformLocation(program_nv12, "texture_y"), glGetUniformLocation(program_nv12, "texture_uv"), }; // Activate the program glUseProgram(program_nv12); // Set uniform values glUniform1i(uniforms[0], 0); glUniform1i(uniforms[1], 1);- 进行blend渲染:
// Activate the program glUseProgram(program_nv12); // Activate the textures glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, texture_rgba); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, texture_nv12_y); glBindTexture(GL_TEXTURE_2D, texture_nv12_uv); // Set the blending mode glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // Draw the textured quad glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, 0); // Disable the blending mode glDisable(GL_BLEND);完整的代码示例可参考以下链接:
https://github.com/zhuaizhuai/opengl-learn/blob/main/blend_image/main.c
- User
基于C语言和opengl es3.2版本加载一张rgba图片和一张NV12并进行blend渲染着色器程序怎么写
- Assistant
以下是基于C语言和opengl es3.2版本加载一张RGBA图片和一张NV12图片,并进行blend渲染的着色器程序代码示例:
- 加载RGBA图片的着色器程序:
顶点着色器程序:
#version 320 es in vec4 position; in vec2 texcoord; out vec2 v_texcoord; void main() { gl_Position = position; v_texcoord = texcoord; }片段着色器程序:
#version 320 es precision mediump float; in vec2 v_texcoord; uniform sampler2D texture; out vec4 frag_color; void main() { frag_color = texture2D(texture, v_texcoord); }- 加载NV12图片的着色器程序:
顶点着色器程序:
#version 320 es in vec4 position; in vec2 texcoord; out vec2 v_texcoord; void main() { gl_Position = position; v_texcoord = texcoord; }片段着色器程序:
#version 320 es precision mediump float; in vec2 v_texcoord; uniform sampler2D texture_y; uniform sampler2D texture_uv; out vec4 frag_color; void main() { vec3 yuv; vec3 rgb; // Load Y and UV textures yuv.x = texture2D(texture_y, v_texcoord).r; yuv.y = texture2D(texture_uv, v_texcoord / 2.0).r - 0.5; yuv.z = texture2D(texture_uv, v_texcoord / 2.0).g - 0.5; // Convert YUV to RGB rgb = mat3(1.0, 1.0, 1.0, 0.0, -0.39465, 2.03211, 1.13983, -0.58060, 0.0) * yuv; frag_color = vec4(rgb, 1.0); }- 进行blend渲染的着色器程序:
顶点着色器程序:
#version 320 es in vec4 position; in vec2 texcoord; out vec2 v_texcoord; void main() { gl_Position = position; v_texcoord = texcoord; }片段着色器程序:
#version 320 es precision mediump float; in vec2 v_texcoord; uniform sampler2D texture_rgba; uniform sampler2D texture_nv12_y; uniform sampler2D texture_nv12_uv; out vec4 frag_color; void main() { vec3 yuv; vec4 rgba; // Load RGBA and NV12 textures rgba = texture2D(texture_rgba, v_texcoord); yuv.x = texture2D(texture_nv12_y, v_texcoord).r; yuv.y = texture2D(texture_nv12_uv, v_texcoord / 2.0).r - 0.5; yuv.z = texture2D(texture_nv12_uv, v_texcoord / 2.0).g - 0.5; // Convert YUV to RGB vec3 rgb = mat3(1.0, 1.0, 1.0, 0.0, -0.39465, 2.03211, 1.13983, -0.58060, 0.0) * yuv; // Blend the colors frag_color = mix(rgba, vec4(rgb, 1.0), rgba.a); }每个着色器程序都需要进行编译和链接操作,这里只给出编译着色器程序的代码示例:
GLuint compile_shader(GLenum type, const char* source) { GLuint shader; // Create the shader object shader = glCreateShader(type); // Load the shader source glShaderSource(shader, 1, &source, NULL); // Compile the shader glCompileShader(shader); // Check for compile errors GLint success; glGetShaderiv(shader, GL_COMPILE_STATUS, &success); if (success == GL_FALSE) { GLint log_length; glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &log_length); char* log = malloc(log_length); glGetShaderInfoLog(shader, log_length, NULL, log); printf("Shader compilation failed:\n%s\n", log); free(log); glDeleteShader(shader); return 0; } return shader; }完整的代码示例可参考以下链接:
https://github.com/zhuaizhuai/opengl-learn/blob/main/blend_image/shader.c
- User
基于C语言和opengl es3.2版本加载两张NV12图片并进行alpha blend渲染
- Assistant
以下是基于C语言和opengl es3.2版本加载两张NV12图片,并进行alpha blend渲染的代码示例:
- 加载NV12图片并拆分成Y和UV两个分量:
GLuint texture_y1, texture_uv1, texture_y2, texture_uv2; // Load the first NV12 texture int w1, h1; unsigned char* image1 = SOIL_load_image("image1_nv12.yuv", &w1, &h1, 0, SOIL_LOAD_AUTO); // Split the texture into Y and UV components unsigned char* y_data1 = image1; unsigned char* uv_data1 = image1 + (w1 * h1); // Generate the Y texture glGenTextures(1, &texture_y1); glBindTexture(GL_TEXTURE_2D, texture_y1); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_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); glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, w1, h1, 0, GL_RED, GL_UNSIGNED_BYTE, y_data1); // Generate the UV texture glGenTextures(1, &texture_uv1); glBindTexture(GL_TEXTURE_2D, texture_uv1); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_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); glTexImage2D(GL_TEXTURE_2D, 0, GL_RG8, w1 / 2, h1 / 2, 0, GL_RG, GL_UNSIGNED_BYTE, uv_data1); // Free the image data SOIL_free_image_data(image1); // Load the second NV12 texture int w2, h2; unsigned char* image2 = SOIL_load_image("image2_nv12.yuv", &w2, &h2, 0, SOIL_LOAD_AUTO); // Split the texture into Y and UV components unsigned char* y_data2 = image2; unsigned char* uv_data2 = image2 + (w2 * h2); // Generate the Y texture glGenTextures(1, &texture_y2); glBindTexture(GL_TEXTURE_2D, texture_y2); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_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); glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, w2, h2, 0, GL_RED, GL_UNSIGNED_BYTE, y_data2); // Generate the UV texture glGenTextures(1, &texture_uv2); glBindTexture(GL_TEXTURE_2D, texture_uv2); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_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); glTexImage2D(GL_TEXTURE_2D, 0, GL_RG8, w2 / 2, h2 / 2, 0, GL_RG, GL_UNSIGNED_BYTE, uv_data2); // Free the image data SOIL_free_image_data(image2);- 创建着色器程序:
顶点着色器程序:
#version 320 es in vec4 position; in vec2 texcoord; out vec2 v_texcoord; void main() { gl_Position = position; v_texcoord = texcoord; }片段着色器程序:
#version 320 es precision mediump float; in vec2 v_texcoord; uniform sampler2D texture_y1; uniform sampler2D texture_uv1; uniform sampler2D texture_y2; uniform sampler2D texture_uv2; out vec4 frag_color; void main() { vec3 yuv1, yuv2; vec3 rgb1, rgb2; // Load Y and UV textures for image 1 yuv1.x = texture2D(texture_y1, v_texcoord).r; yuv1.y = texture2D(texture_uv1, v_texcoord / 2.0).r - 0.5; yuv1.z = texture2D(texture_uv1, v_texcoord / 2.0).g - 0.5; // Load Y and UV textures for image 2 yuv2.x = texture2D(texture_y2, v_texcoord).r; yuv2.y = texture2D(texture_uv2, v_texcoord / 2.0).r - 0.5; yuv2.z = texture2D(texture_uv2, v_texcoord / 2.0).g - 0.5; // Convert YUV to RGB for each image rgb1 = mat3(1.0, 1.0, 1.0, 0.0, -0.39465, 2.03211, 1.13983, -0.58060, 0.0) * yuv1; rgb2 = mat3(1.0, 1.0, 1.0, 0.0, -0.39465, 2.03211, 1.13983, -0.58060, 0.0) * yuv2; // Blend the RGB values using an alpha channel frag_color = mix(vec4(rgb1, 1.0), vec4(rgb2, 1.0), texture2D(texture_uv2, v_texcoord / 2.0).a); }编译着色器程序的代码示例:
GLuint compile_shader(GLenum type, const char* source) { GLuint shader; // Create the shader object shader = glCreateShader(type); // Load the shader source glShaderSource(shader, 1, &source, NULL); // Compile the shader glCompileShader(shader); // Check for compile errors GLint success; glGetShaderiv(shader, GL_COMPILE_STATUS, &success); if (success == GL_FALSE) { GLint log_length; glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &log_length); char* log = malloc(log_length); glGetShaderInfoLog(shader, log_length, NULL, log); printf("Shader compilation failed:\n%s\n", log); free(log); glDeleteShader(shader); return 0; } return shader; }- 渲染图片:
// Compile the shaders GLuint vertex_shader = compile_shader(GL_VERTEX_SHADER, vertex_shader_source); GLuint fragment_shader = compile_shader(GL_FRAGMENT_SHADER, fragment_shader_source); // Create the shader program GLuint program = glCreateProgram(); // Attach the shaders glAttachShader(program, vertex_shader); glAttachShader(program, fragment_shader); // Bind attribute locations glBindAttribLocation(program, 0, "position"); glBindAttribLocation(program, 1, "texcoord"); // Link the program glLinkProgram(program); // Activate the program glUseProgram(program); // Get uniform locations GLint uniforms[] = { glGetUniformLocation(program, "texture_y1"), glGetUniformLocation(program, "texture_uv1"), glGetUniformLocation(program, "texture_y2"), glGetUniformLocation(program, "texture_uv2") }; // Activate the textures glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, texture_y1); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, texture_uv1); glActiveTexture(GL_TEXTURE2); glBindTexture(GL_TEXTURE_2D, texture_y2); glActiveTexture(GL_TEXTURE3); glBindTexture(GL_TEXTURE_2D, texture_uv2); // Set uniform values glUniform1i(uniforms[0], 0); glUniform1i(uniforms
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.