1

好像如果我使用texture(texture0,textCoord)一次然后我尝试再次使用texture(texture1,textCoord);它返回的另一个纹理(0,0,0,0);我只是不知道为什么。

在我的程序中,它似乎是一个工作顺序,如果深度比其他工作不工作,如果我不使用深度比颜色工作等等。(深度,颜色,正常,位置,光泽是顺序)

this is the fragment shader :

uniform sampler2D tex0;
uniform sampler2D tex1;
uniform sampler2D tex2;
uniform sampler2D tex3;
uniform sampler2D depht;

void main()
{
    float UNIT = 1./640. ;

    //vec4 depht = texture(depht,gl_TexCoord[0].st);
    vec4 color = texture(tex0,gl_TexCoord[0].st);
    vec4 norma = texture(tex1,gl_TexCoord[0].st);
    vec4 posit = texture(tex2,gl_TexCoord[0].st);
    vec4 shine = texture(tex3,gl_TexCoord[0].st);


    if(false){
            gl_FragColor=(  texture2D(tex0,gl_TexCoord[0].st +vec2(0,0))+
                            texture2D(tex0,gl_TexCoord[0].st +vec2(0,-UNIT))+
                            texture2D(tex0,gl_TexCoord[0].st +vec2(UNIT,-UNIT))+
                            texture2D(tex0,gl_TexCoord[0].st +vec2(UNIT,0))+
                            texture2D(tex0,gl_TexCoord[0].st +vec2(UNIT,UNIT))+
                            texture2D(tex0,gl_TexCoord[0].st +vec2(0,UNIT))+
                            texture2D(tex0,gl_TexCoord[0].st +vec2(-UNIT,UNIT))+
                            texture2D(tex0,gl_TexCoord[0].st +vec2(-UNIT,0))+
                            texture2D(tex0,gl_TexCoord[0].st +vec2(-UNIT,-UNIT)))/9.;
    }else{
        gl_FragColor=(norma+color);
    }
}

这是绑定部分:

    fbotest.drawFBO();
DEFERRED.UseNow();
DrawGLScene();

fbotest.readFBO();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);

DEFERRED2.UseNow();

//glBlendFunc (GL_ONE, GL_ONE);
glEnable( GL_BLEND );

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, fbotest.Color);
glEnable(GL_TEXTURE_2D);

glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, fbotest.Normal);
glEnable(GL_TEXTURE_2D);

glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, fbotest.Pos);
glEnable(GL_TEXTURE_2D);

glActiveTexture(GL_TEXTURE3);
glBindTexture(GL_TEXTURE_2D, fbotest.Shine);
glEnable(GL_TEXTURE_2D);

glActiveTexture(GL_TEXTURE4);
glBindTexture(GL_TEXTURE_2D, fbotest.Depht);
glEnable(GL_TEXTURE_2D);

glUniform1i(glGetUniformLocation(DEFERRED2.PR,"tex0"),0);
glUniform1i(glGetUniformLocation(DEFERRED2.PR,"tex1"),1);
glUniform1i(glGetUniformLocation(DEFERRED2.PR,"tex2"),2);
glUniform1i(glGetUniformLocation(DEFERRED2.PR,"tex3"),3);
glUniform1i(glGetUniformLocation(DEFERRED2.PR,"depht"),4);

glLoadIdentity();
glBegin(GL_QUADS);
        glMultiTexCoord2f(GL_TEXTURE0, 1.0, 0.0);
        glMultiTexCoord2f(GL_TEXTURE1, 1.0, 0.0);
        glMultiTexCoord2f(GL_TEXTURE2, 1.0, 0.0);
        glMultiTexCoord2f(GL_TEXTURE3, 1.0, 0.0);
        glMultiTexCoord2f(GL_TEXTURE4, 1.0, 0.0);
    glVertex3f( 1,-1,-3);
        glMultiTexCoord2f(GL_TEXTURE0, 1.0, 1.0);
        glMultiTexCoord2f(GL_TEXTURE1, 1.0, 1.0);
        glMultiTexCoord2f(GL_TEXTURE2, 1.0, 1.0);
        glMultiTexCoord2f(GL_TEXTURE3, 1.0, 1.0);
        glMultiTexCoord2f(GL_TEXTURE4, 1.0, 1.0);
    glVertex3f( 1,1 ,-3);
        glMultiTexCoord2f(GL_TEXTURE0, 0.0, 1.0);
        glMultiTexCoord2f(GL_TEXTURE1, 0.0, 1.0);
        glMultiTexCoord2f(GL_TEXTURE2, 0.0, 1.0);
        glMultiTexCoord2f(GL_TEXTURE3, 0.0, 1.0);
        glMultiTexCoord2f(GL_TEXTURE4, 0.0, 1.0);
    glVertex3f( -1,1,-3);
        glMultiTexCoord2f(GL_TEXTURE0, 0.0, 0.0);
        glMultiTexCoord2f(GL_TEXTURE1, 0.0, 0.0);
        glMultiTexCoord2f(GL_TEXTURE2, 0.0, 0.0);
        glMultiTexCoord2f(GL_TEXTURE3, 0.0, 0.0);
        glMultiTexCoord2f(GL_TEXTURE4, 0.0, 0.0);
    glVertex3f(-1,-1,-3);
glEnd();

glActiveTextureARB(GL_TEXTURE4);
    glBindTexture(GL_TEXTURE_2D, 0);
glActiveTextureARB(GL_TEXTURE3);
    glBindTexture(GL_TEXTURE_2D, 0);
glActiveTextureARB(GL_TEXTURE2);
    glBindTexture(GL_TEXTURE_2D, 0);
