OpenGL的。 YUV画

时间:2014-12-13 21:00:30

标签: opengl

我想绘制我从JPEG文件获得的yuv图像,并使用OP进行FFMPEG处理。原始JPEG图像尺寸640x480和输出YUV图像没有任何步幅。我使用着色器:

const GLchar* vertexSource =
    "in vec2 position;"
    "in vec2 texcoord;"
    "out vec2 Texcoord;"
    "void main() {"
    "   Texcoord = texcoord;"
    "   gl_Position = vec4(position, 0.0, 1.0); }";

const GLchar* fragmentSource = ""
    "uniform sampler2D y_tex;"
    "uniform sampler2D u_tex;"
    "uniform sampler2D v_tex;"
    "in vec2 Texcoord;"
    "layout( location = 0 ) out vec4 fragcolor;"
    ""
    "const vec3 R_cf = vec3(1.164383,  0.000000,  1.596027);"
    "const vec3 G_cf = vec3(1.164383, -0.391762, -0.812968);"
    "const vec3 B_cf = vec3(1.164383,  2.017232,  0.000000);"
    ""
    "void main() {"
    "  float y = texture(y_tex, Texcoord).r;"
    "  float u = texture(u_tex, Texcoord).r;"
    "  float v = texture(v_tex, Texcoord).r;"
    "  vec3 yuv = vec3(y,u,v);"
    "  fragcolor = vec4(0.0, 0.0, 0.0, 1.0);"
    "  fragcolor.r = dot(yuv, R_cf);"
    "  fragcolor.g = dot(yuv, G_cf);"
    "  fragcolor.b = dot(yuv, B_cf); }";

以下是数据准备代码:

GLuint elements[] = {
    0, 1, 2,
    2, 3, 0
};

static GLfloat vertices[] = {
    -0.5f, 0.5f, 0.0f, 0.0f, // Top-left
    0.5f, 0.5f, 1.0f, 0.0f, // Top-right
    0.5f, -0.5f, 1.0f, 1.0f, // Bottom-right
    -0.5f, -0.5f, 0.0f, 1.0f  // Bottom-left
};

GLuint vbo;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

GLuint ebo;
glGenBuffers(1, &ebo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(elements), elements, GL_STATIC_DRAW);

GLint m_posAttr = glGetAttribLocation(shaderProgram, "position");
GLint m_texAttr = glGetAttribLocation(shaderProgram, "texcoord");

glEnableVertexAttribArray(m_posAttr);
glEnableVertexAttribArray(m_texAttr);

glVertexAttribPointer(m_posAttr, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), 0);
glVertexAttribPointer(m_texAttr, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat),
     (void*)(2 * sizeof(GLfloat)));

// FFMPEG processing inside
AVFrame* frame = decodeFrame("c:\\test2.jpg");

GLuint ytex;
glGenTextures(1, &ytex);
glActiveTexture(GL_TEXTURE0);
glUniform1i(glGetUniformLocation(shaderProgram, "y_tex"), 0);
glBindTexture(GL_TEXTURE_2D, ytex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, frame->width, frame->height, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, frame->data[0]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

GLuint utex;
glGenTextures(1, &utex);
glActiveTexture(GL_TEXTURE1);
glUniform1i(glGetUniformLocation(shaderProgram, "u_tex"), 1);
glBindTexture(GL_TEXTURE_2D, utex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, frame->width / 2, frame->height / 2, 0,         GL_LUMINANCE, GL_UNSIGNED_BYTE, frame->data[1]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

GLuint vtex;
glGenTextures(1, &vtex);
glActiveTexture(GL_TEXTURE2);
glUniform1i(glGetUniformLocation(shaderProgram, "v_tex"), 2);
glBindTexture(GL_TEXTURE_2D, vtex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, frame->width / 2, frame->height / 2, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, frame->data[2]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

非常简单的画画:

glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);

glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);

glfwSwapBuffers(window);

但我得到的只是黑屏。有人可以指出我的错误吗?

0 个答案:

没有答案