Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java FBO纹理显示为完全黑色_Java_Opengl_Glsl_Lwjgl_Texturing - Fatal编程技术网

Java FBO纹理显示为完全黑色

Java FBO纹理显示为完全黑色,java,opengl,glsl,lwjgl,texturing,Java,Opengl,Glsl,Lwjgl,Texturing,我正试图跟上。为此,我需要创建一个FBO并将其渲染为纹理 然而,正如你所看到的,水是完全黑色的: 我直接使用教程中的(从链接复制的) 基本上,我创建了一个帧缓冲区: public FrameBufferObject() {//call when loading the game initialiseReflectionFrameBuffer(); initialiseRefractionFrameBuffer(); //Let's ignore refraction for n

我正试图跟上。为此,我需要创建一个FBO并将其渲染为纹理

然而,正如你所看到的,水是完全黑色的:

我直接使用教程中的(从链接复制的)

基本上,我创建了一个帧缓冲区:

public FrameBufferObject() {//call when loading the game
    initialiseReflectionFrameBuffer();
    initialiseRefractionFrameBuffer(); //Let's ignore refraction for now, as I only use reflection at the moment.
}
private void initialiseReflectionFrameBuffer() {
    reflectionFrameBuffer = createFrameBuffer();
    reflectionTexture = createTextureAttachment(REFLECTION_WIDTH,REFLECTION_HEIGHT);
    reflectionDepthBuffer = createDepthBufferAttachment(REFLECTION_WIDTH,REFLECTION_HEIGHT);
    unbindCurrentFrameBuffer();
}
然后,我创建一个纹理附件:

private int createTextureAttachment( int width, int height) {
    int texture = GL11.glGenTextures();
    GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture);
    GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGB, width, height,
            0, GL11.GL_RGB, GL11.GL_UNSIGNED_BYTE, (ByteBuffer) null);
    GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
    GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
    GL32.glFramebufferTexture(GL30.GL_FRAMEBUFFER, GL30.GL_COLOR_ATTACHMENT0,
            texture, 0);
    return texture;
}
private int createDepthBufferAttachment(int width, int height) {
    int depthBuffer = GL30.glGenRenderbuffers();
    GL30.glBindRenderbuffer(GL30.GL_RENDERBUFFER, depthBuffer);
    GL30.glRenderbufferStorage(GL30.GL_RENDERBUFFER, GL11.GL_DEPTH_COMPONENT, width,
            height);
    GL30.glFramebufferRenderbuffer(GL30.GL_FRAMEBUFFER, GL30.GL_DEPTH_ATTACHMENT,
            GL30.GL_RENDERBUFFER, depthBuffer);
    return depthBuffer;
}
我还创建了一个深度缓冲区附件:

private int createTextureAttachment( int width, int height) {
    int texture = GL11.glGenTextures();
    GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture);
    GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGB, width, height,
            0, GL11.GL_RGB, GL11.GL_UNSIGNED_BYTE, (ByteBuffer) null);
    GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
    GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
    GL32.glFramebufferTexture(GL30.GL_FRAMEBUFFER, GL30.GL_COLOR_ATTACHMENT0,
            texture, 0);
    return texture;
}
private int createDepthBufferAttachment(int width, int height) {
    int depthBuffer = GL30.glGenRenderbuffers();
    GL30.glBindRenderbuffer(GL30.GL_RENDERBUFFER, depthBuffer);
    GL30.glRenderbufferStorage(GL30.GL_RENDERBUFFER, GL11.GL_DEPTH_COMPONENT, width,
            height);
    GL30.glFramebufferRenderbuffer(GL30.GL_FRAMEBUFFER, GL30.GL_DEPTH_ATTACHMENT,
            GL30.GL_RENDERBUFFER, depthBuffer);
    return depthBuffer;
}
然后,我将对象渲染到帧缓冲区对象:

Main.TerrainDemo.shader.start();
    fbos.bindReflectionFrameBuffer();
    for (Grass g : Main.TerrainDemo.toDraw){
        g.render();
    }
    fbos.unbindCurrentFrameBuffer();
