OpenGL ES 2.0 - 渲染到纹理全黑
OpenGL ES 2.0 - Render to texture is all black
我有一个渲染到屏幕上没有问题的场景,但是当我尝试将其渲染到纹理时,它将全黑。我想我在准备纹理时错过了一些阶段。
代码:
int[] FBO_main = new int[1];
int[] textureId = new int[1];
int[] renderBufferId = new int[1];
// create framebuffers
GLES20.glGenFramebuffers(FBO_main.length, FBO_main, 0);
// create texture object
GLES20.glGenTextures(1, textureId, 0);
// create render buffer
GLES20.glGenRenderbuffers(1, renderBufferId, 0);
// Bind Frame buffer
GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, FBO_main[0]);
// Bind texture
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureId[0]);
int[] buf = new int[800*400];
IntBuffer texBuffer;
texBuffer = ByteBuffer.allocateDirect(buf.length*4).order(ByteOrder.nativeOrder()).asIntBuffer();
// Texture parameters
GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0,
GLES20.GL_RGBA, 800, 400, 0,
GLES20.GL_RGBA,
GLES20.GL_UNSIGNED_BYTE, texBuffer);
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D,
GLES20.GL_TEXTURE_MAG_FILTER,
GLES20.GL_LINEAR);
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D,
GLES20.GL_TEXTURE_MIN_FILTER,
GLES20.GL_LINEAR);
GLES20.glTexParameterf(
GLES20.GL_TEXTURE_2D,
GLES20.GL_TEXTURE_WRAP_S,
GLES20.GL_CLAMP_TO_EDGE);
GLES20.glTexParameterf(
GLES20.GL_TEXTURE_2D,
GLES20.GL_TEXTURE_WRAP_T,
GLES20.GL_CLAMP_TO_EDGE);
GLES20.glTexParameteri(
GLES20.GL_TEXTURE_2D,
GLES20.GL_GENERATE_MIPMAP_HINT,
GLES20.GL_TRUE); // automatic mipmap
// Bind render buffer and define buffer dimension
GLES20.glBindRenderbuffer(GLES20.GL_RENDERBUFFER, renderBufferId[0]);
GLES20.glRenderbufferStorage(GLES20.GL_RENDERBUFFER, GLES20.GL_DEPTH_COMPONENT16, 800, 400);
// Attach texture FBO color attachment
GLES20.glFramebufferTexture2D(GLES20.GL_FRAMEBUFFER, GLES20.GL_COLOR_ATTACHMENT0, GLES20.GL_TEXTURE_2D, textureId[0], 0);
// Attach render buffer to depth attachment
GLES20.glFramebufferRenderbuffer(GLES20.GL_FRAMEBUFFER, GLES20.GL_DEPTH_ATTACHMENT, GLES20.GL_RENDERBUFFER, renderBufferId[0]);
// Reset
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0);
GLES20.glBindRenderbuffer(GLES20.GL_RENDERBUFFER, 0);
GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0);
// Setup render to texture
GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, FBO_main[0]);
GLES20.glViewport(0, 0, 800, 400);
// Draw gamespecific
onDrawGame(timediff, time);
// Draw to buffer
GLES20.glFlush();
// Use buffer as input texture
GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureId[0]);
GLES20.glUniform1i(GLES20.glGetUniformLocation(cube.program, "sampler_prev"), 0);
// switch to screen output
GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0);
// Draw gamespecific
onDrawGame(timediff, time);
// Draw
GLES20.glFlush();
知道我遗漏了什么吗?
我很长时间没有使用 opengl es,但我认为你应该对内部格式和格式都使用 GL_RGBA 并用作类型 GL_UNSIGNED_BYTE 并为数据传递 null .
GLES20.glTexImage2D(
GLES20.GL_TEXTURE_2D, 0,
GLES20.GL_RGBA,
800, 600, 0,
GLES20.GL_RGBA,
GLES20.GL_UNSIGNED_BYTE, null);
我有一个渲染到屏幕上没有问题的场景,但是当我尝试将其渲染到纹理时,它将全黑。我想我在准备纹理时错过了一些阶段。
代码:
int[] FBO_main = new int[1];
int[] textureId = new int[1];
int[] renderBufferId = new int[1];
// create framebuffers
GLES20.glGenFramebuffers(FBO_main.length, FBO_main, 0);
// create texture object
GLES20.glGenTextures(1, textureId, 0);
// create render buffer
GLES20.glGenRenderbuffers(1, renderBufferId, 0);
// Bind Frame buffer
GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, FBO_main[0]);
// Bind texture
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureId[0]);
int[] buf = new int[800*400];
IntBuffer texBuffer;
texBuffer = ByteBuffer.allocateDirect(buf.length*4).order(ByteOrder.nativeOrder()).asIntBuffer();
// Texture parameters
GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0,
GLES20.GL_RGBA, 800, 400, 0,
GLES20.GL_RGBA,
GLES20.GL_UNSIGNED_BYTE, texBuffer);
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D,
GLES20.GL_TEXTURE_MAG_FILTER,
GLES20.GL_LINEAR);
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D,
GLES20.GL_TEXTURE_MIN_FILTER,
GLES20.GL_LINEAR);
GLES20.glTexParameterf(
GLES20.GL_TEXTURE_2D,
GLES20.GL_TEXTURE_WRAP_S,
GLES20.GL_CLAMP_TO_EDGE);
GLES20.glTexParameterf(
GLES20.GL_TEXTURE_2D,
GLES20.GL_TEXTURE_WRAP_T,
GLES20.GL_CLAMP_TO_EDGE);
GLES20.glTexParameteri(
GLES20.GL_TEXTURE_2D,
GLES20.GL_GENERATE_MIPMAP_HINT,
GLES20.GL_TRUE); // automatic mipmap
// Bind render buffer and define buffer dimension
GLES20.glBindRenderbuffer(GLES20.GL_RENDERBUFFER, renderBufferId[0]);
GLES20.glRenderbufferStorage(GLES20.GL_RENDERBUFFER, GLES20.GL_DEPTH_COMPONENT16, 800, 400);
// Attach texture FBO color attachment
GLES20.glFramebufferTexture2D(GLES20.GL_FRAMEBUFFER, GLES20.GL_COLOR_ATTACHMENT0, GLES20.GL_TEXTURE_2D, textureId[0], 0);
// Attach render buffer to depth attachment
GLES20.glFramebufferRenderbuffer(GLES20.GL_FRAMEBUFFER, GLES20.GL_DEPTH_ATTACHMENT, GLES20.GL_RENDERBUFFER, renderBufferId[0]);
// Reset
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0);
GLES20.glBindRenderbuffer(GLES20.GL_RENDERBUFFER, 0);
GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0);
// Setup render to texture
GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, FBO_main[0]);
GLES20.glViewport(0, 0, 800, 400);
// Draw gamespecific
onDrawGame(timediff, time);
// Draw to buffer
GLES20.glFlush();
// Use buffer as input texture
GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureId[0]);
GLES20.glUniform1i(GLES20.glGetUniformLocation(cube.program, "sampler_prev"), 0);
// switch to screen output
GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0);
// Draw gamespecific
onDrawGame(timediff, time);
// Draw
GLES20.glFlush();
知道我遗漏了什么吗?
我很长时间没有使用 opengl es,但我认为你应该对内部格式和格式都使用 GL_RGBA 并用作类型 GL_UNSIGNED_BYTE 并为数据传递 null .
GLES20.glTexImage2D(
GLES20.GL_TEXTURE_2D, 0,
GLES20.GL_RGBA,
800, 600, 0,
GLES20.GL_RGBA,
GLES20.GL_UNSIGNED_BYTE, null);