]> git.lizzy.rs Git - BoundingBoxOutlineReloaded.git/blob - src/main/java/com/irtimaled/bbor/client/renderers/AbstractRenderer.java
Improve performance
[BoundingBoxOutlineReloaded.git] / src / main / java / com / irtimaled / bbor / client / renderers / AbstractRenderer.java
1 package com.irtimaled.bbor.client.renderers;
2
3 import com.irtimaled.bbor.client.Camera;
4 import com.irtimaled.bbor.client.RenderCulling;
5 import com.irtimaled.bbor.client.config.ConfigManager;
6 import com.irtimaled.bbor.client.models.Point;
7 import com.irtimaled.bbor.common.MathHelper;
8 import com.irtimaled.bbor.common.models.AbstractBoundingBox;
9 import com.mojang.blaze3d.systems.RenderSystem;
10 import net.minecraft.client.MinecraftClient;
11 import net.minecraft.client.font.TextRenderer;
12 import net.minecraft.client.render.GameRenderer;
13 import net.minecraft.client.util.math.MatrixStack;
14 import net.minecraft.util.math.BlockPos;
15 import net.minecraft.util.math.Box;
16 import org.lwjgl.opengl.GL11;
17
18 import java.awt.*;
19
20 public abstract class AbstractRenderer<T extends AbstractBoundingBox> {
21     private static final double TAU = 6.283185307179586D;
22     public static final double PHI_SEGMENT = TAU / 90D;
23     private static final double PI = TAU / 2D;
24     public static final double THETA_SEGMENT = PHI_SEGMENT / 2D;
25     private static final float DEFAULT_LINE_WIDTH = 0.0025f;
26
27     private static final Box ORIGIN_BOX = new Box(BlockPos.ORIGIN);
28
29     public abstract void render(MatrixStack matrixStack, T boundingBox);
30
31     void renderCuboid(MatrixStack matrixStack, OffsetBox bb, Color color, boolean fillOnly, int fillAlpha) {
32         OffsetBox nudge = bb.nudge();
33
34         GL11.glEnable(GL11.GL_CULL_FACE);
35         RenderHelper.polygonModeFill();
36         matrixStack.push();
37
38         renderCuboid0(matrixStack, nudge, color, fillOnly, fillAlpha, false);
39
40         matrixStack.pop();
41         RenderSystem.setShaderColor(1, 1, 1, 1);
42     }
43
44     private void renderCuboid0(MatrixStack stack, OffsetBox nudge, Color color, boolean fillOnly, int fillAlpha, boolean mask) {
45         if (!RenderCulling.isVisibleCulling(nudge.toBox())) return;
46         if (ConfigManager.invertBoxColorPlayerInside.get() &&
47                 playerInsideBoundingBox(nudge)) {
48             color = new Color(255 - color.getRed(), 255 - color.getGreen(), 255 - color.getBlue());
49         }
50         final MatrixStack.Entry lastStack = stack.peek();
51         stack.push();
52         int regionX = (((int) Camera.getX()) >> 9) << 9;
53         int regionZ = (((int) Camera.getZ()) >> 9) << 9;
54         RenderHelper.applyRegionalRenderOffset(stack);
55         RenderSystem.setShader(GameRenderer::getPositionShader);
56         final double minX = nudge.getMin().getX();
57         final double minY = nudge.getMin().getY();
58         final double minZ = nudge.getMin().getZ();
59         final double maxX = nudge.getMax().getX();
60         final double maxY = nudge.getMax().getY();
61         final double maxZ = nudge.getMax().getZ();
62         stack.translate(minX - regionX, minY, minZ - regionZ);
63         stack.scale((float) (maxX - minX),
64                 (float) (maxY - minY),
65                 (float) (maxZ - minZ));
66
67 //        Matrix4f viewMatrix = stack.peek().getModel();
68 //        Matrix4f projMatrix = RenderSystem.getProjectionMatrix();
69 //        Shader shader = RenderSystem.getShader();
70         if (fillOnly || ConfigManager.fill.get()) {
71 //            RenderSystem.setShaderColor(color.getRed() / 255F, color.getGreen() / 255F, color.getBlue() / 255F, fillAlpha / 255F);
72             RenderBatch.drawSolidBox(stack.peek(), ORIGIN_BOX, color, fillAlpha, mask);
73         }
74         if (!fillOnly) {
75 //            outlinedBox.setShader(viewMatrix, projMatrix, shader);
76             final double minXL = minX - getLineWidth();
77             final double minYL = minY - getLineWidth();
78             final double minZL = minZ - getLineWidth();
79             final double maxXL = maxX + getLineWidth();
80             final double maxYL = maxY + getLineWidth();
81             final double maxZL = maxZ + getLineWidth();
82             stack.push();
83             stack.peek().getModel().load(lastStack.getModel());
84             stack.peek().getNormal().load(lastStack.getNormal());
85             renderLine(stack, new OffsetPoint(minXL, minYL, minZL), new OffsetPoint(maxXL, minYL, minZL), color);
86             renderLine(stack, new OffsetPoint(maxXL, minYL, minZL), new OffsetPoint(maxXL, minYL, maxZL), color);
87             renderLine(stack, new OffsetPoint(maxXL, minYL, maxZL), new OffsetPoint(minXL, minYL, maxZL), color);
88             renderLine(stack, new OffsetPoint(minXL, minYL, maxZL), new OffsetPoint(minXL, minYL, minZL), color);
89             renderLine(stack, new OffsetPoint(minXL, minYL, minZL), new OffsetPoint(minXL, maxYL, minZL), color);
90             renderLine(stack, new OffsetPoint(maxXL, minYL, minZL), new OffsetPoint(maxXL, maxYL, minZL), color);
91             renderLine(stack, new OffsetPoint(maxXL, minYL, maxZL), new OffsetPoint(maxXL, maxYL, maxZL), color);
92             renderLine(stack, new OffsetPoint(minXL, minYL, maxZL), new OffsetPoint(minXL, maxYL, maxZL), color);
93             renderLine(stack, new OffsetPoint(minXL, maxYL, minZL), new OffsetPoint(maxXL, maxYL, minZL), color);
94             renderLine(stack, new OffsetPoint(maxXL, maxYL, minZL), new OffsetPoint(maxXL, maxYL, maxZL), color);
95             renderLine(stack, new OffsetPoint(maxXL, maxYL, maxZL), new OffsetPoint(minXL, maxYL, maxZL), color);
96             renderLine(stack, new OffsetPoint(minXL, maxYL, maxZL), new OffsetPoint(minXL, maxYL, minZL), color);
97             stack.pop();
98         }
99
100         stack.pop();
101     }
102
103     private boolean playerInsideBoundingBox(OffsetBox nudge) {
104         return nudge.getMin().getX() < 0 && nudge.getMax().getX() > 0 &&
105                 nudge.getMin().getY() < 0 && nudge.getMax().getY() > 0 &&
106                 nudge.getMin().getZ() < 0 && nudge.getMax().getZ() > 0;
107     }
108
109     private double getLineWidth() {
110         return DEFAULT_LINE_WIDTH * ConfigManager.lineWidthModifier.get();
111     }
112
113     void renderLine(MatrixStack matrixStack, OffsetPoint startPoint, OffsetPoint endPoint, Color color) {
114 //        if ((startPoint.getY() == endPoint.getY() && startPoint.getZ() == endPoint.getZ()) ||
115 //                (startPoint.getX() == endPoint.getX() && startPoint.getZ() == endPoint.getZ()) ||
116 //                (startPoint.getX() == endPoint.getX() && startPoint.getY() == endPoint.getY())) {
117 //            renderCuboid0(matrixStack, new OffsetBox(startPoint.offset(-getLineWidth(), -getLineWidth(), -getLineWidth()), endPoint.offset(getLineWidth(), getLineWidth(), getLineWidth())), color, true, 255, true);
118 //            return;
119 //        }
120
121         if (!RenderCulling.isVisibleCulling(new OffsetBox(startPoint, endPoint).toBox())) return; // TODO better culling
122
123         matrixStack.push();
124
125         RenderHelper.applyRegionalRenderOffset(matrixStack);
126
127         RenderBatch.drawLine(matrixStack.peek(), startPoint.getPoint(), endPoint.getPoint(), color, 255);
128
129         matrixStack.pop();
130     }
131
132     void renderText(MatrixStack matrixStack, OffsetPoint offsetPoint, String... texts) {
133         TextRenderer fontRenderer = MinecraftClient.getInstance().textRenderer;
134         RenderHelper.beforeRenderFont(matrixStack, offsetPoint);
135         float top = -(fontRenderer.fontHeight * texts.length) / 2f;
136         for (String text : texts) {
137             float left = fontRenderer.getWidth(text) / 2f;
138             fontRenderer.draw(new MatrixStack(), text, -left, top, -1);
139             top += fontRenderer.fontHeight;
140         }
141         RenderHelper.afterRenderFont(matrixStack);
142     }
143
144     void renderSphere(MatrixStack matrixStack, Point center, double radius, Color color) {
145         if (ConfigManager.renderSphereAsDots.get()) {
146             renderDotSphere(matrixStack, center, radius, color);
147         } else {
148             renderLineSphere(matrixStack, center, radius, color);
149         }
150     }
151
152     private void renderLineSphere(MatrixStack matrixStack, Point center, double radius, Color color) {
153         if (!RenderCulling.isVisibleCulling(new Box(new BlockPos(center.getX(), center.getY(), center.getZ())).expand(radius))) return;
154         RenderHelper.lineWidth2();
155
156         double offset = ((radius - (int) radius) == 0) ? center.getY() - (int) center.getY() : 0;
157         int dyStep = radius < 64 ? 1 : MathHelper.floor(radius / 32);
158         for (double dy = offset - radius; dy <= radius + 1; dy += dyStep) {
159             double circleRadius = Math.sqrt((radius * radius) - (dy * dy));
160             if (circleRadius == 0) circleRadius = Math.sqrt(2) / 2;
161             renderCircle(matrixStack, center, circleRadius, color, dy + 0.001F);
162         }
163     }
164
165     private void renderCircle(MatrixStack matrixStack, Point center, double radius, Color color, double dy) {
166         matrixStack.push();
167
168         RenderHelper.applyRegionalRenderOffset(matrixStack);
169         RenderSystem.setShader(GameRenderer::getPositionShader);
170
171         Point firstPoint = null;
172         Point lastPoint = null;
173
174         for (double phi = 0.0D; phi < TAU; phi += PHI_SEGMENT) {
175             final Point point = center.offset(Math.cos(phi) * radius, dy, Math.sin(phi) * radius);
176             if (firstPoint == null) firstPoint = point;
177             if (lastPoint == null) {
178                 lastPoint = point;
179                 continue;
180             }
181             RenderBatch.drawLine(matrixStack.peek(), lastPoint, point, color, 255);
182         }
183         RenderBatch.drawLine(matrixStack.peek(), lastPoint, firstPoint, color, 255);
184
185         matrixStack.pop();
186     }
187
188     private void renderDotSphere(MatrixStack matrixStack, Point center, double radius, Color color) {
189         if (!RenderCulling.isVisibleCulling(new Box(new BlockPos(center.getX(), center.getY(), center.getZ())).expand(radius))) return;
190         matrixStack.push();
191
192         for (double phi = 0.0D; phi < TAU; phi += PHI_SEGMENT) {
193             double dy = radius * Math.cos(phi);
194             double radiusBySinPhi = radius * Math.sin(phi);
195             for (double theta = 0.0D; theta < PI; theta += THETA_SEGMENT) {
196                 double dx = radiusBySinPhi * Math.cos(theta);
197                 double dz = radiusBySinPhi * Math.sin(theta);
198                 final Point point = center.offset(dx, dy, dz);
199                 renderCuboid0(matrixStack, new OffsetBox(point.offset(-getLineWidth(), -getLineWidth(), -getLineWidth()), point.offset(getLineWidth(), getLineWidth(), getLineWidth())), color, true, 255, true);
200             }
201         }
202         matrixStack.pop();
203     }
204 }