Revision 1dec66e0
Added by Leszek Koltunski over 2 years ago
| src/main/java/org/distorted/library/effect/MatrixEffectMove.java | ||
|---|---|---|
| 20 | 20 |
|
| 21 | 21 |
package org.distorted.library.effect; |
| 22 | 22 |
|
| 23 |
import android.opengl.Matrix; |
|
| 24 |
|
|
| 23 |
import org.distorted.library.helpers.MatrixHelper; |
|
| 25 | 24 |
import org.distorted.library.type.Data3D; |
| 26 | 25 |
|
| 27 | 26 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| ... | ... | |
| 55 | 54 |
float sy = uniforms[NUM_FLOAT_UNIFORMS*index+1]; |
| 56 | 55 |
float sz = uniforms[NUM_FLOAT_UNIFORMS*index+2]; |
| 57 | 56 |
|
| 58 |
Matrix.translateM(matrixP, 0, sx, sy, sz);
|
|
| 59 |
Matrix.translateM(matrixV, 0, sx, sy, sz);
|
|
| 57 |
MatrixHelper.translate(matrixP, sx, sy, sz);
|
|
| 58 |
MatrixHelper.translate(matrixV, sx, sy, sz);
|
|
| 60 | 59 |
} |
| 61 | 60 |
|
| 62 | 61 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| src/main/java/org/distorted/library/effect/MatrixEffectQuaternion.java | ||
|---|---|---|
| 20 | 20 |
|
| 21 | 21 |
package org.distorted.library.effect; |
| 22 | 22 |
|
| 23 |
import android.opengl.Matrix; |
|
| 24 |
|
|
| 23 |
import org.distorted.library.helpers.MatrixHelper; |
|
| 25 | 24 |
import org.distorted.library.type.Data3D; |
| 26 | 25 |
import org.distorted.library.type.Data4D; |
| 27 | 26 |
|
| ... | ... | |
| 66 | 65 |
float y = uniforms[NUM_FLOAT_UNIFORMS*index+CENTER_OFFSET+1]; |
| 67 | 66 |
float z = uniforms[NUM_FLOAT_UNIFORMS*index+CENTER_OFFSET+2]; |
| 68 | 67 |
|
| 69 |
Matrix.translateM(matrixP, 0, x, y, z);
|
|
| 70 |
multiplyByQuat (matrixP, qX, qY, qZ, qW);
|
|
| 71 |
Matrix.translateM(matrixP, 0,-x,-y,-z);
|
|
| 68 |
MatrixHelper.translate(matrixP, x, y, z);
|
|
| 69 |
multiplyByQuat(matrixP, qX, qY, qZ, qW); |
|
| 70 |
MatrixHelper.translate(matrixP,-x,-y,-z);
|
|
| 72 | 71 |
|
| 73 |
Matrix.translateM(matrixV, 0, x, y, z);
|
|
| 74 |
multiplyByQuat (matrixV, qX, qY, qZ, qW);
|
|
| 75 |
Matrix.translateM(matrixV, 0,-x,-y,-z);
|
|
| 72 |
MatrixHelper.translate(matrixV, x, y, z);
|
|
| 73 |
multiplyByQuat(matrixV, qX, qY, qZ, qW); |
|
| 74 |
MatrixHelper.translate(matrixV,-x,-y,-z);
|
|
| 76 | 75 |
} |
| 77 | 76 |
|
| 78 | 77 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| ... | ... | |
| 101 | 100 |
mTmpMatrix1[3] = mTmpMatrix1[7] = mTmpMatrix1[11] = mTmpMatrix1[12] = mTmpMatrix1[13] = mTmpMatrix1[14] = 0; |
| 102 | 101 |
mTmpMatrix1[15] = 1; |
| 103 | 102 |
|
| 104 |
Matrix.multiplyMM(mTmpMatrix2, 0, matrix, 0, mTmpMatrix1, 0);
|
|
| 103 |
MatrixHelper.multiply(mTmpMatrix2, matrix, mTmpMatrix1);
|
|
| 105 | 104 |
|
| 106 | 105 |
matrix[ 0] = mTmpMatrix2[ 0]; |
| 107 | 106 |
matrix[ 1] = mTmpMatrix2[ 1]; |
| src/main/java/org/distorted/library/effect/MatrixEffectRotate.java | ||
|---|---|---|
| 20 | 20 |
|
| 21 | 21 |
package org.distorted.library.effect; |
| 22 | 22 |
|
| 23 |
import android.opengl.Matrix; |
|
| 24 |
|
|
| 23 |
import org.distorted.library.helpers.MatrixHelper; |
|
| 25 | 24 |
import org.distorted.library.type.Data1D; |
| 26 | 25 |
import org.distorted.library.type.Data3D; |
| 27 | 26 |
|
| ... | ... | |
| 64 | 63 |
float y = uniforms[NUM_FLOAT_UNIFORMS*index+CENTER_OFFSET+1]; |
| 65 | 64 |
float z = uniforms[NUM_FLOAT_UNIFORMS*index+CENTER_OFFSET+2]; |
| 66 | 65 |
|
| 67 |
Matrix.translateM(matrixP, 0, x, y, z);
|
|
| 68 |
Matrix.rotateM (matrixP, 0, angle, axisX, axisY, axisZ);
|
|
| 69 |
Matrix.translateM(matrixP, 0,-x,-y,-z);
|
|
| 66 |
MatrixHelper.translate(matrixP, x, y, z);
|
|
| 67 |
MatrixHelper.rotate(matrixP, angle, axisX, axisY, axisZ);
|
|
| 68 |
MatrixHelper.translate(matrixP,-x,-y,-z);
|
|
| 70 | 69 |
|
| 71 |
Matrix.translateM(matrixV, 0, x, y, z);
|
|
| 72 |
Matrix.rotateM (matrixV, 0, angle, axisX, axisY, axisZ);
|
|
| 73 |
Matrix.translateM(matrixV, 0,-x,-y,-z);
|
|
| 70 |
MatrixHelper.translate(matrixV, x, y, z);
|
|
| 71 |
MatrixHelper.rotate(matrixV, angle, axisX, axisY, axisZ);
|
|
| 72 |
MatrixHelper.translate(matrixV,-x,-y,-z);
|
|
| 74 | 73 |
} |
| 75 | 74 |
|
| 76 | 75 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| src/main/java/org/distorted/library/effect/MatrixEffectScale.java | ||
|---|---|---|
| 20 | 20 |
|
| 21 | 21 |
package org.distorted.library.effect; |
| 22 | 22 |
|
| 23 |
import android.opengl.Matrix; |
|
| 24 |
|
|
| 23 |
import org.distorted.library.helpers.MatrixHelper; |
|
| 25 | 24 |
import org.distorted.library.type.Data3D; |
| 26 | 25 |
import org.distorted.library.type.Static3D; |
| 27 | 26 |
|
| ... | ... | |
| 62 | 61 |
float sy = uniforms[NUM_FLOAT_UNIFORMS*index+1]; |
| 63 | 62 |
float sz = uniforms[NUM_FLOAT_UNIFORMS*index+2]; |
| 64 | 63 |
|
| 65 |
Matrix.scaleM(matrixP, 0, sx, sy, sz);
|
|
| 66 |
Matrix.scaleM(matrixV, 0, sy*sz, sx*sz, sx*sy);
|
|
| 64 |
MatrixHelper.scale(matrixP, sx, sy, sz);
|
|
| 65 |
MatrixHelper.scale(matrixV, sy*sz, sx*sz, sx*sy);
|
|
| 67 | 66 |
} |
| 68 | 67 |
|
| 69 | 68 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| src/main/java/org/distorted/library/effect/MatrixEffectShear.java | ||
|---|---|---|
| 20 | 20 |
|
| 21 | 21 |
package org.distorted.library.effect; |
| 22 | 22 |
|
| 23 |
import android.opengl.Matrix; |
|
| 24 |
|
|
| 23 |
import org.distorted.library.helpers.MatrixHelper; |
|
| 25 | 24 |
import org.distorted.library.type.Data3D; |
| 26 | 25 |
|
| 27 | 26 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| ... | ... | |
| 63 | 62 |
float y = uniforms[NUM_FLOAT_UNIFORMS*index+CENTER_OFFSET+1]; |
| 64 | 63 |
float z = uniforms[NUM_FLOAT_UNIFORMS*index+CENTER_OFFSET+2]; |
| 65 | 64 |
|
| 66 |
Matrix.translateM(matrixP, 0, x, y, z);
|
|
| 65 |
MatrixHelper.translate(matrixP, x, y, z);
|
|
| 67 | 66 |
|
| 68 | 67 |
matrixP[4] += sx*matrixP[0]; // Multiply viewMatrix by 1 x 0 0 , i.e. X-shear. |
| 69 | 68 |
matrixP[5] += sx*matrixP[1]; // 0 1 0 0 |
| ... | ... | |
| 80 | 79 |
matrixP[6] += sz*matrixP[10];// 0 z 1 0 |
| 81 | 80 |
matrixP[7] += sz*matrixP[11];// 0 0 0 1 |
| 82 | 81 |
|
| 83 |
Matrix.translateM(matrixP, 0,-x,-y,-z); |
|
| 84 |
|
|
| 85 |
Matrix.translateM(matrixV, 0, x, y, z); |
|
| 82 |
MatrixHelper.translate(matrixP,-x,-y,-z); |
|
| 83 |
MatrixHelper.translate(matrixV, x, y, z); |
|
| 86 | 84 |
|
| 87 | 85 |
matrixV[0] -= sx*matrixV[4]; // Multiply viewMatrix by 1 0 0 0 , i.e. vector X-shear. |
| 88 | 86 |
matrixV[1] -= sx*matrixV[5]; // -x 1 0 0 |
| ... | ... | |
| 99 | 97 |
matrixV[10]-= sz*matrixV[6]; // 0 0 1 0 |
| 100 | 98 |
matrixV[11]-= sz*matrixV[7]; // 0 0 0 1 |
| 101 | 99 |
|
| 102 |
Matrix.translateM(matrixV, 0,-x,-y,-z);
|
|
| 100 |
MatrixHelper.translate(matrixV,-x,-y,-z);
|
|
| 103 | 101 |
} |
| 104 | 102 |
|
| 105 | 103 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| src/main/java/org/distorted/library/effectqueue/EffectQueueMatrix.java | ||
|---|---|---|
| 21 | 21 |
package org.distorted.library.effectqueue; |
| 22 | 22 |
|
| 23 | 23 |
import android.opengl.GLES30; |
| 24 |
import android.opengl.Matrix; |
|
| 25 | 24 |
|
| 25 |
import org.distorted.library.helpers.MatrixHelper; |
|
| 26 | 26 |
import org.distorted.library.effect.EffectType; |
| 27 | 27 |
import org.distorted.library.effect.MatrixEffect; |
| 28 | 28 |
import org.distorted.library.message.EffectMessageSender; |
| ... | ... | |
| 89 | 89 |
|
| 90 | 90 |
void send(float distance, float mipmap, float[] projection, int variant) |
| 91 | 91 |
{
|
| 92 |
Matrix.setIdentityM(mModelViewMatrixP, 0); |
|
| 93 |
Matrix.translateM(mModelViewMatrixP, 0, 0,0, -distance); |
|
| 94 |
|
|
| 95 |
if( mipmap!=1 ) |
|
| 96 |
{
|
|
| 97 |
Matrix.scaleM(mModelViewMatrixP, 0, mipmap, mipmap, mipmap); |
|
| 98 |
} |
|
| 99 |
|
|
| 100 |
mModelViewMatrixV[ 0] = mModelViewMatrixP[ 0]; |
|
| 101 |
mModelViewMatrixV[ 1] = mModelViewMatrixP[ 1]; |
|
| 102 |
mModelViewMatrixV[ 2] = mModelViewMatrixP[ 2]; |
|
| 103 |
mModelViewMatrixV[ 3] = mModelViewMatrixP[ 3]; |
|
| 104 |
mModelViewMatrixV[ 4] = mModelViewMatrixP[ 4]; |
|
| 105 |
mModelViewMatrixV[ 5] = mModelViewMatrixP[ 5]; |
|
| 106 |
mModelViewMatrixV[ 6] = mModelViewMatrixP[ 6]; |
|
| 107 |
mModelViewMatrixV[ 7] = mModelViewMatrixP[ 7]; |
|
| 108 |
mModelViewMatrixV[ 8] = mModelViewMatrixP[ 8]; |
|
| 109 |
mModelViewMatrixV[ 9] = mModelViewMatrixP[ 9]; |
|
| 110 |
mModelViewMatrixV[10] = mModelViewMatrixP[10]; |
|
| 111 |
mModelViewMatrixV[11] = mModelViewMatrixP[11]; |
|
| 112 |
mModelViewMatrixV[12] = mModelViewMatrixP[12]; |
|
| 113 |
mModelViewMatrixV[13] = mModelViewMatrixP[13]; |
|
| 114 |
mModelViewMatrixV[14] = mModelViewMatrixP[14]; |
|
| 115 |
mModelViewMatrixV[15] = mModelViewMatrixP[15]; |
|
| 92 |
// i.e. setIdentity(); translate(0,0,-distance); scale(mipmap,mipmap,mipmap) |
|
| 93 |
mModelViewMatrixV[ 0] = mModelViewMatrixP[ 0] = mipmap; |
|
| 94 |
mModelViewMatrixV[ 1] = mModelViewMatrixP[ 1] = 0; |
|
| 95 |
mModelViewMatrixV[ 2] = mModelViewMatrixP[ 2] = 0; |
|
| 96 |
mModelViewMatrixV[ 3] = mModelViewMatrixP[ 3] = 0; |
|
| 97 |
mModelViewMatrixV[ 4] = mModelViewMatrixP[ 4] = 0; |
|
| 98 |
mModelViewMatrixV[ 5] = mModelViewMatrixP[ 5] = mipmap; |
|
| 99 |
mModelViewMatrixV[ 6] = mModelViewMatrixP[ 6] = 0; |
|
| 100 |
mModelViewMatrixV[ 7] = mModelViewMatrixP[ 7] = 0; |
|
| 101 |
mModelViewMatrixV[ 8] = mModelViewMatrixP[ 8] = 0; |
|
| 102 |
mModelViewMatrixV[ 9] = mModelViewMatrixP[ 9] = 0; |
|
| 103 |
mModelViewMatrixV[10] = mModelViewMatrixP[10] = mipmap; |
|
| 104 |
mModelViewMatrixV[11] = mModelViewMatrixP[11] = 0; |
|
| 105 |
mModelViewMatrixV[12] = mModelViewMatrixP[12] = 0; |
|
| 106 |
mModelViewMatrixV[13] = mModelViewMatrixP[13] = 0; |
|
| 107 |
mModelViewMatrixV[14] = mModelViewMatrixP[14] = -distance; |
|
| 108 |
mModelViewMatrixV[15] = mModelViewMatrixP[15] = 1; |
|
| 116 | 109 |
|
| 117 | 110 |
float[] array = mUBF.getBackingArray(); |
| 118 | 111 |
|
| ... | ... | |
| 123 | 116 |
} |
| 124 | 117 |
|
| 125 | 118 |
// combined Model-View-Projection matrix |
| 126 |
Matrix.multiplyMM(mMVPMatrix, 0, projection, 0, mModelViewMatrixP, 0);
|
|
| 119 |
MatrixHelper.multiply(mMVPMatrix, projection, mModelViewMatrixP);
|
|
| 127 | 120 |
|
| 128 | 121 |
GLES30.glUniformMatrix4fv(mMVMatrixVH[variant], 1, false, mModelViewMatrixV, 0); |
| 129 | 122 |
GLES30.glUniformMatrix4fv(mMVMatrixPH[variant], 1, false, mModelViewMatrixP, 0); |
| src/main/java/org/distorted/library/helpers/MatrixHelper.java | ||
|---|---|---|
| 1 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 2 |
// Copyright 2023 Leszek Koltunski leszek@koltunski.pl // |
|
| 3 |
// // |
|
| 4 |
// This file is part of Distorted. // |
|
| 5 |
// // |
|
| 6 |
// This library is free software; you can redistribute it and/or // |
|
| 7 |
// modify it under the terms of the GNU Lesser General Public // |
|
| 8 |
// License as published by the Free Software Foundation; either // |
|
| 9 |
// version 2.1 of the License, or (at your option) any later version. // |
|
| 10 |
// // |
|
| 11 |
// This library is distributed in the hope that it will be useful, // |
|
| 12 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of // |
|
| 13 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // |
|
| 14 |
// Lesser General Public License for more details. // |
|
| 15 |
// // |
|
| 16 |
// You should have received a copy of the GNU Lesser General Public // |
|
| 17 |
// License along with this library; if not, write to the Free Software // |
|
| 18 |
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA // |
|
| 19 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 20 |
|
|
| 21 |
package org.distorted.library.helpers; |
|
| 22 |
|
|
| 23 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 24 |
|
|
| 25 |
public class MatrixHelper |
|
| 26 |
{
|
|
| 27 |
public static void setIdentity(float[] output) |
|
| 28 |
{
|
|
| 29 |
output[ 0] = 1; |
|
| 30 |
output[ 1] = 0; |
|
| 31 |
output[ 2] = 0; |
|
| 32 |
output[ 3] = 0; |
|
| 33 |
output[ 4] = 0; |
|
| 34 |
output[ 5] = 1; |
|
| 35 |
output[ 6] = 0; |
|
| 36 |
output[ 7] = 0; |
|
| 37 |
output[ 8] = 0; |
|
| 38 |
output[ 9] = 0; |
|
| 39 |
output[10] = 1; |
|
| 40 |
output[11] = 0; |
|
| 41 |
output[12] = 0; |
|
| 42 |
output[13] = 0; |
|
| 43 |
output[14] = 0; |
|
| 44 |
output[15] = 1; |
|
| 45 |
} |
|
| 46 |
|
|
| 47 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 48 |
|
|
| 49 |
public static void multiply(float[] output, float[] m1, float[] m2) |
|
| 50 |
{
|
|
| 51 |
output[ 0] = m1[ 0]*m2[ 0] + m1[ 4]*m2[ 1] + m1[ 8]*m2[ 2] + m1[12]*m2[ 3]; |
|
| 52 |
output[ 1] = m1[ 1]*m2[ 0] + m1[ 5]*m2[ 1] + m1[ 9]*m2[ 2] + m1[13]*m2[ 3]; |
|
| 53 |
output[ 2] = m1[ 2]*m2[ 0] + m1[ 6]*m2[ 1] + m1[10]*m2[ 2] + m1[14]*m2[ 3]; |
|
| 54 |
output[ 3] = m1[ 3]*m2[ 0] + m1[ 7]*m2[ 1] + m1[11]*m2[ 2] + m1[15]*m2[ 3]; |
|
| 55 |
|
|
| 56 |
output[ 4] = m1[ 0]*m2[ 4] + m1[ 4]*m2[ 5] + m1[ 8]*m2[ 6] + m1[12]*m2[ 7]; |
|
| 57 |
output[ 5] = m1[ 1]*m2[ 4] + m1[ 5]*m2[ 5] + m1[ 9]*m2[ 6] + m1[13]*m2[ 7]; |
|
| 58 |
output[ 6] = m1[ 2]*m2[ 4] + m1[ 6]*m2[ 5] + m1[10]*m2[ 6] + m1[14]*m2[ 7]; |
|
| 59 |
output[ 7] = m1[ 3]*m2[ 4] + m1[ 7]*m2[ 5] + m1[11]*m2[ 6] + m1[15]*m2[ 7]; |
|
| 60 |
|
|
| 61 |
output[ 8] = m1[ 0]*m2[ 8] + m1[ 4]*m2[ 9] + m1[ 8]*m2[10] + m1[12]*m2[11]; |
|
| 62 |
output[ 9] = m1[ 1]*m2[ 8] + m1[ 5]*m2[ 9] + m1[ 9]*m2[10] + m1[13]*m2[11]; |
|
| 63 |
output[10] = m1[ 2]*m2[ 8] + m1[ 6]*m2[ 9] + m1[10]*m2[10] + m1[14]*m2[11]; |
|
| 64 |
output[11] = m1[ 3]*m2[ 8] + m1[ 7]*m2[ 9] + m1[11]*m2[10] + m1[15]*m2[11]; |
|
| 65 |
|
|
| 66 |
output[12] = m1[ 0]*m2[12] + m1[ 4]*m2[13] + m1[ 8]*m2[14] + m1[12]*m2[15]; |
|
| 67 |
output[13] = m1[ 1]*m2[12] + m1[ 5]*m2[13] + m1[ 9]*m2[14] + m1[13]*m2[15]; |
|
| 68 |
output[14] = m1[ 2]*m2[12] + m1[ 6]*m2[13] + m1[10]*m2[14] + m1[14]*m2[15]; |
|
| 69 |
output[15] = m1[ 3]*m2[12] + m1[ 7]*m2[13] + m1[11]*m2[14] + m1[15]*m2[15]; |
|
| 70 |
} |
|
| 71 |
|
|
| 72 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 73 |
|
|
| 74 |
public static void frustum(float[] output, float l, float r, float b, float t, float n, float f) |
|
| 75 |
{
|
|
| 76 |
output[0] = 2 * n / (r - l); |
|
| 77 |
output[1] = 0; |
|
| 78 |
output[2] = 0; |
|
| 79 |
output[3] = 0; |
|
| 80 |
output[4] = 0; |
|
| 81 |
output[5] = 2 * n / (t - b); |
|
| 82 |
output[6] = 0; |
|
| 83 |
output[7] = 0; |
|
| 84 |
output[8] = (r + l) / (r - l); |
|
| 85 |
output[9] = (t + b) / (t - b); |
|
| 86 |
output[10] = -(f + n) / (f - n); |
|
| 87 |
output[11] = -1; |
|
| 88 |
output[12] = 0; |
|
| 89 |
output[13] = 0; |
|
| 90 |
output[14] = -(2 * f * n) / (f - n); |
|
| 91 |
output[15] = 0; |
|
| 92 |
} |
|
| 93 |
|
|
| 94 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 95 |
|
|
| 96 |
public static void ortho(float[] output, float l, float r, float b, float t, float n, float f) |
|
| 97 |
{
|
|
| 98 |
output[0] = 2 / (r - l); |
|
| 99 |
output[1] = 0; |
|
| 100 |
output[2] = 0; |
|
| 101 |
output[3] = 0; |
|
| 102 |
output[4] = 0; |
|
| 103 |
output[5] = 2 / (t - b); |
|
| 104 |
output[6] = 0; |
|
| 105 |
output[7] = 0; |
|
| 106 |
output[8] = 0; |
|
| 107 |
output[9] = 0; |
|
| 108 |
output[10] = -2 / (f - n); |
|
| 109 |
output[11] = 0; |
|
| 110 |
output[12] = -(r + l) / (r - l); |
|
| 111 |
output[13] = -(t + b) / (t - b); |
|
| 112 |
output[14] = -(f + n) / (f - n); |
|
| 113 |
output[15] = 1; |
|
| 114 |
} |
|
| 115 |
|
|
| 116 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 117 |
|
|
| 118 |
public static void setLookAt(float[] output, float eyeX , float eyeY , float eyeZ , |
|
| 119 |
float lookX, float lookY, float lookZ, |
|
| 120 |
float upX , float upY , float upZ ) |
|
| 121 |
{
|
|
| 122 |
// TODO |
|
| 123 |
} |
|
| 124 |
|
|
| 125 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 126 |
|
|
| 127 |
public static void rotate(float[] output, float angle, float axisX, float axisY, float axisZ) |
|
| 128 |
{
|
|
| 129 |
android.opengl.Matrix.rotateM(output,0,angle,axisX,axisY,axisZ); |
|
| 130 |
} |
|
| 131 |
|
|
| 132 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 133 |
|
|
| 134 |
public static void translate(float[] output, float dx, float dy, float dz) |
|
| 135 |
{
|
|
| 136 |
output[12] += ( output[ 0]*dx + output[ 4]*dy + output[ 8]*dz); |
|
| 137 |
output[13] += ( output[ 1]*dx + output[ 5]*dy + output[ 9]*dz); |
|
| 138 |
output[14] += ( output[ 2]*dx + output[ 6]*dy + output[10]*dz); |
|
| 139 |
output[15] += ( output[ 3]*dx + output[ 7]*dy + output[11]*dz); |
|
| 140 |
} |
|
| 141 |
|
|
| 142 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 143 |
|
|
| 144 |
public static void scale(float[] output, float sx, float sy, float sz) |
|
| 145 |
{
|
|
| 146 |
output[ 0] *= sx; |
|
| 147 |
output[ 1] *= sx; |
|
| 148 |
output[ 2] *= sx; |
|
| 149 |
output[ 3] *= sx; |
|
| 150 |
|
|
| 151 |
output[ 4] *= sy; |
|
| 152 |
output[ 5] *= sy; |
|
| 153 |
output[ 6] *= sy; |
|
| 154 |
output[ 7] *= sy; |
|
| 155 |
|
|
| 156 |
output[ 8] *= sz; |
|
| 157 |
output[ 9] *= sz; |
|
| 158 |
output[10] *= sz; |
|
| 159 |
output[11] *= sz; |
|
| 160 |
} |
|
| 161 |
} |
|
| src/main/java/org/distorted/library/helpers/QuatHelper.java | ||
|---|---|---|
| 1 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 2 |
// Copyright 2021 Leszek Koltunski leszek@koltunski.pl // |
|
| 3 |
// // |
|
| 4 |
// This file is part of Distorted. // |
|
| 5 |
// // |
|
| 6 |
// This library is free software; you can redistribute it and/or // |
|
| 7 |
// modify it under the terms of the GNU Lesser General Public // |
|
| 8 |
// License as published by the Free Software Foundation; either // |
|
| 9 |
// version 2.1 of the License, or (at your option) any later version. // |
|
| 10 |
// // |
|
| 11 |
// This library is distributed in the hope that it will be useful, // |
|
| 12 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of // |
|
| 13 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // |
|
| 14 |
// Lesser General Public License for more details. // |
|
| 15 |
// // |
|
| 16 |
// You should have received a copy of the GNU Lesser General Public // |
|
| 17 |
// License along with this library; if not, write to the Free Software // |
|
| 18 |
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA // |
|
| 19 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 20 |
|
|
| 21 |
package org.distorted.library.helpers; |
|
| 22 |
|
|
| 23 |
import org.distorted.library.type.Static4D; |
|
| 24 |
|
|
| 25 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 26 |
|
|
| 27 |
public class QuatHelper |
|
| 28 |
{
|
|
| 29 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 30 |
// return quat1*quat2 |
|
| 31 |
|
|
| 32 |
public static Static4D quatMultiply( Static4D quat1, Static4D quat2 ) |
|
| 33 |
{
|
|
| 34 |
float qx = quat1.get0(); |
|
| 35 |
float qy = quat1.get1(); |
|
| 36 |
float qz = quat1.get2(); |
|
| 37 |
float qw = quat1.get3(); |
|
| 38 |
|
|
| 39 |
float rx = quat2.get0(); |
|
| 40 |
float ry = quat2.get1(); |
|
| 41 |
float rz = quat2.get2(); |
|
| 42 |
float rw = quat2.get3(); |
|
| 43 |
|
|
| 44 |
float tx = rw*qx - rz*qy + ry*qz + rx*qw; |
|
| 45 |
float ty = rw*qy + rz*qx + ry*qw - rx*qz; |
|
| 46 |
float tz = rw*qz + rz*qw - ry*qx + rx*qy; |
|
| 47 |
float tw = rw*qw - rz*qz - ry*qy - rx*qx; |
|
| 48 |
|
|
| 49 |
return new Static4D(tx,ty,tz,tw); |
|
| 50 |
} |
|
| 51 |
|
|
| 52 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 53 |
// return quat1*(rx,ry,rz,rw) |
|
| 54 |
|
|
| 55 |
public static Static4D quatMultiply( Static4D quat1, float rx, float ry, float rz, float rw ) |
|
| 56 |
{
|
|
| 57 |
float qx = quat1.get0(); |
|
| 58 |
float qy = quat1.get1(); |
|
| 59 |
float qz = quat1.get2(); |
|
| 60 |
float qw = quat1.get3(); |
|
| 61 |
|
|
| 62 |
float tx = rw*qx - rz*qy + ry*qz + rx*qw; |
|
| 63 |
float ty = rw*qy + rz*qx + ry*qw - rx*qz; |
|
| 64 |
float tz = rw*qz + rz*qw - ry*qx + rx*qy; |
|
| 65 |
float tw = rw*qw - rz*qz - ry*qy - rx*qx; |
|
| 66 |
|
|
| 67 |
return new Static4D(tx,ty,tz,tw); |
|
| 68 |
} |
|
| 69 |
|
|
| 70 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 71 |
// return (qx,qy,qz,qw)*quat2 |
|
| 72 |
|
|
| 73 |
public static Static4D quatMultiply( float qx, float qy, float qz, float qw, Static4D quat2 ) |
|
| 74 |
{
|
|
| 75 |
float rx = quat2.get0(); |
|
| 76 |
float ry = quat2.get1(); |
|
| 77 |
float rz = quat2.get2(); |
|
| 78 |
float rw = quat2.get3(); |
|
| 79 |
|
|
| 80 |
float tx = rw*qx - rz*qy + ry*qz + rx*qw; |
|
| 81 |
float ty = rw*qy + rz*qx + ry*qw - rx*qz; |
|
| 82 |
float tz = rw*qz + rz*qw - ry*qx + rx*qy; |
|
| 83 |
float tw = rw*qw - rz*qz - ry*qy - rx*qx; |
|
| 84 |
|
|
| 85 |
return new Static4D(tx,ty,tz,tw); |
|
| 86 |
} |
|
| 87 |
|
|
| 88 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 89 |
// return (qx,qy,qz,qw)*(rx,ry,rz,rw) |
|
| 90 |
|
|
| 91 |
public static Static4D quatMultiply( float qx, float qy, float qz, float qw, float rx, float ry, float rz, float rw ) |
|
| 92 |
{
|
|
| 93 |
float tx = rw*qx - rz*qy + ry*qz + rx*qw; |
|
| 94 |
float ty = rw*qy + rz*qx + ry*qw - rx*qz; |
|
| 95 |
float tz = rw*qz + rz*qw - ry*qx + rx*qy; |
|
| 96 |
float tw = rw*qw - rz*qz - ry*qy - rx*qx; |
|
| 97 |
|
|
| 98 |
return new Static4D(tx,ty,tz,tw); |
|
| 99 |
} |
|
| 100 |
|
|
| 101 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 102 |
// ret = (qx,qy,qz,qw)*(rx,ry,rz,rw) |
|
| 103 |
|
|
| 104 |
public static void quatMultiply( float[] ret, float[] q, float[] r ) |
|
| 105 |
{
|
|
| 106 |
ret[0] = r[3]*q[0] - r[2]*q[1] + r[1]*q[2] + r[0]*q[3]; |
|
| 107 |
ret[1] = r[3]*q[1] + r[2]*q[0] + r[1]*q[3] - r[0]*q[2]; |
|
| 108 |
ret[2] = r[3]*q[2] + r[2]*q[3] - r[1]*q[0] + r[0]*q[1]; |
|
| 109 |
ret[3] = r[3]*q[3] - r[2]*q[2] - r[1]*q[1] - r[0]*q[0]; |
|
| 110 |
} |
|
| 111 |
|
|
| 112 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 113 |
// ret = (qx,qy,qz,qw)*(rx,ry,rz,rw) |
|
| 114 |
|
|
| 115 |
public static void quatMultiply( float[] ret, float qx, float qy, float qz, float qw, float rx, float ry, float rz, float rw ) |
|
| 116 |
{
|
|
| 117 |
ret[0] = rw*qx - rz*qy + ry*qz + rx*qw; |
|
| 118 |
ret[1] = rw*qy + rz*qx + ry*qw - rx*qz; |
|
| 119 |
ret[2] = rw*qz + rz*qw - ry*qx + rx*qy; |
|
| 120 |
ret[3] = rw*qw - rz*qz - ry*qy - rx*qx; |
|
| 121 |
} |
|
| 122 |
|
|
| 123 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 124 |
// rotate 'vector' by quat ( i.e. return quat*vector*(quat^-1) ) |
|
| 125 |
|
|
| 126 |
public static Static4D rotateVectorByQuat(Static4D vector, Static4D quat) |
|
| 127 |
{
|
|
| 128 |
float qx = quat.get0(); |
|
| 129 |
float qy = quat.get1(); |
|
| 130 |
float qz = quat.get2(); |
|
| 131 |
float qw = quat.get3(); |
|
| 132 |
|
|
| 133 |
Static4D tmp = quatMultiply(qx,qy,qz,qw,vector); |
|
| 134 |
|
|
| 135 |
return quatMultiply(tmp,-qx,-qy,-qz,qw); |
|
| 136 |
} |
|
| 137 |
|
|
| 138 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 139 |
// rotate (x1,x2,x3,x4) by quat ( i.e. return quat*vector*(quat^-1) ) |
|
| 140 |
|
|
| 141 |
public static Static4D rotateVectorByQuat(float x, float y, float z, float w, Static4D quat) |
|
| 142 |
{
|
|
| 143 |
float qx = quat.get0(); |
|
| 144 |
float qy = quat.get1(); |
|
| 145 |
float qz = quat.get2(); |
|
| 146 |
float qw = quat.get3(); |
|
| 147 |
|
|
| 148 |
Static4D tmp = quatMultiply(qx,qy,qz,qw,x,y,z,w); |
|
| 149 |
|
|
| 150 |
return quatMultiply(tmp,-qx,-qy,-qz,qw); |
|
| 151 |
} |
|
| 152 |
|
|
| 153 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 154 |
// rotate (x1,x2,x3,x4) by quat ( i.e. return quat*vector*(quat^-1) ) |
|
| 155 |
|
|
| 156 |
public static void rotateVectorByQuat(float[] output, float x, float y, float z, float w, Static4D quat) |
|
| 157 |
{
|
|
| 158 |
float[] tmp = new float[4]; |
|
| 159 |
|
|
| 160 |
float qx = quat.get0(); |
|
| 161 |
float qy = quat.get1(); |
|
| 162 |
float qz = quat.get2(); |
|
| 163 |
float qw = quat.get3(); |
|
| 164 |
|
|
| 165 |
quatMultiply(tmp,qx,qy,qz,qw,x,y,z,w); |
|
| 166 |
quatMultiply(output,tmp[0],tmp[1],tmp[2],tmp[3],-qx,-qy,-qz,qw); |
|
| 167 |
} |
|
| 168 |
|
|
| 169 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 170 |
// rotate vec by quat ( i.e. return quat*vector*(quat^-1) ) |
|
| 171 |
|
|
| 172 |
public static void rotateVectorByQuat(float[] output, float[] vec, float[] quat) |
|
| 173 |
{
|
|
| 174 |
float[] tmp = new float[4]; |
|
| 175 |
|
|
| 176 |
quatMultiply(tmp,quat,vec); |
|
| 177 |
|
|
| 178 |
quat[0] = -quat[0]; |
|
| 179 |
quat[1] = -quat[1]; |
|
| 180 |
quat[2] = -quat[2]; |
|
| 181 |
|
|
| 182 |
quatMultiply(output,tmp,quat); |
|
| 183 |
|
|
| 184 |
quat[0] = -quat[0]; |
|
| 185 |
quat[1] = -quat[1]; |
|
| 186 |
quat[2] = -quat[2]; |
|
| 187 |
} |
|
| 188 |
|
|
| 189 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 190 |
// rotate 'vector' by quat^(-1) ( i.e. return (quat^-1)*vector*quat ) |
|
| 191 |
|
|
| 192 |
public static Static4D rotateVectorByInvertedQuat(Static4D vector, Static4D quat) |
|
| 193 |
{
|
|
| 194 |
float qx = quat.get0(); |
|
| 195 |
float qy = quat.get1(); |
|
| 196 |
float qz = quat.get2(); |
|
| 197 |
float qw = quat.get3(); |
|
| 198 |
|
|
| 199 |
Static4D tmp = quatMultiply(-qx,-qy,-qz,qw,vector); |
|
| 200 |
|
|
| 201 |
return quatMultiply(tmp,quat); |
|
| 202 |
} |
|
| 203 |
|
|
| 204 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 205 |
|
|
| 206 |
public static Static4D quatFromDrag(float dragX, float dragY) |
|
| 207 |
{
|
|
| 208 |
float axisX = dragY; // inverted X and Y - rotation axis is perpendicular to (dragX,dragY) |
|
| 209 |
float axisY = dragX; // Why not (-dragY, dragX) ? because Y axis is also inverted! |
|
| 210 |
float axisZ = 0; |
|
| 211 |
float axisL = (float)Math.sqrt(axisX*axisX + axisY*axisY + axisZ*axisZ); |
|
| 212 |
|
|
| 213 |
if( axisL>0 ) |
|
| 214 |
{
|
|
| 215 |
axisX /= axisL; |
|
| 216 |
axisY /= axisL; |
|
| 217 |
axisZ /= axisL; |
|
| 218 |
|
|
| 219 |
float ratio = axisL; |
|
| 220 |
ratio = ratio - (int)ratio; // the cos() is only valid in (0,Pi) |
|
| 221 |
|
|
| 222 |
float cosA = (float)Math.cos(Math.PI*ratio); |
|
| 223 |
float sinA = (float)Math.sqrt(1-cosA*cosA); |
|
| 224 |
|
|
| 225 |
return new Static4D(axisX*sinA, axisY*sinA, axisZ*sinA, cosA); |
|
| 226 |
} |
|
| 227 |
|
|
| 228 |
return new Static4D(0f, 0f, 0f, 1f); |
|
| 229 |
} |
|
| 230 |
|
|
| 231 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 232 |
|
|
| 233 |
public static double computeCos(double oldX, double oldY, double newX, double newY, double len1, double len2) |
|
| 234 |
{
|
|
| 235 |
double ret= (oldX*newX+oldY*newY) / (len1*len2); |
|
| 236 |
if( ret<-1.0 ) return -1.0; |
|
| 237 |
if( ret> 1.0 ) return 1.0; |
|
| 238 |
|
|
| 239 |
return ret; |
|
| 240 |
} |
|
| 241 |
|
|
| 242 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 243 |
// sin of (signed!) angle between vectors 'old' and 'new', counterclockwise! |
|
| 244 |
|
|
| 245 |
public static double computeSin(double oldX, double oldY, double newX, double newY, double len1, double len2) |
|
| 246 |
{
|
|
| 247 |
double ret= (newX*oldY-oldX*newY) / (len1*len2); |
|
| 248 |
if( ret<-1.0 ) return -1.0; |
|
| 249 |
if( ret> 1.0 ) return 1.0; |
|
| 250 |
|
|
| 251 |
return ret; |
|
| 252 |
} |
|
| 253 |
|
|
| 254 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 255 |
// return quat Q that turns 3D vector A=(ax,ay,az) to another 3D vector B=(bx,by,bz) |
|
| 256 |
// take care of double-cover by ensuring that always Q.get3() >=0 |
|
| 257 |
|
|
| 258 |
public static Static4D retRotationQuat(float ax, float ay, float az, float bx, float by, float bz) |
|
| 259 |
{
|
|
| 260 |
float nx = ay*bz - az*by; |
|
| 261 |
float ny = az*bx - ax*bz; |
|
| 262 |
float nz = ax*by - ay*bx; |
|
| 263 |
|
|
| 264 |
float sin = (float)Math.sqrt(nx*nx + ny*ny + nz*nz); |
|
| 265 |
float cos = ax*bx + ay*by + az*bz; |
|
| 266 |
|
|
| 267 |
if( sin!=0 ) |
|
| 268 |
{
|
|
| 269 |
nx /= sin; |
|
| 270 |
ny /= sin; |
|
| 271 |
nz /= sin; |
|
| 272 |
} |
|
| 273 |
|
|
| 274 |
// Why sin<=0 and cos>=0 ? |
|
| 275 |
// 0<angle<180 -> 0<halfAngle<90 -> both sin and cos are positive. |
|
| 276 |
// But1: quats work counterclockwise -> negate cos. |
|
| 277 |
// But2: double-cover, we prefer to have the cos positive (so that unit=(0,0,0,1)) |
|
| 278 |
// so negate again both cos and sin. |
|
| 279 |
float sinHalf =-(float)Math.sqrt((1-cos)/2); |
|
| 280 |
float cosHalf = (float)Math.sqrt((1+cos)/2); |
|
| 281 |
|
|
| 282 |
return new Static4D(nx*sinHalf,ny*sinHalf,nz*sinHalf,cosHalf); |
|
| 283 |
} |
|
| 284 |
} |
|
| src/main/java/org/distorted/library/main/InternalOutputSurface.java | ||
|---|---|---|
| 22 | 22 |
|
| 23 | 23 |
import android.opengl.GLES30; |
| 24 | 24 |
import android.opengl.GLES31; |
| 25 |
import android.opengl.Matrix; |
|
| 26 | 25 |
|
| 27 | 26 |
import org.distorted.library.effect.EffectQuality; |
| 28 | 27 |
import org.distorted.library.effectqueue.EffectQueuePostprocess; |
| 28 |
import org.distorted.library.helpers.MatrixHelper; |
|
| 29 | 29 |
import org.distorted.library.mesh.MeshBase; |
| 30 | 30 |
|
| 31 | 31 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| ... | ... | |
| 65 | 65 |
int mCurrFBO; // internal current FBO (see DistortedLibrary.FBO_QUEUE_SIZE) |
| 66 | 66 |
int mWidth, mHeight; |
| 67 | 67 |
|
| 68 |
static final float[] tmpM = new float[16]; |
|
| 69 |
|
|
| 68 | 70 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| 69 | 71 |
|
| 70 | 72 |
InternalOutputSurface(int width, int height, int createColor, int numfbos, int numcolors, int depthStencil, int fbo, int type, int storage) |
| ... | ... | |
| 150 | 152 |
mDistance = mHeight/a; |
| 151 | 153 |
float far = 2*mDistance-near; |
| 152 | 154 |
|
| 153 |
Matrix.frustumM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
|
|
| 155 |
MatrixHelper.frustum(mProjectionMatrix, left, right, bottom, top, near, far);
|
|
| 154 | 156 |
} |
| 155 | 157 |
else // parallel projection |
| 156 | 158 |
{
|
| ... | ... | |
| 162 | 164 |
mDistance = mWidth+mHeight; |
| 163 | 165 |
float far = mWidth+mHeight+mHeight*(1.0f-mNear); |
| 164 | 166 |
|
| 165 |
Matrix.orthoM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
|
|
| 167 |
MatrixHelper.ortho(mProjectionMatrix, left, right, bottom, top, near, far);
|
|
| 166 | 168 |
} |
| 167 | 169 |
} |
| 168 | 170 |
} |
| src/main/java/org/distorted/library/main/QuatHelper.java | ||
|---|---|---|
| 1 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 2 |
// Copyright 2021 Leszek Koltunski leszek@koltunski.pl // |
|
| 3 |
// // |
|
| 4 |
// This file is part of Distorted. // |
|
| 5 |
// // |
|
| 6 |
// This library is free software; you can redistribute it and/or // |
|
| 7 |
// modify it under the terms of the GNU Lesser General Public // |
|
| 8 |
// License as published by the Free Software Foundation; either // |
|
| 9 |
// version 2.1 of the License, or (at your option) any later version. // |
|
| 10 |
// // |
|
| 11 |
// This library is distributed in the hope that it will be useful, // |
|
| 12 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of // |
|
| 13 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // |
|
| 14 |
// Lesser General Public License for more details. // |
|
| 15 |
// // |
|
| 16 |
// You should have received a copy of the GNU Lesser General Public // |
|
| 17 |
// License along with this library; if not, write to the Free Software // |
|
| 18 |
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA // |
|
| 19 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 20 |
|
|
| 21 |
package org.distorted.library.main; |
|
| 22 |
|
|
| 23 |
import org.distorted.library.type.Static4D; |
|
| 24 |
|
|
| 25 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 26 |
|
|
| 27 |
public class QuatHelper |
|
| 28 |
{
|
|
| 29 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 30 |
// return quat1*quat2 |
|
| 31 |
|
|
| 32 |
public static Static4D quatMultiply( Static4D quat1, Static4D quat2 ) |
|
| 33 |
{
|
|
| 34 |
float qx = quat1.get0(); |
|
| 35 |
float qy = quat1.get1(); |
|
| 36 |
float qz = quat1.get2(); |
|
| 37 |
float qw = quat1.get3(); |
|
| 38 |
|
|
| 39 |
float rx = quat2.get0(); |
|
| 40 |
float ry = quat2.get1(); |
|
| 41 |
float rz = quat2.get2(); |
|
| 42 |
float rw = quat2.get3(); |
|
| 43 |
|
|
| 44 |
float tx = rw*qx - rz*qy + ry*qz + rx*qw; |
|
| 45 |
float ty = rw*qy + rz*qx + ry*qw - rx*qz; |
|
| 46 |
float tz = rw*qz + rz*qw - ry*qx + rx*qy; |
|
| 47 |
float tw = rw*qw - rz*qz - ry*qy - rx*qx; |
|
| 48 |
|
|
| 49 |
return new Static4D(tx,ty,tz,tw); |
|
| 50 |
} |
|
| 51 |
|
|
| 52 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 53 |
// return quat1*(rx,ry,rz,rw) |
|
| 54 |
|
|
| 55 |
public static Static4D quatMultiply( Static4D quat1, float rx, float ry, float rz, float rw ) |
|
| 56 |
{
|
|
| 57 |
float qx = quat1.get0(); |
|
| 58 |
float qy = quat1.get1(); |
|
| 59 |
float qz = quat1.get2(); |
|
| 60 |
float qw = quat1.get3(); |
|
| 61 |
|
|
| 62 |
float tx = rw*qx - rz*qy + ry*qz + rx*qw; |
|
| 63 |
float ty = rw*qy + rz*qx + ry*qw - rx*qz; |
|
| 64 |
float tz = rw*qz + rz*qw - ry*qx + rx*qy; |
|
| 65 |
float tw = rw*qw - rz*qz - ry*qy - rx*qx; |
|
| 66 |
|
|
| 67 |
return new Static4D(tx,ty,tz,tw); |
|
| 68 |
} |
|
| 69 |
|
|
| 70 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 71 |
// return (qx,qy,qz,qw)*quat2 |
|
| 72 |
|
|
| 73 |
public static Static4D quatMultiply( float qx, float qy, float qz, float qw, Static4D quat2 ) |
|
| 74 |
{
|
|
| 75 |
float rx = quat2.get0(); |
|
| 76 |
float ry = quat2.get1(); |
|
| 77 |
float rz = quat2.get2(); |
|
| 78 |
float rw = quat2.get3(); |
|
| 79 |
|
|
| 80 |
float tx = rw*qx - rz*qy + ry*qz + rx*qw; |
|
| 81 |
float ty = rw*qy + rz*qx + ry*qw - rx*qz; |
|
| 82 |
float tz = rw*qz + rz*qw - ry*qx + rx*qy; |
|
| 83 |
float tw = rw*qw - rz*qz - ry*qy - rx*qx; |
|
| 84 |
|
|
| 85 |
return new Static4D(tx,ty,tz,tw); |
|
| 86 |
} |
|
| 87 |
|
|
| 88 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 89 |
// return (qx,qy,qz,qw)*(rx,ry,rz,rw) |
|
| 90 |
|
|
| 91 |
public static Static4D quatMultiply( float qx, float qy, float qz, float qw, float rx, float ry, float rz, float rw ) |
|
| 92 |
{
|
|
| 93 |
float tx = rw*qx - rz*qy + ry*qz + rx*qw; |
|
| 94 |
float ty = rw*qy + rz*qx + ry*qw - rx*qz; |
|
| 95 |
float tz = rw*qz + rz*qw - ry*qx + rx*qy; |
|
| 96 |
float tw = rw*qw - rz*qz - ry*qy - rx*qx; |
|
| 97 |
|
|
| 98 |
return new Static4D(tx,ty,tz,tw); |
|
| 99 |
} |
|
| 100 |
|
|
| 101 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 102 |
// ret = (qx,qy,qz,qw)*(rx,ry,rz,rw) |
|
| 103 |
|
|
| 104 |
public static void quatMultiply( float[] ret, float[] q, float[] r ) |
|
| 105 |
{
|
|
| 106 |
ret[0] = r[3]*q[0] - r[2]*q[1] + r[1]*q[2] + r[0]*q[3]; |
|
| 107 |
ret[1] = r[3]*q[1] + r[2]*q[0] + r[1]*q[3] - r[0]*q[2]; |
|
| 108 |
ret[2] = r[3]*q[2] + r[2]*q[3] - r[1]*q[0] + r[0]*q[1]; |
|
| 109 |
ret[3] = r[3]*q[3] - r[2]*q[2] - r[1]*q[1] - r[0]*q[0]; |
|
| 110 |
} |
|
| 111 |
|
|
| 112 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 113 |
// ret = (qx,qy,qz,qw)*(rx,ry,rz,rw) |
|
| 114 |
|
|
| 115 |
public static void quatMultiply( float[] ret, float qx, float qy, float qz, float qw, float rx, float ry, float rz, float rw ) |
|
| 116 |
{
|
|
| 117 |
ret[0] = rw*qx - rz*qy + ry*qz + rx*qw; |
|
| 118 |
ret[1] = rw*qy + rz*qx + ry*qw - rx*qz; |
|
| 119 |
ret[2] = rw*qz + rz*qw - ry*qx + rx*qy; |
|
| 120 |
ret[3] = rw*qw - rz*qz - ry*qy - rx*qx; |
|
| 121 |
} |
|
| 122 |
|
|
| 123 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 124 |
// rotate 'vector' by quat ( i.e. return quat*vector*(quat^-1) ) |
|
| 125 |
|
|
| 126 |
public static Static4D rotateVectorByQuat(Static4D vector, Static4D quat) |
|
| 127 |
{
|
|
| 128 |
float qx = quat.get0(); |
|
| 129 |
float qy = quat.get1(); |
|
| 130 |
float qz = quat.get2(); |
|
| 131 |
float qw = quat.get3(); |
|
| 132 |
|
|
| 133 |
Static4D tmp = quatMultiply(qx,qy,qz,qw,vector); |
|
| 134 |
|
|
| 135 |
return quatMultiply(tmp,-qx,-qy,-qz,qw); |
|
| 136 |
} |
|
| 137 |
|
|
| 138 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 139 |
// rotate (x1,x2,x3,x4) by quat ( i.e. return quat*vector*(quat^-1) ) |
|
| 140 |
|
|
| 141 |
public static Static4D rotateVectorByQuat(float x, float y, float z, float w, Static4D quat) |
|
| 142 |
{
|
|
| 143 |
float qx = quat.get0(); |
|
| 144 |
float qy = quat.get1(); |
|
| 145 |
float qz = quat.get2(); |
|
| 146 |
float qw = quat.get3(); |
|
| 147 |
|
|
| 148 |
Static4D tmp = quatMultiply(qx,qy,qz,qw,x,y,z,w); |
|
| 149 |
|
|
| 150 |
return quatMultiply(tmp,-qx,-qy,-qz,qw); |
|
| 151 |
} |
|
| 152 |
|
|
| 153 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 154 |
// rotate (x1,x2,x3,x4) by quat ( i.e. return quat*vector*(quat^-1) ) |
|
| 155 |
|
|
| 156 |
public static void rotateVectorByQuat(float[] output, float x, float y, float z, float w, Static4D quat) |
|
| 157 |
{
|
|
| 158 |
float[] tmp = new float[4]; |
|
| 159 |
|
|
| 160 |
float qx = quat.get0(); |
|
| 161 |
float qy = quat.get1(); |
|
| 162 |
float qz = quat.get2(); |
|
| 163 |
float qw = quat.get3(); |
|
| 164 |
|
|
| 165 |
quatMultiply(tmp,qx,qy,qz,qw,x,y,z,w); |
|
| 166 |
quatMultiply(output,tmp[0],tmp[1],tmp[2],tmp[3],-qx,-qy,-qz,qw); |
|
| 167 |
} |
|
| 168 |
|
|
| 169 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 170 |
// rotate vec by quat ( i.e. return quat*vector*(quat^-1) ) |
|
| 171 |
|
|
| 172 |
public static void rotateVectorByQuat(float[] output, float[] vec, float[] quat) |
|
| 173 |
{
|
|
| 174 |
float[] tmp = new float[4]; |
|
| 175 |
|
|
| 176 |
quatMultiply(tmp,quat,vec); |
|
| 177 |
|
|
| 178 |
quat[0] = -quat[0]; |
|
| 179 |
quat[1] = -quat[1]; |
|
| 180 |
quat[2] = -quat[2]; |
|
| 181 |
|
|
| 182 |
quatMultiply(output,tmp,quat); |
|
| 183 |
|
|
| 184 |
quat[0] = -quat[0]; |
|
| 185 |
quat[1] = -quat[1]; |
|
| 186 |
quat[2] = -quat[2]; |
|
| 187 |
} |
|
| 188 |
|
|
| 189 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 190 |
// rotate 'vector' by quat^(-1) ( i.e. return (quat^-1)*vector*quat ) |
|
| 191 |
|
|
| 192 |
public static Static4D rotateVectorByInvertedQuat(Static4D vector, Static4D quat) |
|
| 193 |
{
|
|
| 194 |
float qx = quat.get0(); |
|
| 195 |
float qy = quat.get1(); |
|
| 196 |
float qz = quat.get2(); |
|
| 197 |
float qw = quat.get3(); |
|
| 198 |
|
|
| 199 |
Static4D tmp = quatMultiply(-qx,-qy,-qz,qw,vector); |
|
| 200 |
|
|
| 201 |
return quatMultiply(tmp,quat); |
|
| 202 |
} |
|
| 203 |
|
|
| 204 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 205 |
|
|
| 206 |
public static Static4D quatFromDrag(float dragX, float dragY) |
|
| 207 |
{
|
|
| 208 |
float axisX = dragY; // inverted X and Y - rotation axis is perpendicular to (dragX,dragY) |
|
| 209 |
float axisY = dragX; // Why not (-dragY, dragX) ? because Y axis is also inverted! |
|
| 210 |
float axisZ = 0; |
|
| 211 |
float axisL = (float)Math.sqrt(axisX*axisX + axisY*axisY + axisZ*axisZ); |
|
| 212 |
|
|
| 213 |
if( axisL>0 ) |
|
| 214 |
{
|
|
| 215 |
axisX /= axisL; |
|
| 216 |
axisY /= axisL; |
|
| 217 |
axisZ /= axisL; |
|
| 218 |
|
|
| 219 |
float ratio = axisL; |
|
| 220 |
ratio = ratio - (int)ratio; // the cos() is only valid in (0,Pi) |
|
| 221 |
|
|
| 222 |
float cosA = (float)Math.cos(Math.PI*ratio); |
|
| 223 |
float sinA = (float)Math.sqrt(1-cosA*cosA); |
|
| 224 |
|
|
| 225 |
return new Static4D(axisX*sinA, axisY*sinA, axisZ*sinA, cosA); |
|
| 226 |
} |
|
| 227 |
|
|
| 228 |
return new Static4D(0f, 0f, 0f, 1f); |
|
| 229 |
} |
|
| 230 |
|
|
| 231 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 232 |
|
|
| 233 |
public static double computeCos(double oldX, double oldY, double newX, double newY, double len1, double len2) |
|
| 234 |
{
|
|
| 235 |
double ret= (oldX*newX+oldY*newY) / (len1*len2); |
|
| 236 |
if( ret<-1.0 ) return -1.0; |
|
| 237 |
if( ret> 1.0 ) return 1.0; |
|
| 238 |
|
|
| 239 |
return ret; |
|
| 240 |
} |
|
| 241 |
|
|
| 242 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 243 |
// sin of (signed!) angle between vectors 'old' and 'new', counterclockwise! |
|
| 244 |
|
|
| 245 |
public static double computeSin(double oldX, double oldY, double newX, double newY, double len1, double len2) |
|
| 246 |
{
|
|
| 247 |
double ret= (newX*oldY-oldX*newY) / (len1*len2); |
|
| 248 |
if( ret<-1.0 ) return -1.0; |
|
| 249 |
if( ret> 1.0 ) return 1.0; |
|
| 250 |
|
|
| 251 |
return ret; |
|
| 252 |
} |
|
| 253 |
|
|
| 254 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 255 |
// return quat Q that turns 3D vector A=(ax,ay,az) to another 3D vector B=(bx,by,bz) |
|
| 256 |
// take care of double-cover by ensuring that always Q.get3() >=0 |
|
| 257 |
|
|
| 258 |
public static Static4D retRotationQuat(float ax, float ay, float az, float bx, float by, float bz) |
|
| 259 |
{
|
|
| 260 |
float nx = ay*bz - az*by; |
|
| 261 |
float ny = az*bx - ax*bz; |
|
| 262 |
float nz = ax*by - ay*bx; |
|
| 263 |
|
|
| 264 |
float sin = (float)Math.sqrt(nx*nx + ny*ny + nz*nz); |
|
| 265 |
float cos = ax*bx + ay*by + az*bz; |
|
| 266 |
|
|
| 267 |
if( sin!=0 ) |
|
| 268 |
{
|
|
| 269 |
nx /= sin; |
|
| 270 |
ny /= sin; |
|
| 271 |
nz /= sin; |
|
| 272 |
} |
|
| 273 |
|
|
| 274 |
// Why sin<=0 and cos>=0 ? |
|
| 275 |
// 0<angle<180 -> 0<halfAngle<90 -> both sin and cos are positive. |
|
| 276 |
// But1: quats work counterclockwise -> negate cos. |
|
| 277 |
// But2: double-cover, we prefer to have the cos positive (so that unit=(0,0,0,1)) |
|
| 278 |
// so negate again both cos and sin. |
|
| 279 |
float sinHalf =-(float)Math.sqrt((1-cos)/2); |
|
| 280 |
float cosHalf = (float)Math.sqrt((1+cos)/2); |
|
| 281 |
|
|
| 282 |
return new Static4D(nx*sinHalf,ny*sinHalf,nz*sinHalf,cosHalf); |
|
| 283 |
} |
|
| 284 |
} |
|
Also available in: Unified diff
Implement the android.opengl.Matrix functions ourselves.