MatrixStack.java 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package me.hammerle.snuviengine.api;
  2. public final class MatrixStack {
  3. private final int capacity;
  4. private final float[][] stack;
  5. private int index = 0;
  6. protected MatrixStack(int capacity) {
  7. this.capacity = capacity;
  8. stack = new float[capacity][16];
  9. stack[index][0] = 1.0f;
  10. stack[index][5] = 1.0f;
  11. stack[index][10] = 1.0f;
  12. stack[index][15] = 1.0f;
  13. }
  14. protected float[] getData() {
  15. return stack[index];
  16. }
  17. protected boolean push() {
  18. if(index + 1 >= capacity) {
  19. return false;
  20. }
  21. index++;
  22. System.arraycopy(stack[index - 1], 0, stack[index], 0, 16);
  23. return true;
  24. }
  25. protected boolean pop() {
  26. if(index - 1 < 0) {
  27. return false;
  28. }
  29. index--;
  30. return true;
  31. }
  32. protected void scale(float sx, float sy) {
  33. stack[index][0] *= sx;
  34. stack[index][1] *= sx;
  35. stack[index][4] *= sy;
  36. stack[index][5] *= sy;
  37. }
  38. protected void translate(float tx, float ty) {
  39. stack[index][12] += stack[index][0] * tx + stack[index][4] * ty;
  40. stack[index][13] += stack[index][1] * tx + stack[index][5] * ty;
  41. }
  42. protected void translateTo(float tx, float ty) {
  43. stack[index][0] = 1.0f;
  44. stack[index][1] = 0.0f;
  45. stack[index][4] = 0.0f;
  46. stack[index][5] = 1.0f;
  47. stack[index][12] = tx;
  48. stack[index][13] = ty;
  49. }
  50. protected void rotate(float angle) {
  51. angle = (float) Math.toRadians(angle);
  52. float cos = (float) Math.cos(angle);
  53. float sin = (float) Math.sin(angle);
  54. float a1 = stack[index][0];
  55. float a2 = stack[index][4];
  56. stack[index][0] = a1 * cos - a2 * sin;
  57. stack[index][4] = a1 * sin + a2 * cos;
  58. a1 = stack[index][1];
  59. a2 = stack[index][5];
  60. stack[index][1] = a1 * cos - a2 * sin;
  61. stack[index][5] = a1 * sin + a2 * cos;
  62. }
  63. }