]> git.lizzy.rs Git - BoundingBoxOutlineReloaded.git/blobdiff - src/main/java/com/irtimaled/bbor/client/renderers/AbstractRenderer.java
Several fixes
[BoundingBoxOutlineReloaded.git] / src / main / java / com / irtimaled / bbor / client / renderers / AbstractRenderer.java
index d701e47e133a19df7eda7370d7ec129f4402eb55..94683d7030bc1fc1cecf76b062f941acac89d509 100644 (file)
 package com.irtimaled.bbor.client.renderers;
 
+import com.irtimaled.bbor.client.Camera;
+import com.irtimaled.bbor.client.RenderCulling;
 import com.irtimaled.bbor.client.config.ConfigManager;
+import com.irtimaled.bbor.client.models.Point;
+import com.irtimaled.bbor.common.MathHelper;
 import com.irtimaled.bbor.common.models.AbstractBoundingBox;
-import net.minecraft.client.Minecraft;
-import net.minecraft.client.gui.FontRenderer;
-import net.minecraft.client.renderer.OpenGlHelper;
-import org.lwjgl.opengl.GL11;
+import com.mojang.blaze3d.systems.RenderSystem;
+import net.minecraft.client.MinecraftClient;
+import net.minecraft.client.font.TextRenderer;
+import net.minecraft.client.render.GameRenderer;
+import net.minecraft.client.util.math.MatrixStack;
+import net.minecraft.util.math.BlockPos;
+import net.minecraft.util.math.Box;
 
 import java.awt.*;
