Files
FalsePatternLib/src/main/java/com/falsepattern/lib/util/RenderUtil.java
T

114 lines
4.3 KiB
Java
Raw Normal View History

2022-07-31 20:41:56 +02:00
/*
2023-07-16 12:36:11 +02:00
* Copyright (C) 2022-2023 FalsePattern
2022-07-30 15:21:11 +02:00
* All Rights Reserved
*
2023-07-16 12:36:11 +02:00
* The above copyright notice and this permission notice
2022-07-30 15:21:11 +02:00
* shall be included in all copies or substantial portions of the Software.
*
* This program 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.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2022-06-12 11:56:22 +02:00
package com.falsepattern.lib.util;
2022-06-12 11:58:13 +02:00
import com.falsepattern.lib.StableAPI;
2023-06-30 11:21:05 +01:00
import com.falsepattern.lib.internal.render.ClampedIcon;
2023-06-30 11:13:28 +01:00
import com.falsepattern.lib.internal.render.FullTextureIcon;
2022-07-30 23:26:25 +02:00
import lombok.SneakyThrows;
2023-06-30 10:57:30 +01:00
import lombok.experimental.UtilityClass;
2022-07-30 23:26:25 +02:00
import lombok.val;
2023-07-16 13:04:41 +02:00
import org.lwjgl.opengl.GL11;
2022-07-30 23:26:25 +02:00
2022-06-12 11:56:22 +02:00
import net.minecraft.client.Minecraft;
2023-06-30 11:21:05 +01:00
import net.minecraft.client.renderer.RenderBlocks;
2022-07-30 23:26:25 +02:00
import net.minecraft.util.IIcon;
2022-06-12 11:56:22 +02:00
import net.minecraft.util.Timer;
2022-07-30 23:26:25 +02:00
import cpw.mods.fml.relauncher.ReflectionHelper;
import cpw.mods.fml.relauncher.SideOnly;
2023-06-30 11:21:05 +01:00
2022-06-12 11:56:22 +02:00
import static cpw.mods.fml.relauncher.Side.CLIENT;
import static net.minecraft.client.Minecraft.getMinecraft;
@SideOnly(CLIENT)
2023-06-30 10:57:30 +01:00
@UtilityClass
2022-06-12 11:58:13 +02:00
@StableAPI(since = "0.8.0")
2022-06-12 11:56:22 +02:00
public final class RenderUtil {
2023-06-30 10:55:11 +01:00
private static final Timer MINECRAFT_TIMER = getMinecraftTimer();
2022-06-12 11:56:22 +02:00
2023-06-30 11:08:40 +01:00
/**
* 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);
}
2023-06-30 11:08:40 +01:00
/**
* 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.
*/
2022-08-06 14:18:55 +02:00
@StableAPI.Expose(since = "0.10.0")
2022-07-30 23:26:25 +02:00
public static IIcon getFullTextureIcon(String iconName, int width, int height) {
2023-06-30 11:13:28 +01:00
return new FullTextureIcon(iconName, width, height);
2022-07-30 23:26:25 +02:00
}
2023-06-30 11:21:05 +01:00
/**
* 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
*/
2023-07-03 11:34:18 +02:00
@StableAPI.Expose(since = "0.12.0")
2023-06-30 11:21:05 +01:00
public static IIcon wrapAsClampedIcon(IIcon icon) {
return new ClampedIcon(icon);
}
2023-06-30 11:08:40 +01:00
/**
* 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
*/
2022-08-06 13:12:55 +02:00
@StableAPI.Expose
2022-06-12 11:56:22 +02:00
public static float partialTick() {
return MINECRAFT_TIMER.renderPartialTicks;
}
2023-06-30 10:55:11 +01:00
@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());
}
2022-06-12 11:56:22 +02:00
}