Android Open Source - android_shaderize Matrix






From Project

Back to project page android_shaderize.

License

The source code is released under:

Apache License

If you think the Android project android_shaderize listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package fi.harism.shaderize;
// www. j  a  va2  s  . c om
/**
 * Static methods for matrix initialization.
 */
public final class Matrix extends android.opengl.Matrix {

  /**
   * Fast inverse-transpose matrix calculation. See
   * http://content.gpwiki.org/index.php/MathGem:Fast_Matrix_Inversion for
   * more information. Only difference is that we do transpose at the same
   * time and therefore we don't transpose upper-left 3x3 matrix leaving it
   * intact. Also T is written into lowest row of destination matrix instead
   * of last column.
   * 
   * @param dst
   *            Destination matrix
   * @param dstOffset
   *            Destination matrix offset
   * @param src
   *            Source matrix
   * @param srcOffset
   *            Source matrix offset
   */
  public static void invTransposeM(float[] dst, int dstOffset, float[] src,
      int srcOffset) {
    android.opengl.Matrix.setIdentityM(dst, dstOffset);

    // Copy top-left 3x3 matrix into dst matrix.
    dst[dstOffset + 0] = src[srcOffset + 0];
    dst[dstOffset + 1] = src[srcOffset + 1];
    dst[dstOffset + 2] = src[srcOffset + 2];
    dst[dstOffset + 4] = src[srcOffset + 4];
    dst[dstOffset + 5] = src[srcOffset + 5];
    dst[dstOffset + 6] = src[srcOffset + 6];
    dst[dstOffset + 8] = src[srcOffset + 8];
    dst[dstOffset + 9] = src[srcOffset + 9];
    dst[dstOffset + 10] = src[srcOffset + 10];

    // Calculate -(Ri dot T) into last row.
    dst[dstOffset + 3] = -(src[srcOffset + 0] * src[srcOffset + 12]
        + src[srcOffset + 1] * src[srcOffset + 13] + src[srcOffset + 2]
        * src[srcOffset + 14]);
    dst[dstOffset + 7] = -(src[srcOffset + 4] * src[srcOffset + 12]
        + src[srcOffset + 5] * src[srcOffset + 13] + src[srcOffset + 6]
        * src[srcOffset + 14]);
    dst[dstOffset + 11] = -(src[srcOffset + 8] * src[srcOffset + 12]
        + src[srcOffset + 9] * src[srcOffset + 13] + src[srcOffset + 10]
        * src[srcOffset + 14]);
  }

  /**
   * Initializes given matrix as perspective projection matrix.
   * 
   * @param m
   *            Matrix for writing, should be float[16], or bigger.
   * @param fovy
   *            Field of view in degrees.
   * @param aspect
   *            Aspect ratio.
   * @param zNear
   *            Near clipping plane.
   * @param zFar
   *            Far clipping plane.
   */
  public static void setPerspectiveM(float[] m, float fovy, float aspect,
      float zNear, float zFar) {

    setIdentityM(m, 0);
    float h = zNear * (float) Math.tan(fovy * Math.PI / 360);
    float w = h * aspect;
    float d = zFar - zNear;

    m[0] = zNear / w;
    m[5] = zNear / h;
    m[10] = -(zFar + zNear) / d;
    m[11] = -1;
    m[14] = (-2 * zNear * zFar) / d;
    m[15] = 0;
  }

  /**
   * Calculates rotation matrix into given matrix array.
   * 
   * @param m
   *            Matrix float array
   * @param offset
   *            Matrix start offset
   * @param rx
   *            Rotation around x axis
   * @param ry
   *            Rotation around y axis
   * @param rz
   *            Rotation around z axis
   */
  public static void setRotateM(float[] m, int offset, float rx, float ry,
      float rz) {
    double toRadians = Math.PI * 2 / 360;
    double sin0 = Math.sin(rx * toRadians);
    double cos0 = Math.cos(rx * toRadians);
    double sin1 = Math.sin(ry * toRadians);
    double cos1 = Math.cos(ry * toRadians);
    double sin2 = Math.sin(rz * toRadians);
    double cos2 = Math.cos(rz * toRadians);

    android.opengl.Matrix.setIdentityM(m, offset);

    double sin1_cos2 = sin1 * cos2;
    double sin1_sin2 = sin1 * sin2;

    m[0 + offset] = (float) (cos1 * cos2);
    m[1 + offset] = (float) (cos1 * sin2);
    m[2 + offset] = (float) (-sin1);

    m[4 + offset] = (float) ((-cos0 * sin2) + (sin0 * sin1_cos2));
    m[5 + offset] = (float) ((cos0 * cos2) + (sin0 * sin1_sin2));
    m[6 + offset] = (float) (sin0 * cos1);

    m[8 + offset] = (float) ((sin0 * sin2) + (cos0 * sin1_cos2));
    m[9 + offset] = (float) ((-sin0 * cos2) + (cos0 * sin1_sin2));
    m[10 + offset] = (float) (cos0 * cos1);
  }
}




Java Source Code List

fi.harism.shaderize.MainActivity.java
fi.harism.shaderize.MainMenus.java
fi.harism.shaderize.MainRenderer.java
fi.harism.shaderize.Matrix.java
fi.harism.shaderize.ObjCube.java
fi.harism.shaderize.ObjFbo.java
fi.harism.shaderize.ObjScene.java
fi.harism.shaderize.ObjShader.java
fi.harism.shaderize.PrefsSeekBar.java
fi.harism.shaderize.RendererBloom.java
fi.harism.shaderize.RendererDof.java
fi.harism.shaderize.RendererFlat.java
fi.harism.shaderize.RendererFxaa.java
fi.harism.shaderize.RendererHex.java
fi.harism.shaderize.RendererLightning.java
fi.harism.shaderize.RendererRounded.java
fi.harism.shaderize.Renderer.java
fi.harism.shaderize.Utils.java