OpenGL:使用多个纹理渲染到FBO

时间:2010-10-10 22:27:09

标签: c++ opengl rendering glsl fbo

我正在尝试使用渲染器。我想要的是将颜色缓冲区和普通缓冲区写入两个单独的纹理。我找到了那个部分。

然而,颜色缓冲区应该是两个纹理的组合。这应该可以解决问题:

glActiveTexture(GL_TEXTURE0_ARB);
glEnable(GL_TEXTURE_2D);
g_Tex->Bind();

glActiveTexture(GL_TEXTURE1_ARB);
glEnable(GL_TEXTURE_2D);
g_TexNormal->Bind();

    g_Shader->Enable();
        RenderScene();
    g_Shader->Disable();

glActiveTexture(GL_TEXTURE1_ARB);
glDisable(GL_TEXTURE_2D);

glActiveTexture(GL_TEXTURE0_ARB);
glDisable(GL_TEXTURE_2D);

这是片段着色器:(GLSL)

uniform sampler2D tex_diffuse;
uniform sampler2D tex_normal;

void main()
{
    gl_FragColor = texture2D(tex_diffuse, gl_TexCoord[0].st);
    //gl_FragColor = texture2D(tex_normal, gl_TexCoord[0].st);
}   

然而,第二个纹理与第一个相同!无论哪个纹理附加到GL_TEXTURE0,都是用于两个采样器的纹理。

初始化FBO:

glGenFramebuffersEXT(1, &g_FBOColor);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, g_FBOColor);

glGenTextures(1, &g_FBOTexColor);
glBindTexture(GL_TEXTURE_2D, g_FBOTexColor);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
glBindTexture(GL_TEXTURE_2D, 0);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, g_FBOTexColor, 0);

glGenTextures(1, &g_FBOTexNormal);
glBindTexture(GL_TEXTURE_2D, g_FBOTexNormal);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
glBindTexture(GL_TEXTURE_2D, 0);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT1_EXT, GL_TEXTURE_2D, g_FBOTexNormal, 0);

glGenTextures(1, &g_FBOTexDepth);
glBindTexture(GL_TEXTURE_2D, g_FBOTexDepth);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, w, h, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL);
glBindTexture(GL_TEXTURE_2D, 0);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, g_FBOTexDepth, 0);

glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);

完整渲染部分:

glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, g_FBOColor);
glPushAttrib(GL_VIEWPORT_BIT | GL_COLOR_BUFFER_BIT);

glViewport(
    0, 0,
    Window::GetSingleton()->GetWidth(), Window::GetSingleton()->GetHeight()
);

GLenum buffers[] = { GL_COLOR_ATTACHMENT0_EXT, GL_COLOR_ATTACHMENT1_EXT };
glDrawBuffers(2, buffers);
//glReadBuffer(GL_COLOR_ATTACHMENT0_EXT | GL_COLOR_ATTACHMENT1_EXT);

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glActiveTexture(GL_TEXTURE0_ARB);
glEnable(GL_TEXTURE_2D);
g_Tex->Bind();

glActiveTexture(GL_TEXTURE1_ARB);
glEnable(GL_TEXTURE_2D);
g_TexNormal->Bind();

    g_Shader->Enable();
        RenderScene();
    g_Shader->Disable();

glActiveTexture(GL_TEXTURE1_ARB);
glDisable(GL_TEXTURE_2D);

glActiveTexture(GL_TEXTURE0_ARB);
glDisable(GL_TEXTURE_2D);

glPopAttrib();
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);

提前致谢。

1 个答案:

答案 0 :(得分:2)

好吧我明白了。 :)

我的纹理不起作用的原因是因为我没有设置统一的位置。固定代码:

g_Shader->Enable();

    glActiveTexture(GL_TEXTURE0_ARB);
    glEnable(GL_TEXTURE_2D);
    g_Tex->Bind();
    glUniform1i(glGetUniformLocation(g_Shader->GetProgram(), "tex_diffuse"), 0);

    glActiveTexture(GL_TEXTURE1_ARB);
    glEnable(GL_TEXTURE_2D);
    g_TexNormal->Bind();
    glUniform1i(glGetUniformLocation(g_Shader->GetProgram(), "tex_normal"), 1);

        RenderScene();

    glActiveTexture(GL_TEXTURE1_ARB);
    glDisable(GL_TEXTURE_2D);

    glActiveTexture(GL_TEXTURE0_ARB);
    glDisable(GL_TEXTURE_2D);

g_Shader->Disable();