我对framebuffers非常困惑。我想要做的是使用附加了多个纹理的帧缓冲区,填充每个纹理,然后使用着色器组合(混合)所有纹理以创建新输出。听起来很容易?是的,这也是我的想法,但我不明白。
如何将当前绑定的纹理传递给着色器?
答案 0 :(得分:8)
您需要将纹理放在特定的插槽中,然后使用采样器从中读取。在您的应用中:
GLuint frameBuffer;
glGenFramebuffersEXT(1, &frameBuffer); //Create a frame buffer
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, frameBuffer); //Bind it so we draw to it
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, yourTexture, 0); //Attach yourTexture so drawing goes into it
//Draw here and it'll go into yourTexture.
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); //Unbind the framebuffer so that you can draw normally again
//Here we put the texture in slot 1.
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, yourTexture);
glActiveTexture(GL_TEXTURE0); //Don't forget to go back to GL_TEXTURE0 if you're going to do more drawing later
//Now we tell your shader where to look.
GLint var = glGetUniformLocationARB(yourShaderProgram, "yourSampler");
glUniform1i(var, 1); //We use 1 here because we used GL_TEXTURE1 to bind our texture
在片段着色器中:
uniform sampler2D yourSampler;
void main()
{
gl_FragColor = texture2D(yourSampler, whateverCoordinatesYouWant);
}
您可以将其与GL_TEXTURE2
等一起使用,以便在着色器中访问更多纹理。