使用fbo时纹理翻转

时间:2014-02-22 18:01:33

标签: c++ opengl render-to-texture

我有2个纹理(空,带数据),我将空连接到fbo,然后使用绑定填充纹理并在着色器中处理它并将输出绘制到fbo。然后我在fbo中绘制纹理以显示不适。它工作正常,但纹理翻转每一帧如下:flipped texture

我知道它与纹理坐标有关,但我该如何解决呢?

编辑:渲染功能

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);

glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
glFramebufferTexture(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, n_box_texture[1 - n_source_tex], 0);


glViewport(0, 0, datawidth, dataheight);
glClear ( GL_COLOR_BUFFER_BIT);
glBindTexture(GL_TEXTURE_2D, n_box_texture[n_source_tex]);

glUseProgram(n_program_object);
{
    glUniform1i(n_box_sampler_uniform, 0);
}

DrawScene();

glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
glViewport(0, 0, n_width, n_height);

glBindTexture(GL_TEXTURE_2D, n_box_texture[1 - n_source_tex]);
glGenerateMipmap(GL_TEXTURE_2D);

n_source_tex = 1 - n_source_tex;

glClear(GL_COLOR_BUFFER_BIT);

Matrix4f t_mvp;
{
    Matrix4f t_projection;
    CGLTransform::Perspective(t_projection, 90, float(n_width) / n_height, .01f, 1000);

    Matrix4f t_modelview;
    t_modelview.Identity();
    t_modelview.Translate(0, 0, -2.5f);

    t_mvp = t_projection * t_modelview;
}

glUseProgram(n_program_object_pass); 
{
    glUniformMatrix4fv(n_mvp_matrix_uniform_pass, 1, GL_FALSE, &t_mvp[0][0]);
    glUniform1i(n_box_sampler_uniform_pass, 0);
}

DrawScene();

driver.Blit();

编辑:着色器

处理着色器:

#version 330
in vec2 v_tex;
in vec3 v_pos;
out vec2 v_texcoord;

void main()
{
gl_Position = vec4(v_pos, 1.0);
v_texcoord = v_tex;
}


#version 330
in vec2 v_texcoord;
out vec4 frag_color;
uniform sampler2D n_box_tex;

void main()
{
float temp;
temp =  texture(n_box_tex, v_texcoord).r + 
    texture(n_box_tex, v_texcoord+vec2(0,0.01)).r + 
    texture(n_box_tex, v_texcoord+vec2(0.01,0.01)).r + 
    texture(n_box_tex, v_texcoord+vec2(0.01,0)).r;
temp = temp/4.0;
frag_color = temp * vec4(1.0, 1.0, 1.0, 1.0);
}

将纹理传递给输出的着色器:

#version 330
in vec2 v_tex;
in vec3 v_pos;
uniform mat4 t_modelview_projection_matrix;
out vec2 v_texcoord;

void main()
{
gl_Position = t_modelview_projection_matrix * vec4(v_pos, 1.0); /
v_texcoord = v_tex;
}


#version 330
in vec2 v_texcoord;
out vec4 frag_color;
uniform sampler2D n_box_tex;

void main()
{
frag_color = texture(n_box_tex, v_texcoord).r * vec4(0.4, 1.0, 0.8, 1.0);
}

1 个答案:

答案 0 :(得分:0)

修正了它!它非常简单,它的纹理坐标错误。

4个顶点坐标:

  • -1 -1
  • 1 -1
  • 1 1
  • -1 1

和相应的纹理坐标:

  • 0 0
  • 1 0
  • 1 1
  • 0 1

我有:

  • 0 1
  • 1 1
  • 1 0
  • 0 0