From e5873759cc9a33e2b3d70df8f77dd4efe4e6c891 Mon Sep 17 00:00:00 2001 From: Speiger Date: Sat, 13 Jul 2024 04:49:32 +0200 Subject: [PATCH] Progress on the font/Texture system --- .../speiger/src/coreengine/NewInputTest.java | 1 - .../rendering/gui/font/FontGroup.java | 17 ++- .../rendering/gui/font/FontTexture.java | 107 ++++++++++++++++++ .../rendering/gui/font/glyth/GlythData.java | 5 +- .../gui/font/glyth/IGlythSheetInfo.java | 8 ++ .../rendering/textures/custom/Drawable.java | 37 ++++-- .../textures/simple/SimpleTexture.java | 2 +- .../rendering/utils/values/IGLValue.java | 13 +++ .../values/textures/GLTextureFormat.java | 46 ++++++-- 9 files changed, 212 insertions(+), 24 deletions(-) create mode 100644 src/main/java/speiger/src/coreengine/rendering/gui/font/FontTexture.java create mode 100644 src/main/java/speiger/src/coreengine/rendering/gui/font/glyth/IGlythSheetInfo.java diff --git a/src/main/java/speiger/src/coreengine/NewInputTest.java b/src/main/java/speiger/src/coreengine/NewInputTest.java index 6fe1b5c..7b9f0ca 100644 --- a/src/main/java/speiger/src/coreengine/NewInputTest.java +++ b/src/main/java/speiger/src/coreengine/NewInputTest.java @@ -8,7 +8,6 @@ import javax.imageio.ImageIO; import org.lwjgl.glfw.GLFW; import org.lwjgl.stb.STBImage; -import org.lwjgl.system.MemoryUtil; import speiger.src.coreengine.assets.parsers.NativeMemoryParser; import speiger.src.coreengine.math.misc.ColorSpaces; diff --git a/src/main/java/speiger/src/coreengine/rendering/gui/font/FontGroup.java b/src/main/java/speiger/src/coreengine/rendering/gui/font/FontGroup.java index 818b0a2..0619018 100644 --- a/src/main/java/speiger/src/coreengine/rendering/gui/font/FontGroup.java +++ b/src/main/java/speiger/src/coreengine/rendering/gui/font/FontGroup.java @@ -4,17 +4,21 @@ import java.util.List; import speiger.src.collections.ints.maps.impl.hash.Int2ObjectOpenHashMap; import speiger.src.collections.ints.maps.interfaces.Int2ObjectMap; +import speiger.src.collections.objects.lists.ObjectArrayList; import speiger.src.coreengine.assets.AssetLocation; import speiger.src.coreengine.rendering.gui.font.glyth.Glyth; import speiger.src.coreengine.rendering.gui.font.glyth.GlythData; +import speiger.src.coreengine.rendering.gui.font.glyth.IGlythSheetInfo; import speiger.src.coreengine.rendering.gui.font.providers.IFontProvider; public class FontGroup { private static final int BOLD_FLAG = 1<<31; + private static final int TEXTURE_SIZE = 2048; AssetLocation locations; List providers; Int2ObjectMap bakedGlyths = new Int2ObjectOpenHashMap<>(); Int2ObjectMap dataGlyths = new Int2ObjectOpenHashMap<>(); + List textures = new ObjectArrayList<>(); public FontGroup(AssetLocation locations, List providers) { this.locations = locations; @@ -42,8 +46,19 @@ public class FontGroup { codepoint &= ~BOLD_FLAG; for(int i = 0,m=providers.size();i width || ih > height) return null; + if(iw == width && ih == height) { + occupied = true; + return this; + } + int dw = width - iw; + int dh = height - ih; + children = new Slot[dw > 0 && dh > 0 ? 3 : 2]; + children[0] = new Slot(x, y, iw, ih); + expandSlot(iw, ih, dw, dh); + return children[0].insert(info); + } + for(int i = 0,m=children.length;i 0 && dh > 0) { + if(dw >= dh) { + children[1] = new Slot(x + rw, y, dw, rh); + children[2] = new Slot(x, y + rh, width, dh); + } + else { + children[1] = new Slot(x, y + rh, rw, dh); + children[2] = new Slot(x + rw, y, dw, height); + } + } + else if(dw == 0) children[1] = new Slot(x, y + rh, rw, dh); + else if(dh == 0) children[1] = new Slot(x + rw, y, dw, rh); + } + + @Override + public String toString() { + return "Slot[x="+x+", y="+y+", w="+width+", h="+height+"]"; + } + } +} diff --git a/src/main/java/speiger/src/coreengine/rendering/gui/font/glyth/GlythData.java b/src/main/java/speiger/src/coreengine/rendering/gui/font/glyth/GlythData.java index cb470ef..489e9ab 100644 --- a/src/main/java/speiger/src/coreengine/rendering/gui/font/glyth/GlythData.java +++ b/src/main/java/speiger/src/coreengine/rendering/gui/font/glyth/GlythData.java @@ -2,6 +2,9 @@ package speiger.src.coreengine.rendering.gui.font.glyth; public interface GlythData { public float advance(boolean bold); - public Glyth bake(boolean bold); + public Glyth bake(boolean bold, GlythBaker baker); + public static interface GlythBaker { + Glyth bake(IGlythSheetInfo info, boolean bold); + } } \ No newline at end of file diff --git a/src/main/java/speiger/src/coreengine/rendering/gui/font/glyth/IGlythSheetInfo.java b/src/main/java/speiger/src/coreengine/rendering/gui/font/glyth/IGlythSheetInfo.java new file mode 100644 index 0000000..8fbc40b --- /dev/null +++ b/src/main/java/speiger/src/coreengine/rendering/gui/font/glyth/IGlythSheetInfo.java @@ -0,0 +1,8 @@ +package speiger.src.coreengine.rendering.gui.font.glyth; + +public interface IGlythSheetInfo { + public int width(); + public int height(); + + public int upload(int texture, int x, int y); +} diff --git a/src/main/java/speiger/src/coreengine/rendering/textures/custom/Drawable.java b/src/main/java/speiger/src/coreengine/rendering/textures/custom/Drawable.java index 323d74b..3d87e46 100644 --- a/src/main/java/speiger/src/coreengine/rendering/textures/custom/Drawable.java +++ b/src/main/java/speiger/src/coreengine/rendering/textures/custom/Drawable.java @@ -1,24 +1,29 @@ package speiger.src.coreengine.rendering.textures.custom; +import org.lwjgl.opengl.GL46; import org.lwjgl.stb.STBImage; import org.lwjgl.system.MemoryUtil; import speiger.src.coreengine.math.misc.ColorSpaces; +import speiger.src.coreengine.rendering.utils.values.IGLValue.ITextureComponentFormat; public class Drawable implements IDrawable, AutoCloseable { + ITextureComponentFormat format; int width; int height; + long components; long pixels; boolean isSTB; - public Drawable(int width, int height) { - this(width, height, MemoryUtil.nmemAllocChecked(4L * width * height), false); + public Drawable(ITextureComponentFormat format, int width, int height) { + this(format, width, height, MemoryUtil.nmemAllocChecked(format.components() * width * height), false); } - public Drawable(int width, int height, long pixels, boolean isSTB) { + public Drawable(ITextureComponentFormat format, int width, int height, long pixels, boolean isSTB) { this.width = width; this.height = height; this.pixels = pixels; + this.components = format.components(); this.isSTB = isSTB; } @@ -30,6 +35,7 @@ public class Drawable implements IDrawable, AutoCloseable { public int height() { return height; } public long pixels() { return pixels; } public boolean isSTBImage() { return isSTB; } + public ITextureComponentFormat format() { return format; } @Override public void close() { @@ -40,7 +46,7 @@ public class Drawable implements IDrawable, AutoCloseable { } protected long offset(int x, int y) { - return ((y * width()) + x) << 2; + return ((y * width()) + x) * components; } protected void ensureValid(int index) { @@ -53,38 +59,48 @@ public class Drawable implements IDrawable, AutoCloseable { if(pixels == 0L) throw new IllegalStateException("Pixel Data doesn't exist"); } + public void upload(int texture, int level, int x, int y, int texX, int texY, int width, int height) { + + } + @Override public void set(int index, int data) { ensureValid(index); + if(components != 4) throw new IllegalStateException("Format has to be 4 components"); MemoryUtil.memPutInt(index * 4L, data); } @Override public void setR(int index, int red) { + if(!format.hasRed()) throw new IllegalArgumentException("Format doesn't support Red/Luminance"); ensureValid(index); - MemoryUtil.memPutByte(index * 4L, (byte)(red & 0xFF)); + MemoryUtil.memPutByte(index * components + format.redOffset(), (byte)(red & 0xFF)); } @Override public void setG(int index, int green) { + if(!format.hasGreen()) throw new IllegalArgumentException("Format doesn't support Green"); ensureValid(index); - MemoryUtil.memPutByte(index * 4L + 1L, (byte)(green & 0xFF)); + MemoryUtil.memPutByte(index * components + format.greenOffset(), (byte)(green & 0xFF)); } @Override public void setB(int index, int blue) { + if(!format.hasBlue()) throw new IllegalArgumentException("Format doesn't support Blue"); ensureValid(index); - MemoryUtil.memPutByte(index * 4L + 2L, (byte)(blue & 0xFF)); + MemoryUtil.memPutByte(index * components + format.blueOffset(), (byte)(blue & 0xFF)); } @Override public void setA(int index, int alpha) { + if(!format.hasAlpha()) throw new IllegalArgumentException("Format doesn't support Alpha"); ensureValid(index); - MemoryUtil.memPutByte(index * 4L + 3L, (byte)(alpha & 0xFF)); + MemoryUtil.memPutByte(index * components + format.alphaOffset(), (byte)(alpha & 0xFF)); } @Override public void fill(int x, int y, int width, int height, int data) { + if(components != 4) throw new IllegalStateException("Format has to be 4 components"); ensureValid(x, y); ensureValid(x+width, y+height); for(int yOff = 0;yOff 1; case GL11.GL_LUMINANCE_ALPHA -> 2;