我正在尝试使用顶点和片段着色器在OpenGL ES 2.0(Android 4.0.4,Galaxy Nexus)中显示简单的纹理作为全屏背景。最终结果应该是在那里显示摄像机图像,但首先,文件中的简单纹理就足够了。我测试了许多东西,并且在一段时间内它使用了OpenGL ES 1.x但是因为我打算使用着色器进行YUV-> RGB转换,所以我必须使用2.0。
我有以下顶点着色器:
attribute vec4 position;
attribute vec4 inputTextureCoordinate;
varying vec2 textureCoordinate;
void main() {
gl_Position = position;
textureCoordinate = inputTextureCoordinate.xy
}
和这个片段着色器:
varying highp vec2 textureCoordinate;
uniform sampler2D videoFrame;
void main() {
gl_FragColor = texture2D(videoFrame, textureCoordinate);
}
这样的着色器创建应该没问题,因为当我写gl_FragColor=vec4(1,0,0,0)
时,它的效果非常好,并向我显示红色背景。
OnSurfaceCreated
我执行以下操作:
a = makeFloatBuffer(squareVertices);
b = makeFloatBuffer(textureVertices);
squareVertices
和textureVertices
如下:
static float squareVertices[] = { -1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 1.0f,
1.0f, 1.0f, };
static float textureVertices[] = { 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f,
0.0f, 0.0f, };
OnSurfaceChanged
我执行以下操作:
GLES20.glViewport(0, 0, width, height);
Bitmap bitmap = BitmapFactory.decodeResource(mContext.getResources(), R.raw.htc);
GLES20.glGenTextures(1, textures, 0);
GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
R.raw.htc是128x128像素png。
OnDrawFrame
:
GLES20.glUseProgram(program);
GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textures[0]);
GLES20.glUniform1i(GLES20.glGetUniformLocation(program, "videoFrame"),0);
GLES20.glClearColor(0.0f, 0.0f, 1.0f, 0.0f);
GLES20.glClearDepthf(1.0f);
GLES20.glClear(GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
int x = GLES20.glGetAttribLocation(program, "position");
int y = GLES20.glGetAttribLocation(program, "inputTextureCoordinate");
GLES20.glEnableVertexAttribArray(x);
GLES20.glVertexAttribPointer(x, 2, GLES20.GL_FLOAT, false, 0, a);
GLES20.glEnableVertexAttribArray(y);
GLES20.glVertexAttribPointer(y, 4, GLES20.GL_FLOAT, false, 0, b);
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
我唯一看到的是一个完全黑屏,我不明白为什么。我在这里看到了其他一些类似问题的帖子,但是没有一个提议的解决方案帮助我,或者根本没有解决方案。
答案 0 :(得分:2)
如上所述,我们可以找到问题的解决方案,虽然我们不知道它现在正在工作的原因而不是之前(我们改变了一些事情并且突然之间有效)。尽管如此,我想分享我们的工作版本:
现在已完成着色器以进行YUV-> RGB转换: 顶点着色器:
attribute vec4 position;
attribute vec4 inputTextureCoordinate;
varying vec2 textureCoordinate;
void main() {
gl_Position = position;
textureCoordinate = inputTextureCoordinate.xy;
};
片段着色器:
varying highp vec2 textureCoordinate;
uniform sampler2D videoFrame;
uniform sampler2D videoFrame2;
void main(void) {
highp float nx,ny,r,g,b,y,u,v;
highp vec2 uv;
highp vec4 txl,ux,vx;"
nx=(textureCoordinate.x)/1024.0*720.0;
ny=(textureCoordinate.y)/1024.0*480.0;
y=texture2D(videoFrame,vec2(nx,ny)).r;
uv=texture2D(videoFrame2,vec2(nx,ny)).ag;
y=1.1643*(y-0.0625);
u=uv[0]-0.5;
v=uv[1]-0.5;
r=y+1.5958*v;
g=y-0.39173*u-0.81290*v;
b=y+2.017*u;
gl_FragColor=vec4(r,g,b,1);
};
OnSurfaceCreated
仍然相同,但顶点的定义有所改变:
static float squareVertices[] = { -1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 1.0f,
1.0f, 1.0f, };
private float textureVertices[] = { 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
1.0f, 0.0f, };
OnSurfaceChanged
:
buffer = new int[2];
GLES20.glGenTextures(2, buffer, 0);
for (int i = 0; i < 2; i++) {
int texture = buffer[i];
Log.v("Texture", "Texture is at " + texture);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texture);
GLES20.glPixelStorei(GLES20.GL_UNPACK_ALIGNMENT, GLES20.GL_TRUE);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D,
GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D,
GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_NEAREST);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D,
GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D,
GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);
}
buf = new byte[1024 * 1024];
for (int i = 0; i < 1024 * 1024; i++) {
buf[i] = 0;
}
buf2 = new byte[1024 * 1024];
for (int i = 0; i < 1024 * 1024; i++) {
buf2[i] = -128;
}
GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, buffer[0]);
GLES20.glTexImage2D(GL10.GL_TEXTURE_2D, 0, GL10.GL_LUMINANCE, 1024,
1024, 0, GL10.GL_LUMINANCE, GL10.GL_UNSIGNED_BYTE,
ByteBuffer.wrap(buf));
GLES20.glActiveTexture(GLES20.GL_TEXTURE1);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, buffer[1]);
GLES20.glTexImage2D(GL10.GL_TEXTURE_2D, 0, GL10.GL_LUMINANCE_ALPHA,
512, 512, 0, GL10.GL_LUMINANCE_ALPHA, GL10.GL_UNSIGNED_BYTE,
ByteBuffer.wrap(buf2));
OnDrawFrame
:
GLES20.glViewport(0, 0, mWidth, mHeight);
GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, buffer[0]);
GLES20.glTexSubImage2D(GL10.GL_TEXTURE_2D, 0, 0, 0, Util.CAMWIDTH,
Util.CAMHEIGHT, GL10.GL_LUMINANCE, GL10.GL_UNSIGNED_BYTE,
ByteBuffer.wrap(mBuffer, 0, Util.CAMWIDTH * Util.CAMHEIGHT));
GLES20.glActiveTexture(GLES20.GL_TEXTURE1);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, buffer[1]);
GLES20.glTexSubImage2D(
GL10.GL_TEXTURE_2D,
0,
0,
0,
Util.CAMWIDTH / 2,
Util.CAMHEIGHT / 2,
GL10.GL_LUMINANCE_ALPHA,
GL10.GL_UNSIGNED_BYTE,
ByteBuffer.wrap(mBuffer, Util.CAMWIDTH * Util.CAMHEIGHT,
Util.CAMWIDTH / 2 * Util.CAMHEIGHT).slice());
GLES20.glClearColor(1.0f, .0f, .0f, .0f);
GLES20.glUseProgram(program);
GLES20.glUniform1i(GLES20.glGetUniformLocation(program, "videoFrame"),
0);
GLES20.glUniform1i(GLES20.glGetUniformLocation(program, "videoFrame2"),
1);
GLES20.glClearDepthf(1.0f);
GLES20.glClear(GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
int x = GLES20.glGetAttribLocation(program, "position");
int y = GLES20.glGetAttribLocation(program, "inputTextureCoordinate");
GLES20.glEnableVertexAttribArray(x);
GLES20.glVertexAttribPointer(x, 2, GLES20.GL_FLOAT, false, 0, a);
GLES20.glEnableVertexAttribArray(y);
GLES20.glVertexAttribPointer(y, 2, GLES20.GL_FLOAT, false, 0, b);
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
答案 1 :(得分:0)
您将纹理坐标声明为4个组件,而不是看起来应该是2.代码看起来不错。
答案 2 :(得分:0)
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_NEAREST);
这两行代码为我做了诀窍
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);
我还需要这两个才能显示PNG文件