渲染到附着在帧缓冲区对象上的纹理(纹理显示为黑色)

问题描述 投票:0回答:1

我已经生成了帧缓冲对象(FBO),将其绑定,并将纹理附加到其GL_COLOR_ATTACHMENT0。然后,确保我的状态为GL_FRAMEBUFFER_COMPLETE。我还有另一个纹理(正在显示),我们称它为workingTexture。现在,我想将此WorkingTexture渲染到FBO上,因此我绑定了该FBO,然后渲染了workingTexture。然后,我绑定默认的帧缓冲区(保留用于显示),并尝试渲染附加到FBO的纹理,以为我可以将纹理显示到显示器上,但是会得到黑色纹理。

相关代码

生成workTexture ...的代码

int[] workingTexture = new int[1];

glGenTextures(1, workingTexture, 0); // generate workingTexture
glActiveTexture(GL_TEXTURE0);  // attach it to TEXTURE_UNIT0
glBindTexture(GL_TEXTURE_2D, workingTexture[0]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);

生成fbo的代码

int[] fboName = new int[1];
int[] fboTextureName = new int[1];
final int fboTextureUnit = 1;  // attach this texture to texture unit GL_TEXTURE1
glGenFramebuffers(1, fboName, 0);

int generatedTextureNameForFBO =
                generateTextureForFBO(fboTextureUnit,
                        width, height);
fboTextureName[0] = generatedTextureNameForFBO;

glBindFramebuffer(GL_FRAMEBUFFER, fboName[0]);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
                GL_TEXTURE_2D, generatedTextureNameForFBO, 0);

if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
            // created FBO is not complete
            throw new RuntimeException("FBO status INCOMPLETE 😔😔");

        }

用于生成附加到fbo的纹理的方法

 private int generateTextureForFBO(@IntRange(from = 0) final int textureUnit,
                                  @IntRange(from = 1) final int width,
                                  @IntRange(from = 1) final int height) {
    int[] textureName = new int[1];
    glGenTextures(1, textureName, 0);
    glActiveTexture(GL_TEXTURE0 + textureUnit);
    glBindTexture(GL_TEXTURE_2D, textureName[0]);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, null);
  //        glBindTexture(GL_TEXTURE_2D, 0);
  //        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  //        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  //        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);
  //        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);
  //        glBindTexture(GL_TEXTURE_2D, 0);
    return textureName[0];
}

现在要开始绘制零件...

@Override
public void onDrawFrame(GL10 unused) {
    glClear(GL_COLOR_BUFFER_BIT);
    //renderTex(mViewMatrix, 0, texProgram);  // this texture is drawing on screen

    glBindFramebuffer(GL_FRAMEBUFFER, fboName[0]);
    renderTex(mViewMatrix, 0, texProgram);   // rendering workingTexture onto FBO

    glBindFramebuffer(GL_FRAMEBUFFER, 0);
    renderTex(mViewMatrix, fboTextureUnit[0], texProgram);  // texture appears black

}

renderTex是一种渲染纹理的简单方法。一切正常。

而且我已经检查了GL_ERROR,但没有错误。

我对应用程序创建的FBO的理解是,每次读写glCall都会在当前绑定的FBO上发生,所以我对FBO的理解不正确,或者我的代码中是否有错误。

Android平台上的OpenGL | es 20

android opengl-es opengl-es-2.0
1个回答
0
投票

generateTextureForFBO中生成的纹理是mipmap不完整的。

如果未生成mipmaps(由glGenerateMipmap生成),则设置glGenerateMipmap很重要。由于默认过滤器为GL_TEXTURE_MIN_FILTER,因此如果不将缩小功能更改为GL_NEAREST_MIPMAP_LINEARGL_NEAREST,则纹理将为mipmap不完整。

通过GL_LINEAR设置纹理缩小功能(GL_TEXTURE_MIN_FILTER):

glTexParameter
© www.soinside.com 2019 - 2024. All rights reserved.