1

我想在 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();
4

1 回答 1

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。

于 2013-10-17T16:45:42.893 回答