-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathRenderUtil.java
133 lines (121 loc) · 5.07 KB
/
RenderUtil.java
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
/*
* This file is part of FalsePatternLib.
*
* Copyright (C) 2022-2024 FalsePattern
* All Rights Reserved
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* FalsePatternLib is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* FalsePatternLib is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with FalsePatternLib. If not, see <https://www.gnu.org/licenses/>.
*/
package com.falsepattern.lib.util;
import com.falsepattern.lib.StableAPI;
import com.falsepattern.lib.internal.Tags;
import com.falsepattern.lib.internal.render.ClampedIcon;
import com.falsepattern.lib.internal.render.FullTextureIcon;
import lombok.SneakyThrows;
import lombok.experimental.UtilityClass;
import lombok.val;
import net.minecraft.util.ResourceLocation;
import org.lwjgl.opengl.GL11;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.RenderBlocks;
import net.minecraft.util.IIcon;
import net.minecraft.util.Timer;
import cpw.mods.fml.relauncher.ReflectionHelper;
import cpw.mods.fml.relauncher.SideOnly;
import static cpw.mods.fml.relauncher.Side.CLIENT;
import static net.minecraft.client.Minecraft.getMinecraft;
@SideOnly(CLIENT)
@UtilityClass
@StableAPI(since = "0.8.0")
public final class RenderUtil {
private static final Timer MINECRAFT_TIMER = getMinecraftTimer();
private static final ResourceLocation EMPTY_TEXTURE = new ResourceLocation(Tags.MODID, "textures/empty_texture.png");
/**
* Sets the OpenGL translation, relative to the player's position.
* <p>
* This is useful for rendering things that are not part of the world mesh, but should be rendered as if they were.
* <p>
* It's good practice to make this call inside a {@link GL11#glPushMatrix() push}/{@link GL11#glPopMatrix() pop} matrix block.
*/
@StableAPI.Expose(since = "0.12.0")
public static void setGLTranslationRelativeToPlayer() {
val player = getMinecraft().thePlayer;
val partialTick = partialTick();
val offsetX = (float) (player.lastTickPosX + (player.posX - player.lastTickPosX) * partialTick);
val offsetY = (float) (player.lastTickPosY + (player.posY - player.lastTickPosY) * partialTick);
val offsetZ = (float) (player.lastTickPosZ + (player.posZ - player.lastTickPosZ) * partialTick);
GL11.glTranslatef(-offsetX, -offsetY, -offsetZ);
}
/**
* Provides a texture icon with the given name and dimensions.
* <p>
* This is useful for rendering textures that are not part of the Minecraft texture atlas.
*
* @param iconName The icon name
* @param width The icon width in pixels
* @param height The icon height in pixels
*
* @return The full resolution texture icon.
*/
@StableAPI.Expose(since = "0.10.0")
public static IIcon getFullTextureIcon(String iconName, int width, int height) {
return new FullTextureIcon(iconName, width, height);
}
/**
* Wraps the given icon as a clamped icon.
* <p>
* A clamped icon will clamp the coordinates given to {@link IIcon#getInterpolatedU(double)} and {@link IIcon#getInterpolatedV(double)} to the range of 0 to 16.
* <p>
* This is helpful when using {@link RenderBlocks} but having different bounds.
*
* @param icon The icon to clamp
*/
@StableAPI.Expose(since = "0.12.0")
public static IIcon wrapAsClampedIcon(IIcon icon) {
return new ClampedIcon(icon);
}
/**
* Binds an empty texture.
* <p>
* When rendering without shaders, using {@link GL11#glDisable(int)} with {@link GL11#GL_TEXTURE_2D}
* is sufficient to achieve the same effect.
* <p>
* However, when shaders are enabled, disabling textures using this method will have no effect. Therefore this method can be used as a workaround.
*/
@StableAPI.Expose(since = "1.3.0")
public static void bindEmptyTexture() {
getMinecraft().renderEngine.bindTexture(EMPTY_TEXTURE);
}
/**
* Provides the partial tick between the last and next client tick, in the range of 0 to 1.
* <p>
* Sometimes referred to as 'subTick', it is used mostly for interpolation in rendering.
*
* @return The current partial tick
*/
@StableAPI.Expose
public static float partialTick() {
return MINECRAFT_TIMER.renderPartialTicks;
}
@StableAPI.Expose
@SneakyThrows
private static Timer getMinecraftTimer() {
val timerField = ReflectionHelper.findField(Minecraft.class, "timer", "field_71428_T");
timerField.setAccessible(true);
return (Timer) timerField.get(getMinecraft());
}
}