帧缓冲区和在opengl中使用着色器 [英] framebuffer and using shaders in opengl

查看:162
本文介绍了帧缓冲区和在opengl中使用着色器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对帧缓冲区有些困惑.我想做的是使用带有多个纹理的帧缓冲区,填充每个纹理,然后使用着色器组合(混合)所有纹理以创建新的输出.听起来容易吗?是的,我也是这么想的,但我听不懂.

I'm quite a bit confused about framebuffers. What I want to do is using a framebuffer with multiple textures attached, fill every texture and then use a shader to combine (blend) all textures to create a new output. Sounds easy? yeah that's what I thought too, but I don't understand it.

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

How can I pass the currently binded texture to a shader?

推荐答案

您需要的是将纹理放在特定的插槽中,然后使用采样器读取纹理.在您的应用中:

What you need is to put the texture in a specific slot, then use a sampler to read from it. In your app:

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一起使用,以此类推,以在着色器中访问更多纹理.

You can use this with GL_TEXTURE2 and so on to access even more textures in your shader.

这篇关于帧缓冲区和在opengl中使用着色器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