我像这样绑定帧缓冲区:

private void bindFrameBuffer(int frameBuffer, int width, int height){
    GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);//To make sure the texture isn't bound
    GL30.glBindFramebuffer(GL30.GL_FRAMEBUFFER, frameBuffer);
    GL11.glViewport(0, 0, width, height);
    System.out.println("Bound");
    if(GL30.glCheckFramebufferStatus(GL30.GL_FRAMEBUFFER) == GL30.GL_FRAMEBUFFER_COMPLETE) {
        System.out.println("Frame buffer setup is complete: " + GL30.glCheckFramebufferStatus(GL30.GL_FRAMEBUFFER));
    }
    System.out.println("Error: " + GL11.glGetError());
}
打印glGetError()时的“错误”是正常的
0
。“帧缓冲区设置”消息不会打印出来

在此之后,我希望调用
fbos.getReflectionTexture()
将返回纹理ID。。。的确如此!它成功返回纹理ID 12。但是,绑定时的纹理是完全黑色的

public int getReflectionTexture() {//get the resulting texture
    return reflectionTexture; //Remember, this was our original texture attachment.
}
 reflectionTexture = createTextureAttachment(REFLECTION_WIDTH,REFLECTION_HEIGHT);
我无法找出什么是错误的,以及为什么纹理没有显示渲染到它的任何内容

我知道的事情没有错:

我肯定画和纹理的水本身正确。我可以使用任何预加载的纹理,并使水的纹理非常好:


此外,渲染到FBO的对象具有正确的平移、旋转等。如果我不绑定任何帧缓冲区,则将在屏幕上正确绘制(并看到)用于FBO的树叶。

因此我意识到这是一个可能再也没有人关心的超旧问题,但我最近发现自己在做了同样的教程之后,遇到了类似的问题

在使用了一点GoogleFoo之后,我意识到我在代码中犯了一个非常基本的错误,可能是相同的解决方案

我只是没有做

GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
正在使用帧缓冲区对象时,在再次填充之前:

我只是继续把它放在bind frame buffer函数的末尾,我工作了

 private void bindFrameBuffer(int frameBuffer, int width, int height){
        GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);//To make sure the texture isn't bound
        GL30.glBindFramebuffer(GL30.GL_FRAMEBUFFER, frameBuffer);
        GL11.glViewport(0, 0, width, height);
        System.out.println("Bound");
        if(GL30.glCheckFramebufferStatus(GL30.GL_FRAMEBUFFER) == GL30.GL_FRAMEBUFFER_COMPLETE) {
            System.out.println("Frame buffer setup is complete: " + GL30.glCheckFramebufferStatus(GL30.GL_FRAMEBUFFER));
        }
        System.out.println("Error: " + GL11.glGetError());
    }
可能不是同一个问题,因为我的错误是特别基本的,但值得一试


是否有人知道此解决方案是否正确,或者它是针对更大问题的创可贴?

尝试使用glFramebufferTexture2D而不是glFramebufferTexture。还可以尝试在某些地方调用glGetError,这可能会让您了解问题所在。@Kvaleya您能解释一下我将为
glFramebufferTexture2D()
设置哪些参数吗?我似乎在internet上找到了相互矛盾的答案。参数应该与glFramebufferTexture相同,唯一的新参数是texturetarget,它应该是GL_TEXTURE_2D,因为这是您要附加的纹理类型。@Kvaleya感谢您的建议。我已经按照你的建议做了,并且发现了错误。请检查我的编辑。我从来没有在opengl中遇到堆栈溢出,但从我在谷歌上搜索的结果来看,常见的原因似乎是使用了glPushMatrix,之后没有弹出它。您可以尝试的另一件事是为颜色和深度附件使用不同的格式。创建renderbuffer时,可以指定GL_DEPTH_组件格式,该格式未列为上renderbuffers的必需格式。还可以尝试使用一些RGBA颜色格式作为颜色附件,尽管不确定它是否会有任何效果。老实说,不确定,我甚至不确定是否还有我的OpenGL代码!然而,这听起来确实是一个合理的解决方案,特别是如果您使用相同的教程。