Jump to content
  • Advertisement
Sign in to follow this  
jocasa

Feedback effect with FBO, textures and shaders

This topic is 2642 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi!

I am trying to program a kind of feedback loop with a FBO that has three textures attached. The "final texture" gets drawn scaled and rotated in the FBO's first texture, then without modifications in the second. This is done with fixed functionality. The last part of the loop is rendering the first and second into the third and "final texture" by means of a couple of shaders that blend them. Thus a feedback loop is created.

I cannot get this working, though. All I get is a black screen. I have tried rendering in the FBO textures without GLSL and they work OK, so it seems to me a problem of the shaders. Besides, the fragment shader blends both input textures and sets gl_FragColor accordingly. What I don't know is how to output the result in the screen. My guess is just drawing a quad. Somehow the pixels of the quad would be filled with the gl_FragColor result.

These are the shaders:


// Fragment shader

#version 110

uniform sampler2D tex_0;
uniform sampler2D tex_1;

void main()
{
vec4 color_0 = texture2D (tex_0, gl_TexCoord[0].st);
vec4 color_1 = texture2D (tex_1, gl_TexCoord[1].st);

gl_FragColor = 0.5*(color_0+color_1);
}



// Vertex shader

#version 110

void main()
{
gl_TexCoord[0] = gl_MultiTexCoord0;
gl_TexCoord[1] = gl_MultiTexCoord1;
gl_Position = gl_ProjectionMatrix*gl_Vertex;
}


And the rendering is done as follows.


void Rendering()
{
glUseProgram(0);

glBindFramebuffer (GL_FRAMEBUFFER, fbo);

// Displaying rotated and scaled final texture

glDrawBuffer (GL_COLOR_ATTACHMENT0);

glClear (GL_COLOR_BUFFER_BIT);

glLoadIdentity();

glBindTexture(GL_TEXTURE_2D, texture[2]);

glPushMatrix();
glScalef(scale, scale, 1.0);
glRotatef(angle, 0.0, 0.0, 1.0);
glColor3f(brightness, brightness, brightness);
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0); glVertex2f(-1.0, -1.0);
glTexCoord2f(1.0, 0.0); glVertex2f(1.0, -1.0);
glTexCoord2f(1.0, 1.0); glVertex2f(1.0, 1.0);
glTexCoord2f(0.0, 1.0); glVertex2f(-1.0, 1.0);
glEnd();
glPopMatrix();

glBindTexture (GL_TEXTURE_2D, 0);

// Displaying unmodified final texture

glDrawBuffer (GL_COLOR_ATTACHMENT1);

glClear (GL_COLOR_BUFFER_BIT);

glLoadIdentity();

glBindTexture(GL_TEXTURE_2D, texture[2]);

glPushMatrix();
glColor3f (1.0, 1.0, 1.0);
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0); glVertex2f(-1.0, -1.0);
glTexCoord2f(1.0, 0.0); glVertex2f(1.0, -1.0);
glTexCoord2f(1.0, 1.0); glVertex2f(1.0, 1.0);
glTexCoord2f(0.0, 1.0); glVertex2f(-1.0, 1.0);
glEnd();
glPopMatrix();

glBindTexture (GL_TEXTURE_2D, 0);

// Merging the two previous textures

glDrawBuffer (GL_COLOR_ATTACHMENT2);

glClear (GL_COLOR_BUFFER_BIT);

glLoadIdentity();

glUseProgram (program_texture);

glActiveTexture (GL_TEXTURE0);
glBindTexture (GL_TEXTURE_2D, texture[0]);

glActiveTexture (GL_TEXTURE1);
glBindTexture (GL_TEXTURE_2D, texture[1]);

glPushMatrix();
glBegin(GL_QUADS);
glVertex2f(-1.0, -1.0);
glVertex2f(1.0, -1.0);
glVertex2f(1.0, 1.0);
glVertex2f(-1.0, 1.0);
glEnd();
glPopMatrix();

glUseProgram(0);

DrawLine(0.0);

glBindFramebuffer (GL_FRAMEBUFFER, 0);

// Display in screen the final texture

glClear (GL_COLOR_BUFFER_BIT);

glLoadIdentity();

glBindTexture (GL_TEXTURE_2D, texture[2]);

glPushMatrix();
glColor3f (1.0, 1.0, 1.0);
glBegin(GL_QUADS);
glTexCoord2f (0.0, 0.0); glVertex2f (-1.0, -1.0);
glTexCoord2f (1.0, 0.0); glVertex2f (1.0, -1.0);
glTexCoord2f (1.0, 1.0); glVertex2f (1.0, 1.0);
glTexCoord2f (0.0, 1.0); glVertex2f (-1.0, 1.0);
glEnd();
glPopMatrix();

glBindTexture (GL_TEXTURE_2D, 0);

glfwSwapBuffers();
}


The GLSL program, textures and FBO are initialized in the following function.


