在不同的fbo上的多个纹理输出

时间:2015-08-27 11:00:17

标签: opengl glsl

我正在尝试使用纹理计算数据,并在写入Pong fbo时从Ping fbo读取数据。

我想要实现的解释图片: fbo pingponging

这是我的通话代码:

glUseProgram(TexturedShader);
        glBindFramebuffer(GL_FRAMEBUFFER, PongFbo);
        //  glClear(GL_COLOR_BUFFER_BIT);
        glBindVertexArray(n0->PingVao);

        Write(ping, n0, TexturedShader);
        Write(ping, nN, TexturedShader);
        Write(ping, nE, TexturedShader);
        Write(ping, nW, TexturedShader);
        Write(ping, nS, TexturedShader);
        Write(ping, nSE, TexturedShader);
        Write(ping, nSW, TexturedShader);

        WriteOut();

        glDrawArrays(GL_TRIANGLES, 0, 6);
        glBindVertexArray(0);


void Write(bool ping, Tabela* N, GLuint Shader)
{
        if (ping)
        {
            glActiveTexture(static_cast<GLenum>(static_cast<int>(GL_TEXTURE0)+N->MyNr()));
            glBindTexture(GL_TEXTURE_2D, N->PingTexture);
        }
        else
        {
            glActiveTexture(static_cast<GLenum>(static_cast<int>(GL_TEXTURE0)+N->MyNr()));
            glBindTexture(GL_TEXTURE_2D, N->PongTexture);
        }

        glUniform1i(glGetUniformLocation(Shader, N->MyName().c_str()), N->MyNr());

        if (glGetUniformLocation(Shader, N->MyName().c_str()) == -1)
        {
            std::cout << "not bound sucessfully " << Shader << " " << N->MyNr() << " " << N->MyName().c_str() << std::endl;
        }

}

void WriteOut()
{

    GLenum DrawBuffers[7] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2, GL_COLOR_ATTACHMENT3, GL_COLOR_ATTACHMENT4, GL_COLOR_ATTACHMENT5, GL_COLOR_ATTACHMENT6 };
    glDrawBuffers(7, DrawBuffers);

}

由于某种原因,它可以在没有在着色器上设置布局的情况下工作,但仅适用于第一个缓冲区?我也没有错误或缺少制服。

和我的着色器:

#version 450 core
in vec2 TexCoords;
layout(location=0) out vec4 n0out;
layout(location=1) out vec4 nNnSnEout;
layout(location=2) out vec4 nWnNEnNWout;
layout(location=3) out vec4 nSEnSWout;

layout(location=4) out vec4 densityout;
layout(location=5) out vec4 velxvelyout;

layout(location=6) out vec4 speed2out;


layout(binding=0) uniform sampler2D n0;
layout(binding=1) uniform sampler2D nNnSnE;
layout(binding=2) uniform sampler2D nWnNEnNW;
layout(binding=3) uniform sampler2D nSEnSW;

layout(binding=4) uniform sampler2D density;
layout(binding=5) uniform sampler2D velxvely;

layout(binding=6) uniform sampler2D speed2;

//uniform vec2 barrier;

float viskoznost = 0.02f;
float omega = 1 / (3 * viskoznost + 0.5); //Relaksacijski cas
float stiri9ne = 4.0 / 9;
float ena9na = 1.0 / 9;
float ena36na = 1.0 / 36;


float nN0 = 0;
float nN = 0;
float nS = 0;
float nE = 0;
float nW = 0;
float nNE = 0;
float nNW = 0;
float nSE = 0;
float nSW = 0;

float ndensity = 0;
float nvelx = 0;
float nvely = 0;

float nspeed2 = 0;

void Trk(){

            if (!(gl_FragCoord.x > 300 &&  gl_FragCoord.x < 301 && gl_FragCoord.y >400 && gl_FragCoord.y < 600))
            {

                float n = nN0 + nN + nS + nE + nW + nNW + nNE + nSW + nSE;
                ndensity = n; // macroscopic density may be needed for plotting
                float vx, vy;
                float ena9naN = ena9na * n;
                float ena36naN = ena36na * n;
                if (n > 0)
                {
                    vx = (nE + nNE + nSE - nW - nNW - nSW) / n;
                }


                else vx = 0;
                nvelx = vx; // may be needed for plotting
                if (n > 0){
                    vy = (nN + nNE + nNW - nS - nSE - nSW) / n;
                }
                else vy = 0;
                nvely= vy; //needed for plotting
                float vx3 = 3 * vx;
                float vy3 = 3 * vy;
                float vx2 = vx * vx;
                float vy2 = vy * vy;
                float vxvy2 = 2 * vx * vy;
                float v2 = vx2 + vy2;
                nspeed2 = v2;       // may be needed for plotting
                float v215 = 1.5 * v2;

                nN0 += omega * (stiri9ne * n * (1 - v215) - nN0);
                nE += omega * (ena9naN * (1 + vx3 + 4.5*vx2 - v215) - nE);
                nW += omega * (ena9naN * (1 - vx3 + 4.5*vx2 - v215) - nW);
                nN += omega * (ena9naN * (1 + vy3 + 4.5*vy2 - v215) - nN);
                nS += omega * (ena9naN * (1 - vy3 + 4.5*vy2 - v215) - nS);
                nNE += omega * (ena36naN * (1 + vx3 + vy3 + 4.5*(v2 + vxvy2) - v215) - nNE);
                nNW += omega * (ena36naN * (1 - vx3 + vy3 + 4.5*(v2 - vxvy2) - v215) - nNW);
                nSE += omega * (ena36naN * (1 + vx3 - vy3 + 4.5*(v2 - vxvy2) - v215) - nSE);
                nSW += omega * (ena36naN * (1 - vx3 - vy3 + 4.5*(v2 + vxvy2) - v215) - nSW);
            }
}

void main()
{ 
nN0 = texture(n0, TexCoords).x;
nN = texture(nNnSnE, TexCoords).x;
nS = texture(nNnSnE, TexCoords).y;
nE = texture(nNnSnE, TexCoords).z;
nW = texture(nWnNEnNW, TexCoords).x;
nNE = texture(nWnNEnNW, TexCoords).y;
nNW = texture(nWnNEnNW, TexCoords).z;
nSE = texture(nSEnSW, TexCoords).x;
nSW = texture(nSEnSW, TexCoords).y;

ndensity = texture(density, TexCoords).x;
nvelx = texture(velxvely, TexCoords).x;
nvely = texture(velxvely, TexCoords).y;

nspeed2 = texture(speed2, TexCoords).x;
    Trk();
      n0out.x = nN0;
      nNnSnEout.xyz = vec3(nN,nS,nE);
      nWnNEnNWout.xyz = vec3(nW,nNE,nNW);
      nSEnSWout.xy = vec2(nSE,nSW);

      densityout.x = ndensity;
      velxvelyout.xy = vec2(nvelx, nvely);

      speed2out.x = nspeed2;
}

编辑:修复代码

1 个答案:

答案 0 :(得分:0)

几天后,我终于解决了问题,似乎我不得不使用布局(绑定)绑定输入纹理,然后使用glDrawBuffers告诉它应该将哪个纹理写入当前的FBO。 阅读FBO并不重要,我只需要调用纹理名称(用glGenTextures调用的名称),并且根本不需要指定fbo。 我已经更新了问题中的代码。