views:

488

answers:

3

Since I've started learning about rendering to a texture I grew to understand that glCopyTexSubImage2D() will copy the designated portion of the screen upside down. I tried a couple of simple things that came to mind in order to prevent/work around this but couldn't find an elegant solution.

  • there are two problems with doing a ::glScalef(1.0f, -1.0f, 1.0f) before rendering the texture to the screen: 1, I have to do this every time I'm using the texture. 2, I'm mostly working with 2D graphics and have backface culling turned off for GL_BACKsides. As much as possible I'd love to save switching this on and off.

  • tried switching matrix mode to GL_TEXTURE and doing the ::glScalef(1.0f, -1.0f, 1.0f) transformation on capturing, but the results were the same. (I'm guessing the texture matrix only has an effect on glTexCoord calls?)

So, how can I fix the up-down directions of textures captured with glCopyTexSubImage2D?

A: 

If my understanding is correct, it depends on where your origin is. That function seems to assume your origin is in the bottom-left, whereas most 2D stuff assumes an origin of the top-left, which is why it seems upside down.

I suppose you could change the origin to the bottom-left, do the capture, then change the origin back to the top-left and render again before doing the swap. But that's a horrible solution since you're effectively rendering twice, but it might be fine if you don't plan to do it every frame.

Sydius
+1  A: 

What are you going to be using the texture images for? Actually trying to render them upside down would usually take more work than moving that code somewhere else.

If you're trying to use the image without exporting it, just flipping the texture coordinates wherever you're using the result would be the most efficient way.

If you're trying to export it, then you either want to flip them yourself, after rendering.

On a related note, if you are making a 2D game, why is backface culling turned on?

Andrei Krotkov
Good point. It was a case of guesswork optimization, I'm afraid.=) Also, I didn't need to flip textures for this game - not until now, anyhow. I have still to see how much slower is it going to be, but in the meantime, turning backface culling off and flipping does seem to be the easiest and most elegant solution. Thank you!
iCE-9
A: 

Use glReadPixels() to copy to a buffer, flip the image, then use glTexImage2D() to write it back.

sean riley