2022-07-31 20:41:56 +02:00
/*
2024-04-17 17:23:43 +02:00
* This file is part of FalsePatternLib.
*
* Copyright (C) 2022-2024 FalsePattern
2022-07-30 15:21:11 +02:00
* All Rights Reserved
*
2024-04-17 17:23:43 +02:00
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
2022-07-30 15:21:11 +02:00
*
2024-04-17 17:23:43 +02:00
* FalsePatternLib is free software: you can redistribute it and/or modify
2022-07-30 15:21:11 +02:00
* 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.
*
2024-04-17 17:23:43 +02:00
* FalsePatternLib is distributed in the hope that it will be useful,
2022-07-30 15:21:11 +02:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
2024-04-17 17:23:43 +02:00
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
2022-07-30 15:21:11 +02:00
*
* You should have received a copy of the GNU Lesser General Public License
2024-04-17 17:23:43 +02:00
* along with FalsePatternLib. If not, see <https://www.gnu.org/licenses/>.
2022-07-30 15:21:11 +02:00
*/
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 ;
2024-07-15 07:46:17 +01:00
import com.falsepattern.lib.internal.Tags ;
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 ;
2024-07-15 07:46:17 +01:00
import net.minecraft.util.ResourceLocation ;
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 ();
2024-07-15 07:46:17 +01:00
private static final ResourceLocation EMPTY_TEXTURE = new ResourceLocation ( Tags . MODID , "textures/gui/empty_texture.png" );
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.
*/
2023-06-30 11:00:13 +01:00
@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
2023-07-16 13:09:05 +02:00
*
2023-06-30 11:08:40 +01:00
* @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 );
}
2024-07-15 07:46:17 +01:00
/**
* 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.
*/
2024-07-15 12:02:31 +02:00
@StableAPI.Expose ( since = "1.3.0" )
2024-07-15 07:46:17 +01:00
public static void bindEmptyTexture () {
getMinecraft (). renderEngine . bindTexture ( EMPTY_TEXTURE );
}
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
}