1 package com.irtimaled.bbor.client.renderers;
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;
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;
27 private static final Box ORIGIN_BOX = new Box(BlockPos.ORIGIN);
29 public abstract void render(MatrixStack matrixStack, T boundingBox);
31 void renderCuboid(MatrixStack matrixStack, OffsetBox bb, Color color, boolean fillOnly, int fillAlpha) {
32 OffsetBox nudge = bb.nudge();
34 GL11.glEnable(GL11.GL_CULL_FACE);
35 RenderHelper.polygonModeFill();
38 renderCuboid0(matrixStack, nudge, color, fillOnly, fillAlpha, false);
41 RenderSystem.setShaderColor(1, 1, 1, 1);
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());
50 final MatrixStack.Entry lastStack = stack.peek();
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));
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);
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();
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);
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;
109 private double getLineWidth() {
110 return DEFAULT_LINE_WIDTH * ConfigManager.lineWidthModifier.get();
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);
121 if (!RenderCulling.isVisibleCulling(new OffsetBox(startPoint, endPoint).toBox())) return; // TODO better culling
125 RenderHelper.applyRegionalRenderOffset(matrixStack);
127 RenderBatch.drawLine(matrixStack.peek(), startPoint.getPoint(), endPoint.getPoint(), color, 255);
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;
141 RenderHelper.afterRenderFont(matrixStack);
144 void renderSphere(MatrixStack matrixStack, Point center, double radius, Color color) {
145 if (ConfigManager.renderSphereAsDots.get()) {
146 renderDotSphere(matrixStack, center, radius, color);
148 renderLineSphere(matrixStack, center, radius, color);
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();
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);
165 private void renderCircle(MatrixStack matrixStack, Point center, double radius, Color color, double dy) {
168 RenderHelper.applyRegionalRenderOffset(matrixStack);
169 RenderSystem.setShader(GameRenderer::getPositionShader);
171 Point firstPoint = null;
172 Point lastPoint = null;
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) {
181 RenderBatch.drawLine(matrixStack.peek(), lastPoint, point, color, 255);
183 RenderBatch.drawLine(matrixStack.peek(), lastPoint, firstPoint, color, 255);
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;
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);