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 Opengl-后处理多个FBO-我做错了什么?_Java_Opengl_Graphics_Lwjgl_Fbo - Fatal编程技术网

Java Opengl-后处理多个FBO-我做错了什么?

Java Opengl-后处理多个FBO-我做错了什么?,java,opengl,graphics,lwjgl,fbo,Java,Opengl,Graphics,Lwjgl,Fbo,我对OpenGL还很陌生。我以为我了解FBO,但我很难让他们工作 我的程序绘制多采样fbo基本图形信息(colorFbo)。 从colorFbo I blit到后处理FBO。 然后是带有无符号int值的backefbo,我只是用对象的id填充每个对象的轮廓 我想对后处理fbo和counterbo进行后处理,但是,我看不到counterbo的任何效果。。。请问哪里有问题? 以下是我的代码部分: 初始化: colorFbo = new Framebuffer(true, false); //mu

我对OpenGL还很陌生。我以为我了解FBO,但我很难让他们工作

我的程序绘制多采样fbo基本图形信息(colorFbo)。 从colorFbo I blit到后处理FBO。 然后是带有无符号int值的backefbo,我只是用对象的id填充每个对象的轮廓

我想对后处理fbo和counterbo进行后处理,但是,我看不到counterbo的任何效果。。。请问哪里有问题? 以下是我的代码部分:

初始化:

colorFbo = new Framebuffer(true, false);   //multisampled, rgba
counterFbo = new Framebuffer(false, true); //not MS, red

...seting polygonmode, viewport, depthtest, blending...   

modelProgram = loadProgram("model");
counterProgram = loadProgram("counter");
...
postprocessProgram = loadProgram("postprocess");
...        
postprocessFbo = new Framebuffer(false, false); //not MS, rgba
渲染循环:

glBindFramebuffer(GL_FRAMEBUFFER, colorBuffer.fbo());
Main.clear(0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glEnable(GL_MULTISAMPLE);        
drawing objects using model program...

glBindFramebuffer(GL_FRAMEBUFFER, counterBuffer.fbo());
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDisable(GL_MULTISAMPLE);        
drawing objects using counter program...

blit colorFbo to postprocessFbo...

glBindFramebuffer(GL_FRAMEBUFFER, 0);
glClear(GL_COLOR_BUFFER_BIT);
glDisable(GL_DEPTH_TEST);
glUseProgram(postprocessProgram);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, postprocessFbo.fboColorTexture());
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, counterFbo.fboColorTexture());

glBindVertexArray(screenQuadArray);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);        
计数器片段着色器:

#version 330

out uint counter;

in vec3 vPosition;

uniform uint id;

void main() {
    counter = id;
}
#version 330

uniform sampler2D colorTex;
uniform usampler2D counterTex;

out vec4 finalColor;

in vec2 texCoord;

void main() {
    finalColor = texture(colorTex, vec2(tex));
    uint id = texture(counterTex, vec2(tex)).s;
    if (id > 0) {
        finalColor = black;
    }
}
后处理片段着色器:

#version 330

out uint counter;

in vec3 vPosition;

uniform uint id;

void main() {
    counter = id;
}
#version 330

uniform sampler2D colorTex;
uniform usampler2D counterTex;

out vec4 finalColor;

in vec2 texCoord;

void main() {
    finalColor = texture(colorTex, vec2(tex));
    uint id = texture(counterTex, vec2(tex)).s;
    if (id > 0) {
        finalColor = black;
    }
}
帧缓冲区构造函数:

public Framebuffer(boolean multisampled, boolean redOnly) {
    int internalFormat = redOnly ? GL_R32UI : GL_RGBA;
    int format = redOnly ? GL_RED_INTEGER : GL_RGBA;
    int dataType = redOnly ? GL_UNSIGNED_INT : GL_UNSIGNED_BYTE;
    int interpolation = redOnly ? GL_NEAREST : GL_LINEAR;

    int textureType = multisampled ? GL_TEXTURE_2D_MULTISAMPLE : GL_TEXTURE_2D;
    // Prepare FBO textures
    fboColorTexture = glGenTextures();
    fboDepthStencilTexture = glGenTextures();
    if (multisampled) {
        glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, fboColorTexture);
        glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, SAMPLES, internalFormat, Main.width(), Main.height(), false);
        glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, fboDepthStencilTexture);
        glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, SAMPLES, GL_DEPTH24_STENCIL8, Main.width(), Main.height(), false);            
        glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, 0);
    } else {
        glBindTexture(GL_TEXTURE_2D, fboColorTexture);
        glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, Main.width(), Main.height(), 0, format, dataType, (float[]) null);            
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, interpolation);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, interpolation);
        glBindTexture(GL_TEXTURE_2D, fboDepthStencilTexture);
        glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH24_STENCIL8, Main.width(), Main.height(), 0, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, (float[]) null);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
        glBindTexture(GL_TEXTURE_2D, 0);
    }        

    fbo = glGenFramebuffers();
    glBindFramebuffer(GL_FRAMEBUFFER, fbo);
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, textureType, fboColorTexture, 0);
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, textureType, fboDepthStencilTexture, 0);
    glDrawBuffers(GL_COLOR_ATTACHMENT0);
    int status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
    if (status != GL_FRAMEBUFFER_COMPLETE) {
        System.err.println("Framebuffer not complete.");
        System.exit(1);
    }
    glBindFramebuffer(GL_FRAMEBUFFER, 0);
}

您似乎缺少将纹理单元指定给纹理名称。因此,当我加载纹理时,我通常在初始化中执行此操作

uColorTex = glGetUniformLocation(shaders.shaderProgram, "colorTex");
uCounterTex = glGetUniformLocation(shaders.shaderProgram, "counterTex");
。。。当你画画的时候

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, postprocessFbo.fboColorTexture());
glUniform1i(uColourTex, 0);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, counterFbo.fboColorTexture());
glUniform1i(uCounterTex, 1);

问题出在计数器程序本身(在
glvertexattributepointer
中),我没有写这方面的内容。 我只需要计数器程序中的位置,所以我写了这个:

glVertexAttribPointer(positionAttribLoc, 3, GL_FLOAT, false, FLOAT_VEC3_SIZE, 0);
但是我忘了,数据总是在同一个位置,所以我不能“节省空间”,因为我只是绑定,顶点数据保持不变:

glVertexAttribPointer(positionAttribLoc, 3, GL_FLOAT, false, SIZEOF_MODEL_VERTEX, 0);

谢谢你的回答,很抱歉我没有显示更多的代码,我认为这个问题可能在任何地方,也不想用太多的代码压倒任何阅读者。

我在写问题时错误地删除了它,在原始代码中没有问题。很抱歉让人迷惑。谢谢你的回答,我做了初始化的第一部分(三点),我跳过了这一部分,因为我觉得我已经显示了太多的代码。实际上,这两部分都是,很抱歉混淆了。记住colorTex很好用,只是我没法用它,包括glUniform1i钻头?我只是想问一下,因为我让你的代码运行得很好,只是缺少了一点。我在初始化部分做了这件事,我也试着将它移动到渲染,但没有任何改变。我看到了后处理FBO中的对象,但由于使用了Counterbo,它们应该都是黑色的,而且它们不是…嗯,尝试交换周围的纹理单位,因此计数器纹理是GL_texture0。您在问题中没有显示这部分代码。所以不可能回答这个问题。