gpt-3.5-turbo-0301· OpenAI WildChat-4.8M (AI2)
基于C语言和opengl es3.2版本加载一张NV12图片并做渲染
- User
基于C语言和opengl es3.2版本加载一张NV12图片并做渲染
- Assistant
以下是基于C语言和OpenGL ES 3.2版本加载一张NV12图片并进行渲染的代码示例:
#include <stdio.h> #include <stdlib.h> #include <GLES3/gl32.h> #define SCREEN_WIDTH 800 #define SCREEN_HEIGHT 600 GLuint programID, textureID; GLint texSamplerLoc, vertexLoc, uvLoc; const char* vertexShaderSource = "#version 320 es\n" "layout(location = 0) in vec4 vPosition;\n" "layout(location = 1) in vec2 vTexCoord;\n" "out vec2 texCoord;\n" "void main() {\n" " gl_Position = vPosition;\n" " texCoord = vTexCoord;\n" "}\n"; const char* fragmentShaderSource = "#version 320 es\n" "precision mediump float;\n" "in vec2 texCoord;\n" "out vec4 fragColor;\n" "uniform sampler2D texSampler;\n" "void main() {\n" " fragColor = texture(texSampler, texCoord);\n" "}\n"; void checkGLError(const char* tag) { GLenum err = glGetError(); if (err != GL_NO_ERROR) { printf("Error %s: %d\n", tag, err); } } GLuint createShader(GLenum shaderType, const GLchar* source) { GLuint shader = glCreateShader(shaderType); glShaderSource(shader, 1, &source, NULL); glCompileShader(shader); GLint compileStatus; glGetShaderiv(shader, GL_COMPILE_STATUS, &compileStatus); if (compileStatus != GL_TRUE) { GLsizei logLength = 0; GLchar message[1024]; glGetShaderInfoLog(shader, 1024, &logLength, message); printf("Shader compilation failed: %s\n", message); exit(1); } return shader; } GLuint createProgram(const char* vertexSource, const char* fragmentSource) { GLuint vertexShader = createShader(GL_VERTEX_SHADER, vertexSource); GLuint fragmentShader = createShader(GL_FRAGMENT_SHADER, fragmentSource); GLuint program = glCreateProgram(); glAttachShader(program, vertexShader); glAttachShader(program, fragmentShader); glLinkProgram(program); GLint linkStatus; glGetProgramiv(program, GL_LINK_STATUS, &linkStatus); if (linkStatus != GL_TRUE) { GLsizei logLength = 0; GLchar message[1024]; glGetProgramInfoLog(program, 1024, &logLength, message); printf("Program linking failed: %s\n", message); exit(1); } return program; } void loadNV12Texture(const char* fileName) { FILE* fp = fopen(fileName, "rb"); if (fp == NULL) { printf("Cannot open file %s!\n", fileName); exit(1); } // read image size int width, height; fread(&width, sizeof(int), 1, fp); fread(&height, sizeof(int), 1, fp); // read Y component GLuint yDataSize = width * height; GLubyte* yData = (GLubyte*) malloc(yDataSize); fread(yData, sizeof(GLubyte), yDataSize, fp); // read UV component GLuint uvDataSize = width * height / 2; GLubyte* uvData = (GLubyte*) malloc(uvDataSize); fread(uvData, sizeof(GLubyte), uvDataSize, fp); fclose(fp); // create texture object glGenTextures(1, &textureID); glBindTexture(GL_TEXTURE_2D, textureID); // set texture parameters glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // upload Y component to texture glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, width, height, 0, GL_RED, GL_UNSIGNED_BYTE, yData); // upload UV component to texture glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glTexImage2D(GL_TEXTURE_2D, 0, GL_RG8, width / 2, height / 2, 0, GL_RG, GL_UNSIGNED_BYTE, uvData); // bind texture to sampler uniform glUseProgram(programID); glUniform1i(texSamplerLoc, 0); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, textureID); free(yData); free(uvData); } void render() { // clear screen glClear(GL_COLOR_BUFFER_BIT); // draw textured rectangle GLfloat vertexData[] = {-1.0f, -1.0f, 0.0f, 1.0f, 1.0f, -1.0f, 0.0f, 1.0f, -1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f}; glVertexAttribPointer(vertexLoc, 4, GL_FLOAT, GL_FALSE, 0, vertexData); glEnableVertexAttribArray(vertexLoc); GLfloat uvData[] = {0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f}; glVertexAttribPointer(uvLoc, 2, GL_FLOAT, GL_FALSE, 0, uvData); glEnableVertexAttribArray(uvLoc); glUseProgram(programID); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); // swap buffers checkGLError("Before swapBuffers"); // actually swap buffers here checkGLError("After swapBuffers"); } int main(int argc, char** argv) { // initialize OpenGL if (!glfwInit()) { printf("Failed to initialize GLFW!\n"); exit(1); } glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2); glfwWindowHint(GLFW_RESIZABLE, GL_FALSE); GLFWwindow* window = glfwCreateWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "NV12 Texture", NULL, NULL); if (window == NULL) { printf("Failed to open GLFW window!\n"); glfwTerminate(); exit(1); } glfwMakeContextCurrent(window); glViewport(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT); // create shader program programID = createProgram(vertexShaderSource, fragmentShaderSource); texSamplerLoc = glGetUniformLocation(programID, "texSampler"); vertexLoc = glGetAttribLocation(programID, "vPosition"); uvLoc = glGetAttribLocation(programID, "vTexCoord"); // load NV12 texture from file loadNV12Texture("test_nv12.yuv"); while (!glfwWindowShouldClose(window)) { glfwPollEvents(); render(); } // cleanup resources glDeleteTextures(1, &textureID); glDeleteProgram(programID); glfwDestroyWindow(window); glfwTerminate(); return 0; }其中,loadNV12Texture()函数负责从文件中读取NV12格式的图片数据,并将其上传到OpenGL texture中。函数中先打开指定的文件,读取图片的宽度和高度,接着读取Y、UV分量数据,分别上传到GL_RED和GL_RG格式的texture中。最后激活texture单元0,将texture绑定到sampler uniform变量上。
在主函数中,先使用glfwInit()初始化GLFW库,创建一个OpenGL ES 3.2上下文的窗口。然后创建OpenGL程序对象,编译顶点和片元着色器,链接程序对象并获取uniform和attribute变量的位置。接着载入NV12格式图片,并在渲染函数中绘制一个矩形,使用该NV12纹理进行渲染,并最终清理OpenGL对象和窗口资源。