OpenGL帧缓冲区渲染到纹理不起作用

Wagan8r:

我正在尝试使用Java和JOGL使用framebuffers创建发光效果,但是遇到了问题。目前,我的目标是将用照明纹理进行纹理处理的飞船模型渲染为帧缓冲区纹理,然后将帧缓冲区纹理绘制到三角形扇形四边形。我正在与OpenGL 2.1和/或2.0一起运行这是用于生成帧缓冲区及其纹理的代码:

private int createFrameBuffer(GL2GL3 gl) {
    _frameBufferTextureId = GLHelper.makeTextureBuffer(gl, GL2GL3.GL_RGBA, _width / 2, _height / 2, 0, GL2GL3.GL_RGBA,
            GL2GL3.GL_UNSIGNED_BYTE, GL2GL3.GL_TEXTURE_MIN_FILTER, GL2GL3.GL_TEXTURE_MIN_FILTER, GL2GL3.GL_REPEAT, null);
    int frameBufferId = GLHelper.makeFrameBuffer(gl, _frameBufferTextureId, _width / 2, _height / 2);
    return frameBufferId;
}

public static int makeTextureBuffer(GL2GL3 gl, int glEnumInternalFormat, int width, int height, int border,
        int glEnumPixelFormat, int glEnumPixelType, int glEnumMinFilter, int glEnumMagFilter, int glEnumWrapMode,
        File textureFile) {
    ByteBuffer textureDataBuffer = null;
    if (textureFile != null) {
        String filePath = textureFile.getPath();
        String extension = filePath.substring(filePath.lastIndexOf('.') + 1, filePath.length());
        TextureData textureData = null;
        try {
            textureData = TextureIO.newTextureData(gl.getGLProfile(), textureFile, false, extension);
        } catch (IOException e) {
            throw new RuntimeException("Error reading texture");
        }
        textureDataBuffer = getProperlyFormattedBuffer(textureData);
    }
    IntBuffer texture = IntBuffer.allocate(1);
    gl.glGenTextures(1, texture);
    gl.glBindTexture(GL2GL3.GL_TEXTURE_2D, texture.get(0));
    gl.glTexParameteri(GL2GL3.GL_TEXTURE_2D, GL2GL3.GL_TEXTURE_MIN_FILTER, glEnumMinFilter);
    gl.glTexParameteri(GL2GL3.GL_TEXTURE_2D, GL2GL3.GL_TEXTURE_MAG_FILTER, glEnumMagFilter);
    gl.glTexParameteri(GL2GL3.GL_TEXTURE_2D, GL2GL3.GL_TEXTURE_WRAP_S, glEnumWrapMode);
    gl.glTexParameteri(GL2GL3.GL_TEXTURE_2D, GL2GL3.GL_TEXTURE_WRAP_T, glEnumWrapMode);
    gl.glTexImage2D(GL2GL3.GL_TEXTURE_2D, 0, glEnumInternalFormat, width, height, border, glEnumPixelFormat, glEnumPixelType,
            textureDataBuffer);
    gl.glBindTexture(GL2GL3.GL_TEXTURE_2D, 0);
    return texture.get(0);
}

public static int makeFrameBuffer(GL2GL3 gl, int textureBufferId, int width, int height) {
    IntBuffer frameBuffer = IntBuffer.allocate(1);
    gl.glGenFramebuffers(1, frameBuffer);
    gl.glBindFramebuffer(GL2GL3.GL_FRAMEBUFFER, frameBuffer.get(0));
    gl.glFramebufferTexture2D(GL2GL3.GL_FRAMEBUFFER, GL2GL3.GL_COLOR_ATTACHMENT0, GL2GL3.GL_TEXTURE_2D, textureBufferId, 0);
    verifyFrameBuffer(gl);
    gl.glBindFramebuffer(GL2GL3.GL_FRAMEBUFFER, 0);
    return frameBuffer.get(0);
}

private void drawTextureQuad(GL2GL3 gl) {
    _glowShader.bind(gl);
    int textureLocation = _glowShader.getUniformFields().getIlluminationTextureLocation();
    gl.glUniform1i(textureLocation, 0);
    gl.glActiveTexture(GL2GL3.GL_TEXTURE0);
    gl.glBindTexture(GL2GL3.GL_TEXTURE_2D, _frameBufferTextureId);

    int vertexLocation = _glowShader.getAttributeFields().getVertexLocation();
    gl.glBindBuffer(GL2GL3.GL_ARRAY_BUFFER, _textureQuadId);
    gl.glVertexAttribPointer(vertexLocation, 3, GL2GL3.GL_FLOAT, false, 0, 0);
    gl.glEnableVertexAttribArray(vertexLocation);

    int textureCoordLocation = _glowShader.getAttributeFields().getVertexTextureCoordinateLocation();
    gl.glBindBuffer(GL2GL3.GL_ARRAY_BUFFER, _textureQuadCoordsId);
    gl.glVertexAttribPointer(textureCoordLocation, 2, GL2GL3.GL_FLOAT, false, 0, 0);
    gl.glEnableVertexAttribArray(textureCoordLocation);

    gl.glDrawArrays(GL2GL3.GL_TRIANGLE_FAN, 0, 4);
    _glowShader.unbind(gl);
}

