Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //#ifdef JAVA_ONLY
- package cross;
- import engine.M;
- public class Affine
- {
- public static final int STACK_SIZE = 100;
- public static float[] stack = new float[STACK_SIZE * 16];
- public static float[] inverseStack = new float[16];
- private static boolean mbRebuildInverse = true;
- public static int stackPos = 0;
- public static float outX;
- public static float outY;
- public static float[] identityStack = new float[] {
- 1.0f, 0.0f, 0.0f, 0.0f,
- 0.0f, 1.0f, 0.0f, 0.0f,
- 0.0f, 0.0f, 1.0f, 0.0f,
- 0.0f, 0.0f, 0.0f, 1.0f
- };
- /*
- * a b 0 u 0 1 2 3
- * c d 0 v 4 5 6 7
- * 0 0 1 0 8 9 10 11
- * x y 0 1 12 13 14 15
- */
- public static void push()
- {
- if (stackPos == (STACK_SIZE - 1) * 16)
- {
- System.out.println("Affine Stack Overflow! Increase Affine.STACK_SIZE!");
- return;
- }
- for (int i = 0; i < 16; i++)
- {
- stack[stackPos + i + 16] = stack[stackPos + i];
- }
- stackPos += 16;
- }
- public static void pop()
- {
- if (stackPos == 0)
- {
- System.out.println("Affine Stack Underflow! Too many GFX.pop()'s?");
- return;
- }
- stackPos -= 16;
- mbRebuildInverse = true;
- }
- public static void identity()
- {
- for (int i = 0; i < 16; i++)
- {
- stack[stackPos + i] = identityStack[i];
- }
- mbRebuildInverse = true;
- }
- public static void rotate(float _angle)
- {
- float sin = M.sin(M.deg2rad(_angle));
- float cos = M.cos(M.deg2rad(_angle));
- float a = stack[stackPos + 0];
- float b = stack[stackPos + 1];
- float c = stack[stackPos + 4];
- float d = stack[stackPos + 5];
- stack[stackPos + 0] = cos * a + sin * c;
- stack[stackPos + 1] = cos * b + sin * d;
- stack[stackPos + 4] = -sin * a + cos * c;
- stack[stackPos + 5] = -sin * b + cos * d;
- mbRebuildInverse = true;
- }
- public static void translate(float _x, float _y)
- {
- final float a = stack[stackPos + 0];
- final float b = stack[stackPos + 1];
- final float c = stack[stackPos + 4];
- final float d = stack[stackPos + 5];
- final float tx = stack[stackPos + 12];
- final float ty = stack[stackPos + 13];
- stack[stackPos + 12] = a * _x + c * _y + tx;
- stack[stackPos + 13] = b * _x + d * _y + ty;
- mbRebuildInverse = true;
- }
- public static void scale(float _sx, float _sy)
- {
- stack[stackPos + 0] *= _sx;
- stack[stackPos + 1] *= _sx;
- stack[stackPos + 4] *= _sy;
- stack[stackPos + 5] *= _sy;
- mbRebuildInverse = true;
- }
- public static void skew(float _sx, float _sy)
- {
- final float a = stack[stackPos + 0];
- final float b = stack[stackPos + 1];
- final float c = stack[stackPos + 4];
- final float d = stack[stackPos + 5];
- stack[stackPos + 0] = _sx * c;
- stack[stackPos + 1] = _sx * d;
- stack[stackPos + 4] = _sy * a;
- stack[stackPos + 5] = _sy * b;
- mbRebuildInverse = true;
- }
- public static void localToGlobal(float _x, float _y)
- {
- final float a = stack[stackPos + 0];
- final float b = stack[stackPos + 1];
- final float c = stack[stackPos + 4];
- final float d = stack[stackPos + 5];
- final float tx = stack[stackPos + 12];
- final float ty = stack[stackPos + 13];
- outX = _x * a + _y * c + tx;
- outY = _x * b + _y * d + ty;
- }
- public static void globalToLocal(float _x, float _y)
- {
- updateInverse();
- final float a = inverseStack[0];
- final float b = inverseStack[1];
- final float c = inverseStack[4];
- final float d = inverseStack[5];
- final float tx = inverseStack[12];
- final float ty = inverseStack[13];
- outX = _x * a + _y * c + tx;
- outY = _x * b + _y * d + ty;
- }
- public static void updateInverse()
- {
- if (mbRebuildInverse)
- {
- mbRebuildInverse = false;
- final float a = stack[stackPos + 0];
- final float b = stack[stackPos + 1];
- final float c = stack[stackPos + 4];
- final float d = stack[stackPos + 5];
- final float tx = stack[stackPos + 12];
- final float ty = stack[stackPos + 13];
- final float adbc = (a * d - b * c);
- inverseStack[0] = d / adbc; // a
- inverseStack[1] = -b / adbc; // b
- inverseStack[4] = -c / adbc; // c
- inverseStack[5] = a / adbc; // d
- inverseStack[12] = (c * ty - d * tx) / adbc; // tx
- inverseStack[13] = -(a * ty - b * tx) / adbc; // ty
- }
- }
- }
- //#endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement