问题
I've been trying to resolve this issue I have been having with displaying a texture correctly on my libgdx desktop program.
I have an Orthographic camera with which when I set as:
camera.setOrtho(false);
I get this image:
And when I set it as:
camera.setOrtho(true);
I get this image:
The red image is drawn with a SpriteBatch:
batch.draw(texture, x, y, width, height);
While the white image is drawn from individual points plotted based on if their alpha value was 1.
TextureData td = texture.getTextureData();
td.prepare;
Pixmap map = td.consumePixmap();
for (int i = 0; i < map.getWidth(); i++)
for (int j = 0; j < map.getHeight(); j++)
if (new Color(map.getPixel(i, j)).a == 1)
points.add(new Point(i, j));
Above is the code used to get all the non-transparent pixels. They are displayed as the white image, and seem to be the inverse of the original texture.
However, the points plotted and the image itself is always an inverse of one another.
Is there any way of resolving this issue?
Thank you.
PS: I've tried multiple ways of trying to fix this:
- Using Sprites and flipping them
- Using TextureRegions and flipping them
Absolutely nothing seems to work.
回答1:
Yes this is very anoying, Libgdx is not consistent with it's coordinate system. Like font.draw()
draws from top downward. Alter the way you iterate over you draw/plot inside your loop, basically you have to flip it manually. So when j == 0
then y would become the highest value and if j == map.getHeight
y would become the lowest value. Then interpolate linearly within these values. If you shown whats inside your loop I could help out a bit more.
来源:https://stackoverflow.com/questions/35110167/libgdx-texture-drawn-as-inverse-of-what-gettexturedata-gives