aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/gregtech/common/render/GTRenderedTexture.java
blob: b6bffc955329baa16f9ac963753f53acac2e93ea (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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
package gregtech.common.render;

import static gregtech.api.util.LightingHelper.MAX_BRIGHTNESS;

import net.minecraft.block.Block;
import net.minecraft.client.renderer.RenderBlocks;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.IIcon;
import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection;

import com.gtnewhorizon.structurelib.alignment.IAlignment;
import com.gtnewhorizon.structurelib.alignment.IAlignmentProvider;
import com.gtnewhorizon.structurelib.alignment.enumerable.ExtendedFacing;
import com.gtnewhorizon.structurelib.alignment.enumerable.Flip;
import com.gtnewhorizon.structurelib.alignment.enumerable.Rotation;

import gregtech.GTMod;
import gregtech.api.interfaces.IColorModulationContainer;
import gregtech.api.interfaces.IIconContainer;
import gregtech.api.interfaces.ITexture;
import gregtech.api.interfaces.metatileentity.IMetaTileEntity;
import gregtech.api.interfaces.tileentity.IGregTechTileEntity;
import gregtech.api.util.LightingHelper;

public class GTRenderedTexture extends GTTextureBase implements ITexture, IColorModulationContainer {

    protected final IIconContainer mIconContainer;
    private final short[] mRGBa;
    private final boolean glow;
    private final boolean stdOrient;
    private final boolean useExtFacing;

    protected GTRenderedTexture(IIconContainer aIcon, short[] aRGBa, boolean allowAlpha, boolean glow,
        boolean stdOrient, boolean extFacing) {
        if (aRGBa.length != 4) throw new IllegalArgumentException("RGBa doesn't have 4 Values @ GTRenderedTexture");
        mIconContainer = aIcon;
        mRGBa = aRGBa;
        this.glow = glow;
        this.stdOrient = stdOrient;
        this.useExtFacing = extFacing;
    }

    @Override
    public boolean isOldTexture() {
        return false;
    }

    @Override
    public void renderXPos(RenderBlocks aRenderer, Block aBlock, int aX, int aY, int aZ) {
        startDrawingQuads(aRenderer, 1.0f, 0.0f, 0.0f);
        final boolean enableAO = aRenderer.enableAO;
        final LightingHelper lighting = new LightingHelper(aRenderer);
        if (glow) {
            if (!GTMod.gregtechproxy.mRenderGlowTextures) {
                draw(aRenderer);
                return;
            }
            aRenderer.enableAO = false;
            lighting.setLightnessOverride(1.0F);
            if (enableAO) lighting.setBrightnessOverride(MAX_BRIGHTNESS);
        }
        lighting.setupLightingXPos(aBlock, aX, aY, aZ)
            .setupColor(ForgeDirection.EAST, mRGBa);
        final ExtendedFacing rotation = getExtendedFacing(aX, aY, aZ);
        renderFaceXPos(aRenderer, aX, aY, aZ, mIconContainer.getIcon(), rotation);
        if (mIconContainer.getOverlayIcon() != null) {
            lighting.setupColor(ForgeDirection.EAST, 0xffffff);
            renderFaceXPos(aRenderer, aX, aY, aZ, mIconContainer.getOverlayIcon(), rotation);
        }
        aRenderer.enableAO = enableAO;
        draw(aRenderer);
    }

    @Override
    public void renderXNeg(RenderBlocks aRenderer, Block aBlock, int aX, int aY, int aZ) {
        startDrawingQuads(aRenderer, -1.0f, 0.0f, 0.0f);
        final boolean enableAO = aRenderer.enableAO;
        final LightingHelper lighting = new LightingHelper(aRenderer);
        if (glow) {
            if (!GTMod.gregtechproxy.mRenderGlowTextures) {
                draw(aRenderer);
                return;
            }
            aRenderer.enableAO = false;
            lighting.setLightnessOverride(1.0F);
            lighting.setBrightnessOverride(MAX_BRIGHTNESS);
        }
        lighting.setupLightingXNeg(aBlock, aX, aY, aZ)
            .setupColor(ForgeDirection.WEST, mRGBa);
        final ExtendedFacing rotation = getExtendedFacing(aX, aY, aZ);
        renderFaceXNeg(aRenderer, aX, aY, aZ, mIconContainer.getIcon(), rotation);
        if (mIconContainer.getOverlayIcon() != null) {
            lighting.setupColor(ForgeDirection.WEST, 0xffffff);
            renderFaceXNeg(aRenderer, aX, aY, aZ, mIconContainer.getOverlayIcon(), rotation);
        }
        aRenderer.enableAO = enableAO;
        draw(aRenderer);
    }

    @Override
    public void renderYPos(RenderBlocks aRenderer, Block aBlock, int aX, int aY, int aZ) {
        startDrawingQuads(aRenderer, 0.0f, 1.0f, 0.0f);
        final boolean enableAO = aRenderer.enableAO;
        final LightingHelper lighting = new LightingHelper(aRenderer);
        if (glow) {
            if (!GTMod.gregtechproxy.mRenderGlowTextures) {
                draw(aRenderer);
                return;
            }
            aRenderer.enableAO = false;
            lighting.setLightnessOverride(1.0F);
            lighting.setBrightnessOverride(MAX_BRIGHTNESS);
        }
        lighting.setupLightingYPos(aBlock, aX, aY, aZ)
            .setupColor(ForgeDirection.UP, mRGBa);
        final ExtendedFacing rotation = getExtendedFacing(aX, aY, aZ);
        renderFaceYPos(aRenderer, aX, aY, aZ, mIconContainer.getIcon(), rotation);
        if (mIconContainer.getOverlayIcon() != null) {
            lighting.setupColor(ForgeDirection.UP, 0xffffff);
            renderFaceYPos(aRenderer, aX, aY, aZ, mIconContainer.getOverlayIcon(), rotation);
        }
        aRenderer.enableAO = enableAO;
        draw(aRenderer);
    }

    @Override
    public void renderYNeg(RenderBlocks aRenderer, Block aBlock, int aX, int aY, int aZ) {
        startDrawingQuads(aRenderer, 0.0f, -1.0f, 0.0f);
        final boolean enableAO = aRenderer.enableAO;
        final LightingHelper lighting = new LightingHelper(aRenderer);
        if (glow) {
            if (!GTMod.gregtechproxy.mRenderGlowTextures) {
                draw(aRenderer);
                return;
            }
            aRenderer.enableAO = false;
            lighting.setLightnessOverride(1.0F);
            lighting.setBrightnessOverride(MAX_BRIGHTNESS);
        }
        lighting.setupLightingYNeg(aBlock, aX, aY, aZ)
            .setupColor(ForgeDirection.DOWN, mRGBa);
        final ExtendedFacing rotation = getExtendedFacing(aX, aY, aZ);
        renderFaceYNeg(aRenderer, aX, aY, aZ, mIconContainer.getIcon(), rotation);
        if (mIconContainer.getOverlayIcon() != null) {
            Tessellator.instance.setColorRGBA(255, 255, 255, 255);
            renderFaceYNeg(aRenderer, aX, aY, aZ, mIconContainer.getOverlayIcon(), rotation);
        }
        aRenderer.enableAO = enableAO;
        draw(aRenderer);
    }

    @Override
    public void renderZPos(RenderBlocks aRenderer, Block aBlock, int aX, int aY, int aZ) {
        startDrawingQuads(aRenderer, 0.0f, 0.0f, 1.0f);
        final boolean enableAO = aRenderer.enableAO;
        final LightingHelper lighting = new LightingHelper(aRenderer);
        if (glow) {
            if (!GTMod.gregtechproxy.mRenderGlowTextures) {
                draw(aRenderer);
                return;
            }
            aRenderer.enableAO = false;
            lighting.setLightnessOverride(1.0F);
            lighting.setBrightnessOverride(MAX_BRIGHTNESS);
        }
        lighting.setupLightingZPos(aBlock, aX, aY, aZ)
            .setupColor(ForgeDirection.SOUTH, mRGBa);
        final ExtendedFacing rotation = getExtendedFacing(aX, aY, aZ);
        renderFaceZPos(aRenderer, aX, aY, aZ, mIconContainer.getIcon(), rotation);
        if (mIconContainer.getOverlayIcon() != null) {
            lighting.setupColor(ForgeDirection.SOUTH, 0xffffff);
            renderFaceZPos(aRenderer, aX, aY, aZ, mIconContainer.getOverlayIcon(), rotation);
        }
        aRenderer.enableAO = enableAO;
        draw(aRenderer);
    }

    @Override
    public void renderZNeg(RenderBlocks aRenderer, Block aBlock, int aX, int aY, int aZ) {
        startDrawingQuads(aRenderer, 0.0f, 0.0f, -1.0f);
        final boolean enableAO = aRenderer.enableAO;
        final LightingHelper lighting = new LightingHelper(aRenderer);
        if (glow) {
            if (!GTMod.gregtechproxy.mRenderGlowTextures) {
                draw(aRenderer);
                return;
            }
            aRenderer.enableAO = false;
            lighting.setLightnessOverride(1.0F);
            lighting.setBrightnessOverride(MAX_BRIGHTNESS);
        }
        lighting.setupLightingZNeg(aBlock, aX, aY, aZ)
            .setupColor(ForgeDirection.NORTH, mRGBa);
        final ExtendedFacing rotation = getExtendedFacing(aX, aY, aZ);
        renderFaceZNeg(aRenderer, aX, aY, aZ, mIconContainer.getIcon(), rotation);
        if (mIconContainer.getOverlayIcon() != null) {
            lighting.setupColor(ForgeDirection.NORTH, 0xffffff);
            renderFaceZNeg(aRenderer, aX, aY, aZ, mIconContainer.getOverlayIcon(), rotation);
        }
        aRenderer.enableAO = enableAO;
        draw(aRenderer);
    }

    @Override
    public short[] getRGBA() {
        return mRGBa;
    }

    @Override
    public boolean isValidTexture() {
        return mIconContainer != null;
    }

    /**
     * Renders the given texture to the bottom face of the block. Args: block, x, y, z, texture
     */
    protected void renderFaceYNeg(RenderBlocks aRenderer, double x, double y, double z, IIcon icon,
        ExtendedFacing extendedFacing) {

        switch (useExtFacing ? extendedFacing.getRotation() : Rotation.NORMAL) {
            case COUNTER_CLOCKWISE -> aRenderer.uvRotateBottom = 2;
            case CLOCKWISE -> aRenderer.uvRotateBottom = 1;
            case UPSIDE_DOWN -> aRenderer.uvRotateBottom = 3;
            default -> aRenderer.uvRotateBottom = 0;
        }

        final Flip aFlip = extendedFacing.getFlip();
        aRenderer.renderFaceYNeg(
            Blocks.air,
            x,
            y,
            z,
            useExtFacing && GTMod.gregtechproxy.mRenderFlippedMachinesFlipped
                ? new GTIconFlipped(icon, aFlip.isHorizontallyFlipped() ^ !stdOrient, aFlip.isVerticallyFliped())
                : new GTIconFlipped(icon, !stdOrient, false));
        aRenderer.uvRotateBottom = 0;
    }

    /**
     * Renders the given texture to the top face of the block. Args: block, x, y, z, texture
     */
    protected void renderFaceYPos(RenderBlocks aRenderer, double x, double y, double z, IIcon icon,
        ExtendedFacing extendedFacing) {

        switch (useExtFacing ? extendedFacing.getRotation() : Rotation.NORMAL) {
            case COUNTER_CLOCKWISE -> aRenderer.uvRotateTop = 2;
            case CLOCKWISE -> aRenderer.uvRotateTop = 1;
            case UPSIDE_DOWN -> aRenderer.uvRotateTop = 3;
            default -> aRenderer.uvRotateTop = 0;
        }

        final Flip aFlip = extendedFacing.getFlip();
        aRenderer.renderFaceYPos(
            Blocks.air,
            x,
            y,
            z,
            useExtFacing && GTMod.gregtechproxy.mRenderFlippedMachinesFlipped
                ? new GTIconFlipped(icon, aFlip.isHorizontallyFlipped(), aFlip.isVerticallyFliped())
                : icon);
        aRenderer.uvRotateTop = 0;
    }

    /**
     * Renders the given texture to the north (z-negative) face of the block. Args: block, x, y, z, texture
     */
    protected void renderFaceZNeg(RenderBlocks aRenderer, double x, double y, double z, IIcon icon,
        ExtendedFacing extendedFacing) {
        aRenderer.field_152631_f = true;
        // **NOT A BUG**: aRenderer.uvRotateEast REALLY CONTROLS THE ROTATION OF THE NORTH SIDE
        switch (useExtFacing ? extendedFacing.getRotation() : Rotation.NORMAL) {
            case COUNTER_CLOCKWISE -> aRenderer.uvRotateEast = 2;
            case CLOCKWISE -> aRenderer.uvRotateEast = 1;
            case UPSIDE_DOWN -> aRenderer.uvRotateEast = 3;
            default -> aRenderer.uvRotateEast = 0;
        }

        final Flip aFlip = extendedFacing.getFlip();
        aRenderer.renderFaceZNeg(
            Blocks.air,
            x,
            y,
            z,
            useExtFacing && GTMod.gregtechproxy.mRenderFlippedMachinesFlipped
                ? new GTIconFlipped(icon, aFlip.isHorizontallyFlipped(), aFlip.isVerticallyFliped())
                : icon);
        aRenderer.uvRotateEast = 0;
        aRenderer.field_152631_f = false;
    }

    /**
     * Renders the given texture to the south (z-positive) face of the block. Args: block, x, y, z, texture
     */
    protected void renderFaceZPos(RenderBlocks aRenderer, double x, double y, double z, IIcon icon,
        ExtendedFacing extendedFacing) {
        // **NOT A BUG**: aRenderer.uvRotateWest REALLY CONTROLS THE ROTATION OF THE SOUTH SIDE
        switch (useExtFacing ? extendedFacing.getRotation() : Rotation.NORMAL) {
            case COUNTER_CLOCKWISE -> aRenderer.uvRotateWest = 2;
            case CLOCKWISE -> aRenderer.uvRotateWest = 1;
            case UPSIDE_DOWN -> aRenderer.uvRotateWest = 3;
            default -> aRenderer.uvRotateWest = 0;
        }

        final Flip aFlip = extendedFacing.getFlip();
        aRenderer.renderFaceZPos(
            Blocks.air,
            x,
            y,
            z,
            useExtFacing && GTMod.gregtechproxy.mRenderFlippedMachinesFlipped
                ? new GTIconFlipped(icon, aFlip.isHorizontallyFlipped(), aFlip.isVerticallyFliped())
                : icon);
        aRenderer.uvRotateWest = 0;
    }

    /**
     * Renders the given texture to the west (x-negative) face of the block. Args: block, x, y, z, texture
     */
    protected void renderFaceXNeg(RenderBlocks aRenderer, double x, double y, double z, IIcon icon,
        ExtendedFacing extendedFacing) {
        // **NOT A BUG**: aRenderer.uvRotateNorth REALLY CONTROLS THE ROTATION OF THE WEST SIDE
        switch (useExtFacing ? extendedFacing.getRotation() : Rotation.NORMAL) {
            case COUNTER_CLOCKWISE -> aRenderer.uvRotateNorth = 2;
            case CLOCKWISE -> aRenderer.uvRotateNorth = 1;
            case UPSIDE_DOWN -> aRenderer.uvRotateNorth = 3;
            default -> aRenderer.uvRotateNorth = 0;
        }

        final Flip aFlip = extendedFacing.getFlip();
        aRenderer.renderFaceXNeg(
            Blocks.air,
            x,
            y,
            z,
            useExtFacing && GTMod.gregtechproxy.mRenderFlippedMachinesFlipped
                ? new GTIconFlipped(icon, aFlip.isHorizontallyFlipped(), aFlip.isVerticallyFliped())
                : icon);
        aRenderer.uvRotateNorth = 0;
    }

    /**
     * Renders the given texture to the east (x-positive) face of the block. Args: block, x, y, z, texture
     */
    protected void renderFaceXPos(RenderBlocks aRenderer, double x, double y, double z, IIcon icon,
        ExtendedFacing extendedFacing) {
        aRenderer.field_152631_f = true;
        // **NOT A BUG**: aRenderer.uvRotateSouth REALLY CONTROLS THE ROTATION OF THE EAST SIDE
        switch (useExtFacing ? extendedFacing.getRotation() : Rotation.NORMAL) {
            case COUNTER_CLOCKWISE -> aRenderer.uvRotateSouth = 2;
            case CLOCKWISE -> aRenderer.uvRotateSouth = 1;
            case UPSIDE_DOWN -> aRenderer.uvRotateSouth = 3;
            default -> aRenderer.uvRotateSouth = 0;
        }

        final Flip aFlip = extendedFacing.getFlip();
        aRenderer.renderFaceXPos(
            Blocks.air,
            x,
            y,
            z,
            useExtFacing && GTMod.gregtechproxy.mRenderFlippedMachinesFlipped
                ? new GTIconFlipped(icon, aFlip.isHorizontallyFlipped(), aFlip.isVerticallyFliped())
                : icon);
        aRenderer.uvRotateSouth = 0;
        aRenderer.field_152631_f = false;
    }

    private ExtendedFacing getExtendedFacing(int x, int y, int z) {
        if (stdOrient) return ExtendedFacing.DEFAULT;
        final EntityPlayer player = GTMod.gregtechproxy.getThePlayer();
        if (player == null) return ExtendedFacing.DEFAULT;
        final World w = player.getEntityWorld();
        if (w == null) return ExtendedFacing.DEFAULT;
        final TileEntity te = w.getTileEntity(x, y, z);
        IAlignment alignment = null;
        if (te instanceof IGregTechTileEntity) {
            final IMetaTileEntity meta = ((IGregTechTileEntity) te).getMetaTileEntity();
            if (meta instanceof IAlignmentProvider) {
                alignment = ((IAlignmentProvider) meta).getAlignment();
            } else if (meta != null) {
                return ExtendedFacing.of(
                    meta.getBaseMetaTileEntity()
                        .getFrontFacing());
            }
        } else if (te instanceof IAlignmentProvider) {
            alignment = ((IAlignmentProvider) te).getAlignment();
        }
        if (alignment != null) return alignment.getExtendedFacing();
        return ExtendedFacing.DEFAULT;
    }
}