aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/gregtech/common/render/BlackholeRenderer.java
blob: 733bd3aa9eab383c52451d62d7776427ce8a8ab8 (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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package gregtech.common.render;

import static gregtech.api.enums.Mods.GregTech;

import java.nio.FloatBuffer;

import net.minecraft.client.renderer.ActiveRenderInfo;
import net.minecraft.client.renderer.tileentity.TileEntitySpecialRenderer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.client.model.AdvancedModelLoader;

import org.joml.Matrix4fStack;
import org.joml.Vector4f;
import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL20;

import com.gtnewhorizon.gtnhlib.client.renderer.CapturingTessellator;
import com.gtnewhorizon.gtnhlib.client.renderer.TessellatorManager;
import com.gtnewhorizon.gtnhlib.client.renderer.shader.ShaderProgram;
import com.gtnewhorizon.gtnhlib.client.renderer.vbo.IModelCustomExt;
import com.gtnewhorizon.gtnhlib.client.renderer.vbo.VertexBuffer;
import com.gtnewhorizon.gtnhlib.client.renderer.vertex.DefaultVertexFormat;

import cpw.mods.fml.client.registry.ClientRegistry;
import gregtech.common.tileentities.render.TileEntityBlackhole;

public class BlackholeRenderer extends TileEntitySpecialRenderer {

    private boolean initialized = false;

    private ShaderProgram blackholeProgram;
    private static int u_CameraPosition = -1, u_Scale = -1, u_Time = -1, u_Stability = -1;
    private static final Matrix4fStack modelMatrixStack = new Matrix4fStack(4);

    private static IModelCustomExt blackholeModel;
    private static ResourceLocation blackholeTexture;
    private static float modelScale = .5f;

    private ShaderProgram laserProgram;
    private static int u_LaserCameraPosition = -1, u_LaserColor = -1, u_LaserModelMatrix = -1;
    private static VertexBuffer laserVBO;
    private static ResourceLocation laserTexture;

    private static final Matrix4fStack modelMatrix = new Matrix4fStack(2);

    private static final float WIDTH = .1f;
    private static final float EXCLUSION = 1f;

    public BlackholeRenderer() {
        ClientRegistry.bindTileEntitySpecialRenderer(TileEntityBlackhole.class, this);
    }

    private void init() {
        try {
            blackholeProgram = new ShaderProgram(
                GregTech.resourceDomain,
                "shaders/blackhole.vert.glsl",
                "shaders/blackhole.frag.glsl");

            u_CameraPosition = blackholeProgram.getUniformLocation("u_CameraPosition");

            u_Scale = blackholeProgram.getUniformLocation("u_Scale");
            u_Time = blackholeProgram.getUniformLocation("u_Time");
            u_Stability = blackholeProgram.getUniformLocation("u_Stability");

        } catch (Exception e) {
            System.out.println(e.getMessage());
            return;
        }

        blackholeModel = (IModelCustomExt) AdvancedModelLoader
            .loadModel(new ResourceLocation(GregTech.resourceDomain, "textures/model/blackhole.obj"));
        blackholeTexture = new ResourceLocation(GregTech.resourceDomain, "textures/model/blackhole.png");

        blackholeProgram.use();
        GL20.glUniform1f(u_Scale, modelScale);
        GL20.glUniform1f(u_Stability, .1f);
        ShaderProgram.clear();

        try {
            laserProgram = new ShaderProgram(
                GregTech.resourceDomain,
                "shaders/laser.vert.glsl",
                "shaders/laser.frag.glsl");
            u_LaserCameraPosition = laserProgram.getUniformLocation("u_CameraPosition");
            u_LaserColor = laserProgram.getUniformLocation("u_Color");
            u_LaserModelMatrix = laserProgram.getUniformLocation("u_ModelMatrix");

        } catch (Exception e) {
            System.out.println(e.getMessage());
            return;
        }

        laserTexture = new ResourceLocation(GregTech.resourceDomain, "textures/model/laser.png");

        TessellatorManager.startCapturing();
        CapturingTessellator tess = (CapturingTessellator) TessellatorManager.get();

        tess.startDrawingQuads();

        tess.addVertexWithUV(.5 + 8, 0, -WIDTH, 0, 0);
        tess.addVertexWithUV(.5 + 8, 0, WIDTH, 0, 1);
        tess.addVertexWithUV(EXCLUSION, 0, WIDTH / 5, 1, 1);
        tess.addVertexWithUV(EXCLUSION, 0, -WIDTH / 5, 1, 0);

        tess.addVertexWithUV(-.5 - 8, 0, -WIDTH, 0, 0);
        tess.addVertexWithUV(-.5 - 8, 0, WIDTH, 0, 1);
        tess.addVertexWithUV(-EXCLUSION, 0, WIDTH / 5, 1, 1);
        tess.addVertexWithUV(-EXCLUSION, 0, -WIDTH / 5, 1, 0);

        tess.draw();

        laserVBO = TessellatorManager.stopCapturingToVBO(DefaultVertexFormat.POSITION_TEXTURE_NORMAL);

        initialized = true;
    }

    private void renderBlackHole(TileEntityBlackhole tile, double x, double y, double z, float timer) {

        blackholeProgram.use();
        bindTexture(blackholeTexture);
        GL20.glUniform1f(u_Stability, tile.getStability());
        modelMatrixStack.clear();

        float xLocal = ((float) x + .5f);
        float yLocal = ((float) y + .5f);
        float zLocal = ((float) z + .5f);
        GL11.glPushAttrib(GL11.GL_ALL_ATTRIB_BITS);
        GL11.glPushMatrix();
        GL20.glUniform3f(
            u_CameraPosition,
            ActiveRenderInfo.objectX - xLocal,
            ActiveRenderInfo.objectY - yLocal,
            ActiveRenderInfo.objectZ - zLocal);

        GL20.glUniform1f(u_Time, timer);
        GL11.glTranslated(x + .5f, y + .5f, z + .5f);
        blackholeModel.renderAllVBO();

        GL11.glPopMatrix();
        GL11.glPopAttrib();
        ShaderProgram.clear();
    }

    private void renderLasers(TileEntityBlackhole tile, double x, double y, double z, float timer) {
        laserProgram.use();
        bindTexture(laserTexture);

        float cx = ((float) x + .5f);
        float cy = ((float) y + .5f);
        float cz = ((float) z + .5f);
        GL11.glPushAttrib(GL11.GL_ALL_ATTRIB_BITS);
        GL11.glDisable(GL11.GL_CULL_FACE);
        GL11.glPushMatrix();
        GL20.glUniform3f(u_LaserColor, tile.getLaserR(), tile.getLaserG(), tile.getLaserB());

        modelMatrix.clear();
        modelMatrix.translate(cx, cy, cz);

        // First set
        FloatBuffer matrixBuffer = BufferUtils.createFloatBuffer(16);
        GL20.glUniformMatrix4(u_LaserModelMatrix, false, modelMatrix.get(matrixBuffer));
        modelMatrix.pushMatrix();
        modelMatrix.invert();
        Vector4f cameraPosition = new Vector4f(
            ActiveRenderInfo.objectX,
            ActiveRenderInfo.objectY,
            ActiveRenderInfo.objectZ,
            1);
        cameraPosition = modelMatrix.transform(cameraPosition);
        GL20.glUniform3f(u_LaserCameraPosition, cameraPosition.x, cameraPosition.y, cameraPosition.z);
        laserVBO.render();

        // Second set

        modelMatrix.popMatrix();
        matrixBuffer.clear();
        modelMatrix.rotate((float) Math.PI / 2, 0, 1, 0);

        GL20.glUniformMatrix4(u_LaserModelMatrix, false, modelMatrix.get(matrixBuffer));

        modelMatrix.invert();
        cameraPosition.set(ActiveRenderInfo.objectX, ActiveRenderInfo.objectY, ActiveRenderInfo.objectZ, 1);
        cameraPosition = modelMatrix.transform(cameraPosition);
        GL20.glUniform3f(u_LaserCameraPosition, cameraPosition.x, cameraPosition.y, cameraPosition.z);
        laserVBO.render();

        GL11.glPopMatrix();
        GL11.glPopAttrib();
        ShaderProgram.clear();
    }

    public void renderTileEntityAt(TileEntity tile, double x, double y, double z, float timeSinceLastTick) {
        if (!(tile instanceof TileEntityBlackhole blackhole)) return;

        if (!initialized) {
            init();
            if (!initialized) return;
        }
        if (((TileEntityBlackhole) tile).getLaserRender()) {
            renderLasers(
                blackhole,
                x,
                y,
                z,
                tile.getWorldObj()
                    .getWorldTime() + timeSinceLastTick);
        }

        renderBlackHole(
            blackhole,
            x,
            y,
            z,
            tile.getWorldObj()
                .getWorldTime() + timeSinceLastTick);

    }

}