glActiveTextureARB(GL_TEXTURE1);
    glBindTexture(GL_TEXTURE_2D, 0);
glActiveTextureARB(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, 0);
glDisable( GL_BLEND );

这可能是 FBO 类:

    class deferred{
        public :
    unsigned int FBO;
    unsigned int Depht;
    unsigned int Color;
    unsigned int Normal;
    unsigned int Pos;
    unsigned int Shine;

    deferred();
    void initFBO();
    void readFBO();
    void drawFBO();

};

    void deferred::readFBO(){
        glBindFramebuffer(GL_DRAW_FRAMEBUFFER,0);
        glBindFramebuffer(GL_READ_FRAMEBUFFER,FBO);
    }

    void deferred::drawFBO(){
        glBindFramebuffer(GL_READ_FRAMEBUFFER,0);
        glBindFramebuffer(GL_DRAW_FRAMEBUFFER,FBO);
    }

    deferred::deferred(){
        FBO     =0;
        Depht   =0;
        Color   =0;
        Normal  =0;
        Pos     =0;
        Shine   =0;
    }

    void deferred::initFBO(){
        glGenFramebuffers(1, &FBO);
        glBindFramebuffer(GL_DRAW_FRAMEBUFFER, FBO);


        glGenTextures(1,&Depht );
        glGenTextures(1,&Color );
        glGenTextures(1,&Normal);
        glGenTextures(1,&Pos   );
        glGenTextures(1,&Shine );

        int WindowWidth = Ww;
        int WindowHeight= Wh;

        glBindTexture(GL_TEXTURE_2D, Color);
        glTexImage2D (GL_TEXTURE_2D, 0, GL_RGB32F, WindowWidth, WindowHeight, 0, GL_RGB, GL_FLOAT, NULL);
        glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, Color, 0);

        glBindTexture(GL_TEXTURE_2D, Normal);
        glTexImage2D (GL_TEXTURE_2D, 0, GL_RGB32F, WindowWidth, WindowHeight, 0, GL_RGB, GL_FLOAT, NULL);
        glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, Normal, 0);

        glBindTexture(GL_TEXTURE_2D, Pos);
        glTexImage2D (GL_TEXTURE_2D, 0, GL_RGB32F, WindowWidth, WindowHeight, 0, GL_RGB, GL_FLOAT, NULL);
        glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT2, GL_TEXTURE_2D, Pos, 0);

        glBindTexture(GL_TEXTURE_2D, Shine);
        glTexImage2D (GL_TEXTURE_2D, 0, GL_RGB32F, WindowWidth, WindowHeight, 0, GL_RGB, GL_FLOAT, NULL);
        glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT3, GL_TEXTURE_2D, Shine, 0);

        glBindTexture(GL_TEXTURE_2D, Depht);
        glTexImage2D (GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32F, WindowWidth, WindowHeight, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
        glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, Depht, 0);

        GLenum DrawBuffers[] = { GL_COLOR_ATTACHMENT0,
                                 GL_COLOR_ATTACHMENT1,
                                 GL_COLOR_ATTACHMENT2,
                                 GL_COLOR_ATTACHMENT3};

        glDrawBuffers(4, DrawBuffers);

        GLenum Status = glCheckFramebufferStatus(GL_FRAMEBUFFER);

        if (Status != GL_FRAMEBUFFER_COMPLETE) {
            cout <<"Frame Buffer ERROR : "<< Status << "\n" ;
        }

        glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
    }
4

2 回答 2

0
glEnable(GL_TEXTURE_2D);

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, 0);
glUniform1i(glGetUniformLocation(DEFERRED2.PR,"tex0"),0);

尝试将这些更改为如下所示。绑定时应启用 GL_TEXTURE_2D。此外,glBindTexture 肩部末尾的 ID 会上升 - 0 1 2 3 4 5。我不确定您的值返回了什么。然后每次使用 glUinform1i 发送该数据,它将绑定在 0 处的纹理。

于 2013-01-26T17:21:45.870 回答
0

我改变了这个:

void deferred::readFBO(){
    glBindFramebuffer(GL_DRAW_FRAMEBUFFER,0);
    glBindFramebuffer(GL_READ_FRAMEBUFFER,FBO);

}

void deferred::drawFBO(){
    glBindFramebuffer(GL_READ_FRAMEBUFFER,0);
    glBindFramebuffer(GL_DRAW_FRAMEBUFFER,FBO);
}

有了这个 :

> void deferred::readFBO(){
>     glBindFramebuffer(GL_DRAW_FRAMEBUFFER,0);
>     glBindFramebuffer(GL_READ_FRAMEBUFFER,FBO);
> 
>     glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
>     glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, 0, 0);
>     glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT2, GL_TEXTURE_2D, 0, 0);
>     glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT3, GL_TEXTURE_2D, 0, 0);
>     glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT , GL_TEXTURE_2D, 0, 0); 
}
> 
> void deferred::drawFBO(){
>     glBindFramebuffer(GL_READ_FRAMEBUFFER,0);
>     glBindFramebuffer(GL_DRAW_FRAMEBUFFER,FBO);
> 
>     glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, Color , 0);
>     glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, Normal, 0);
>     glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT2, GL_TEXTURE_2D, Pos   , 0);
>     glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT3, GL_TEXTURE_2D, Shine , 0);
>     glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT , GL_TEXTURE_2D, Depht , 0); 
}

现在它可以工作了。我相信即使我没有看到帧速率的任何变化,它也会有点慢,而且我不知道为什么我不能让纹理绑定到帧缓冲区,但是是的,它解决了问题。

于 2013-02-01T12:00:19.143 回答