mirror of
https://github.com/MineLittlePony/MineLittlePony.git
synced 2025-02-13 16:24:23 +01:00
Horrible hack to fix unrecognised image formats (aaaaaaaaaaahhhh)
This commit is contained in:
parent
386dc0bcc0
commit
5e4d27181e
3 changed files with 86 additions and 15 deletions
|
@ -12,8 +12,8 @@ org.gradle.daemon=false
|
||||||
displayname=Mine Little Pony
|
displayname=Mine Little Pony
|
||||||
authors=Verdana, Rene_Z, Mumfrey, Killjoy1221, Sollace
|
authors=Verdana, Rene_Z, Mumfrey, Killjoy1221, Sollace
|
||||||
description=Mine Little Pony turns players and mobs into ponies. Press F9 ingame to access settings.
|
description=Mine Little Pony turns players and mobs into ponies. Press F9 ingame to access settings.
|
||||||
version=3.3
|
version=3.3.1
|
||||||
release=RELEASE
|
release=SNAPSHOT
|
||||||
|
|
||||||
# Dependencies
|
# Dependencies
|
||||||
fabric_version=0.3.0+
|
fabric_version=0.3.0+
|
||||||
|
|
|
@ -56,7 +56,12 @@ public class PonyData implements IPonyData {
|
||||||
MineLittlePony.logger.warn("Unable to read {} metadata", identifier, e);
|
MineLittlePony.logger.warn("Unable to read {} metadata", identifier, e);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
return NativeUtil.parseImage(identifier, PonyData::new);
|
return NativeUtil.parseImage(identifier, PonyData::new);
|
||||||
|
} catch (IllegalStateException e) {
|
||||||
|
MineLittlePony.logger.fatal("Unable to read {} metadata", identifier, e);
|
||||||
|
return new PonyData();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Expose
|
@Expose
|
||||||
|
|
|
@ -5,25 +5,88 @@ import net.minecraft.client.texture.NativeImage;
|
||||||
import net.minecraft.client.texture.TextureManager;
|
import net.minecraft.client.texture.TextureManager;
|
||||||
import net.minecraft.util.Identifier;
|
import net.minecraft.util.Identifier;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
|
|
||||||
import static com.mojang.blaze3d.platform.GlStateManager.getTexLevelParameter;
|
import static com.mojang.blaze3d.platform.GlStateManager.getTexLevelParameter;
|
||||||
import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D;
|
import static org.lwjgl.opengl.GL11.*;
|
||||||
import static org.lwjgl.opengl.GL11.GL_TEXTURE_HEIGHT;
|
|
||||||
import static org.lwjgl.opengl.GL11.GL_TEXTURE_INTERNAL_FORMAT;
|
|
||||||
import static org.lwjgl.opengl.GL11.GL_TEXTURE_WIDTH;
|
|
||||||
|
|
||||||
public class NativeUtil {
|
public class NativeUtil {
|
||||||
private static final NativeImage.Format[] formats = NativeImage.Format.values();
|
enum InternalFormat {
|
||||||
|
RGB(NativeImage.Format.RGB),
|
||||||
|
RGBA(NativeImage.Format.RGBA),
|
||||||
|
LUMINANCE_ALPHA(NativeImage.Format.LUMINANCE_ALPHA),
|
||||||
|
LUMINANCE(NativeImage.Format.LUMINANCE),
|
||||||
|
|
||||||
public static NativeImage.Format getFormat(int glFormat) {
|
ALPHA4(GL_ALPHA4, NativeImage.Format.LUMINANCE_ALPHA),
|
||||||
for (NativeImage.Format i : formats) {
|
ALPHA8(GL_ALPHA8, NativeImage.Format.LUMINANCE_ALPHA),
|
||||||
if (i.getPixelDataFormat() == glFormat) {
|
ALPHA12(GL_ALPHA12, NativeImage.Format.LUMINANCE_ALPHA),
|
||||||
return i;
|
ALPHA16(GL_ALPHA16, NativeImage.Format.LUMINANCE_ALPHA),
|
||||||
}
|
|
||||||
|
LUMINANCE4(GL_LUMINANCE4, NativeImage.Format.LUMINANCE),
|
||||||
|
LUMINANCE8(GL_LUMINANCE8, NativeImage.Format.LUMINANCE),
|
||||||
|
LUMINANCE12(GL_LUMINANCE12, NativeImage.Format.LUMINANCE),
|
||||||
|
LUMINANCE16(GL_LUMINANCE16, NativeImage.Format.LUMINANCE),
|
||||||
|
LUMINANCE4_ALPHA4(GL_LUMINANCE4_ALPHA4, NativeImage.Format.LUMINANCE_ALPHA),
|
||||||
|
LUMINANCE6_ALPHA2(GL_LUMINANCE6_ALPHA2, NativeImage.Format.LUMINANCE_ALPHA),
|
||||||
|
LUMINANCE8_ALPHA8(GL_LUMINANCE8_ALPHA8, NativeImage.Format.LUMINANCE_ALPHA),
|
||||||
|
LUMINANCE12_ALPHA4(GL_LUMINANCE12_ALPHA4, NativeImage.Format.LUMINANCE_ALPHA),
|
||||||
|
LUMINANCE12_ALPHA12(GL_LUMINANCE12_ALPHA12, NativeImage.Format.LUMINANCE_ALPHA),
|
||||||
|
LUMINANCE16_ALPHA16(GL_LUMINANCE16_ALPHA16, NativeImage.Format.LUMINANCE_ALPHA),
|
||||||
|
|
||||||
|
INTENSITY(GL_INTENSITY, NativeImage.Format.LUMINANCE),
|
||||||
|
INTENSITY4(GL_INTENSITY4, NativeImage.Format.LUMINANCE),
|
||||||
|
INTENSITY8(GL_INTENSITY8, NativeImage.Format.LUMINANCE),
|
||||||
|
INTENSITY12(GL_INTENSITY12, NativeImage.Format.LUMINANCE),
|
||||||
|
INTENSITY16(GL_INTENSITY16, NativeImage.Format.LUMINANCE),
|
||||||
|
|
||||||
|
R3_G3_B2(GL_R3_G3_B2, NativeImage.Format.RGB),
|
||||||
|
RGB4(GL_RGB4, NativeImage.Format.RGB),
|
||||||
|
RGB5(GL_RGB5, NativeImage.Format.RGB),
|
||||||
|
RGB8(GL_RGB8, NativeImage.Format.RGB),
|
||||||
|
RGB10(GL_RGB10, NativeImage.Format.RGB),
|
||||||
|
RGB12(GL_RGB12, NativeImage.Format.RGB),
|
||||||
|
RGB16(GL_RGB16, NativeImage.Format.RGB),
|
||||||
|
|
||||||
|
RGBA2(GL_RGBA2, NativeImage.Format.RGBA),
|
||||||
|
RGBA4(GL_RGBA4, NativeImage.Format.RGBA),
|
||||||
|
RGB5_A1(GL_RGB5_A1, NativeImage.Format.RGBA),
|
||||||
|
RGBA8(GL_RGBA8, NativeImage.Format.RGBA),
|
||||||
|
RGB10_A2(GL_RGB10_A2, NativeImage.Format.RGBA),
|
||||||
|
RGBA12(GL_RGBA12, NativeImage.Format.RGBA),
|
||||||
|
RGBA16(GL_RGBA16, NativeImage.Format.RGBA);
|
||||||
|
|
||||||
|
private final NativeImage.Format formatClass;
|
||||||
|
private final int glId;
|
||||||
|
|
||||||
|
public static Map<Integer, InternalFormat> LOOKUP = new HashMap<>();
|
||||||
|
|
||||||
|
private InternalFormat(NativeImage.Format formatClass) {
|
||||||
|
this(formatClass.getPixelDataFormat(), formatClass);
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new RuntimeException("Unsupported image format");
|
private InternalFormat(int glId, NativeImage.Format formatClass) {
|
||||||
|
this.glId = glId;
|
||||||
|
this.formatClass = formatClass;
|
||||||
|
}
|
||||||
|
|
||||||
|
public NativeImage.Format getClassification() {
|
||||||
|
return formatClass;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static InternalFormat valueOf(int glId) {
|
||||||
|
if (!LOOKUP.containsKey(glId)) {
|
||||||
|
throw new IllegalStateException("Unsupported image format: " + glId);
|
||||||
|
}
|
||||||
|
return LOOKUP.get(glId);
|
||||||
|
}
|
||||||
|
|
||||||
|
static {
|
||||||
|
for (InternalFormat f : values()) {
|
||||||
|
LOOKUP.put(f.glId, f);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <T> T parseImage(Identifier resource, Function<NativeImage, T> consumer) {
|
public static <T> T parseImage(Identifier resource, Function<NativeImage, T> consumer) {
|
||||||
|
@ -37,6 +100,9 @@ public class NativeUtil {
|
||||||
// recreate NativeImage from the GL matrix
|
// recreate NativeImage from the GL matrix
|
||||||
textures.bindTexture(resource);
|
textures.bindTexture(resource);
|
||||||
|
|
||||||
|
// TODO: This returns values that are too specific.
|
||||||
|
// Can we change the level (0) here to something
|
||||||
|
// else to actually get what we need?
|
||||||
int format = getTexLevelParameter(GL_TEXTURE_2D, 0, GL_TEXTURE_INTERNAL_FORMAT);
|
int format = getTexLevelParameter(GL_TEXTURE_2D, 0, GL_TEXTURE_INTERNAL_FORMAT);
|
||||||
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);
|
||||||
|
@ -45,7 +111,7 @@ public class NativeUtil {
|
||||||
throw new IllegalStateException("GL texture not uploaded yet");
|
throw new IllegalStateException("GL texture not uploaded yet");
|
||||||
}
|
}
|
||||||
|
|
||||||
try (NativeImage image = new NativeImage(getFormat(format), width, height, false)) {
|
try (NativeImage image = new NativeImage(InternalFormat.valueOf(format).getClassification(), 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.
|
||||||
// Don't do this every time. Keep a cache and store it so we don't destroy memory.
|
// Don't do this every time. Keep a cache and store it so we don't destroy memory.
|
||||||
image.loadFromTextureImage(0, false);
|
image.loadFromTextureImage(0, false);
|
||||||
|
|
Loading…
Reference in a new issue