GLfloat vVertices[] = { -1, 1, 0.0f, // Position 0
0.0f, 1.0f, // TexCoord 0
-1, -1, 0.0f, // Position 1
0.0f, 0.0f, // TexCoord 1
1, -1, 0.0f, // Position 2
1.0f, 0.0f, // TexCoord 2
1, 1, 0.0f, // Position 3
1.0f, 1.0f // TexCoord 3
};
GLushort indices[] = { 0, 3, 2, 0, 2, 1}; //{ 0, 1, 2, 0, 3, 2 };//
char vShaderStr[] =
"attribute vec4 a_position; \n"
"attribute vec2 a_texCoord; \n"
"varying vec2 v_texCoord; \n"
"void main() \n"
"{ \n"
" gl_Position = a_position; \n"
" v_texCoord = a_texCoord; \n"
"} \n";
char fShaderStr[] =
"precision mediump float;"
"uniform sampler2D s_texture;"
"varying vec2 v_texCoord;"
"void main(void)"
"{"
"float nx, ny, r, g, b, y, u, v;"
"float u1,u2,v1,v2;"
"nx = v_texCoord.x;"
"ny = v_texCoord.y;"
"y = texture2D(s_texture, vec2( (nx), (ny)*(4.0/6.0) )).r;"
"u1 = texture2D(s_texture, vec2( (nx/2.0), (ny+4.0)/6.0 )).r;"
"u2 = texture2D(s_texture, vec2( (nx/2.0)+0.5, (ny+4.0)/6.0 )).r;"
"v1 = texture2D(s_texture, vec2( (nx/2.0), (ny+5.0)/6.0 )).r;"
"v2 = texture2D(s_texture, vec2( (nx/2.0)+0.5, (ny+5.0)/6.0 )).r;"
"y = 1.1643 * (y - 0.0625);"
"u = u1 - 0.5;"
"v = v1 - 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);"
"}";
GLuint programObject = esLoadProgram ( vShaderStr, fShaderStr );
GLint positionLoc = glGetAttribLocation ( programObject, "a_position" );
GLint texCoordLoc = glGetAttribLocation ( programObject, "a_texCoord" );
glViewport ( 0, 0, g_nScreenWidth, g_nScreenHeight );
glUseProgram ( programObject );
glGenTextures(1, &textureID);
glPixelStorei(GL_PACK_ALIGNMENT, 1);
get_updated_video_buf = get_video_updated_buf();
glBindTexture(GL_TEXTURE_2D, textureID);
glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, 720, 720, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, get_updated_video_buf);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glVertexAttribPointer ( positionLoc, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), vVertices );
glVertexAttribPointer ( texCoordLoc, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), &vVertices[3] );
glEnableVertexAttribArray ( positionLoc );
glEnableVertexAttribArray ( texCoordLoc );
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClearColor(1.0,1.0,1.0,1.0);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,textureID);
glDrawElements ( GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices );
eglSwapBuffers( eglDisplay , eglWindowSurface );
上面是我的代码部分,用于将图像纹理从YUV转换为RGB格式。 我看到显示屏中的图像已损坏。我的代码中可能存在什么问题。 如果我使用以下逻辑进行手动转换,片段着色器会更改并尝试渲染它正在工作。我是OpenGL的新手。
len = width * height/2;
for (i = 0; i < len; i++)
{
int y0 = yuyv[yuyv_ptr];
int u0 = yuyv[yuyv_ptr+1];
int y1 = yuyv[yuyv_ptr+2];
int v0 = yuyv[yuyv_ptr+3];
yuyv_ptr +=4;
int r = ((298*(y0-16)+ 409*(v0-128) + 128) >> 8);
int g = ((298*(y0-16)- 100*(u0-128) - 208*(v0-128) + 128) >> 8);
int b = ((298*(y0-16)+ 516*(u0-128) + 128) >> 8);
if(r>255) r=255;
if(g>255) g=255;
if(b>255) b=255;
if(r<0) r=0;
if(g<0) g=0;
if(b<0) b=0;
rgb_buff[rgb_ptr] = r;
rgb_buff[rgb_ptr+1] = g;
rgb_buff[rgb_ptr+2] = b;
rgb_buff[rgb_ptr+3] = 0xff;
rgb_ptr +=4;
r = ((298*(y1-16)+ 409*(v0-128) + 128) >> 8);
g = ((298*(y1-16)- 100*(u0-128) - 208*(v0-128) + 128) >> 8);
b = ((298*(y1-16)+ 516*(u0-128) + 128) >> 8);
if(r>255) r=255;
if(g>255) g=255;
if(b>255) b=255;
if(r<0) r=0;
if(g<0) g=0;
if(b<0) b=0;
rgb_buff[rgb_ptr] = r;
rgb_buff[rgb_ptr+1] = g;
rgb_buff[rgb_ptr+2] = b;
rgb_buff[rgb_ptr+3] = 0xff;
rgb_ptr +=4;
get_updated_video_buf = get_rgb_updated_buf(); //will return rgb_ptr
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 720, 480, 0, GL_RGBA, GL_UNSIGNED_BYTE, get_updated_video_buf);