0
\$\begingroup\$

How do I flip a texture in modern OpenGL (3.1+)?

This is the unflipped render, keep in mind it is drawn with 8x8 tiles from a texture atlas, so I edited green squares below him into the image to represent it. Rendered without horizontal flip:

rendered without h flip

I have tried doing 1-texCoord.x in my vec2 texCoord in my vertex shader. It gave me... this result:

rendered with attempted h flip

This is how texture atlas is laid out, if it matters:

enter image description here

Attempting other methods such as inverting texture coordinates of the VBO gave me similarly garbled results. What should I actually be doing, to flip the texture, or more accurately each individual tile? I know I would also have to change each tile's coordinates, but that's for after.

\$\endgroup\$
0
\$\begingroup\$

If your texture coords normally go from x0 to x1 then any given coordinate x between those values can be flipped by doing this:

flippedX = x0 + (x1 - x);
| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

In modern OpenGL and for your specific use case - equally sized tiles in a texture atlas - you have the option of not actually using a texture atlas at all.

Instead you would use a texture array, meaning that your texcoords for each tile are in the 0..1 range and flipping becomes trivial. You also gain the advantages of having wrap modes, clamp modes and mipmapping work as expected.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.