使用帧缓冲器进行高斯模糊2次传递

时间:2013-10-15 20:34:26

标签: c++ opengl glsl blur framebuffer

我想在256x256纹理中应用高斯模糊但是我在将第一遍传递给帧缓冲区时遇到问题并且重新使用它会导致图像的一部分,这看起来是纹理变换。

引起注意

顶点着色器:

void main()
{
   gl_Position = ftransform();
   gl_TexCoord[0] = gl_MultiTexCoord0;
} 

片段着色器: 我删除了模糊的所有部分仅用于测试。

uniform sampler2D texture_0; 
uniform float switcher;
vec4 cor;

void main()
{
   cor = vec4(0.0,0.0,0.0,0.0);
   vec2 position = gl_TexCoord[0].xy;

   cor = texture2D(texture_0,position);

   gl_FragColor = cor;
}

初始化:

glGenTextures( 1, &tex1 );
glBindTexture( GL_TEXTURE_2D, tex1 );
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img1->getWidth(),img1->getHeight(), 0,GL_RGB,     GL_UNSIGNED_BYTE, data1);
glBindTexture(GL_TEXTURE_2D, 0);


glGenTextures( 1, &tex2 );
glBindTexture( GL_TEXTURE_2D, tex2 );
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img1->getWidth(),img1->getHeight(), 0,GL_RGB,  GL_UNSIGNED_BYTE, 0);
glBindTexture(GL_TEXTURE_2D, 0);

glEnable(GL_TEXTURE_2D);

glActiveTexture(GL_TEXTURE0);
glBindTexture( GL_TEXTURE_2D, tex1 );
glActiveTexture(GL_TEXTURE1);
glBindTexture( GL_TEXTURE_2D, tex2 );

glGenFramebuffers(1,&framebuffer);
glBindFramebuffer(GL_FRAMEBUFFER,framebuffer);
glFramebufferTexture2D(GL_FRAMEBUFFER,GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tex2,0);
glBindFramebuffer(GL_FRAMEBUFFER, 0);

渲染:

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glLoadIdentity();

glUniform1i(loc_u_texture_0, 0);
glUniform1f(loc_switcher_1, 0);

glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);

glNormal3f(0, 1, 0);
glBegin(GL_QUADS);
  glTexCoord2f(0, 0);
  glVertex3f(-2, -2,  -10);
  glTexCoord2f(1, 0);
  glVertex3f(2, -2,   -10);
  glTexCoord2f(1, 1);
  glVertex3f(2, 2,  -10);
  glTexCoord2f(0, 1);
  glVertex3f(-2, 2, -10);
glEnd();

glBindFramebuffer(GL_FRAMEBUFFER, 0);
usleep(50);

glUniform1i(loc_u_texture_0, 1);
glUniform1f(loc_switcher_1, 1);

glNormal3f(0, 1, 0);
glBegin(GL_QUADS);

  glTexCoord2f(0, 0);
  glVertex3f(-2, -2,  -10);
  glTexCoord2f(1, 0);
  glVertex3f(2, -2,   -10);
  glTexCoord2f(1, 1);
  glVertex3f(2, 2,  -10);
  glTexCoord2f(0, 1);
  glVertex3f(-2, 2, -10);
glEnd();

glutSwapBuffers();

1 个答案:

答案 0 :(得分:0)

使用以下行绘制四边形:

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(-1.0f, -1.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(1.0f, -1.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(1.0f, 1.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, 0.0f);
glEnd();

当您重置投影和模型视图矩阵时,基元不会被变换,您可以直接在剪切空间中绘制对象(这是一个带角(-1,-1,-1)和(1,1)的立方体,1))。

另外,请勿使用以下内容:

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

如果您不打算同时为每个着色器使用多个输入采样器,则只需要第一个纹理通道GL_TEXTURE0,默认设置为。