1 package com.irtimaled.bbor.client.renderers;
3 import com.irtimaled.bbor.client.config.ConfigManager;
4 import com.irtimaled.bbor.common.models.AbstractBoundingBox;
5 import com.irtimaled.bbor.common.models.Point;
6 import net.minecraft.client.Minecraft;
7 import net.minecraft.client.gui.FontRenderer;
8 import net.minecraft.client.renderer.OpenGlHelper;
9 import org.lwjgl.opengl.GL11;
13 public abstract class AbstractRenderer<T extends AbstractBoundingBox> {
14 private static final double TAU = 6.283185307179586D;
15 private static final double PI = TAU / 2D;
17 public abstract void render(T boundingBox);
19 void renderCuboid(OffsetBox bb, Color color) {
20 OffsetBox nudge = bb.nudge();
21 if (ConfigManager.fill.get()) {
22 renderFilledFaces(nudge.getMin(), nudge.getMax(), color, 30);
24 renderOutlinedCuboid(nudge, color);
27 void renderOutlinedCuboid(OffsetBox bb, Color color) {
28 GL11.glPolygonMode(GL11.GL_FRONT_AND_BACK, GL11.GL_LINE);
29 renderFaces(bb.getMin(), bb.getMax(), color, 255);
32 private void renderFaces(OffsetPoint min, OffsetPoint max, Color color, int alpha) {
33 double minX = min.getX();
34 double minY = min.getY();
35 double minZ = min.getZ();
37 double maxX = max.getX();
38 double maxY = max.getY();
39 double maxZ = max.getZ();
41 if(ConfigManager.invertBoxColorPlayerInside.get() &&
42 playerInsideBoundingBox(minX, minY, minZ, maxX, maxY, maxZ)) {
43 color = new Color(255 - color.getRed(), 255 - color.getGreen(), 255 - color.getBlue());
46 Renderer renderer = Renderer.startQuads()
50 if(minX != maxX && minZ != maxZ) {
51 renderer.addPoint(minX, minY, minZ)
52 .addPoint(maxX, minY, minZ)
53 .addPoint(maxX, minY, maxZ)
54 .addPoint(minX, minY, maxZ);
57 renderer.addPoint(minX, maxY, minZ)
58 .addPoint(maxX, maxY, minZ)
59 .addPoint(maxX, maxY, maxZ)
60 .addPoint(minX, maxY, maxZ);
64 if(minX != maxX && minY != maxY) {
65 renderer.addPoint(minX, minY, maxZ)
66 .addPoint(minX, maxY, maxZ)
67 .addPoint(maxX, maxY, maxZ)
68 .addPoint(maxX, minY, maxZ);
71 renderer.addPoint(minX, minY, minZ)
72 .addPoint(minX, maxY, minZ)
73 .addPoint(maxX, maxY, minZ)
74 .addPoint(maxX, minY, minZ);
77 if(minY != maxY && minZ != maxZ) {
78 renderer.addPoint(minX, minY, minZ)
79 .addPoint(minX, minY, maxZ)
80 .addPoint(minX, maxY, maxZ)
81 .addPoint(minX, maxY, minZ);
84 renderer.addPoint(maxX, minY, minZ)
85 .addPoint(maxX, minY, maxZ)
86 .addPoint(maxX, maxY, maxZ)
87 .addPoint(maxX, maxY, minZ);
93 private boolean playerInsideBoundingBox(double minX, double minY, double minZ, double maxX, double maxY, double maxZ) {
94 return minX < 0 && maxX > 0 && minY < 0 && maxY > 0 && minZ < 0 && maxZ > 0;
97 void renderLine(OffsetPoint startPoint, OffsetPoint endPoint, Color color) {
98 GL11.glPolygonMode(GL11.GL_FRONT_AND_BACK, GL11.GL_LINE);
101 .addPoint(startPoint)
106 void renderFilledFaces(OffsetPoint min, OffsetPoint max, Color color, int alpha) {
107 GL11.glPolygonMode(GL11.GL_FRONT_AND_BACK, GL11.GL_FILL);
108 GL11.glEnable(GL11.GL_BLEND);
109 renderFaces(min, max, color, alpha);
110 GL11.glDisable(GL11.GL_BLEND);
111 GL11.glEnable(GL11.GL_POLYGON_OFFSET_LINE);
112 GL11.glPolygonOffset(-1.f, -1.f);
115 void renderText(OffsetPoint offsetPoint, String... texts) {
116 FontRenderer fontRenderer = Minecraft.getInstance().fontRenderer;
119 GL11.glPolygonMode(GL11.GL_FRONT_AND_BACK, GL11.GL_FILL);
120 GL11.glTranslated(offsetPoint.getX(), offsetPoint.getY() + 0.002D, offsetPoint.getZ());
121 GL11.glNormal3f(0.0F, 1.0F, 0.0F);
122 GL11.glRotatef(0.0F, 0.0F, 1.0F, 0.0F);
123 GL11.glRotatef(90.0F, 1.0F, 0.0F, 0.0F);
124 GL11.glScalef(-0.0175F, -0.0175F, 0.0175F);
125 GL11.glEnable(GL11.GL_TEXTURE_2D);
127 GL11.glEnable(GL11.GL_BLEND);
128 OpenGlHelper.glBlendFuncSeparate(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA, GL11.GL_ONE, GL11.GL_ZERO);
130 GL11.glDisable(GL11.GL_DEPTH_TEST);
131 GL11.glEnable(GL11.GL_DEPTH_TEST);
132 GL11.glDepthMask(true);
133 float top = -(fontRenderer.FONT_HEIGHT * texts.length) / 2f;
134 for (String text : texts) {
135 float left = fontRenderer.getStringWidth(text) / 2f;
136 fontRenderer.drawString(text, -left, top, -1);
137 top += fontRenderer.FONT_HEIGHT;
139 GL11.glDisable(GL11.GL_TEXTURE_2D);
140 GL11.glDisable(GL11.GL_BLEND);
144 void renderSphere(Point center, double radius, Color color, int density, int dotSize) {
145 if (ConfigManager.renderSphereAsDots.get()) {
146 renderDotSphere(center, radius, color, density, dotSize);
148 renderLineSphere(center, radius, color, density);
152 private void renderLineSphere(Point center, double radius, Color color, int density) {
153 GL11.glLineWidth(2f);
154 int segments = 24 + (density * 8);
156 double offset = ((radius - (int) radius) == 0) ? center.getY() - (int) center.getY() : 0;
157 for (double dy = offset - radius; dy <= radius + 1; dy++) {
158 double circleRadius = Math.sqrt((radius * radius) - (dy * dy));
159 if (circleRadius == 0) circleRadius = Math.sqrt(2) / 2;
160 renderCircle(center, circleRadius, color, segments, dy + 0.001F);
164 private void renderCircle(Point center, double radius, Color color, int segments, double dy) {
165 Renderer renderer = Renderer.startCircle()
168 for (int a = 0; a < 360; a += 360 / segments) {
169 double heading = a * PI / 180;
170 renderer.addPoint(new OffsetPoint(center.offset(Math.cos(heading) * radius, dy, Math.sin(heading) * radius)));
176 private void renderDotSphere(Point center, double radius, Color color, int density, int dotSize) {
177 GL11.glEnable(GL11.GL_POINT_SMOOTH);
178 GL11.glPointSize(dotSize);
179 Renderer renderer = Renderer.startPoints()
181 int segments = 24 + (density * 8);
183 double thetaSegment = PI / (double) segments;
184 double phiSegment = TAU / (double) segments;
186 for (double phi = 0.0D; phi < TAU; phi += phiSegment) {
187 for (double theta = 0.0D; theta < PI; theta += thetaSegment) {
188 double dx = radius * Math.sin(phi) * Math.cos(theta);
189 double dz = radius * Math.sin(phi) * Math.sin(theta);
190 double dy = radius * Math.cos(phi);
192 renderer.addPoint(new OffsetPoint(center.offset(dx, dy, dz)));