aboutsummaryrefslogtreecommitdiff
path: root/utils/renderLib2d.js
blob: 30ac28fc9ee9f58c163a76a7b1084f312123b8f0 (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
//--------------------------------------------------------------------------
//                          CODE BY DJtheRedstoner
//             IM COPYING THIS BECAUSE THE UPLOADED VERSION IS FOR
//                             CT 2.0.0 ONLY
//
//  Edit: iv added some features to this so might keep as is
//--------------------------------------------------------------------------

import { f, m } from "../../mappings/mappings";


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);

if(!GlStateManager){
    var GL11 = Java.type("org.lwjgl.opengl.GL11"); //using var so it goes to global scope
    var GlStateManager = Java.type("net.minecraft.client.renderer.GlStateManager");
}

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

const AxisAlignedBB = Java.type("net.minecraft.util.AxisAlignedBB")

register('renderWorld', () => {
    GlStateManager[m.pushMatrix]();

	let x = Player.getX();
	let y = Player.getY();
	let z = Player.getZ();

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

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

	GlStateManager[m.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[m.getScaleFactor](); // getScaleFactor
        let y = coords.get(1) / sr[m.getScaleFactor](); // getScaleFactor
        // OpenGL starts at bottom left, mc starts at top left
        y = sr[m.getScaledHeight]() - y; // getScaledHeight
    
        return { x, y, z };
    }

    static drawLine(x1, y1, z1, x2, y2, z2, color, thickness=1) {
        let pos1 = RenderLib2D.projectPoint(x1, y1, z1);
        let pos2 = RenderLib2D.projectPoint(x2, y2, z2);

        if(!pos1 || !pos2) return;

        let {x, y} = pos1
        let {x:ox, y:oy} = pos2

        // console.log(x, y, ox, oy, thickness)
        Renderer.drawLine(color, x, y, ox, oy, thickness);
    }

    // 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[f.minX.AxisAlignedBB], y: box[f.minY.AxisAlignedBB], z: box[f.minZ.AxisAlignedBB] });
        list.push({ x: box[f.maxX.AxisAlignedBB], y: box[f.minY.AxisAlignedBB], z: box[f.minZ.AxisAlignedBB] });
        list.push({ x: box[f.maxX.AxisAlignedBB], y: box[f.maxY.AxisAlignedBB], z: box[f.minZ.AxisAlignedBB] });
        list.push({ x: box[f.minX.AxisAlignedBB], y: box[f.maxY.AxisAlignedBB], z: box[f.minZ.AxisAlignedBB] });
        list.push({ x: box[f.minX.AxisAlignedBB], y: box[f.minY.AxisAlignedBB], z: box[f.maxZ.AxisAlignedBB] });
        list.push({ x: box[f.maxX.AxisAlignedBB], y: box[f.minY.AxisAlignedBB], z: box[f.maxZ.AxisAlignedBB] });
        list.push({ x: box[f.maxX.AxisAlignedBB], y: box[f.maxY.AxisAlignedBB], z: box[f.maxZ.AxisAlignedBB] });
        list.push({ x: box[f.minX.AxisAlignedBB], y: box[f.maxY.AxisAlignedBB], z: box[f.maxZ.AxisAlignedBB] });
    
        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);
    }

    static getBlockAABB = (x, y, z) => {
        return new AxisAlignedBB(x, y, z, x + 1, y + 1, z + 1);
    }
}