]> git.lizzy.rs Git - BoundingBoxOutlineReloaded.git/blob - src/main/java/com/irtimaled/bbor/client/renderers/Renderer.java
8c354f1581f2e159c9df0c99547435ba2f10271f
[BoundingBoxOutlineReloaded.git] / src / main / java / com / irtimaled / bbor / client / renderers / Renderer.java
1 package com.irtimaled.bbor.client.renderers;
2
3 import com.irtimaled.bbor.client.Camera;
4 import net.minecraft.client.render.BufferBuilder;
5 import net.minecraft.client.render.Tessellator;
6 import net.minecraft.client.render.VertexFormat;
7 import net.minecraft.client.render.VertexFormats;
8 import net.minecraft.client.util.math.MatrixStack;
9
10 import java.awt.*;
11
12 public class Renderer {
13     private final VertexFormat.DrawMode glMode;
14
15     static Renderer startLines() {
16         return new Renderer(VertexFormat.DrawMode.LINES, VertexFormats.POSITION_COLOR);
17     }
18
19     static Renderer startLineLoop() {
20         return new Renderer(VertexFormat.DrawMode.DEBUG_LINE_STRIP, VertexFormats.POSITION_COLOR);
21     }
22
23     public static Renderer startQuads() {
24         return new Renderer(VertexFormat.DrawMode.QUADS, VertexFormats.POSITION_COLOR);
25     }
26
27     public static Renderer startTextured() {
28         return new Renderer(VertexFormat.DrawMode.QUADS, VertexFormats.POSITION_TEXTURE_COLOR);
29     }
30
31     private static final Tessellator tessellator = Tessellator.getInstance();
32     private static final BufferBuilder bufferBuilder = tessellator.getBuffer();
33
34     private int red;
35     private int green;
36     private int blue;
37     private int alpha;
38     private MatrixStack matrixStack;
39
40     private Renderer(VertexFormat.DrawMode glMode, VertexFormat vertexFormat) {
41         bufferBuilder.begin(glMode, vertexFormat);
42         this.glMode = glMode;
43     }
44
45     public Renderer setMatrixStack(MatrixStack stack) {
46         this.matrixStack = stack;
47         matrixStack.push();
48         return this;
49     }
50
51     public Renderer setColor(Color color) {
52         return setColor(color.getRed(), color.getGreen(), color.getBlue())
53                 .setAlpha(color.getAlpha());
54     }
55
56     public Renderer setColor(int red, int green, int blue) {
57         this.red = red;
58         this.green = green;
59         this.blue = blue;
60         return this;
61     }
62
63     public Renderer setAlpha(int alpha) {
64         this.alpha = alpha;
65         return this;
66     }
67
68     Renderer addPoint(OffsetPoint point) {
69         return addPoint(point.getX(), point.getY(), point.getZ());
70     }
71
72     public Renderer addPoints(OffsetPoint[] points) {
73         Renderer renderer = this;
74         for (OffsetPoint point : points) {
75             renderer = renderer.addPoint(point);
76         }
77         return renderer;
78     }
79
80     public Renderer addPoint(double x, double y, double z) {
81         matrixStack.push();
82         pos(x, y, z);
83         color();
84         end();
85         matrixStack.pop();
86         return this;
87     }
88
89     public Renderer addPoint(double x, double y, double z, double u, double v) {
90         pos(x, y, z);
91         tex(u, v);
92         color();
93         end();
94         return this;
95     }
96
97     public void render() {
98         if (glMode == VertexFormat.DrawMode.QUADS) {
99             bufferBuilder.setCameraPosition((float) Camera.getX(), (float) Camera.getY(), (float) Camera.getZ());
100         }
101         tessellator.draw();
102         matrixStack.pop();
103     }
104
105     private void pos(double x, double y, double z) {
106         bufferBuilder.vertex(matrixStack.peek().getModel(), (float) x, (float) y, (float) z);
107     }
108
109     private void tex(double u, double v) {
110         bufferBuilder.texture((float) u, (float) v);
111     }
112
113     private void color() {
114         bufferBuilder.color(red / 255F, green / 255F, blue / 255F, alpha / 255F);
115     }
116
117     private void end() {
118         bufferBuilder.next();
119     }
120 }