void Initialization()
{
program_texture = LoadShader ("texture.vert", "texture.frag");
printProgramInfoLog (program_texture);

loc_tex[0] = glGetUniformLocation (program_texture, "tex_0");
loc_tex[1] = glGetUniformLocation (program_texture, "tex_1");

glUniform1i (loc_tex[0], 0);
glUniform1i (loc_tex[1], 1);

glEnable(GL_TEXTURE_2D);
glGenTextures(1, &texture[0]);
glBindTexture(GL_TEXTURE_2D, texture[0]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_FLOAT, NULL);

glGenTextures(1, &texture[1]);
glBindTexture(GL_TEXTURE_2D, texture[1]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_FLOAT, NULL);

glGenTextures(1, &texture[2]);
glBindTexture(GL_TEXTURE_2D, texture[2]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_FLOAT, NULL);

glBindTexture (GL_TEXTURE_2D, 0);

glGenFramebuffers(1, &fbo);
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture[0], 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, texture[1], 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2, GL_TEXTURE_2D, texture[2], 0);

glBindFramebuffer(GL_FRAMEBUFFER, 0);
}


In the rendering section there is a call to DrawLine(angle), which simply draws a line rotated by "angle" degrees. Rendering something is necessary to see some feedback effect, otherwise nothing would be iterated nor drawn.

I would appreciate any comment and help to make this code function properly.

Thanks!

Share this post


Link to post
Share on other sites
Advertisement
Hi again.

I've try simplifying the code to see if I could find the error. But nothing new happens. Could it be that I should include some glTexEnv command?


void Rendering()
{
glUseProgram(0);

glBindFramebuffer (GL_FRAMEBUFFER, fbo);

// Displaying horizontal line

glDrawBuffer (GL_COLOR_ATTACHMENT0);

glClear (GL_COLOR_BUFFER_BIT);

glLoadIdentity();

DrawLine(90.0);

// Displaying vertical line

glDrawBuffer (GL_COLOR_ATTACHMENT1);

glClear (GL_COLOR_BUFFER_BIT);

glLoadIdentity();

DrawLine(180.0);

// Merging the two previous textures

glDrawBuffer (GL_COLOR_ATTACHMENT2);

glClear (GL_COLOR_BUFFER_BIT);

glLoadIdentity();

glUseProgram (program_texture);

glActiveTexture (GL_TEXTURE0);
glBindTexture (GL_TEXTURE_2D, texture[0]);

glActiveTexture (GL_TEXTURE1);
glBindTexture (GL_TEXTURE_2D, texture[1]);

glPushMatrix();
glBegin(GL_QUADS);
glMultiTexCoord2f (GL_TEXTURE0, 0.0, 0.0); glMultiTexCoord2f (GL_TEXTURE1, 0.0, 0.0); glVertex2f(-1.0, -1.0);
glMultiTexCoord2f (GL_TEXTURE0, 1.0, 0.0); glMultiTexCoord2f (GL_TEXTURE1, 1.0, 0.0); glVertex2f(1.0, -1.0);
glMultiTexCoord2f (GL_TEXTURE0, 1.0, 1.0); glMultiTexCoord2f (GL_TEXTURE1, 1.0, 1.0); glVertex2f(1.0, 1.0);
glMultiTexCoord2f (GL_TEXTURE0, 0.0, 1.0); glMultiTexCoord2f (GL_TEXTURE1, 0.0, 1.0); glVertex2f(-1.0, 1.0);
glEnd();
glPopMatrix();

glBindFramebuffer (GL_FRAMEBUFFER, 0);

// Display in screen the final texture

glUseProgram(0);

glClear (GL_COLOR_BUFFER_BIT);

glLoadIdentity();

glBindTexture (GL_TEXTURE_2D, texture[2]);

glPushMatrix();
glColor3f (1.0, 1.0, 1.0);
glBegin(GL_QUADS);
glTexCoord2f (0.0, 0.0); glVertex2f (-1.0, -1.0);
glTexCoord2f (1.0, 0.0); glVertex2f (1.0, -1.0);
glTexCoord2f (1.0, 1.0); glVertex2f (1.0, 1.0);
glTexCoord2f (0.0, 1.0); glVertex2f (-1.0, 1.0);
glEnd();
glPopMatrix();

glBindTexture (GL_TEXTURE_2D, 0);

glfwSwapBuffers();
}


As far as I know the shaders, which are the same as in the previous post, are working as expected. And the FBO with its textures are performing also OK.

Share this post


Link to post
Share on other sites
Just in case someone is interested in this: I have solved the issue. Just had to unbind the textures after they were done with, as follows:


glActiveTexture (GL_TEXTURE0);
glBindTexture (GL_TEXTURE_2D, 0);

glActiveTexture (GL_TEXTURE1);
glBindTexture (GL_TEXTURE_2D, 0);

glActiveTexture (GL_TEXTURE0);


Bye!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!