123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- package me.hammerle.snuviengine.api;
- public 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 void push()
- {
- index++;
- if(index >= capacity)
- {
- throw new ShaderException("matrix stack overflow, max capacity is " + capacity);
- }
- System.arraycopy(stack[index - 1], 0, stack[index], 0, 16);
- }
-
- protected void pop()
- {
- if(index <= 0)
- {
- throw new ShaderException("matrix stack underflow");
- }
- index--;
- }
-
- 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;
- }
- }
|