Skip to content
The Chat Archive
gpt-3.5-turbo-0301· OpenAI WildChat-4.8M (AI2)

基于Opengles3.2版本和C语言实现分别加载一张yuv图片和一张bmp图片并进行alpha blend

  1. User

    基于Opengles3.2版本和C语言实现分别加载一张yuv图片和一张bmp图片并进行alpha blend

  2. Assistant

    首先需要了解Opengles和C语言的基本知识。

    Opengles是一种图形库,用于在嵌入式系统和移动设备上渲染2D和3D图形。其中, Opengles3.2是最新版本,它提供了更多的功能和优化,可以实现更复杂的图形渲染。

    C语言是一种通用的高级编程语言,通常用于系统和应用程序开发。它是一种结构化的语言,可以在各种平台上使用。

    接下来,我们将详细介绍如何使用Opengles3.2和C语言加载图像并进行alpha blend。

    1. 加载bmp图片

    在Opengles中加载bmp图片需要使用外部库,比如FreeImage或者stb_image等库。

    首先需要安装这些库,然后引入头文件和相关库文件。

    代码示例:

    #include <GLES3/gl32.h>
    #include <stdlib.h>
    #include "FreeImage.h"
    
    GLuint load_texture(const char *filename) {
        GLuint texture;
        FIBITMAP* dib;
        BYTE* bits;
        GLsizei width, height;
        GLenum format;
    
        // Load the image data from disk
        dib = FreeImage_Load(FIF_BMP, filename, BMP_DEFAULT);
    
        // Check for errors
        if (!dib) {
            return 0;
        }
    
        // Get the image dimensions and format
        width = FreeImage_GetWidth(dib);
        height = FreeImage_GetHeight(dib);
        format = GL_RGBA;
    
        // Allocate memory for the image data
        bits = (BYTE*)malloc(width * height * 4);
    
        // Copy the image data into the memory buffer
        FreeImage_ConvertToRawBits(bits, dib, width * 4, 32, FI_RGBA_RED_MASK, FI_RGBA_GREEN_MASK, FI_RGBA_BLUE_MASK, FALSE);
    
        // Release the image data
        FreeImage_Unload(dib);
    
        // Generate a new texture object
        glGenTextures(1, &texture);
    
        // Bind the texture object
        glBindTexture(GL_TEXTURE_2D, texture);
    
        // Set the texture parameters
        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);
    
        // Create the texture from the image data
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, format, GL_UNSIGNED_BYTE, bits);
    
        // Free the memory buffer
        free(bits);
    
        // Return the texture object
        return texture;
    }
    
    
    1. 加载yuv图片

    Opengles中加载yuv图片需要对图片进行解码和转换。

    代码示例:

    #include <GLES3/gl32.h>
    #include <stdlib.h>
    #include <stdio.h>
    
    GLuint load_yuv_texture(const char* filename, GLsizei width, GLsizei height) {
        GLuint texture;
        unsigned char* data = NULL;
        unsigned char* data_y = NULL;
        unsigned char* data_u = NULL;
        unsigned char* data_v = NULL;
        FILE* fp;
    
        // Open the file
        fp = fopen(filename, "rb");
    
        // Check for errors
        if (!fp) {
            return 0;
        }
    
        // Allocate memory for the image data
        data = (unsigned char*)malloc(width * height * 1.5);
    
        // Read the image data from the file
        fread(data, 1, width * height * 1.5, fp);
    
        //  Separate the yuv data
        data_y = data;
        data_u = data_y + width * height;
        data_v = data_u + width * height / 4;
    
        // Generate a new texture object
        glGenTextures(1, &texture);
    
        // Bind the texture object
        glBindTexture(GL_TEXTURE_2D, texture);
    
        // Set the texture parameters
        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);
    
        // Create the texture from the yuv data
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
        glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
        glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
    
        // Free the memory buffer
        free(data);
    
        // Return the texture object
        return texture;
    }
    
    1. 进行alpha blend

    alpha blend是将两张图片按照一定比例混合的过程。在Opengles中,使用glBlendFunc和glEnable分别设置混合函数和开启混合模式。

    代码示例:

    // Bind the texture objects
    glBindTexture(GL_TEXTURE_2D, texture1);
    glBindTexture(GL_TEXTURE_2D, texture2);
    
    // Enable blending
    glEnable(GL_BLEND);
    
    // Set the blend function
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    
    // Draw the textures
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
    

    此时,两张图片将按照一定比例混合,达到alpha blend的效果。

    完整代码示例:

    #include <GLES3/gl32.h>
    #include <stdlib.h>
    #include "FreeImage.h"
    
    GLuint load_texture(const char *filename) {
        GLuint texture;
        FIBITMAP* dib;
        BYTE* bits;
        GLsizei width, height;
        GLenum format;
    
        // Load the image data from disk
        dib = FreeImage_Load(FIF_BMP, filename, BMP_DEFAULT);
    
        // Check for errors
        if (!dib) {
            return 0;
        }
    
        // Get the image dimensions and format
        width = FreeImage_GetWidth(dib);
        height = FreeImage_GetHeight(dib);
        format = GL_RGBA;
    
        // Allocate memory for the image data
        bits = (BYTE*)malloc(width * height * 4);
    
        // Copy the image data into the memory buffer
        FreeImage_ConvertToRawBits(bits, dib, width * 4, 32, FI_RGBA_RED_MASK, FI_RGBA_GREEN_MASK, FI_RGBA_BLUE_MASK, FALSE);
    
        // Release the image data
        FreeImage_Unload(dib);
    
        // Generate a new texture object
        glGenTextures(1, &texture);
    
        // Bind the texture object
        glBindTexture(GL_TEXTURE_2D, texture);
    
        // Set the texture parameters
        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);
    
        // Create the texture from the image data
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, format, GL_UNSIGNED_BYTE, bits);
    
        // Free the memory buffer
        free(bits);
    
        // Return the texture object
        return texture;
    }
    
    GLuint load_yuv_texture(const char* filename, GLsizei width, GLsizei height) {
        GLuint texture;
        unsigned char* data = NULL;
        unsigned char* data_y = NULL;
        unsigned char* data_u = NULL;
        unsigned char* data_v = NULL;
        FILE* fp;
    
        // Open the file
        fp = fopen(filename, "rb");
    
        // Check for errors
        if (!fp) {
            return 0;
        }
    
        // Allocate memory for the image data
        data = (unsigned char*)malloc(width * height * 1.5);
    
        // Read the image data from the file
        fread(data, 1, width * height * 1.5, fp);
    
        //  Separate the yuv data
        data_y = data;
        data_u = data_y + width * height;
        data_v = data_u + width * height / 4;
    
        // Generate a new texture object
        glGenTextures(1, &texture);
    
        // Bind the texture object
        glBindTexture(GL_TEXTURE_2D, texture);
    
        // Set the texture parameters
        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);
    
        // Create the texture from the yuv data
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
        glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
        glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
    
        // Free the memory buffer
        free(data);
    
        // Return the texture object
        return texture;
    }
    
    int main() {
        GLuint texture1, texture2;
    
        // Initialize the Opengles context
    
        // Load the textures
        texture1 = load_texture("test.bmp");
        texture2 = load_yuv_texture("test.yuv", 640, 360);
    
        // Bind the texture objects
        glBindTexture(GL_TEXTURE_2D, texture1);
        glBindTexture(GL_TEXTURE_2D, texture2);
    
        // Enable blending
        glEnable(GL_BLEND);
    
        // Set the blend function
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    
        // Draw the textures
        glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
    
        // Cleanup
    
        return 0;
    }