1 package com.irtimaled.bbor.client.renderers;
3 import com.irtimaled.bbor.common.models.AbstractBoundingBox;
4 import com.irtimaled.bbor.config.ConfigManager;
5 import org.lwjgl.opengl.GL11;
8 import java.util.HashSet;
11 public abstract class AbstractRenderer<T extends AbstractBoundingBox> {
12 private static double TAU = 6.283185307179586D;
13 private static double PI = TAU / 2D;
15 public abstract void render(T boundingBox);
17 void renderCuboid(OffsetBox bb, Color color) {
18 if (ConfigManager.fill.get()) {
19 renderFilledCuboid(bb, color);
21 renderUnfilledCuboid(bb, color);
24 private void renderFilledCuboid(OffsetBox bb, Color color) {
25 GL11.glPolygonMode(GL11.GL_FRONT_AND_BACK, GL11.GL_FILL);
26 GL11.glEnable(GL11.GL_BLEND);
27 renderCuboid(bb.nudge(), color, 30);
28 GL11.glDisable(GL11.GL_BLEND);
29 GL11.glEnable(GL11.GL_POLYGON_OFFSET_LINE);
30 GL11.glPolygonOffset(-1.f, -1.f);
33 void renderUnfilledCuboid(OffsetBox bb, Color color) {
34 GL11.glPolygonMode(GL11.GL_FRONT_AND_BACK, GL11.GL_LINE);
35 renderCuboid(bb.nudge(), color, 255);
38 private void renderCuboid(OffsetBox box, Color color, int alpha) {
39 OffsetPoint min = box.getMin();
40 OffsetPoint max = box.getMax();
42 double minX = min.getX();
43 double minY = min.getY();
44 double minZ = min.getZ();
46 double maxX = max.getX();
47 double maxY = max.getY();
48 double maxZ = max.getZ();
50 Renderer renderer = Renderer.startQuads()
53 .addPoint(minX, minY, minZ)
54 .addPoint(maxX, minY, minZ)
55 .addPoint(maxX, minY, maxZ)
56 .addPoint(minX, minY, maxZ);
59 renderer.addPoint(minX, maxY, minZ)
60 .addPoint(maxX, maxY, minZ)
61 .addPoint(maxX, maxY, maxZ)
62 .addPoint(minX, maxY, maxZ)
64 .addPoint(minX, minY, maxZ)
65 .addPoint(minX, maxY, maxZ)
66 .addPoint(maxX, maxY, maxZ)
67 .addPoint(maxX, minY, maxZ)
69 .addPoint(minX, minY, minZ)
70 .addPoint(minX, maxY, minZ)
71 .addPoint(maxX, maxY, minZ)
72 .addPoint(maxX, minY, minZ)
74 .addPoint(minX, minY, minZ)
75 .addPoint(minX, minY, maxZ)
76 .addPoint(minX, maxY, maxZ)
77 .addPoint(minX, maxY, minZ)
79 .addPoint(maxX, minY, minZ)
80 .addPoint(maxX, minY, maxZ)
81 .addPoint(maxX, maxY, maxZ)
82 .addPoint(maxX, maxY, minZ);
87 void renderSphere(OffsetPoint center, double radius, Color color, int density, int dotSize) {
88 GL11.glEnable(GL11.GL_POINT_SMOOTH);
89 GL11.glPointSize(dotSize);
90 Renderer renderer = Renderer.startPoints()
92 buildPoints(center, radius, density)
93 .forEach(renderer::addPoint);
97 private Set<OffsetPoint> buildPoints(OffsetPoint center, double radius, int density) {
98 int segments = 24 + (density * 8);
100 Set<OffsetPoint> points = new HashSet<>(segments * segments);
102 double thetaSegment = PI / (double) segments;
103 double phiSegment = TAU / (double) segments;
105 for (double phi = 0.0D; phi < TAU; phi += phiSegment) {
106 for (double theta = 0.0D; theta < PI; theta += thetaSegment) {
107 double dx = radius * Math.sin(phi) * Math.cos(theta);
108 double dz = radius * Math.sin(phi) * Math.sin(theta);
109 double dy = radius * Math.cos(phi);
111 points.add(center.offset(dx, dy, dz));