Question

I have 2 textures (empty, with data), I attach empty to fbo, then use bind filled texture and process it in shaders and draw output to fbo. Then I draw texture in fbo to displey. It works fine, however texture flips every frame like this:flipped texture

I know it has something to do with texture coordinates, but how can I fix it?

Edit: Render function

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();

Edit: Shaders

Processing shaders:

#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);
}

Shaders for passing texture to output:

#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);
}
Was it helpful?

Solution

Fixed it! It was very simple, it had my texture coordinates wrong.

4 vertex coordinates:

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

and coresponding texture coordinates:

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

I had:

  • 0 1
  • 1 1
  • 1 0
  • 0 0
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top