1
\$\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\$

2 Answers 2

1
\$\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);
\$\endgroup\$
1
  • \$\begingroup\$ I want to thank you from the bottom of my heart for this explanation! I've used this to flip a texture in lwjgl and it worked like a charm. \$\endgroup\$ Commented Mar 11, 2021 at 8:58
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.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .