ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java – OpenGL Framebuffer渲染到纹理不工作

2019-07-04 21:00:50  阅读:272  来源: 互联网

标签:java opengl textures framebuffer jogl


我正在尝试使用框架缓冲区来创建使用Java和JOGL的发光效果,但我遇到了问题.目前,我的目标是将我的太空飞船模型(用照明纹理纹理化)渲染到帧缓冲纹理,然后将帧缓冲纹理绘制到三角形扇形四边形.我正在运行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上阅读了无数的教程和帖子,但我不能为我的生活弄清楚我做错了什么.任何帮助都会很棒!谢谢.

解决方法:

正如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修复了所有内容.

标签:java,opengl,textures,framebuffer,jogl
来源: https://codeday.me/bug/20190704/1380849.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有