-import java.util.HashSet;
-import java.util.Set;
 
 public abstract class AbstractRenderer<T extends AbstractBoundingBox> {
     private static final double TAU = 6.283185307179586D;
+    public static final double PHI_SEGMENT = TAU / 90D;
     private static final double PI = TAU / 2D;
+    public static final double THETA_SEGMENT = PHI_SEGMENT / 2D;
 
-    public abstract void render(T boundingBox);
+    private static final Box ORIGIN_BOX = new Box(BlockPos.ORIGIN);
 
-    void renderCuboid(OffsetBox bb, Color color) {
-        OffsetBox nudge = bb.nudge();
-        if (ConfigManager.fill.get()) {
-            renderFilledFaces(nudge.getMin(), nudge.getMax(), color, 30);
-        }
-        renderOutlinedCuboid(nudge, color);
-    }
+    public abstract void render(MatrixStack matrixStack, T boundingBox);
+
+    void renderCuboid(MatrixStack matrixStack, OffsetBox bb, Color color, boolean fillOnly, int fillAlpha) {
+        matrixStack.push();
 
-    void renderOutlinedCuboid(OffsetBox bb, Color color) {
-        GL11.glPolygonMode(GL11.GL_FRONT_AND_BACK, GL11.GL_LINE);
-        renderFaces(bb.getMin(), bb.getMax(), color, 255);
+        renderCuboid0(matrixStack, bb.nudge(), color, fillOnly, fillAlpha, false);
+
+        matrixStack.pop();
     }
 
-    private void renderFaces(OffsetPoint min, OffsetPoint max, Color color, int alpha) {
-        double minX = min.getX();
-        double minY = min.getY();
-        double minZ = min.getZ();
-
-        double maxX = max.getX();
-        double maxY = max.getY();
-        double maxZ = max.getZ();
-
-        Renderer renderer = Renderer.startQuads()
-                .setColor(color)
-                .setAlpha(alpha);
-
-        if(minX != maxX && minZ != maxZ) {
-            renderer.addPoint(minX, minY, minZ)
-                    .addPoint(maxX, minY, minZ)
-                    .addPoint(maxX, minY, maxZ)
-                    .addPoint(minX, minY, maxZ);
-
-            if (minY != maxY) {
-                renderer.addPoint(minX, maxY, minZ)
-                        .addPoint(maxX, maxY, minZ)
-                        .addPoint(maxX, maxY, maxZ)
-                        .addPoint(minX, maxY, maxZ);
-            }
+    private void renderCuboid0(MatrixStack stack, OffsetBox nudge, Color color, boolean fillOnly, int fillAlpha, boolean mask) {
+        if (ConfigManager.fastRender.get() >= 1 && !RenderCulling.isVisibleCulling(nudge.toBox())) return;
+        if (ConfigManager.invertBoxColorPlayerInside.get() &&
+                playerInsideBoundingBox(nudge)) {
+            color = new Color(255 - color.getRed(), 255 - color.getGreen(), 255 - color.getBlue());
         }
-
-        if(minX != maxX && minY != maxY) {
-            renderer.addPoint(minX, minY, maxZ)
-                    .addPoint(minX, maxY, maxZ)
-                    .addPoint(maxX, maxY, maxZ)
-                    .addPoint(maxX, minY, maxZ);
-
-            if(minZ != maxZ) {
-                renderer.addPoint(minX, minY, minZ)
-                        .addPoint(minX, maxY, minZ)
-                        .addPoint(maxX, maxY, minZ)
-                        .addPoint(maxX, minY, minZ);
-            }
+        final MatrixStack.Entry lastStack = stack.peek();
+        stack.push();
+        int regionX = (((int) Camera.getX()) >> 9) << 9;
+        int regionZ = (((int) Camera.getZ()) >> 9) << 9;
+        RenderHelper.applyRegionalRenderOffset(stack);
+        final double minX = nudge.getMin().getX();
+        final double minY = nudge.getMin().getY();
+        final double minZ = nudge.getMin().getZ();
+        final double maxX = nudge.getMax().getX();
+        final double maxY = nudge.getMax().getY();
+        final double maxZ = nudge.getMax().getZ();
+        stack.translate(minX - regionX, minY, minZ - regionZ);
+        stack.scale((float) (maxX - minX),
+                (float) (maxY - minY),
+                (float) (maxZ - minZ));
+
+        if (fillOnly || ConfigManager.fill.get()) {
+            RenderBatch.drawSolidBox(stack.peek(), ORIGIN_BOX, color, fillAlpha, mask, minX == maxX, minY == maxY, minZ == maxZ);
         }
-        if(minY != maxY && minZ != maxZ) {
-            renderer.addPoint(minX, minY, minZ)
-                    .addPoint(minX, minY, maxZ)
-                    .addPoint(minX, maxY, maxZ)
-                    .addPoint(minX, maxY, minZ);
-
-            if(minX != maxX) {
-                renderer.addPoint(maxX, minY, minZ)
-                        .addPoint(maxX, minY, maxZ)
-                        .addPoint(maxX, maxY, maxZ)
-                        .addPoint(maxX, maxY, minZ);
-            }
+        if (!fillOnly) {
+            stack.push();
+            stack.peek().getPositionMatrix().load(lastStack.getPositionMatrix());
+            stack.peek().getNormalMatrix().load(lastStack.getNormalMatrix());
+            renderLine(stack, new OffsetPoint(minX, minY, minZ), new OffsetPoint(maxX, minY, minZ), color, true);
+            renderLine(stack, new OffsetPoint(maxX, minY, minZ), new OffsetPoint(maxX, minY, maxZ), color, true);
+            renderLine(stack, new OffsetPoint(maxX, minY, maxZ), new OffsetPoint(minX, minY, maxZ), color, true);
+            renderLine(stack, new OffsetPoint(minX, minY, maxZ), new OffsetPoint(minX, minY, minZ), color, true);
+            renderLine(stack, new OffsetPoint(minX, minY, minZ), new OffsetPoint(minX, maxY, minZ), color, true);
+            renderLine(stack, new OffsetPoint(maxX, minY, minZ), new OffsetPoint(maxX, maxY, minZ), color, true);
+            renderLine(stack, new OffsetPoint(maxX, minY, maxZ), new OffsetPoint(maxX, maxY, maxZ), color, true);
+            renderLine(stack, new OffsetPoint(minX, minY, maxZ), new OffsetPoint(minX, maxY, maxZ), color, true);
+            renderLine(stack, new OffsetPoint(minX, maxY, minZ), new OffsetPoint(maxX, maxY, minZ), color, true);
+            renderLine(stack, new OffsetPoint(maxX, maxY, minZ), new OffsetPoint(maxX, maxY, maxZ), color, true);
+            renderLine(stack, new OffsetPoint(maxX, maxY, maxZ), new OffsetPoint(minX, maxY, maxZ), color, true);
+            renderLine(stack, new OffsetPoint(minX, maxY, maxZ), new OffsetPoint(minX, maxY, minZ), color, true);
+            stack.pop();
         }
-        renderer.render();
+
+        stack.pop();
     }
 
-    void renderLine(OffsetPoint startPoint, OffsetPoint endPoint, Color color) {
-        GL11.glPolygonMode(GL11.GL_FRONT_AND_BACK, GL11.GL_LINE);
-        Renderer.startLines()
-                .setColor(color)
-                .addPoint(startPoint)
-                .addPoint(endPoint)
-                .render();
+    private boolean playerInsideBoundingBox(OffsetBox nudge) {
+        return nudge.getMin().getX() < 0 && nudge.getMax().getX() > 0 &&
+                nudge.getMin().getY() < 0 && nudge.getMax().getY() > 0 &&
+                nudge.getMin().getZ() < 0 && nudge.getMax().getZ() > 0;
     }
 
-    void renderFilledFaces(OffsetPoint min, OffsetPoint max, Color color, int alpha) {
-        GL11.glPolygonMode(GL11.GL_FRONT_AND_BACK, GL11.GL_FILL);
-        GL11.glEnable(GL11.GL_BLEND);
-        renderFaces(min, max, color, alpha);
-        GL11.glDisable(GL11.GL_BLEND);
-        GL11.glEnable(GL11.GL_POLYGON_OFFSET_LINE);
-        GL11.glPolygonOffset(-1.f, -1.f);
+
+    void renderLine(MatrixStack matrixStack, OffsetPoint startPoint, OffsetPoint endPoint, Color color, boolean cullIfEmpty) {
+//        if ((startPoint.getY() == endPoint.getY() && startPoint.getZ() == endPoint.getZ()) ||
+//                (startPoint.getX() == endPoint.getX() && startPoint.getZ() == endPoint.getZ()) ||
+//                (startPoint.getX() == endPoint.getX() && startPoint.getY() == endPoint.getY())) {
+//            renderCuboid0(matrixStack, new OffsetBox(startPoint.offset(-getLineWidth(), -getLineWidth(), -getLineWidth()), endPoint.offset(getLineWidth(), getLineWidth(), getLineWidth())), color, true, 255, true);
+//            return;
+//        }
+
+        if (cullIfEmpty && startPoint.equals(endPoint)) return;
+        if (ConfigManager.fastRender.get() >= 1 && !RenderCulling.isVisibleCulling(new OffsetBox(startPoint, endPoint).toBox())) return; // TODO better culling
+
+        matrixStack.push();
+
+        RenderHelper.applyRegionalRenderOffset(matrixStack);
+
+        RenderBatch.drawLine(matrixStack.peek(), startPoint.getPoint(), endPoint.getPoint(), color, 255);
+
+        matrixStack.pop();
     }
 
-    void renderText(OffsetPoint offsetPoint, String... texts) {
-        FontRenderer fontRenderer = Minecraft.getInstance().fontRenderer;
-
-        GL11.glPushMatrix();
-        GL11.glPolygonMode(GL11.GL_FRONT_AND_BACK, GL11.GL_FILL);
-        GL11.glTranslated(offsetPoint.getX(), offsetPoint.getY() + 0.002D, offsetPoint.getZ());
-        GL11.glNormal3f(0.0F, 1.0F, 0.0F);
-        GL11.glRotatef(0.0F, 0.0F, 1.0F, 0.0F);
-        GL11.glRotatef(90.0F, 1.0F, 0.0F, 0.0F);
-        GL11.glScalef(-0.0175F, -0.0175F, 0.0175F);
-        GL11.glEnable(GL11.GL_TEXTURE_2D);
-
-        GL11.glEnable(GL11.GL_BLEND);
-        OpenGlHelper.glBlendFuncSeparate(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA, GL11.GL_ONE, GL11.GL_ZERO);
-
-        GL11.glDisable(GL11.GL_DEPTH_TEST);
-        GL11.glEnable(GL11.GL_DEPTH_TEST);
-        GL11.glDepthMask(true);
-        float top = -(fontRenderer.FONT_HEIGHT * texts.length) / 2f;
+    void renderText(MatrixStack matrixStack, OffsetPoint offsetPoint, String... texts) {
+        TextRenderer fontRenderer = MinecraftClient.getInstance().textRenderer;
+        RenderHelper.beforeRenderFont(matrixStack, offsetPoint);
+        float top = -(fontRenderer.fontHeight * texts.length) / 2f;
         for (String text : texts) {
-            float left = fontRenderer.getStringWidth(text) / 2f;
-            fontRenderer.drawString(text, -left, top, -1);
-            top += fontRenderer.FONT_HEIGHT;
+            float left = fontRenderer.getWidth(text) / 2f;
+            fontRenderer.draw(new MatrixStack(), text, -left, top, -1);
+            top += fontRenderer.fontHeight;
+        }
+        RenderHelper.afterRenderFont(matrixStack);
+    }
+
+    void renderSphere(MatrixStack matrixStack, Point center, double radius, Color color) {
+        if (ConfigManager.renderSphereAsDots.get()) {
+            renderDotSphere(matrixStack, center, radius, color);
+        } else {
+            renderLineSphere(matrixStack, center, radius, color);
         }
-        GL11.glDisable(GL11.GL_TEXTURE_2D);
-        GL11.glDisable(GL11.GL_BLEND);
-        GL11.glPopMatrix();
     }
 
-    void renderSphere(OffsetPoint center, double radius, Color color, int density, int dotSize) {
-        GL11.glEnable(GL11.GL_POINT_SMOOTH);
-        GL11.glPointSize(dotSize);
-        Renderer renderer = Renderer.startPoints()
-                .setColor(color);
-        buildPoints(center, radius, density)
-                .forEach(renderer::addPoint);
-        renderer.render();
+    private void renderLineSphere(MatrixStack matrixStack, Point center, double radius, Color color) {
+        if (ConfigManager.fastRender.get() >= 1 && !RenderCulling.isVisibleCulling(new Box(new BlockPos(center.getX(), center.getY(), center.getZ())).expand(radius))) return;
+
+        double offset = ((radius - (int) radius) == 0) ? center.getY() - (int) center.getY() : 0;
+        int dyStep = radius < 64 ? 1 : MathHelper.floor(radius / 32);
+        for (double dy = offset - radius; dy <= radius + 1; dy += dyStep) {
+            double circleRadius = Math.sqrt((radius * radius) - (dy * dy));
+            if (circleRadius == 0) circleRadius = Math.sqrt(2) / 2;
+            renderCircle(matrixStack, center, circleRadius, color, dy + 0.001F);
+        }
     }
 
-    private Set<OffsetPoint> buildPoints(OffsetPoint center, double radius, int density) {
-        int segments = 24 + (density * 8);
+    private void renderCircle(MatrixStack matrixStack, Point center, double radius, Color color, double dy) {
+        matrixStack.push();
+
+        RenderHelper.applyRegionalRenderOffset(matrixStack);
+        RenderSystem.setShader(GameRenderer::getPositionShader);
 
-        Set<OffsetPoint> points = new HashSet<>(segments * segments);
+        Point firstPoint = null;
+        Point lastPoint = null;
 
-        double thetaSegment = PI / (double) segments;
-        double phiSegment = TAU / (double) segments;
+        for (double phi = 0.0D; phi < TAU; phi += PHI_SEGMENT) {
+            final Point point = center.offset(Math.cos(phi) * radius, dy, Math.sin(phi) * radius);
+            if (firstPoint == null) firstPoint = point;
+            if (lastPoint == null) {
+                lastPoint = point;
+                continue;
+            }
+            RenderBatch.drawLine(matrixStack.peek(), lastPoint, point, color, 255);
+            lastPoint = point;
+        }
+        RenderBatch.drawLine(matrixStack.peek(), lastPoint, firstPoint, color, 255);
 
-        for (double phi = 0.0D; phi < TAU; phi += phiSegment) {
-            for (double theta = 0.0D; theta < PI; theta += thetaSegment) {
-                double dx = radius * Math.sin(phi) * Math.cos(theta);
-                double dz = radius * Math.sin(phi) * Math.sin(theta);
-                double dy = radius * Math.cos(phi);
+        matrixStack.pop();
+    }
 
-                points.add(center.offset(dx, dy, dz));
+    private void renderDotSphere(MatrixStack matrixStack, Point center, double radius, Color color) {
+        if (ConfigManager.fastRender.get() >= 1 && !RenderCulling.isVisibleCulling(new Box(new BlockPos(center.getX(), center.getY(), center.getZ())).expand(radius))) return;
+        matrixStack.push();
+
+        for (double phi = 0.0D; phi < TAU; phi += PHI_SEGMENT) {
+            double dy = radius * Math.cos(phi);
+            double radiusBySinPhi = radius * Math.sin(phi);
+            for (double theta = 0.0D; theta < PI; theta += THETA_SEGMENT) {
+                double dx = radiusBySinPhi * Math.cos(theta);
+                double dz = radiusBySinPhi * Math.sin(theta);
+                final Point point = center.offset(dx, dy, dz);
+                renderCuboid0(matrixStack, new OffsetBox(point.offset(-0.0025f, -0.0025f, -0.0025f), point.offset(0.0025f, 0.0025f, 0.0025f)), color, true, 255, true);
             }
         }
-        return points;
+        matrixStack.pop();
     }
 }