我正在尝试使用GLSL着色器将12位YUV帧(YUV420P)转换为24位RGB帧。 在Nvidia显卡上它可以工作,但在ATI显卡上它也可以工作,但工作不正确。图像无法正确绘制。颜色不同,图像是完整尺寸的四分之一。
以下是着色器的代码:
uniform sampler2DRect y_texture, u_texture, v_texture;
uniform float imageHeight;
void main(void) {
float sx, sy, r, g, b, y, u, v;
sx = gl_TexCoord[0].x;
sy = imageHeight - gl_TexCoord[0].y;
y = texture2DRect(y_texture, vec2(sx / 2.0, sy / 2.0)).r;
u = texture2DRect(u_texture, vec2(sx / 2.0, sy / 2.0)).r;
v = texture2DRect(v_texture, vec2(sx / 2.0, sy / 2.0)).r;
y = 1.1643 * (y - 0.0625);
u = u - 0.5;
v = v - 0.5;
r = y + 1.5958 * v;
g = y - 0.39173 * u - 0.8129 * v;
b = y + 2.017 * u;
gl_FragColor = vec4(r, g, b, 1.0);
}
如何修改(或创建新的)?
以下是示例代码:
glEnable(GL_TEXTURE_RECTANGLE_ARB);
//FrameHeight
int i_imageHeight = glGetUniformLocation(handleProgram, "imageHeight");
glUniform1f(i_imageHeight, (float)frameHeight);
//U
glActiveTexture(GL_TEXTURE1);
int i_u = glGetUniformLocationARB(handleProgram, "u_texture");
glUniform1iARB(i_u, 1);
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, 1);
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_LUMINANCE, frameWidth / 2, frameHeight / 2, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, uFrameData);
//V
glActiveTexture(GL_TEXTURE2);
int i_v = glGetUniformLocationARB(handleProgram, "v_texture");
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, 2);
glUniform1iARB(i_v, 2);
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_LUMINANCE, frameWidth / 2, frameHeight / 2, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, vFrameData);
//Y
glActiveTexture(GL_TEXTURE0);
int i_y = glGetUniformLocationARB(handleProgram, "y_texture");
glUniform1iARB(i_y, 3);
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0);
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_LUMINANCE, frameWidth, frameHeight, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, yFrameData);
//Draw
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_QUADS);
glTexCoord2i(0, 0); glVertex2i(0, 0);
glTexCoord2i(frameWidth, 0); glVertex2i(this->width, 0);
glTexCoord2i(frameWidth, frameHeight); glVertex2i(this->width, this->height);
glTexCoord2i(0, frameHeight); glVertex2i(0, this->height);
glEnd();
glFlush();
SwapBuffers(hDC);
答案 0 :(得分:0)
您使用的纹理不正确。我无处可见你创建纹理对象(glGenTextures
)。您需要首先生成3个纹理对象,并将其传递到glBindTexture
内。相反,你正在做的是使用0,1和2索引,这也是不正确的,因为0是不存在的纹理的索引。我怀疑OpenGL会在任何改变0纹理状态的尝试中给出错误代码。因此,在调用之间添加一些glError
()检查。从技术上讲,你的程序是GL上“未定义行为”的一个例子,所以应该预期各种硬件/驱动程序的执行差异(直到你修复它)。
供您参考,纹理管理应如下所示:
GLuint textures[3];
glGenTextures(3,textures);
...
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, textures[1]);
//initialize state of t1
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, textures[2]);
//initialize state of t2
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, textures[0]);
//initialize state of t0
请注意,您对glUniform和glActiveTexture的调用仍然正确,因此无需修复它们。