gpt4 book ai didi

opengl - 帧缓冲区和在 opengl 中使用着色器

转载 作者:行者123 更新时间:2023-12-02 05:37:50 24 4
gpt4 key购买 nike

我对帧缓冲区有点困惑。我想要做的是使用附加了多个纹理的帧缓冲区,填充每个纹理,然后使用着色器组合(混合)所有纹理以创建新的输出。听起来很容易?是的,我也是这么想的,但我不明白。

如何将当前绑定(bind)的纹理传递给着色器?

最佳答案

您需要的是将纹理放入特定的槽中,然后使用采样器从中读取。在您的应用程序中:

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 等一起使用,以访问着色器中的更多纹理。

关于opengl - 帧缓冲区和在 opengl 中使用着色器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7357626/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com