Michael IV
Michael IV

Reputation: 11424

Flipping texture when copying to another texture

I need to flip my texture vertically when copying it into another texture.I know about 3 simple ways to do it:

1 . Blit from once FBO into another using full screen quad (and flip in frag shader)

2 . Blit using glBlitFrameBuffer.

3 . Using glCopyImageSubData

I need to perform this copy between 2 textures which aren't attached to any FBO so I am trying to avoid first 2 solutions.I am trying the third one.

Doing it like this:

glCopyImageSubData(srcTex ,GL_TEXTURE_2D,0,0,0,0,targetTex,GL_TEXTURE_2D,0,0,width ,0,height,0,1);

It doesn't work.The copy returns garbage.Is this method supposed to be able to flip when reading?Is there an alternative FBO unrelated method(GPU side only)?

Btw:

glCopyTexSubImage2D(GL_TEXTURE_2D,0,0,0,0,height ,width,0 );

Doesn't work too.

Upvotes: 3

Views: 1613

Answers (1)

Jean-Simon Brochu
Jean-Simon Brochu

Reputation: 950

Rendering a textured quad to a pbo by drawing the inverted quad would work.

Or you could go with a simple fragment shader doing a imageLoad + imageStore by inverting the y coordinate with 2 bound image buffers.

glBindImageTexture(0, copyFrom, 0, GL_FALSE, 0, GL_READ_ONLY, GL_RGBAUI32);        
glBindImageTexture(1, copyTo, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_RGBAUI32);

the shader would look something like:

layout(binding = 0, rbga32ui) uniform uimage2d input_buffer;
layout(binding = 1, rbga32ui) uniform uimage2d output_buffer;
uniform float u_texHeight;
void main(void)
{
    vec4 color = imageLoad( input_buffer, ivec2(gl_FragCoord.xy) );
    imageStore( output_buffer, ivec2(gl_FragCoord.x,u_texHeight-gl_FragCoord.y-1), color );
}

You'll have to tweak it a little, but I know it works I used it before.

Hope this helps

Upvotes: 3

Related Questions