mirror of
https://github.com/xiaozhou233/OrangeX-Client.git
synced 2026-12-05 13:05:52 +08:00
Feat: Add 2D GUI shape rendering
- Extend Render2D with rect, gradient, outline, rounded rect, line and scissor primitives\n- Add shared 1.8.9 fixed-function render state for shapes and glyphs\n- Render 1.8.9 rounded rects with triangle fans and framebuffer scissor\n- Submit 26.2 shapes through GuiGraphicsExtractor fill/fillGradient/scissor\n- Assemble 26.2 rounded corners from cached quarter-circle textures\n- Add GUI rendering documentation and extend the debug overlay
This commit is contained in:
1 parent
b9ac9d5a92
commit
715cb0755f
13 files changed
+851
-131
No files matched your search
@@ -14,6 +14,13 @@ public final class FontDebugOverlay {
|
||||
@Subscribe(threadMode = ThreadMode.POSTING)
|
||||
public void onRender2D(EventRender2D event) {
|
||||
Render2D render2D = event.getRender2D();
|
||||
|
||||
render2D.fillRoundedRect(4, 4, 250, 120, 8,
|
||||
1.0, 0xD0141414, 0xFF303030);
|
||||
render2D.fillGradient(10, 112, 238, 6,
|
||||
0xFF55FFFF, 0xFF55FF55);
|
||||
render2D.drawLine(10, 124, 248, 124, 1.0, 0x80FFFFFF);
|
||||
|
||||
FontRenderer title = render2D.getFont(14.0f, true);
|
||||
FontRenderer body = render2D.getFont(10.0f, false);
|
||||
|
||||
|
||||
@@ -20,6 +20,28 @@ public interface Render2D {
|
||||
|
||||
FontRenderer getFont(float size, boolean bold);
|
||||
|
||||
void fillRect(double x, double y, double width, double height, int argb);
|
||||
|
||||
void fillGradient(double x, double y, double width, double height,
|
||||
int topArgb, int bottomArgb);
|
||||
|
||||
void drawRect(double x, double y, double width, double height,
|
||||
double thickness, int argb);
|
||||
|
||||
void fillRoundedRect(double x, double y, double width, double height,
|
||||
double radius, int argb);
|
||||
|
||||
void fillRoundedRect(double x, double y, double width, double height,
|
||||
double radius, double borderWidth,
|
||||
int argb, int borderArgb);
|
||||
|
||||
void drawLine(double x1, double y1, double x2, double y2,
|
||||
double thickness, int argb);
|
||||
|
||||
void enableScissor(double x, double y, double width, double height);
|
||||
|
||||
void disableScissor();
|
||||
|
||||
/**
|
||||
* Framework-internal: restores any state the backend changed for this
|
||||
* frame. Feature code should not call it.
|
||||
|
||||
+3
-2
@@ -26,7 +26,8 @@ public final class V1_8_9FontBackend implements FontBackend {
|
||||
private final RuntimeInfo runtimeInfo;
|
||||
private final MappingResolver resolver;
|
||||
private final Object minecraft;
|
||||
private final V1_8_9GlyphDrawer drawer = new V1_8_9GlyphDrawer();
|
||||
private final V1_8_9RenderState state = new V1_8_9RenderState();
|
||||
private final V1_8_9GlyphDrawer drawer = new V1_8_9GlyphDrawer(state);
|
||||
|
||||
public V1_8_9FontBackend(RuntimeInfo runtimeInfo,
|
||||
MappingResolver resolver,
|
||||
@@ -80,7 +81,7 @@ public final class V1_8_9FontBackend implements FontBackend {
|
||||
|
||||
@Override
|
||||
public Render2D createRender2D(FontManager fonts, Object platformContext) {
|
||||
return new V1_8_9Render2D(fonts, runtimeInfo, resolver, minecraft, drawer);
|
||||
return new V1_8_9Render2D(fonts, runtimeInfo, resolver, minecraft, drawer, state);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
+8
-87
@@ -3,32 +3,19 @@ package cn.xiaozhou233.orangex.render.internal.v1_8_9;
|
||||
import cn.xiaozhou233.orangex.render.font.Glyph;
|
||||
import cn.xiaozhou233.orangex.render.internal.FontTexture;
|
||||
import cn.xiaozhou233.orangex.render.internal.GlyphDrawer;
|
||||
import org.lwjgl.BufferUtils;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
import org.lwjgl.opengl.GL13;
|
||||
|
||||
import java.nio.FloatBuffer;
|
||||
|
||||
/**
|
||||
* Immediate-mode glyph drawer for the legacy (1.8.9) OpenGL pipeline.
|
||||
*/
|
||||
final class V1_8_9GlyphDrawer implements GlyphDrawer {
|
||||
|
||||
private final V1_8_9RenderState state;
|
||||
private int boundTexture = -1;
|
||||
|
||||
private boolean initialized;
|
||||
private boolean previousBlend;
|
||||
private boolean previousTexture2D;
|
||||
private boolean previousAlphaTest;
|
||||
private boolean previousDepthTest;
|
||||
private boolean previousCullFace;
|
||||
private boolean previousLighting;
|
||||
private boolean previousScissorTest;
|
||||
private boolean previousFog;
|
||||
private int previousTexture;
|
||||
private int previousTextureEnvMode;
|
||||
private int previousActiveTexture;
|
||||
private final FloatBuffer previousColor = BufferUtils.createFloatBuffer(16);
|
||||
V1_8_9GlyphDrawer(V1_8_9RenderState state) {
|
||||
this.state = state;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawGlyph(FontTexture texture, Glyph glyph,
|
||||
@@ -36,19 +23,15 @@ final class V1_8_9GlyphDrawer implements GlyphDrawer {
|
||||
if (!(texture instanceof V1_8_9FontTexture)) {
|
||||
return;
|
||||
}
|
||||
initialize();
|
||||
state.begin();
|
||||
state.enableTexture();
|
||||
|
||||
int textureId = ((V1_8_9FontTexture) texture).getTextureId();
|
||||
if (textureId != boundTexture) {
|
||||
GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureId);
|
||||
boundTexture = textureId;
|
||||
}
|
||||
|
||||
float red = ((argb >> 16) & 0xFF) / 255.0f;
|
||||
float green = ((argb >> 8) & 0xFF) / 255.0f;
|
||||
float blue = (argb & 0xFF) / 255.0f;
|
||||
float alpha = ((argb >>> 24) & 0xFF) / 255.0f;
|
||||
GL11.glColor4f(red, green, blue, alpha);
|
||||
state.setColor(argb);
|
||||
|
||||
double left = penX + glyph.getX0() * scale;
|
||||
double right = penX + glyph.getX1() * scale;
|
||||
@@ -67,69 +50,7 @@ final class V1_8_9GlyphDrawer implements GlyphDrawer {
|
||||
GL11.glEnd();
|
||||
}
|
||||
|
||||
void flush() {
|
||||
if (!initialized) {
|
||||
return;
|
||||
}
|
||||
GL11.glBindTexture(GL11.GL_TEXTURE_2D, previousTexture);
|
||||
GL11.glColor4f(previousColor.get(0), previousColor.get(1),
|
||||
previousColor.get(2), previousColor.get(3));
|
||||
setEnabled(GL11.GL_BLEND, previousBlend);
|
||||
setEnabled(GL11.GL_TEXTURE_2D, previousTexture2D);
|
||||
setEnabled(GL11.GL_ALPHA_TEST, previousAlphaTest);
|
||||
setEnabled(GL11.GL_DEPTH_TEST, previousDepthTest);
|
||||
setEnabled(GL11.GL_CULL_FACE, previousCullFace);
|
||||
setEnabled(GL11.GL_LIGHTING, previousLighting);
|
||||
setEnabled(GL11.GL_SCISSOR_TEST, previousScissorTest);
|
||||
setEnabled(GL11.GL_FOG, previousFog);
|
||||
GL11.glTexEnvi(GL11.GL_TEXTURE_ENV,
|
||||
GL11.GL_TEXTURE_ENV_MODE, previousTextureEnvMode);
|
||||
GL13.glActiveTexture(previousActiveTexture);
|
||||
initialized = false;
|
||||
void reset() {
|
||||
boundTexture = -1;
|
||||
}
|
||||
|
||||
private void initialize() {
|
||||
if (initialized) {
|
||||
return;
|
||||
}
|
||||
initialized = true;
|
||||
|
||||
previousTexture = GL11.glGetInteger(GL11.GL_TEXTURE_BINDING_2D);
|
||||
previousBlend = GL11.glIsEnabled(GL11.GL_BLEND);
|
||||
previousTexture2D = GL11.glIsEnabled(GL11.GL_TEXTURE_2D);
|
||||
previousAlphaTest = GL11.glIsEnabled(GL11.GL_ALPHA_TEST);
|
||||
previousDepthTest = GL11.glIsEnabled(GL11.GL_DEPTH_TEST);
|
||||
previousCullFace = GL11.glIsEnabled(GL11.GL_CULL_FACE);
|
||||
previousLighting = GL11.glIsEnabled(GL11.GL_LIGHTING);
|
||||
previousScissorTest = GL11.glIsEnabled(GL11.GL_SCISSOR_TEST);
|
||||
previousFog = GL11.glIsEnabled(GL11.GL_FOG);
|
||||
previousTextureEnvMode = GL11.glGetTexEnvi(GL11.GL_TEXTURE_ENV,
|
||||
GL11.GL_TEXTURE_ENV_MODE);
|
||||
previousActiveTexture = GL11.glGetInteger(GL13.GL_ACTIVE_TEXTURE);
|
||||
GL13.glActiveTexture(GL13.GL_TEXTURE0);
|
||||
|
||||
previousColor.clear();
|
||||
GL11.glGetFloat(GL11.GL_CURRENT_COLOR, previousColor);
|
||||
|
||||
GL11.glEnable(GL11.GL_BLEND);
|
||||
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
|
||||
GL11.glEnable(GL11.GL_TEXTURE_2D);
|
||||
GL11.glDisable(GL11.GL_ALPHA_TEST);
|
||||
GL11.glDisable(GL11.GL_DEPTH_TEST);
|
||||
GL11.glDisable(GL11.GL_CULL_FACE);
|
||||
GL11.glDisable(GL11.GL_LIGHTING);
|
||||
GL11.glDisable(GL11.GL_SCISSOR_TEST);
|
||||
GL11.glDisable(GL11.GL_FOG);
|
||||
GL11.glTexEnvi(GL11.GL_TEXTURE_ENV,
|
||||
GL11.GL_TEXTURE_ENV_MODE, GL11.GL_MODULATE);
|
||||
}
|
||||
|
||||
private static void setEnabled(int capability, boolean enabled) {
|
||||
if (enabled) {
|
||||
GL11.glEnable(capability);
|
||||
} else {
|
||||
GL11.glDisable(capability);
|
||||
}
|
||||
}
|
||||
}
|
||||
+210
-2
@@ -10,9 +10,14 @@ import cn.xiaozhou233.orangex.render.font.FontFamily;
|
||||
import cn.xiaozhou233.orangex.render.font.FontManager;
|
||||
import cn.xiaozhou233.orangex.render.font.TrueTypeFontRenderer;
|
||||
import cn.xiaozhou233.orangex.runtime.RuntimeInfo;
|
||||
import org.lwjgl.BufferUtils;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.Method;
|
||||
import java.nio.IntBuffer;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 1.8.9 2D context. Coordinates are the vanilla {@code ScaledResolution}
|
||||
@@ -22,18 +27,23 @@ final class V1_8_9Render2D implements Render2D {
|
||||
|
||||
private static final String SCALED_RESOLUTION =
|
||||
"net/minecraft/client/gui/ScaledResolution";
|
||||
private static final int ARC_SEGMENTS = 8;
|
||||
|
||||
private final FontManager fonts;
|
||||
private final double guiScale;
|
||||
private final V1_8_9GlyphDrawer drawer;
|
||||
private final V1_8_9RenderState state;
|
||||
private final int width;
|
||||
private final int height;
|
||||
private final List<int[]> scissorStack = new ArrayList<int[]>();
|
||||
private final IntBuffer scissorBuffer = BufferUtils.createIntBuffer(16);
|
||||
|
||||
V1_8_9Render2D(FontManager fonts, RuntimeInfo runtimeInfo,
|
||||
MappingResolver resolver, Object minecraft,
|
||||
V1_8_9GlyphDrawer drawer) {
|
||||
V1_8_9GlyphDrawer drawer, V1_8_9RenderState state) {
|
||||
this.fonts = fonts;
|
||||
this.drawer = drawer;
|
||||
this.state = state;
|
||||
|
||||
int resolvedWidth = 854;
|
||||
int resolvedHeight = 480;
|
||||
@@ -101,8 +111,206 @@ final class V1_8_9Render2D implements Render2D {
|
||||
return new TrueTypeFontRenderer(fonts, face, bold, guiScale, drawer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fillRect(double x, double y, double width, double height, int argb) {
|
||||
if (width <= 0.0 || height <= 0.0) {
|
||||
return;
|
||||
}
|
||||
state.begin();
|
||||
state.disableTexture();
|
||||
state.setColor(argb);
|
||||
GL11.glBegin(GL11.GL_QUADS);
|
||||
GL11.glVertex2d(x, y);
|
||||
GL11.glVertex2d(x + width, y);
|
||||
GL11.glVertex2d(x + width, y + height);
|
||||
GL11.glVertex2d(x, y + height);
|
||||
GL11.glEnd();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fillGradient(double x, double y, double width, double height,
|
||||
int topArgb, int bottomArgb) {
|
||||
if (width <= 0.0 || height <= 0.0) {
|
||||
return;
|
||||
}
|
||||
state.begin();
|
||||
state.disableTexture();
|
||||
GL11.glShadeModel(GL11.GL_SMOOTH);
|
||||
GL11.glBegin(GL11.GL_QUADS);
|
||||
|
||||
state.setColor(topArgb);
|
||||
GL11.glVertex2d(x, y);
|
||||
GL11.glVertex2d(x + width, y);
|
||||
state.setColor(bottomArgb);
|
||||
GL11.glVertex2d(x + width, y + height);
|
||||
GL11.glVertex2d(x, y + height);
|
||||
GL11.glEnd();
|
||||
GL11.glShadeModel(GL11.GL_FLAT);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawRect(double x, double y, double width, double height,
|
||||
double thickness, int argb) {
|
||||
if (thickness <= 0.0 || width <= 0.0 || height <= 0.0) {
|
||||
return;
|
||||
}
|
||||
fillRect(x, y, width, thickness, argb);
|
||||
fillRect(x, y + height - thickness, width, thickness, argb);
|
||||
fillRect(x, y + thickness, thickness, height - thickness * 2.0, argb);
|
||||
fillRect(x + width - thickness, y + thickness,
|
||||
thickness, height - thickness * 2.0, argb);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fillRoundedRect(double x, double y, double width, double height,
|
||||
double radius, int argb) {
|
||||
fillRoundedRectInternal(x, y, width, height, radius, argb);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fillRoundedRect(double x, double y, double width, double height,
|
||||
double radius, double borderWidth,
|
||||
int argb, int borderArgb) {
|
||||
if (borderWidth <= 0.0) {
|
||||
fillRoundedRectInternal(x, y, width, height, radius, argb);
|
||||
return;
|
||||
}
|
||||
fillRoundedRectInternal(x, y, width, height, radius, borderArgb);
|
||||
double inner = Math.max(0.0, borderWidth);
|
||||
double innerWidth = width - inner * 2.0;
|
||||
double innerHeight = height - inner * 2.0;
|
||||
if (innerWidth > 0.0 && innerHeight > 0.0) {
|
||||
fillRoundedRectInternal(x + inner, y + inner,
|
||||
innerWidth, innerHeight,
|
||||
Math.max(0.0, radius - inner), argb);
|
||||
}
|
||||
}
|
||||
|
||||
private void fillRoundedRectInternal(double x, double y, double width,
|
||||
double height, double radius, int argb) {
|
||||
if (width <= 0.0 || height <= 0.0) {
|
||||
return;
|
||||
}
|
||||
double r = Math.min(radius, Math.min(width, height) / 2.0);
|
||||
if (r <= 0.0) {
|
||||
fillRect(x, y, width, height, argb);
|
||||
return;
|
||||
}
|
||||
|
||||
List<double[]> points = roundedRectPerimeter(x, y, width, height, r);
|
||||
if (points.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
state.begin();
|
||||
state.disableTexture();
|
||||
state.setColor(argb);
|
||||
GL11.glBegin(GL11.GL_TRIANGLE_FAN);
|
||||
GL11.glVertex2d(x + width / 2.0, y + height / 2.0);
|
||||
for (double[] point : points) {
|
||||
GL11.glVertex2d(point[0], point[1]);
|
||||
}
|
||||
double[] first = points.get(0);
|
||||
GL11.glVertex2d(first[0], first[1]);
|
||||
GL11.glEnd();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawLine(double x1, double y1, double x2, double y2,
|
||||
double thickness, int argb) {
|
||||
if (thickness <= 0.0) {
|
||||
return;
|
||||
}
|
||||
double dx = x2 - x1;
|
||||
double dy = y2 - y1;
|
||||
if (Math.abs(dy) < 0.01) {
|
||||
fillRect(Math.min(x1, x2), y1 - thickness / 2.0,
|
||||
Math.abs(dx), thickness, argb);
|
||||
return;
|
||||
}
|
||||
if (Math.abs(dx) < 0.01) {
|
||||
fillRect(x1 - thickness / 2.0, Math.min(y1, y2),
|
||||
thickness, Math.abs(dy), argb);
|
||||
return;
|
||||
}
|
||||
state.begin();
|
||||
state.disableTexture();
|
||||
state.setColor(argb);
|
||||
GL11.glLineWidth((float) thickness);
|
||||
GL11.glBegin(GL11.GL_LINES);
|
||||
GL11.glVertex2d(x1, y1);
|
||||
GL11.glVertex2d(x2, y2);
|
||||
GL11.glEnd();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void enableScissor(double x, double y, double width, double height) {
|
||||
if (width <= 0.0 || height <= 0.0) {
|
||||
return;
|
||||
}
|
||||
state.begin();
|
||||
|
||||
scissorBuffer.clear();
|
||||
GL11.glGetInteger(GL11.GL_SCISSOR_BOX, scissorBuffer);
|
||||
scissorStack.add(new int[]{
|
||||
scissorBuffer.get(0), scissorBuffer.get(1),
|
||||
scissorBuffer.get(2), scissorBuffer.get(3)
|
||||
});
|
||||
|
||||
int scale = (int) Math.round(guiScale);
|
||||
int sx = (int) Math.round(x * scale);
|
||||
int sy = (int) Math.round((this.height - y - height) * scale);
|
||||
int sw = (int) Math.round(width * scale);
|
||||
int sh = (int) Math.round(height * scale);
|
||||
|
||||
GL11.glEnable(GL11.GL_SCISSOR_TEST);
|
||||
GL11.glScissor(sx, sy, Math.max(0, sw), Math.max(0, sh));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void disableScissor() {
|
||||
if (scissorStack.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
int[] box = scissorStack.remove(scissorStack.size() - 1);
|
||||
if (scissorStack.isEmpty()) {
|
||||
GL11.glDisable(GL11.GL_SCISSOR_TEST);
|
||||
} else {
|
||||
GL11.glScissor(box[0], box[1], box[2], box[3]);
|
||||
}
|
||||
}
|
||||
|
||||
private static List<double[]> roundedRectPerimeter(double x, double y,
|
||||
double width, double height,
|
||||
double radius) {
|
||||
List<double[]> points = new ArrayList<double[]>();
|
||||
double right = x + width;
|
||||
double bottom = y + height;
|
||||
|
||||
addArc(points, x + radius, y + radius, radius, 180.0, 270.0);
|
||||
addArc(points, right - radius, y + radius, radius, 270.0, 360.0);
|
||||
addArc(points, right - radius, bottom - radius, radius, 0.0, 90.0);
|
||||
addArc(points, x + radius, bottom - radius, radius, 90.0, 180.0);
|
||||
return points;
|
||||
}
|
||||
|
||||
private static void addArc(List<double[]> points, double centerX,
|
||||
double centerY, double radius,
|
||||
double startDegrees, double endDegrees) {
|
||||
for (int i = 0; i <= ARC_SEGMENTS; i++) {
|
||||
double angle = Math.toRadians(startDegrees
|
||||
+ (endDegrees - startDegrees) * i / ARC_SEGMENTS);
|
||||
points.add(new double[]{
|
||||
centerX + Math.cos(angle) * radius,
|
||||
centerY + Math.sin(angle) * radius
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void flush() {
|
||||
drawer.flush();
|
||||
state.end();
|
||||
drawer.reset();
|
||||
scissorStack.clear();
|
||||
}
|
||||
}
|
||||
+137
@@ -0,0 +1,137 @@
|
||||
package cn.xiaozhou233.orangex.render.internal.v1_8_9;
|
||||
|
||||
import org.lwjgl.BufferUtils;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
import org.lwjgl.opengl.GL13;
|
||||
|
||||
import java.nio.FloatBuffer;
|
||||
import java.nio.IntBuffer;
|
||||
|
||||
/**
|
||||
* Shared fixed-function state for one 2D frame.
|
||||
*
|
||||
* <p>Shapes and glyphs both use this object so that interleaved draw calls do
|
||||
* not save/restore each other's GL state. {@link #begin()} is idempotent and
|
||||
* {@link #end()} restores the state that existed before the first draw.</p>
|
||||
*/
|
||||
final class V1_8_9RenderState {
|
||||
|
||||
private final FloatBuffer colorBuffer = BufferUtils.createFloatBuffer(16);
|
||||
private final IntBuffer scissorBuffer = BufferUtils.createIntBuffer(16);
|
||||
|
||||
private boolean active;
|
||||
private boolean previousBlend;
|
||||
private boolean previousTexture2D;
|
||||
private boolean previousAlphaTest;
|
||||
private boolean previousDepthTest;
|
||||
private boolean previousCullFace;
|
||||
private boolean previousLighting;
|
||||
private boolean previousFog;
|
||||
private boolean previousScissorTest;
|
||||
private int previousTexture;
|
||||
private int previousTextureEnvMode;
|
||||
private int previousActiveTexture;
|
||||
private int previousShadeModel;
|
||||
private float previousLineWidth;
|
||||
private int previousScissorX;
|
||||
private int previousScissorY;
|
||||
private int previousScissorWidth;
|
||||
private int previousScissorHeight;
|
||||
|
||||
void begin() {
|
||||
if (active) {
|
||||
return;
|
||||
}
|
||||
active = true;
|
||||
|
||||
previousBlend = GL11.glIsEnabled(GL11.GL_BLEND);
|
||||
previousTexture2D = GL11.glIsEnabled(GL11.GL_TEXTURE_2D);
|
||||
previousAlphaTest = GL11.glIsEnabled(GL11.GL_ALPHA_TEST);
|
||||
previousDepthTest = GL11.glIsEnabled(GL11.GL_DEPTH_TEST);
|
||||
previousCullFace = GL11.glIsEnabled(GL11.GL_CULL_FACE);
|
||||
previousLighting = GL11.glIsEnabled(GL11.GL_LIGHTING);
|
||||
previousFog = GL11.glIsEnabled(GL11.GL_FOG);
|
||||
previousScissorTest = GL11.glIsEnabled(GL11.GL_SCISSOR_TEST);
|
||||
previousTextureEnvMode = GL11.glGetTexEnvi(
|
||||
GL11.GL_TEXTURE_ENV, GL11.GL_TEXTURE_ENV_MODE);
|
||||
previousShadeModel = GL11.glGetInteger(GL11.GL_SHADE_MODEL);
|
||||
previousLineWidth = GL11.glGetFloat(GL11.GL_LINE_WIDTH);
|
||||
previousActiveTexture = GL11.glGetInteger(GL13.GL_ACTIVE_TEXTURE);
|
||||
|
||||
GL13.glActiveTexture(GL13.GL_TEXTURE0);
|
||||
previousTexture = GL11.glGetInteger(GL11.GL_TEXTURE_BINDING_2D);
|
||||
|
||||
scissorBuffer.clear();
|
||||
GL11.glGetInteger(GL11.GL_SCISSOR_BOX, scissorBuffer);
|
||||
previousScissorX = scissorBuffer.get(0);
|
||||
previousScissorY = scissorBuffer.get(1);
|
||||
previousScissorWidth = scissorBuffer.get(2);
|
||||
previousScissorHeight = scissorBuffer.get(3);
|
||||
|
||||
colorBuffer.clear();
|
||||
GL11.glGetFloat(GL11.GL_CURRENT_COLOR, colorBuffer);
|
||||
|
||||
GL11.glEnable(GL11.GL_BLEND);
|
||||
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
|
||||
GL11.glDisable(GL11.GL_TEXTURE_2D);
|
||||
GL11.glDisable(GL11.GL_ALPHA_TEST);
|
||||
GL11.glDisable(GL11.GL_DEPTH_TEST);
|
||||
GL11.glDisable(GL11.GL_CULL_FACE);
|
||||
GL11.glDisable(GL11.GL_LIGHTING);
|
||||
GL11.glDisable(GL11.GL_FOG);
|
||||
GL11.glDisable(GL11.GL_SCISSOR_TEST);
|
||||
GL11.glTexEnvi(GL11.GL_TEXTURE_ENV,
|
||||
GL11.GL_TEXTURE_ENV_MODE, GL11.GL_MODULATE);
|
||||
}
|
||||
|
||||
void end() {
|
||||
if (!active) {
|
||||
return;
|
||||
}
|
||||
active = false;
|
||||
|
||||
GL11.glBindTexture(GL11.GL_TEXTURE_2D, previousTexture);
|
||||
GL11.glColor4f(colorBuffer.get(0), colorBuffer.get(1),
|
||||
colorBuffer.get(2), colorBuffer.get(3));
|
||||
setEnabled(GL11.GL_BLEND, previousBlend);
|
||||
setEnabled(GL11.GL_TEXTURE_2D, previousTexture2D);
|
||||
setEnabled(GL11.GL_ALPHA_TEST, previousAlphaTest);
|
||||
setEnabled(GL11.GL_DEPTH_TEST, previousDepthTest);
|
||||
setEnabled(GL11.GL_CULL_FACE, previousCullFace);
|
||||
setEnabled(GL11.GL_LIGHTING, previousLighting);
|
||||
setEnabled(GL11.GL_FOG, previousFog);
|
||||
setEnabled(GL11.GL_SCISSOR_TEST, previousScissorTest);
|
||||
GL11.glScissor(previousScissorX, previousScissorY,
|
||||
previousScissorWidth, previousScissorHeight);
|
||||
GL11.glTexEnvi(GL11.GL_TEXTURE_ENV,
|
||||
GL11.GL_TEXTURE_ENV_MODE, previousTextureEnvMode);
|
||||
GL11.glShadeModel(previousShadeModel);
|
||||
GL11.glLineWidth(previousLineWidth);
|
||||
GL13.glActiveTexture(previousActiveTexture);
|
||||
}
|
||||
|
||||
void enableTexture() {
|
||||
GL11.glEnable(GL11.GL_TEXTURE_2D);
|
||||
}
|
||||
|
||||
void disableTexture() {
|
||||
GL11.glDisable(GL11.GL_TEXTURE_2D);
|
||||
}
|
||||
|
||||
void setColor(int argb) {
|
||||
GL11.glColor4f(
|
||||
((argb >> 16) & 0xFF) / 255.0f,
|
||||
((argb >> 8) & 0xFF) / 255.0f,
|
||||
(argb & 0xFF) / 255.0f,
|
||||
((argb >>> 24) & 0xFF) / 255.0f
|
||||
);
|
||||
}
|
||||
|
||||
private static void setEnabled(int capability, boolean enabled) {
|
||||
if (enabled) {
|
||||
GL11.glEnable(capability);
|
||||
} else {
|
||||
GL11.glDisable(capability);
|
||||
}
|
||||
}
|
||||
}
|
||||
+7
-39
@@ -7,10 +7,6 @@ import cn.xiaozhou233.orangex.render.internal.FontBackend;
|
||||
import cn.xiaozhou233.orangex.render.internal.FontTexture;
|
||||
import cn.xiaozhou233.orangex.runtime.RuntimeInfo;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* 26.x backend.
|
||||
*
|
||||
@@ -21,14 +17,15 @@ import java.util.function.Supplier;
|
||||
public final class V26_2FontBackend implements FontBackend {
|
||||
|
||||
private final ClassLoader loader;
|
||||
private final V26_2ShapeCache shapes;
|
||||
|
||||
public V26_2FontBackend(RuntimeInfo runtimeInfo) {
|
||||
this.loader = runtimeInfo.getMinecraftClassLoader();
|
||||
this.shapes = new V26_2ShapeCache(loader);
|
||||
}
|
||||
|
||||
@Override
|
||||
public FontTexture createTexture(FontAtlas atlas) {
|
||||
try {
|
||||
int width = atlas.getWidth();
|
||||
int height = atlas.getHeight();
|
||||
byte[] coverage = atlas.getPixels();
|
||||
@@ -41,44 +38,15 @@ public final class V26_2FontBackend implements FontBackend {
|
||||
rgba[j + 3] = coverage[i];
|
||||
}
|
||||
|
||||
Class<?> nativeImageClass = Class.forName(
|
||||
"com.mojang.blaze3d.platform.NativeImage", false, loader);
|
||||
Constructor<?> nativeImageConstructor = nativeImageClass
|
||||
.getDeclaredConstructor(int.class, int.class, boolean.class);
|
||||
Object image = nativeImageConstructor.newInstance(width, height, false);
|
||||
ByteBuffer buffer = (ByteBuffer) nativeImageClass
|
||||
.getMethod("getPixelBytes").invoke(image);
|
||||
buffer.put(rgba);
|
||||
|
||||
Class<?> dynamicTextureClass = Class.forName(
|
||||
"net.minecraft.client.renderer.texture.DynamicTexture", false, loader);
|
||||
Constructor<?> dynamicTextureConstructor = dynamicTextureClass
|
||||
.getDeclaredConstructor(Supplier.class, nativeImageClass);
|
||||
Object dynamicTexture = dynamicTextureConstructor.newInstance(
|
||||
new Supplier<String>() {
|
||||
@Override
|
||||
public String get() {
|
||||
return "OrangeX font atlas";
|
||||
}
|
||||
},
|
||||
image);
|
||||
|
||||
Object textureView = dynamicTextureClass.getMethod("getTextureView")
|
||||
.invoke(dynamicTexture);
|
||||
Object sampler = dynamicTextureClass.getMethod("getSampler")
|
||||
.invoke(dynamicTexture);
|
||||
|
||||
return new V26_2FontTexture(dynamicTexture, textureView, sampler,
|
||||
width, height);
|
||||
} catch (Throwable throwable) {
|
||||
throw new IllegalStateException("Cannot upload the 26.2 font atlas",
|
||||
throwable);
|
||||
}
|
||||
V26_2GpuTexture texture = V26_2TextureHelper.create(
|
||||
loader, width, height, rgba);
|
||||
return new V26_2FontTexture(texture.texture, texture.textureView,
|
||||
texture.sampler, width, height);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Render2D createRender2D(FontManager fonts, Object platformContext) {
|
||||
return new V26_2Render2D(fonts, platformContext, loader);
|
||||
return new V26_2Render2D(fonts, platformContext, loader, shapes);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
package cn.xiaozhou233.orangex.render.internal.v26_2;
|
||||
|
||||
/**
|
||||
* Reflective holder for a 26.x GPU texture and its sampler.
|
||||
*/
|
||||
final class V26_2GpuTexture {
|
||||
|
||||
final Object texture;
|
||||
final Object textureView;
|
||||
final Object sampler;
|
||||
final int width;
|
||||
final int height;
|
||||
|
||||
V26_2GpuTexture(Object texture, Object textureView, Object sampler,
|
||||
int width, int height) {
|
||||
this.texture = texture;
|
||||
this.textureView = textureView;
|
||||
this.sampler = sampler;
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
}
|
||||
}
|
||||
+202
-1
@@ -24,12 +24,22 @@ final class V26_2Render2D implements Render2D {
|
||||
|
||||
private final FontManager fonts;
|
||||
private final V26_2GlyphDrawer drawer;
|
||||
private final Object extractor;
|
||||
private final Method innerBlit;
|
||||
private final Object guiPipeline;
|
||||
private final Method fillMethod;
|
||||
private final Method fillGradientMethod;
|
||||
private final Method enableScissorMethod;
|
||||
private final Method disableScissorMethod;
|
||||
private final V26_2ShapeCache shapes;
|
||||
private final int width;
|
||||
private final int height;
|
||||
private final double guiScale;
|
||||
|
||||
V26_2Render2D(FontManager fonts, Object gui, ClassLoader loader) {
|
||||
V26_2Render2D(FontManager fonts, Object gui, ClassLoader loader,
|
||||
V26_2ShapeCache shapes) {
|
||||
this.fonts = fonts;
|
||||
this.shapes = shapes;
|
||||
|
||||
try {
|
||||
Field minecraftField = field(gui.getClass(), "minecraft");
|
||||
@@ -77,6 +87,17 @@ final class V26_2Render2D implements Render2D {
|
||||
int.class);
|
||||
innerBlit.setAccessible(true);
|
||||
|
||||
this.extractor = extractor;
|
||||
this.innerBlit = innerBlit;
|
||||
this.guiPipeline = guiPipeline;
|
||||
this.fillMethod = extractorClass.getMethod("fill",
|
||||
int.class, int.class, int.class, int.class, int.class);
|
||||
this.fillGradientMethod = extractorClass.getMethod("fillGradient",
|
||||
int.class, int.class, int.class, int.class,
|
||||
int.class, int.class);
|
||||
this.enableScissorMethod = extractorClass.getMethod("enableScissor",
|
||||
int.class, int.class, int.class, int.class);
|
||||
this.disableScissorMethod = extractorClass.getMethod("disableScissor");
|
||||
this.drawer = new V26_2GlyphDrawer(extractor, innerBlit, guiPipeline);
|
||||
} catch (Throwable throwable) {
|
||||
throw new IllegalStateException(
|
||||
@@ -123,6 +144,186 @@ final class V26_2Render2D implements Render2D {
|
||||
return new TrueTypeFontRenderer(fonts, face, bold, guiScale, drawer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fillRect(double x, double y, double width, double height, int argb) {
|
||||
if (width <= 0.0 || height <= 0.0) {
|
||||
return;
|
||||
}
|
||||
int x0 = (int) Math.round(x);
|
||||
int y0 = (int) Math.round(y);
|
||||
int x1 = (int) Math.round(x + width);
|
||||
int y1 = (int) Math.round(y + height);
|
||||
invokeFill(x0, y0, x1, y1, argb);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fillGradient(double x, double y, double width, double height,
|
||||
int topArgb, int bottomArgb) {
|
||||
if (width <= 0.0 || height <= 0.0) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
fillGradientMethod.invoke(extractor,
|
||||
(int) Math.round(x), (int) Math.round(y),
|
||||
(int) Math.round(x + width), (int) Math.round(y + height),
|
||||
topArgb, bottomArgb);
|
||||
} catch (Throwable throwable) {
|
||||
throw new IllegalStateException("Failed to draw a GUI gradient", throwable);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawRect(double x, double y, double width, double height,
|
||||
double thickness, int argb) {
|
||||
if (thickness <= 0.0 || width <= 0.0 || height <= 0.0) {
|
||||
return;
|
||||
}
|
||||
fillRect(x, y, width, thickness, argb);
|
||||
fillRect(x, y + height - thickness, width, thickness, argb);
|
||||
fillRect(x, y + thickness, thickness, height - thickness * 2.0, argb);
|
||||
fillRect(x + width - thickness, y + thickness,
|
||||
thickness, height - thickness * 2.0, argb);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fillRoundedRect(double x, double y, double width, double height,
|
||||
double radius, int argb) {
|
||||
fillRoundedRectInternal(x, y, width, height, radius, argb);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fillRoundedRect(double x, double y, double width, double height,
|
||||
double radius, double borderWidth,
|
||||
int argb, int borderArgb) {
|
||||
if (borderWidth <= 0.0) {
|
||||
fillRoundedRectInternal(x, y, width, height, radius, argb);
|
||||
return;
|
||||
}
|
||||
fillRoundedRectInternal(x, y, width, height, radius, borderArgb);
|
||||
double inset = Math.max(0.0, borderWidth);
|
||||
double innerWidth = width - inset * 2.0;
|
||||
double innerHeight = height - inset * 2.0;
|
||||
if (innerWidth > 0.0 && innerHeight > 0.0) {
|
||||
fillRoundedRectInternal(x + inset, y + inset,
|
||||
innerWidth, innerHeight,
|
||||
Math.max(0.0, radius - inset), argb);
|
||||
}
|
||||
}
|
||||
|
||||
private void fillRoundedRectInternal(double x, double y, double width,
|
||||
double height, double radius, int argb) {
|
||||
if (width <= 0.0 || height <= 0.0) {
|
||||
return;
|
||||
}
|
||||
double clamped = Math.min(radius, Math.min(width, height) / 2.0);
|
||||
if (clamped <= 0.5) {
|
||||
fillRect(x, y, width, height, argb);
|
||||
return;
|
||||
}
|
||||
|
||||
int x0 = (int) Math.round(x);
|
||||
int y0 = (int) Math.round(y);
|
||||
int x1 = (int) Math.round(x + width);
|
||||
int y1 = (int) Math.round(y + height);
|
||||
int corner = Math.max(1, (int) Math.round(clamped));
|
||||
corner = Math.min(corner, Math.min((x1 - x0) / 2, (y1 - y0) / 2));
|
||||
if (corner <= 0) {
|
||||
fillRect(x, y, width, height, argb);
|
||||
return;
|
||||
}
|
||||
|
||||
// Straight strips.
|
||||
invokeFill(x0 + corner, y0, x1 - corner, y0 + corner, argb);
|
||||
invokeFill(x0 + corner, y1 - corner, x1 - corner, y1, argb);
|
||||
invokeFill(x0 + corner, y0 + corner, x1 - corner, y1 - corner, argb);
|
||||
invokeFill(x0, y0 + corner, x0 + corner, y1 - corner, argb);
|
||||
invokeFill(x1 - corner, y0 + corner, x1, y1 - corner, argb);
|
||||
|
||||
int radiusPixels = Math.max(2, (int) Math.round(clamped * guiScale));
|
||||
V26_2GpuTexture texture = shapes.corner(radiusPixels);
|
||||
|
||||
// Antialiased quarter-circle corners. Flipping U/V mirrors the same
|
||||
// texture for the other three corners.
|
||||
invokeBlit(texture, x0, y0, x0 + corner, y0 + corner,
|
||||
0.0f, 0.0f, 1.0f, 1.0f, argb);
|
||||
invokeBlit(texture, x1 - corner, y0, x1, y0 + corner,
|
||||
1.0f, 0.0f, 0.0f, 1.0f, argb);
|
||||
invokeBlit(texture, x0, y1 - corner, x0 + corner, y1,
|
||||
0.0f, 1.0f, 1.0f, 0.0f, argb);
|
||||
invokeBlit(texture, x1 - corner, y1 - corner, x1, y1,
|
||||
1.0f, 1.0f, 0.0f, 0.0f, argb);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawLine(double x1, double y1, double x2, double y2,
|
||||
double thickness, int argb) {
|
||||
if (thickness <= 0.0) {
|
||||
return;
|
||||
}
|
||||
double dx = x2 - x1;
|
||||
double dy = y2 - y1;
|
||||
if (Math.abs(dy) < 0.01) {
|
||||
fillRect(Math.min(x1, x2), y1 - thickness / 2.0,
|
||||
Math.abs(dx), thickness, argb);
|
||||
return;
|
||||
}
|
||||
if (Math.abs(dx) < 0.01) {
|
||||
fillRect(x1 - thickness / 2.0, Math.min(y1, y2),
|
||||
thickness, Math.abs(dy), argb);
|
||||
return;
|
||||
}
|
||||
|
||||
int steps = (int) Math.ceil(Math.max(Math.abs(dx), Math.abs(dy)));
|
||||
for (int i = 0; i <= steps; i++) {
|
||||
double t = steps == 0 ? 0.0 : (double) i / (double) steps;
|
||||
fillRect(x1 + dx * t - thickness / 2.0,
|
||||
y1 + dy * t - thickness / 2.0,
|
||||
thickness, thickness, argb);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void enableScissor(double x, double y, double width, double height) {
|
||||
if (width <= 0.0 || height <= 0.0) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
enableScissorMethod.invoke(extractor,
|
||||
(int) Math.round(x), (int) Math.round(y),
|
||||
(int) Math.round(x + width), (int) Math.round(y + height));
|
||||
} catch (Throwable throwable) {
|
||||
throw new IllegalStateException("Failed to enable a GUI scissor", throwable);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void disableScissor() {
|
||||
try {
|
||||
disableScissorMethod.invoke(extractor);
|
||||
} catch (Throwable throwable) {
|
||||
throw new IllegalStateException("Failed to disable a GUI scissor", throwable);
|
||||
}
|
||||
}
|
||||
|
||||
private void invokeFill(int x0, int y0, int x1, int y1, int argb) {
|
||||
try {
|
||||
fillMethod.invoke(extractor, x0, y0, x1, y1, argb);
|
||||
} catch (Throwable throwable) {
|
||||
throw new IllegalStateException("Failed to draw a GUI rectangle", throwable);
|
||||
}
|
||||
}
|
||||
|
||||
private void invokeBlit(V26_2GpuTexture texture, int x0, int y0, int x1, int y1,
|
||||
float u0, float v0, float u1, float v1, int argb) {
|
||||
try {
|
||||
innerBlit.invoke(extractor, guiPipeline,
|
||||
texture.textureView, texture.sampler,
|
||||
x0, y0, x1, y1, u0, u1, v0, v1, argb);
|
||||
} catch (Throwable throwable) {
|
||||
throw new IllegalStateException("Failed to draw a GUI corner", throwable);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void flush() {
|
||||
// Everything was submitted to the GuiRenderState; nothing to restore.
|
||||
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
package cn.xiaozhou233.orangex.render.internal.v26_2;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Caches white quarter-circle corner textures used to assemble rounded
|
||||
* rectangles through the 26.x GUI blit pipeline.
|
||||
*/
|
||||
final class V26_2ShapeCache {
|
||||
|
||||
private final ClassLoader loader;
|
||||
private final Map<Integer, V26_2GpuTexture> corners =
|
||||
new HashMap<Integer, V26_2GpuTexture>();
|
||||
|
||||
V26_2ShapeCache(ClassLoader loader) {
|
||||
this.loader = loader;
|
||||
}
|
||||
|
||||
synchronized V26_2GpuTexture corner(int radiusPixels) {
|
||||
int radius = Math.max(2, radiusPixels);
|
||||
V26_2GpuTexture cached = corners.get(radius);
|
||||
if (cached != null) {
|
||||
return cached;
|
||||
}
|
||||
|
||||
int size = radius;
|
||||
byte[] rgba = new byte[size * size * 4];
|
||||
for (int y = 0; y < size; y++) {
|
||||
for (int x = 0; x < size; x++) {
|
||||
double dx = radius - (x + 0.5);
|
||||
double dy = radius - (y + 0.5);
|
||||
double distance = Math.sqrt(dx * dx + dy * dy);
|
||||
float coverage = (float) clamp(radius - distance + 0.5, 0.0, 1.0);
|
||||
|
||||
int offset = (y * size + x) * 4;
|
||||
rgba[offset] = (byte) 0xFF;
|
||||
rgba[offset + 1] = (byte) 0xFF;
|
||||
rgba[offset + 2] = (byte) 0xFF;
|
||||
rgba[offset + 3] = (byte) Math.round(coverage * 255.0f);
|
||||
}
|
||||
}
|
||||
|
||||
V26_2GpuTexture texture = V26_2TextureHelper.create(
|
||||
loader, size, size, rgba);
|
||||
corners.put(radius, texture);
|
||||
return texture;
|
||||
}
|
||||
|
||||
private static double clamp(double value, double min, double max) {
|
||||
return Math.max(min, Math.min(max, value));
|
||||
}
|
||||
}
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
package cn.xiaozhou233.orangex.render.internal.v26_2;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* Creates a {@code DynamicTexture} from an RGBA byte array without linking
|
||||
* against Mojang's classes.
|
||||
*/
|
||||
final class V26_2TextureHelper {
|
||||
|
||||
static V26_2GpuTexture create(ClassLoader loader, int width, int height,
|
||||
byte[] rgba) {
|
||||
try {
|
||||
Class<?> nativeImageClass = Class.forName(
|
||||
"com.mojang.blaze3d.platform.NativeImage", false, loader);
|
||||
Constructor<?> nativeImageConstructor = nativeImageClass
|
||||
.getDeclaredConstructor(int.class, int.class, boolean.class);
|
||||
Object image = nativeImageConstructor.newInstance(width, height, false);
|
||||
|
||||
ByteBuffer buffer = (ByteBuffer) nativeImageClass
|
||||
.getMethod("getPixelBytes").invoke(image);
|
||||
buffer.put(rgba);
|
||||
|
||||
Class<?> dynamicTextureClass = Class.forName(
|
||||
"net.minecraft.client.renderer.texture.DynamicTexture", false, loader);
|
||||
Constructor<?> dynamicTextureConstructor = dynamicTextureClass
|
||||
.getDeclaredConstructor(Supplier.class, nativeImageClass);
|
||||
Object texture = dynamicTextureConstructor.newInstance(
|
||||
new Supplier<String>() {
|
||||
@Override
|
||||
public String get() {
|
||||
return "OrangeX texture";
|
||||
}
|
||||
},
|
||||
image);
|
||||
|
||||
Object textureView = dynamicTextureClass.getMethod("getTextureView")
|
||||
.invoke(texture);
|
||||
Object sampler = dynamicTextureClass.getMethod("getSampler")
|
||||
.invoke(texture);
|
||||
return new V26_2GpuTexture(texture, textureView, sampler, width, height);
|
||||
} catch (Throwable throwable) {
|
||||
throw new IllegalStateException("Cannot create a 26.2 texture", throwable);
|
||||
}
|
||||
}
|
||||
|
||||
private V26_2TextureHelper() {
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,7 @@
|
||||
| --- | --- |
|
||||
| [vape-font-gui-rendering.md](vape-font-gui-rendering.md) | Vape 4.21 的字体光栅化、文本排版、GUI 组件树、双渲染后端与 1.7.10–26.2 跨版本路径分析 |
|
||||
| [font-port.md](font-port.md) | OrangeX 的字体移植实现:TTF 烘焙、Render2D API、1.8.9 / 26.2 接入、调试参数与限制 |
|
||||
| [gui-rendering.md](gui-rendering.md) | 2D GUI 绘制层:矩形、渐变、圆角、描边、直线、裁剪及两版本实现 |
|
||||
|
||||
## 快速导航
|
||||
|
||||
|
||||
@@ -0,0 +1,128 @@
|
||||
# GUI 2D 渲染
|
||||
|
||||
本文说明 OrangeX 的基础 2D GUI 绘制层。它建立在字体移植的 `Render2D` 上下文之上,供后续 ClickGUI / HUD / 面板组件直接使用。
|
||||
|
||||
## 1. API
|
||||
|
||||
`cn.xiaozhou233.orangex.render.api.Render2D`:
|
||||
|
||||
| 方法 | 说明 |
|
||||
| --- | --- |
|
||||
| `fillRect(x, y, w, h, argb)` | 实心矩形 |
|
||||
| `fillGradient(x, y, w, h, top, bottom)` | 垂直渐变矩形 |
|
||||
| `drawRect(x, y, w, h, thickness, argb)` | 矩形描边 |
|
||||
| `fillRoundedRect(x, y, w, h, radius, argb)` | 实心圆角矩形 |
|
||||
| `fillRoundedRect(x, y, w, h, radius, borderWidth, fill, border)` | 带边框圆角矩形 |
|
||||
| `drawLine(x1, y1, x2, y2, thickness, argb)` | 直线 |
|
||||
| `enableScissor(x, y, w, h)` | 开启裁剪 |
|
||||
| `disableScissor()` | 关闭当前裁剪 |
|
||||
| `getFont(size[, bold])` | 获取字体 |
|
||||
| `getWidth()` / `getHeight()` / `getGuiScale()` | 当前 GUI 尺寸 |
|
||||
|
||||
颜色统一使用 **ARGB int**,例如:
|
||||
|
||||
```java
|
||||
0xFFFFFFFF // 不透明白
|
||||
0xCC101010 // 80% 黑
|
||||
0xFF55FFFF // 青色
|
||||
```
|
||||
|
||||
坐标是 Minecraft GUI 像素(`ScaledResolution` / `getGuiScaledWidth` 空间)。
|
||||
|
||||
## 2. 示例
|
||||
|
||||
```java
|
||||
@Subscribe(threadMode = ThreadMode.POSTING)
|
||||
public void onRender2D(EventRender2D event) {
|
||||
Render2D g = event.getRender2D();
|
||||
|
||||
g.fillRoundedRect(10, 10, 220, 120, 8,
|
||||
1.0, 0xCC151515, 0xFF353535);
|
||||
|
||||
g.fillGradient(18, 100, 204, 6,
|
||||
0xFF55FFFF, 0xFF55FF55);
|
||||
|
||||
g.drawLine(18, 112, 222, 112, 1.0, 0x80FFFFFF);
|
||||
|
||||
FontRenderer font = g.getFont(10.0f);
|
||||
font.drawString("OrangeX", 18, 18, 0xFFFFFFFF);
|
||||
|
||||
g.enableScissor(18, 40, 120, 18);
|
||||
font.drawString("只在裁剪区域内显示的一段文字", 18, 40, 0xFFAAAAAA);
|
||||
g.disableScissor();
|
||||
}
|
||||
```
|
||||
|
||||
带边框圆角面板的常见写法:
|
||||
|
||||
```java
|
||||
g.fillRoundedRect(x, y, width, height, 6.0,
|
||||
1.0, 0xCC181818, 0xFF3A3A3A);
|
||||
```
|
||||
|
||||
## 3. 版本实现
|
||||
|
||||
### 3.1 1.8.9
|
||||
|
||||
`V1_8_9Render2D` 使用固定管线立即模式:
|
||||
|
||||
- 矩形 / 描边 / 渐变:`GL_QUADS`,渐变使用 `GL_SMOOTH`
|
||||
- 圆角矩形:按圆角半径生成外轮廓采样点,`GL_TRIANGLE_FAN` 填充
|
||||
- 边框:先画外层圆角矩形(边框色),再画内层圆角矩形(填充色)
|
||||
- 裁剪:`GL_SCISSOR_TEST` + `glScissor`,坐标按 GUI Scale 转成 framebuffer 像素
|
||||
- 直线:水平/垂直用矩形,斜线用 `GL_LINES`
|
||||
|
||||
所有 GL 状态由 `V1_8_9RenderState` 统一保存/恢复:
|
||||
|
||||
- blend / texture / alpha test / depth / cull / lighting / fog / scissor
|
||||
- texture binding / active texture / texture env
|
||||
- shade model / line width / current color
|
||||
|
||||
`Render2D.flush()` 在一帧结束时统一恢复,形状和字体可以任意交错绘制。
|
||||
|
||||
### 3.2 26.2
|
||||
|
||||
`V26_2Render2D` 不直接调用 OpenGL,而是把绘制提交给 `GuiRenderState`:
|
||||
|
||||
- 矩形:`GuiGraphicsExtractor#fill`
|
||||
- 渐变:`GuiGraphicsExtractor#fillGradient`
|
||||
- 裁剪:`GuiGraphicsExtractor#enableScissor/disableScissor`
|
||||
- 圆角矩形:
|
||||
- 直边用 `fill` 拼出 5 个矩形区域
|
||||
- 4 个圆角使用缓存的白色四分之一圆纹理,通过 `innerBlit` 翻转 UV 绘制
|
||||
- 纹理按 `radius * guiScale` 生成,保证一个纹素对应一个物理像素
|
||||
- 边框:外层圆角矩形 + 内层圆角矩形
|
||||
|
||||
圆角纹理由 `V26_2ShapeCache` 缓存,按半径像素复用,不会每帧创建。
|
||||
|
||||
## 4. 裁剪
|
||||
|
||||
裁剪支持嵌套:
|
||||
|
||||
```java
|
||||
g.enableScissor(0, 0, 200, 100);
|
||||
g.enableScissor(20, 20, 100, 50);
|
||||
// ...
|
||||
g.disableScissor(); // 回到第一层
|
||||
g.disableScissor(); // 关闭
|
||||
```
|
||||
|
||||
- 1.8.9 在 `V1_8_9Render2D` 内维护 scissor 栈
|
||||
- 26.2 交给 `GuiGraphicsExtractor` 的 scissor 栈
|
||||
|
||||
## 5. 当前限制
|
||||
|
||||
- 没有旋转、缩放矩阵 API(坐标就是 GUI 像素)
|
||||
- 26.2 的斜线用 1px 方块近似,复杂斜线建议用贴图
|
||||
- 圆角矩形在 26.2 依赖生成的角纹理,半径种类很多时会增加纹理数量
|
||||
- 还没有图片/纹理绘制 API,下一步可以补 `drawTexture` / `drawImage`
|
||||
- 还没有组件树、布局和输入系统
|
||||
|
||||
## 6. 下一步
|
||||
|
||||
建议顺序:
|
||||
|
||||
1. `Texture` / `drawTexture` 图片绘制
|
||||
2. `Component` / `Frame` 组件树与布局
|
||||
3. 鼠标/键盘输入与 ClickGUI 打开逻辑
|
||||
4. 主题、动画和滚动容器
|
||||
Reference in new issue
Block a user