| 1 | package net.minecraft.client.renderer.texture;
|
| 2 |
|
| 3 | import java.awt.image.BufferedImage;
|
| 4 | import java.io.IOException;
|
| 5 | import java.io.InputStream;
|
| 6 | import java.nio.IntBuffer;
|
| 7 | import javax.imageio.ImageIO;
|
| 8 | import net.minecraft.client.Minecraft;
|
| 9 | import net.minecraft.client.renderer.GLAllocation;
|
| 10 | import net.minecraft.client.renderer.GlStateManager;
|
| 11 | import net.minecraft.client.resources.IResourceManager;
|
| 12 | import net.minecraft.util.ResourceLocation;
|
| 13 | import org.apache.commons.io.IOUtils;
|
| 14 | import org.apache.logging.log4j.LogManager;
|
| 15 | import org.apache.logging.log4j.Logger;
|
| 16 | import org.lwjgl.opengl.GL11;
|
| 17 | import org.lwjgl.opengl.GL14;
|
| 18 | import org.lwjgl.opengl.GL12;
|
| 19 |
|
| 20 | public class TextureUtil
|
| 21 | {
|
| 22 | private static final Logger logger = LogManager.getLogger();
|
| 23 | private static final IntBuffer dataBuffer = GLAllocation.createDirectIntBuffer(4194304);
|
| 24 | public static final DynamicTexture missingTexture = new DynamicTexture(16, 16);
|
| 25 | public static final int[] missingTextureData = missingTexture.getTextureData();
|
| 26 | private static final int[] mipmapBuffer;
|
| 27 |
|
| 28 | public static int glGenTextures()
|
| 29 | {
|
| 30 | return GlStateManager.generateTexture();
|
| 31 | }
|
| 32 |
|
| 33 | public static void deleteTexture(int textureId)
|
| 34 | {
|
| 35 | GlStateManager.deleteTexture(textureId);
|
| 36 | }
|
| 37 |
|
| 38 | public static int uploadTextureImage(int p_110987_0_, BufferedImage p_110987_1_)
|
| 39 | {
|
| 40 | return uploadTextureImageAllocate(p_110987_0_, p_110987_1_, false, false);
|
| 41 | }
|
| 42 |
|
| 43 | public static void uploadTexture(int textureId, int[] p_110988_1_, int p_110988_2_, int p_110988_3_)
|
| 44 | {
|
| 45 | bindTexture(textureId);
|
| 46 | uploadTextureSub(0, p_110988_1_, p_110988_2_, p_110988_3_, 0, 0, false, false, false);
|
| 47 | }
|
| 48 |
|
| 49 | public static int[][] generateMipmapData(int p_147949_0_, int p_147949_1_, int[][] p_147949_2_)
|
| 50 | {
|
| 51 | int[][] aint = new int[p_147949_0_ + 1][];
|
| 52 | aint[0] = p_147949_2_[0];
|
| 53 |
|
| 54 | if (p_147949_0_ > 0)
|
| 55 | {
|
| 56 | boolean flag = false;
|
| 57 |
|
| 58 | for (int i = 0; i < p_147949_2_.length; ++i)
|
| 59 | {
|
| 60 | if (p_147949_2_[0][i] >> 24 == 0)
|
| 61 | {
|
| 62 | flag = true;
|
| 63 | break;
|
| 64 | }
|
| 65 | }
|
| 66 |
|
| 67 | for (int l1 = 1; l1 <= p_147949_0_; ++l1)
|
| 68 | {
|
| 69 | if (p_147949_2_[l1] != null)
|
| 70 | {
|
| 71 | aint[l1] = p_147949_2_[l1];
|
| 72 | }
|
| 73 | else
|
| 74 | {
|
| 75 | int[] aint1 = aint[l1 - 1];
|
| 76 | int[] aint2 = new int[aint1.length >> 2];
|
| 77 | int j = p_147949_1_ >> l1;
|
| 78 | int k = aint2.length / j;
|
| 79 | int l = j << 1;
|
| 80 |
|
| 81 | for (int i1 = 0; i1 < j; ++i1)
|
| 82 | {
|
| 83 | for (int j1 = 0; j1 < k; ++j1)
|
| 84 | {
|
| 85 | int k1 = 2 * (i1 + j1 * l);
|
| 86 | aint2[i1 + j1 * j] = blendColors(aint1[k1 + 0], aint1[k1 + 1], aint1[k1 + 0 + l], aint1[k1 + 1 + l], flag);
|
| 87 | }
|
| 88 | }
|
| 89 |
|
| 90 | aint[l1] = aint2;
|
| 91 | }
|
| 92 | }
|
| 93 | }
|
| 94 |
|
| 95 | return aint;
|
| 96 | }
|
| 97 |
|
| 98 | private static int blendColors(int p_147943_0_, int p_147943_1_, int p_147943_2_, int p_147943_3_, boolean p_147943_4_)
|
| 99 | {
|
| 100 | if (!p_147943_4_)
|
| 101 | {
|
| 102 | int i1 = blendColorComponent(p_147943_0_, p_147943_1_, p_147943_2_, p_147943_3_, 24);
|
| 103 | int j1 = blendColorComponent(p_147943_0_, p_147943_1_, p_147943_2_, p_147943_3_, 16);
|
| 104 | int k1 = blendColorComponent(p_147943_0_, p_147943_1_, p_147943_2_, p_147943_3_, 8);
|
| 105 | int l1 = blendColorComponent(p_147943_0_, p_147943_1_, p_147943_2_, p_147943_3_, 0);
|
| 106 | return i1 << 24 | j1 << 16 | k1 << 8 | l1;
|
| 107 | }
|
| 108 | else
|
| 109 | {
|
| 110 | mipmapBuffer[0] = p_147943_0_;
|
| 111 | mipmapBuffer[1] = p_147943_1_;
|
| 112 | mipmapBuffer[2] = p_147943_2_;
|
| 113 | mipmapBuffer[3] = p_147943_3_;
|
| 114 | float f = 0.0F;
|
| 115 | float f1 = 0.0F;
|
| 116 | float f2 = 0.0F;
|
| 117 | float f3 = 0.0F;
|
| 118 |
|
| 119 | for (int i = 0; i < 4; ++i)
|
| 120 | {
|
| 121 | if (mipmapBuffer[i] >> 24 != 0)
|
| 122 | {
|
| 123 | f += (float)Math.pow((double)((float)(mipmapBuffer[i] >> 24 & 255) / 255.0F), 2.2D);
|
| 124 | f1 += (float)Math.pow((double)((float)(mipmapBuffer[i] >> 16 & 255) / 255.0F), 2.2D);
|
| 125 | f2 += (float)Math.pow((double)((float)(mipmapBuffer[i] >> 8 & 255) / 255.0F), 2.2D);
|
| 126 | f3 += (float)Math.pow((double)((float)(mipmapBuffer[i] >> 0 & 255) / 255.0F), 2.2D);
|
| 127 | }
|
| 128 | }
|
| 129 |
|
| 130 | f = f / 4.0F;
|
| 131 | f1 = f1 / 4.0F;
|
| 132 | f2 = f2 / 4.0F;
|
| 133 | f3 = f3 / 4.0F;
|
| 134 | int i2 = (int)(Math.pow((double)f, 0.45454545454545453D) * 255.0D);
|
| 135 | int j = (int)(Math.pow((double)f1, 0.45454545454545453D) * 255.0D);
|
| 136 | int k = (int)(Math.pow((double)f2, 0.45454545454545453D) * 255.0D);
|
| 137 | int l = (int)(Math.pow((double)f3, 0.45454545454545453D) * 255.0D);
|
| 138 |
|
| 139 | if (i2 < 96)
|
| 140 | {
|
| 141 | i2 = 0;
|
| 142 | }
|
| 143 |
|
| 144 | return i2 << 24 | j << 16 | k << 8 | l;
|
| 145 | }
|
| 146 | }
|
| 147 |
|
| 148 | private static int blendColorComponent(int p_147944_0_, int p_147944_1_, int p_147944_2_, int p_147944_3_, int p_147944_4_)
|
| 149 | {
|
| 150 | float f = (float)Math.pow((double)((float)(p_147944_0_ >> p_147944_4_ & 255) / 255.0F), 2.2D);
|
| 151 | float f1 = (float)Math.pow((double)((float)(p_147944_1_ >> p_147944_4_ & 255) / 255.0F), 2.2D);
|
| 152 | float f2 = (float)Math.pow((double)((float)(p_147944_2_ >> p_147944_4_ & 255) / 255.0F), 2.2D);
|
| 153 | float f3 = (float)Math.pow((double)((float)(p_147944_3_ >> p_147944_4_ & 255) / 255.0F), 2.2D);
|
| 154 | float f4 = (float)Math.pow((double)(f + f1 + f2 + f3) * 0.25D, 0.45454545454545453D);
|
| 155 | return (int)((double)f4 * 255.0D);
|
| 156 | }
|
| 157 |
|
| 158 | public static void uploadTextureMipmap(int[][] p_147955_0_, int p_147955_1_, int p_147955_2_, int p_147955_3_, int p_147955_4_, boolean p_147955_5_, boolean p_147955_6_)
|
| 159 | {
|
| 160 | for (int i = 0; i < p_147955_0_.length; ++i)
|
| 161 | {
|
| 162 | int[] aint = p_147955_0_[i];
|
| 163 | uploadTextureSub(i, aint, p_147955_1_ >> i, p_147955_2_ >> i, p_147955_3_ >> i, p_147955_4_ >> i, p_147955_5_, p_147955_6_, p_147955_0_.length > 1);
|
| 164 | }
|
| 165 | }
|
| 166 |
|
| 167 | private static void uploadTextureSub(int p_147947_0_, int[] p_147947_1_, int p_147947_2_, int p_147947_3_, int p_147947_4_, int p_147947_5_, boolean p_147947_6_, boolean p_147947_7_, boolean p_147947_8_)
|
| 168 | {
|
| 169 | int i = 4194304 / p_147947_2_;
|
| 170 | setTextureBlurMipmap(p_147947_6_, p_147947_8_);
|
| 171 | setTextureClamped(p_147947_7_);
|
| 172 | int l;
|
| 173 |
|
| 174 | for (int j = 0; j < p_147947_2_ * p_147947_3_; j += p_147947_2_ * l)
|
| 175 | {
|
| 176 | int k = j / p_147947_2_;
|
| 177 | l = Math.min(i, p_147947_3_ - k);
|
| 178 | int i1 = p_147947_2_ * l;
|
| 179 | copyToBufferPos(p_147947_1_, j, i1);
|
| 180 | GL11.glTexSubImage2D(GL11.GL_TEXTURE_2D, p_147947_0_, p_147947_4_, p_147947_5_ + k, p_147947_2_, l, GL12.GL_BGRA, GL12.GL_UNSIGNED_INT_8_8_8_8_REV, (IntBuffer)dataBuffer);
|
| 181 | }
|
| 182 | }
|
| 183 |
|
| 184 | public static int uploadTextureImageAllocate(int p_110989_0_, BufferedImage p_110989_1_, boolean p_110989_2_, boolean p_110989_3_)
|
| 185 | {
|
| 186 | allocateTexture(p_110989_0_, p_110989_1_.getWidth(), p_110989_1_.getHeight());
|
| 187 | return uploadTextureImageSub(p_110989_0_, p_110989_1_, 0, 0, p_110989_2_, p_110989_3_);
|
| 188 | }
|
| 189 |
|
| 190 | public static void allocateTexture(int p_110991_0_, int p_110991_1_, int p_110991_2_)
|
| 191 | {
|
| 192 | allocateTextureImpl(p_110991_0_, 0, p_110991_1_, p_110991_2_);
|
| 193 | }
|
| 194 |
|
| 195 | public static void allocateTextureImpl(int p_180600_0_, int p_180600_1_, int p_180600_2_, int p_180600_3_)
|
| 196 | {
|
| 197 | deleteTexture(p_180600_0_);
|
| 198 | bindTexture(p_180600_0_);
|
| 199 |
|
| 200 | if (p_180600_1_ >= 0)
|
| 201 | {
|
| 202 | GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL12.GL_TEXTURE_MAX_LEVEL, p_180600_1_);
|
| 203 | GL11.glTexParameterf(GL11.GL_TEXTURE_2D, GL12.GL_TEXTURE_MIN_LOD, 0.0F);
|
| 204 | GL11.glTexParameterf(GL11.GL_TEXTURE_2D, GL12.GL_TEXTURE_MAX_LOD, (float)p_180600_1_);
|
| 205 | GL11.glTexParameterf(GL11.GL_TEXTURE_2D, GL14.GL_TEXTURE_LOD_BIAS, 0.0F);
|
| 206 | }
|
| 207 |
|
| 208 | for (int i = 0; i <= p_180600_1_; ++i)
|
| 209 | {
|
| 210 | GL11.glTexImage2D(GL11.GL_TEXTURE_2D, i, GL11.GL_RGBA, p_180600_2_ >> i, p_180600_3_ >> i, 0, GL12.GL_BGRA, GL12.GL_UNSIGNED_INT_8_8_8_8_REV, (IntBuffer)((IntBuffer)null));
|
| 211 | }
|
| 212 | }
|
| 213 |
|
| 214 | public static int uploadTextureImageSub(int textureId, BufferedImage p_110995_1_, int p_110995_2_, int p_110995_3_, boolean p_110995_4_, boolean p_110995_5_)
|
| 215 | {
|
| 216 | bindTexture(textureId);
|
| 217 | uploadTextureImageSubImpl(p_110995_1_, p_110995_2_, p_110995_3_, p_110995_4_, p_110995_5_);
|
| 218 | return textureId;
|
| 219 | }
|
| 220 |
|
| 221 | private static void uploadTextureImageSubImpl(BufferedImage p_110993_0_, int p_110993_1_, int p_110993_2_, boolean p_110993_3_, boolean p_110993_4_)
|
| 222 | {
|
| 223 | int i = p_110993_0_.getWidth();
|
| 224 | int j = p_110993_0_.getHeight();
|
| 225 | int k = 4194304 / i;
|
| 226 | int[] aint = new int[k * i];
|
| 227 | setTextureBlurred(p_110993_3_);
|
| 228 | setTextureClamped(p_110993_4_);
|
| 229 |
|
| 230 | for (int l = 0; l < i * j; l += i * k)
|
| 231 | {
|
| 232 | int i1 = l / i;
|
| 233 | int j1 = Math.min(k, j - i1);
|
| 234 | int k1 = i * j1;
|
| 235 | p_110993_0_.getRGB(0, i1, i, j1, aint, 0, i);
|
| 236 | copyToBuffer(aint, k1);
|
| 237 | GL11.glTexSubImage2D(GL11.GL_TEXTURE_2D, 0, p_110993_1_, p_110993_2_ + i1, i, j1, GL12.GL_BGRA, GL12.GL_UNSIGNED_INT_8_8_8_8_REV, (IntBuffer)dataBuffer);
|
| 238 | }
|
| 239 | }
|
| 240 |
|
| 241 | private static void setTextureClamped(boolean p_110997_0_)
|
| 242 | {
|
| 243 | if (p_110997_0_)
|
| 244 | {
|
| 245 | GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL11.GL_CLAMP);
|
| 246 | GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL11.GL_CLAMP);
|
| 247 | }
|
| 248 | else
|
| 249 | {
|
| 250 | GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL11.GL_REPEAT);
|
| 251 | GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL11.GL_REPEAT);
|
| 252 | }
|
| 253 | }
|
| 254 |
|
| 255 | private static void setTextureBlurred(boolean p_147951_0_)
|
| 256 | {
|
| 257 | setTextureBlurMipmap(p_147951_0_, false);
|
| 258 | }
|
| 259 |
|
| 260 | private static void setTextureBlurMipmap(boolean p_147954_0_, boolean p_147954_1_)
|
| 261 | {
|
| 262 | if (p_147954_0_)
|
| 263 | {
|
| 264 | GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, p_147954_1_ ? GL11.GL_LINEAR_MIPMAP_LINEAR : GL11.GL_LINEAR);
|
| 265 | GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
|
| 266 | }
|
| 267 | else
|
| 268 | {
|
| 269 | GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, p_147954_1_ ? GL11.GL_NEAREST_MIPMAP_LINEAR : GL11.GL_NEAREST);
|
| 270 | GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
|
| 271 | }
|
| 272 | }
|
| 273 |
|
| 274 | private static void copyToBuffer(int[] p_110990_0_, int p_110990_1_)
|
| 275 | {
|
| 276 | copyToBufferPos(p_110990_0_, 0, p_110990_1_);
|
| 277 | }
|
| 278 |
|
| 279 | private static void copyToBufferPos(int[] p_110994_0_, int p_110994_1_, int p_110994_2_)
|
| 280 | {
|
| 281 | int[] aint = p_110994_0_;
|
| 282 |
|
| 283 | if (Minecraft.getMinecraft().gameSettings.anaglyph)
|
| 284 | {
|
| 285 | aint = updateAnaglyph(p_110994_0_);
|
| 286 | }
|
| 287 |
|
| 288 | dataBuffer.clear();
|
| 289 | dataBuffer.put(aint, p_110994_1_, p_110994_2_);
|
| 290 | dataBuffer.position(0).limit(p_110994_2_);
|
| 291 | }
|
| 292 |
|
| 293 | static void bindTexture(int p_94277_0_)
|
| 294 | {
|
| 295 | GlStateManager.bindTexture(p_94277_0_);
|
| 296 | }
|
| 297 |
|
| 298 | public static int[] readImageData(IResourceManager resourceManager, ResourceLocation imageLocation) throws IOException
|
| 299 | {
|
| 300 | BufferedImage bufferedimage = readBufferedImage(resourceManager.getResource(imageLocation).getInputStream());
|
| 301 | int i = bufferedimage.getWidth();
|
| 302 | int j = bufferedimage.getHeight();
|
| 303 | int[] aint = new int[i * j];
|
| 304 | bufferedimage.getRGB(0, 0, i, j, aint, 0, i);
|
| 305 | return aint;
|
| 306 | }
|
| 307 |
|
| 308 | public static BufferedImage readBufferedImage(InputStream imageStream) throws IOException
|
| 309 | {
|
| 310 | BufferedImage bufferedimage;
|
| 311 |
|
| 312 | try
|
| 313 | {
|
| 314 | bufferedimage = ImageIO.read(imageStream);
|
| 315 | }
|
| 316 | finally
|
| 317 | {
|
| 318 | IOUtils.closeQuietly(imageStream);
|
| 319 | }
|
| 320 |
|
| 321 | return bufferedimage;
|
| 322 | }
|
| 323 |
|
| 324 | public static int[] updateAnaglyph(int[] p_110985_0_)
|
| 325 | {
|
| 326 | int[] aint = new int[p_110985_0_.length];
|
| 327 |
|
| 328 | for (int i = 0; i < p_110985_0_.length; ++i)
|
| 329 | {
|
| 330 | aint[i] = anaglyphColor(p_110985_0_[i]);
|
| 331 | }
|
| 332 |
|
| 333 | return aint;
|
| 334 | }
|
| 335 |
|
| 336 | public static int anaglyphColor(int p_177054_0_)
|
| 337 | {
|
| 338 | int i = p_177054_0_ >> 24 & 255;
|
| 339 | int j = p_177054_0_ >> 16 & 255;
|
| 340 | int k = p_177054_0_ >> 8 & 255;
|
| 341 | int l = p_177054_0_ & 255;
|
| 342 | int i1 = (j * 30 + k * 59 + l * 11) / 100;
|
| 343 | int j1 = (j * 30 + k * 70) / 100;
|
| 344 | int k1 = (j * 30 + l * 70) / 100;
|
| 345 | return i << 24 | i1 << 16 | j1 << 8 | k1;
|
| 346 | }
|
| 347 |
|
| 348 | public static void processPixelValues(int[] p_147953_0_, int p_147953_1_, int p_147953_2_)
|
| 349 | {
|
| 350 | int[] aint = new int[p_147953_1_];
|
| 351 | int i = p_147953_2_ / 2;
|
| 352 |
|
| 353 | for (int j = 0; j < i; ++j)
|
| 354 | {
|
| 355 | System.arraycopy(p_147953_0_, j * p_147953_1_, aint, 0, p_147953_1_);
|
| 356 | System.arraycopy(p_147953_0_, (p_147953_2_ - 1 - j) * p_147953_1_, p_147953_0_, j * p_147953_1_, p_147953_1_);
|
| 357 | System.arraycopy(aint, 0, p_147953_0_, (p_147953_2_ - 1 - j) * p_147953_1_, p_147953_1_);
|
| 358 | }
|
| 359 | }
|
| 360 |
|
| 361 | static
|
| 362 | {
|
| 363 | int i = -16777216;
|
| 364 | int j = -524040;
|
| 365 | int[] aint = new int[] { -524040, -524040, -524040, -524040, -524040, -524040, -524040, -524040};
|
| 366 | int[] aint1 = new int[] { -16777216, -16777216, -16777216, -16777216, -16777216, -16777216, -16777216, -16777216};
|
| 367 | int k = aint.length;
|
| 368 |
|
| 369 | for (int l = 0; l < 16; ++l)
|
| 370 | {
|
| 371 | System.arraycopy(l < k ? aint : aint1, 0, missingTextureData, 16 * l, k);
|
| 372 | System.arraycopy(l < k ? aint1 : aint, 0, missingTextureData, 16 * l + k, k);
|
| 373 | }
|
| 374 |
|
| 375 | missingTexture.updateDynamicTexture();
|
| 376 | mipmapBuffer = new int[4];
|
| 377 | }
|
| 378 | }
|
| 379 |
|