Check empty image dimensions, and actually attempt to use the correct format

This commit is contained in:
Sollace 2019-07-14 01:56:08 +02:00
parent 1c33cc7461
commit 3355a7cb8a

View file

@ -41,6 +41,8 @@ public class Pony implements IPony {
private static final AtomicInteger ponyCount = new AtomicInteger(); private static final AtomicInteger ponyCount = new AtomicInteger();
private static final NativeImage.Format[] formats = NativeImage.Format.values();
private final int ponyId = ponyCount.getAndIncrement(); private final int ponyId = ponyCount.getAndIncrement();
private final Identifier texture; private final Identifier texture;
@ -91,6 +93,16 @@ public class Pony implements IPony {
return null; return null;
} }
private static NativeImage.Format getFormat(int glFormat) {
for (NativeImage.Format i : formats) {
if (i.getPixelDataFormat() == glFormat) {
return i;
}
}
throw new RuntimeException("Unsupported image format");
}
@Nullable @Nullable
private static NativeImage getBufferedImage(@Nullable Identifier resource) { private static NativeImage getBufferedImage(@Nullable Identifier resource) {
if (resource == null) { if (resource == null) {
@ -110,11 +122,12 @@ public class Pony implements IPony {
int width = getTexLevelParameter(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH); int width = getTexLevelParameter(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH);
int height = getTexLevelParameter(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT); int height = getTexLevelParameter(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT);
NativeImage.Format channels = NativeImage.Format.RGBA; if (width == 0 || height == 0) {
if (format == GL_RGB) { return null;
channels = NativeImage.Format.RGB;
} }
NativeImage.Format channels = getFormat(format);
NativeImage image = new NativeImage(channels, width, height, false); NativeImage image = new NativeImage(channels, width, height, false);
// This allocates a new array to store the image every time. // This allocates a new array to store the image every time.