在我的渲染循环中,我正在执行以下操作:

_glowShader.bind(gl);// bind my Shader object.
gl.glUniformMatrix4fv(_glowShader.getUniformFields().getProjectionMatrixLocation(), 1, true, _projectionMatrix.getBuffer());
gl.glUniformMatrix4fv(_glowShader.getUniformFields().getModelViewMatrixLocation(), 1, true, _modelViewMat.getBuffer());
gl.glBindFramebuffer(GL.GL_FRAMEBUFFER, _framebufferId);
gl.glDrawBuffers(1, _frameBufferAttachmentss); //contains GL_COLOR_ATTACHMENT0
gl.glViewport(0, 0, _width / 2, _height / 2); // same size as texture
_spaceship.draw(gl, _glowShader);// draw method in my ModelObject class
gl.glBindFramebuffer(GL.GL_FRAMEBUFFER, 0);
_glowShader.unbind(gl);

drawTextureQuad(gl);

运行此命令时,将按原样绘制四边形,但纹理全为黑色。当我在渲染循环中注释掉任何帧缓冲区绑定时,将按应有的方式渲染模型,因此我知道我的着色器代码没有任何问题。当我将_frameBufferTextureId替换为从文件加载的另一个纹理时,该纹理已正确绘制,因此我知道我的纹理坐标在起作用,并且我的着色器代码也是正确的。但是,每当绑定帧缓冲区时,我都无法得到任何期望的东西。我也在验证我的帧缓冲区,它是GL_FRAMEBUFFER_COMPLETE。

我在这里阅读了无数有关stackoverflow的教程和文章,但是我终生无法弄清自己在做什么错。任何帮助将是巨大的!谢谢。

Wagan8r:

正如Bethor指出的那样,我两次将GL_TEXTURE_MIN_FILTER传递给我的makeTextureBuffer方法。这解决了glTexParameteri()的以下调用:

gl.glTexParameteri(GL2GL3.GL_TEXTURE_2D, GL2GL3.GL_TEXTURE_MIN_FILTER, GL2GL2.GL_TEXTURE_MIN_FILTER);
gl.glTexParameteri(GL2GL3.GL_TEXTURE_2D, GL2GL3.GL_TEXTURE_MAG_FILTER, GL2GL2.GL_TEXTURE_MIN_FILTER);

当然,这毫无意义。更改我的通话以传递GL_LINEAR可以解决所有问题。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

OpenGL sRGB帧缓冲区奇数

OpenGL:帧缓冲区,不完整的纹理附件

将反转的场景渲染到帧缓冲区

OpenGL-如何绘制到多样本帧缓冲区,然后将结果用作普通纹理?

Android上的OpenGL绘图与Unity结合以通过帧缓冲区传输纹理无法正常工作

从帧缓冲区对象读取时,OpenGL纹理坐标相反

Opengl和Webgl:从附加到当前帧缓冲区的纹理采样

使用模板缓冲区渲染时纹理的OpenGL透明度

渲染到帧缓冲区仅渲染场景的左下角

渲染到帧缓冲区/纹理显示为空

如何访问默认帧缓冲区的纹理

屏幕外渲染到帧缓冲区

OpenGL:帧缓冲区-绘制纹理-glClearColor

如何从OpenGL中的帧缓冲区纹理中采样像素?

OpenGL ES2.0 glReadPixels()从渲染缓冲区通过帧缓冲区读取数据

附加到帧缓冲区的OpenGL多样本整数纹理无法正确解析

无法使用lwjgl和顶点缓冲区渲染纹理

渲染到纹理不起作用

opengl:基于着色器的渲染到帧缓冲区,然后进行固定管线渲染

在OpenGL ES 2.0中,将FBO与纹理深度缓冲区一起使用时,深度纹理缓冲区不起作用

OpenGL帧缓冲区绑定目标

OpenGL累积缓冲区不起作用

使用帧缓冲区渲染为立方体贴图纹理

OpenGL (LWJGL):渲染到纹理不起作用

使用自定义帧缓冲区时深度测试不起作用

如何在 OpenGL 中重新定位绑定到帧缓冲区的 2D 纹理?

GTK3 GLArea 与 GLFW 的帧缓冲区问题(渲染到纹理):相同的 OpenGL 程序在 GLFW 中工作,但在 GTK3 的 GLArea 中无效

渲染到(或从中读取?)帧缓冲区在移动 Safari 中不起作用

一次渲染到多个帧缓冲区