hnOsmium0001

ScissorTest.java

Dec 10th, 2019
485
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.49 KB | None | 0 0
  1. import com.google.common.base.Preconditions;
  2. import net.minecraft.client.MainWindow;
  3. import net.minecraft.client.Minecraft;
  4.  
  5. import static org.lwjgl.opengl.GL11.*;
  6.  
  7. // https://stackoverflow.com/questions/30254602/java-lwjgl-multiple-scissor-test
  8. public final class ScissorTest {
  9.  
  10.     private static final int max = 100;
  11.     private static ScissorTest[] objects = new ScissorTest[max];
  12.     private static int lastObject = -1;
  13.  
  14.     private boolean destroyed = false;
  15.     private int index;
  16.  
  17.     private int left;
  18.     private int right;
  19.     private int top;
  20.     private int bottom;
  21.  
  22.     public static ScissorTest scaled(int x, int y, int width, int height) {
  23.         MainWindow mainWindow = Minecraft.getInstance().mainWindow;
  24.         double scale = mainWindow.getGuiScaleFactor();
  25.         return new ScissorTest((int) (x * scale), (int) (mainWindow.getHeight() - ((y + height) * scale)),
  26.                 (int) (width * scale), (int) (height * scale));
  27.     }
  28.  
  29.     public ScissorTest(int x, int y, int width, int height) {
  30.         lastObject++;
  31.         if (lastObject < max) {
  32.             index = lastObject;
  33.             objects[index] = this;
  34.  
  35.             left = x;
  36.             right = x + width - 1;
  37.             top = y;
  38.             bottom = y + height - 1;
  39.  
  40.             if (index > 0) {
  41.                 ScissorTest parent = objects[index - 1];
  42.  
  43.                 if (left < parent.left) left = parent.left;
  44.                 if (right > parent.right) right = parent.right;
  45.                 if (top < parent.top) top = parent.top;
  46.                 if (bottom > parent.bottom) bottom = parent.bottom;
  47.             }
  48.  
  49.             resume();
  50.         } else {
  51.             Mod.logger.error("Scissor count limit reached: " + max);
  52.         }
  53.     }
  54.  
  55.     private void resume() {
  56.         scissor(left, top, right - left + 1, bottom - top + 1);
  57.         glEnable(GL_SCISSOR_TEST);
  58.     }
  59.  
  60.     public void destroy() {
  61.         Preconditions.checkState(!destroyed);
  62.  
  63.         if (index < lastObject) {
  64.             Mod.logger.error("There are scissors below this one");
  65.         }
  66.  
  67.         glDisable(GL_SCISSOR_TEST);
  68.  
  69.         objects[index] = null;
  70.         lastObject--;
  71.  
  72.         if (lastObject > -1)
  73.             objects[lastObject].resume(); // Resuming previous scissor
  74.  
  75.         destroyed = true;
  76.     }
  77.  
  78.     private static void scissor(int x, int y, int width, int height) {
  79.         if (width < 0) width = 0;
  80.         if (height < 0) height = 0;
  81.  
  82.         glScissor(x, y, width, height);
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment