1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- package me.hammerle.snuviengine.api;
- public final class MatrixStack {
- private final int capacity;
- private final float[][] stack;
- private int index = 0;
- protected MatrixStack(int capacity) {
- this.capacity = capacity;
- stack = new float[capacity][16];
- stack[index][0] = 1.0f;
- stack[index][5] = 1.0f;
- stack[index][10] = 1.0f;
- stack[index][15] = 1.0f;
- }
- protected float[] getData() {
- return stack[index];
- }
- protected boolean push() {
- if(index + 1 >= capacity) {
- return false;
- }
- index++;
- System.arraycopy(stack[index - 1], 0, stack[index], 0, 16);
- return true;
- }
- protected boolean pop() {
- if(index - 1 < 0) {
- return false;
- }
- index--;
- return true;
- }
- protected void scale(float sx, float sy) {
- stack[index][0] *= sx;
- stack[index][1] *= sx;
- stack[index][4] *= sy;
- stack[index][5] *= sy;
- }
- protected void translate(float tx, float ty) {
- stack[index][12] += stack[index][0] * tx + stack[index][4] * ty;
- stack[index][13] += stack[index][1] * tx + stack[index][5] * ty;
- }
- protected void translateTo(float tx, float ty) {
- stack[index][0] = 1.0f;
- stack[index][1] = 0.0f;
- stack[index][4] = 0.0f;
- stack[index][5] = 1.0f;
- stack[index][12] = tx;
- stack[index][13] = ty;
- }
- protected void rotate(float angle) {
- angle = (float) Math.toRadians(angle);
- float cos = (float) Math.cos(angle);
- float sin = (float) Math.sin(angle);
- float a1 = stack[index][0];
- float a2 = stack[index][4];
- stack[index][0] = a1 * cos - a2 * sin;
- stack[index][4] = a1 * sin + a2 * cos;
- a1 = stack[index][1];
- a2 = stack[index][5];
- stack[index][1] = a1 * cos - a2 * sin;
- stack[index][5] = a1 * sin + a2 * cos;
- }
- }
|