2011-09-09 21 views
5

Estoy bastante confundido acerca de framebuffers. Lo que quiero hacer es usar un framebuffer con múltiples texturas adjuntas, llenar cada textura y luego usar un shader para combinar (mezclar) todas las texturas para crear una nueva salida. ¿Suena fácil? Sí, eso es lo que yo también pensé, pero no lo entiendo.framebuffer y uso de sombreadores en opengl

¿Cómo puedo pasar la textura actualmente encuadernada a un sombreado?

Respuesta

8

Lo que necesita es poner la textura en una ranura específica, luego use una muestra para leer de ella. En su aplicación:

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 

Y en su fragment shader:

uniform sampler2D yourSampler; 

void main() 
{ 
    gl_FragColor = texture2D(yourSampler, whateverCoordinatesYouWant); 
} 

Puede usar esto con GL_TEXTURE2 y así sucesivamente para acceder a más texturas en su sombreado.

+0

y más, tendría que seleccionar el drawbuffer, ¿verdad? teniendo algo así como: glDrawBuffer (GL_COLOR_ATTACHMENT0_EXT); \t glActiveTexture (GL_TEXTURE1); \t glBindTexture (GL_TEXTURE_2D, yourTexture); // dibuja GLint var = glGetUniformLocationARB (yourShaderProgram, "tuMuestra)"; glUniform1i (var, 1); // Usamos 1 aquí porque usamos GL_TEXTURE1 para unir nuestra textura – Donny

+3

@Donny Estás mezclando dos cosas completamente diferentes (es decir, renderizar en un framebuffer y leer desde una textura previamente renderizada). –

+0

Lo que estoy tratando de hacer es renderizar a un framebuffer, tomar ese resultado en un shader. La idea es que así puedo generar muchas texturas diferentes generadas en el framebuffer. Como resultado, quiero mezclar todas estas texturas. ¿Esto es posible? – Donny

Cuestiones relacionadas