Skip to content

Instantly share code, notes, and snippets.

@dmide
Last active March 31, 2019 19:08
Show Gist options
  • Save dmide/0fcfbceed892a265d9f501fd65ddfdcb to your computer and use it in GitHub Desktop.
Save dmide/0fcfbceed892a265d9f501fd65ddfdcb to your computer and use it in GitHub Desktop.
class RotationMatrix {
/**
* Draws the data from SurfaceTexture onto the current EGL surface.
*/
public void draw(boolean flipY, boolean flipX) {
float[] normaliseMatrix = new float[16];
// unfortunately this gives unconsistent results, so we'll perform all fixes manually
//mSurfaceTexture.getTransformMatrix(normaliseMatrix);
System.arraycopy(IDENTITY, 0, normaliseMatrix, 0, IDENTITY.length);
float[] rotationMatrix;
// absRotation is clockwise, but our normalise matrices are counterclockwise
switch (Math.abs(videoRotation)) {
case 90:
rotationMatrix = NORMALISE_270;
break;
case 180:
rotationMatrix = NORMALISE_180;
break;
case 270:
rotationMatrix = NORMALISE_90;
break;
default:
rotationMatrix = IDENTITY;
}
System.arraycopy(rotationMatrix, 0, normaliseMatrix, 0, rotationMatrix.length);
if (flipY) {
// this code is equal to: FLIP_Y * normaliseMatrix
normaliseMatrix[M03] += normaliseMatrix[M01];
normaliseMatrix[M13] += normaliseMatrix[M11];
normaliseMatrix[M01] = -normaliseMatrix[M01];
normaliseMatrix[M11] = -normaliseMatrix[M11];
}
if (flipX) {
// this code is equal to: FLIP_X * normaliseMatrix
normaliseMatrix[M03] += normaliseMatrix[M00];
normaliseMatrix[M13] += normaliseMatrix[M10];
normaliseMatrix[M00] = -normaliseMatrix[M00];
normaliseMatrix[M10] = -normaliseMatrix[M10];
}
mTextureRender.drawFrame(normaliseMatrix);
}
// these matrices are transposed and have 0 for z values since we don't need it.
// Normalise matrices are constructed from corresponding
// counterclockwise rotation and necessary translation to be in the [0,1] region again
public static final float[] IDENTITY = {
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0};
public static final float[] NORMALISE_90 = {
0, 1, 0, 0,
-1, 0, 0, 0,
0, 0, 0, 0,
1, 0, 0, 0};
public static final float[] NORMALISE_180 = {
-1, 0, 0, 0,
0, -1, 0, 0,
0, 0, 0, 0,
1, 1, 0, 0};
public static final float[] NORMALISE_270 = {
0, -1, 0, 0,
1, 0, 0, 0,
0, 0, 0, 0,
0, 1, 0, 0};
// XX: Typically the unrotated X component for scaling, also the cosine of the angle when rotated on the Y and/or Z axis. On
// Vector3 multiplication this value is multiplied with the source X component and added to the target X component.
public static final int M00 = 0;
// XY: Typically the negative sine of the angle when rotated on the Z axis. On Vector3 multiplication this value is multiplied
// with the source Y component and added to the target X component.
public static final int M01 = 4;
// XZ: Typically the sine of the angle when rotated on the Y axis. On Vector3 multiplication this value is multiplied with the
// source Z component and added to the target X component.
public static final int M02 = 8;
// XW: Typically the translation of the X component. On Vector3 multiplication this value is added to the target X component.
public static final int M03 = 12;
// YX: Typically the sine of the angle when rotated on the Z axis. On Vector3 multiplication this value is multiplied with the
// source X component and added to the target Y component.
public static final int M10 = 1;
// YY: Typically the unrotated Y component for scaling, also the cosine of the angle when rotated on the X and/or Z axis. On
// Vector3 multiplication this value is multiplied with the source Y component and added to the target Y component.
public static final int M11 = 5;
// YZ: Typically the negative sine of the angle when rotated on the X axis. On Vector3 multiplication this value is multiplied
// with the source Z component and added to the target Y component.
public static final int M12 = 9;
// YW: Typically the translation of the Y component. On Vector3 multiplication this value is added to the target Y component.
public static final int M13 = 13;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment