aboutsummaryrefslogtreecommitdiff
path: root/utils/renderLib2d.js
blob: b2defd10ac1579171977bdabcf955be076ba934b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
//--------------------------------------------------------------------------
//                          CODE BY DJtheRedstoner
//             IM COPYING THIS BECAUSE THE UPLOADED VERSION IS FOR
//                             CT 2.0.0 ONLY
//--------------------------------------------------------------------------

//TODO: just require the module when ct 2.0 comes out



const BufferUtils = org.lwjgl.BufferUtils;
const Project = org.lwjgl.util.glu.Project;

const modelViewMatrix = BufferUtils.createFloatBuffer(16);
const projectionMatrix = BufferUtils.createFloatBuffer(16);
const viewportDims = BufferUtils.createIntBuffer(16);

const ScaledResolution = net.minecraft.client.gui.ScaledResolution;

register('renderWorld', () => {
    Tessellator.pushMatrix();

	let x = Player.getRenderX();
	let y = Player.getRenderY();
	let z = Player.getRenderZ();

	Tessellator.translate(-x, -y, -z);

	GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, modelViewMatrix);
	GL11.glGetFloat(GL11.GL_PROJECTION_MATRIX, projectionMatrix);

	Tessellator.popMatrix();

	GL11.glGetInteger(GL11.GL_VIEWPORT, viewportDims);
});

export default class RenderLib2D {
    // Utils

    // Original made by DJtheRedstoner
    static projectPoint = (posX, posY, posZ) => {
        const coords = BufferUtils.createFloatBuffer(3);
        const success = Project.gluProject(
            posX,
            posY,
            posZ,
            modelViewMatrix,
            projectionMatrix,
            viewportDims,
            coords
        );
    
        const z = coords.get(2);
        if (!success || !(z > 0 && z < 1)) return null;
    
        const sr = new ScaledResolution(Client.getMinecraft());
    
        const x = coords.get(0) / sr.func_78325_e(); // getScaleFactor
        let y = coords.get(1) / sr.func_78325_e(); // getScaleFactor
        // OpenGL starts at bottom left, mc starts at top left
        y = sr.func_78328_b() - y; // getScaledHeight
    
        return { x, y, z };
    }

    // Original made by DJtheRedstoner
    static calculateBoundingBox = (box) => {
        let vertices = RenderLib2D.getVertices(box);
    
        let x1 = java.lang.Float.MAX_VALUE;
        let x2 = 0;
        let y1 = java.lang.Float.MAX_VALUE;
        let y2 = 0;

        vertices.forEach(vertex => {
            let vec = RenderLib2D.projectPoint(vertex.x, vertex.y, vertex.z);
            if (vec == null) return null;
    
            let x = vec.x;
            let y = vec.y;
    
            if (x < x1) x1 = x;
            if (x > x2) x2 = x;
            if (y < y1) y1 = y;
            if (y > y2) y2 = y;
        });
    
        return { x1, y1, x2, y2 };
    }
    
    static getVertices = (box) => {
        let list = [];
    
        list.push({ x: box.field_72340_a, y: box.field_72338_b, z: box.field_72339_c });
        list.push({ x: box.field_72336_d, y: box.field_72338_b, z: box.field_72339_c });
        list.push({ x: box.field_72336_d, y: box.field_72337_e, z: box.field_72339_c });
        list.push({ x: box.field_72340_a, y: box.field_72337_e, z: box.field_72339_c });
        list.push({ x: box.field_72340_a, y: box.field_72338_b, z: box.field_72334_f });
        list.push({ x: box.field_72336_d, y: box.field_72338_b, z: box.field_72334_f });
        list.push({ x: box.field_72336_d, y: box.field_72337_e, z: box.field_72334_f });
        list.push({ x: box.field_72340_a, y: box.field_72337_e, z: box.field_72334_f });
    
        return list;
    }
    
    // Rendering Functions

    static drawNameTag = (vec, string) => {
        if (vec === null) return;
    
        Renderer.drawStringWithShadow(string, vec.x - Renderer.getStringWidth(string) / 2, vec.y);
    }

    static draw2DESP = (aabb, color, thickness) => {
        let bb = RenderLib2D.calculateBoundingBox(aabb);
    
        Renderer.drawLine(color, bb.x1, bb.y1, bb.x1, bb.y2, thickness);
        Renderer.drawLine(color, bb.x1, bb.y1, bb.x2, bb.y1, thickness);
        Renderer.drawLine(color, bb.x2, bb.y2, bb.x2, bb.y1, thickness);
        Renderer.drawLine(color, bb.x2, bb.y2, bb.x1, bb.y2, thickness);
    }

    static draw3DESP = (aabb, color, thickness) => {
        let vertices = RenderLib2D.getVertices(aabb);
        let projected = [];

        vertices.forEach(vertex => {
            let vec = RenderLib2D.projectPoint(vertex.x, vertex.y, vertex.z);
            if (vec == null) return null;
            projected.push(vec);
        });

        if (projected[0] && projected[1]) Renderer.drawLine(color, projected[0].x, projected[0].y, projected[1].x, projected[1].y, thickness);
        if (projected[0] && projected[4]) Renderer.drawLine(color, projected[0].x, projected[0].y, projected[4].x, projected[4].y, thickness);
        if (projected[5] && projected[1]) Renderer.drawLine(color, projected[5].x, projected[5].y, projected[1].x, projected[1].y, thickness);
        if (projected[5] && projected[4]) Renderer.drawLine(color, projected[5].x, projected[5].y, projected[4].x, projected[4].y, thickness);
        if (projected[3] && projected[2]) Renderer.drawLine(color, projected[3].x, projected[3].y, projected[2].x, projected[2].y, thickness);
        if (projected[3] && projected[7]) Renderer.drawLine(color, projected[3].x, projected[3].y, projected[7].x, projected[7].y, thickness);
        if (projected[6] && projected[2]) Renderer.drawLine(color, projected[6].x, projected[6].y, projected[2].x, projected[2].y, thickness);
        if (projected[6] && projected[7]) Renderer.drawLine(color, projected[6].x, projected[6].y, projected[7].x, projected[7].y, thickness);
        if (projected[1] && projected[2]) Renderer.drawLine(color, projected[1].x, projected[1].y, projected[2].x, projected[2].y, thickness);
        if (projected[0] && projected[3]) Renderer.drawLine(color, projected[0].x, projected[0].y, projected[3].x, projected[3].y, thickness);
        if (projected[4] && projected[7]) Renderer.drawLine(color, projected[4].x, projected[4].y, projected[7].x, projected[7].y, thickness);
        if (projected[5] && projected[6]) Renderer.drawLine(color, projected[5].x, projected[5].y, projected[6].x, projected[6].y, thickness);
    }
}