| Jason Sams | 0826a6f | 2009-06-15 19:04:56 -0700 | [diff] [blame] | 1 | /* | 
| Stephen Hines | ec6f200 | 2012-07-10 16:16:22 -0700 | [diff] [blame] | 2 | * Copyright (C) 2009-2012 The Android Open Source Project | 
| Jason Sams | 0826a6f | 2009-06-15 19:04:56 -0700 | [diff] [blame] | 3 | * | 
|  | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | * you may not use this file except in compliance with the License. | 
|  | 6 | * You may obtain a copy of the License at | 
|  | 7 | * | 
|  | 8 | *      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | * | 
|  | 10 | * Unless required by applicable law or agreed to in writing, software | 
|  | 11 | * distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | * See the License for the specific language governing permissions and | 
|  | 14 | * limitations under the License. | 
|  | 15 | */ | 
|  | 16 |  | 
|  | 17 | package android.renderscript; | 
|  | 18 |  | 
| Artur Satayev | 53ada2a | 2019-12-10 17:47:56 +0000 | [diff] [blame] | 19 | import android.compat.annotation.UnsupportedAppUsage; | 
| Jason Sams | 0826a6f | 2009-06-15 19:04:56 -0700 | [diff] [blame] | 20 |  | 
|  | 21 |  | 
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 22 | /** | 
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 23 | * Class for exposing the native RenderScript rs_matrix4x4 type back to the Android system. | 
| Jason Sams | e29d471 | 2009-07-23 15:19:03 -0700 | [diff] [blame] | 24 | * | 
| Xusong Wang | 1f8dc65 | 2021-01-05 10:09:52 -0800 | [diff] [blame] | 25 | * @deprecated Renderscript has been deprecated in API level 31. Please refer to the <a | 
|  | 26 | * href="https://developer.android.com/guide/topics/renderscript/migration-guide">migration | 
|  | 27 | * guide</a> for the proposed alternatives. | 
| Jason Sams | e29d471 | 2009-07-23 15:19:03 -0700 | [diff] [blame] | 28 | **/ | 
| Xusong Wang | 1f8dc65 | 2021-01-05 10:09:52 -0800 | [diff] [blame] | 29 | @Deprecated | 
| Jason Sams | 25430d0 | 2010-02-02 15:26:40 -0800 | [diff] [blame] | 30 | public class Matrix4f { | 
| Jason Sams | 0826a6f | 2009-06-15 19:04:56 -0700 | [diff] [blame] | 31 |  | 
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 32 | /** | 
| Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 33 | * Creates a new identity 4x4 matrix | 
|  | 34 | */ | 
| Jason Sams | 25430d0 | 2010-02-02 15:26:40 -0800 | [diff] [blame] | 35 | public Matrix4f() { | 
| Jason Sams | 0826a6f | 2009-06-15 19:04:56 -0700 | [diff] [blame] | 36 | mMat = new float[16]; | 
|  | 37 | loadIdentity(); | 
|  | 38 | } | 
|  | 39 |  | 
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 40 | /** | 
| Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 41 | * Creates a new matrix and sets its values from the given | 
|  | 42 | * parameter | 
|  | 43 | * | 
|  | 44 | * @param dataArray values to set the matrix to, must be 16 | 
|  | 45 | *                  floats long | 
|  | 46 | */ | 
| Alex Sakhartchouk | e27cdee | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 47 | public Matrix4f(float[] dataArray) { | 
|  | 48 | mMat = new float[16]; | 
|  | 49 | System.arraycopy(dataArray, 0, mMat, 0, mMat.length); | 
|  | 50 | } | 
|  | 51 |  | 
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 52 | /** | 
| Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 53 | * Return a reference to the internal array representing matrix | 
|  | 54 | * values. Modifying this array will also change the matrix | 
|  | 55 | * | 
|  | 56 | * @return internal array representing the matrix | 
|  | 57 | */ | 
| Alex Sakhartchouk | e27cdee | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 58 | public float[] getArray() { | 
|  | 59 | return mMat; | 
|  | 60 | } | 
|  | 61 |  | 
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 62 | /** | 
| Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 63 | * Returns the value for a given row and column | 
|  | 64 | * | 
| Stephen Hines | ec6f200 | 2012-07-10 16:16:22 -0700 | [diff] [blame] | 65 | * @param x column of the value to return | 
|  | 66 | * @param y row of the value to return | 
| Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 67 | * | 
| Stephen Hines | ec6f200 | 2012-07-10 16:16:22 -0700 | [diff] [blame] | 68 | * @return value in the yth row and xth column | 
| Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 69 | */ | 
| Stephen Hines | ec6f200 | 2012-07-10 16:16:22 -0700 | [diff] [blame] | 70 | public float get(int x, int y) { | 
|  | 71 | return mMat[x*4 + y]; | 
| Jason Sams | 0826a6f | 2009-06-15 19:04:56 -0700 | [diff] [blame] | 72 | } | 
|  | 73 |  | 
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 74 | /** | 
| Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 75 | * Sets the value for a given row and column | 
|  | 76 | * | 
| Stephen Hines | ec6f200 | 2012-07-10 16:16:22 -0700 | [diff] [blame] | 77 | * @param x column of the value to set | 
|  | 78 | * @param y row of the value to set | 
| Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 79 | */ | 
| Stephen Hines | ec6f200 | 2012-07-10 16:16:22 -0700 | [diff] [blame] | 80 | public void set(int x, int y, float v) { | 
|  | 81 | mMat[x*4 + y] = v; | 
| Jason Sams | 0826a6f | 2009-06-15 19:04:56 -0700 | [diff] [blame] | 82 | } | 
|  | 83 |  | 
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 84 | /** | 
| Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 85 | * Sets the matrix values to identity | 
|  | 86 | */ | 
| Jason Sams | 0826a6f | 2009-06-15 19:04:56 -0700 | [diff] [blame] | 87 | public void loadIdentity() { | 
|  | 88 | mMat[0] = 1; | 
|  | 89 | mMat[1] = 0; | 
|  | 90 | mMat[2] = 0; | 
|  | 91 | mMat[3] = 0; | 
|  | 92 |  | 
|  | 93 | mMat[4] = 0; | 
|  | 94 | mMat[5] = 1; | 
|  | 95 | mMat[6] = 0; | 
|  | 96 | mMat[7] = 0; | 
| Jason Sams | 25430d0 | 2010-02-02 15:26:40 -0800 | [diff] [blame] | 97 |  | 
| Jason Sams | 0826a6f | 2009-06-15 19:04:56 -0700 | [diff] [blame] | 98 | mMat[8] = 0; | 
|  | 99 | mMat[9] = 0; | 
|  | 100 | mMat[10] = 1; | 
|  | 101 | mMat[11] = 0; | 
|  | 102 |  | 
|  | 103 | mMat[12] = 0; | 
|  | 104 | mMat[13] = 0; | 
|  | 105 | mMat[14] = 0; | 
|  | 106 | mMat[15] = 1; | 
|  | 107 | } | 
|  | 108 |  | 
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 109 | /** | 
| Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 110 | * Sets the values of the matrix to those of the parameter | 
|  | 111 | * | 
|  | 112 | * @param src matrix to load the values from | 
|  | 113 | */ | 
| Jason Sams | 25430d0 | 2010-02-02 15:26:40 -0800 | [diff] [blame] | 114 | public void load(Matrix4f src) { | 
| Alex Sakhartchouk | b3b89f6 | 2010-12-29 08:43:49 -0800 | [diff] [blame] | 115 | System.arraycopy(src.getArray(), 0, mMat, 0, mMat.length); | 
| Jason Sams | 0826a6f | 2009-06-15 19:04:56 -0700 | [diff] [blame] | 116 | } | 
|  | 117 |  | 
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 118 | /** | 
| Jason Sams | 8fd5853 | 2012-09-05 15:30:18 -0700 | [diff] [blame] | 119 | * Sets the values of the matrix to those of the parameter | 
|  | 120 | * | 
|  | 121 | * @param src matrix to load the values from | 
|  | 122 | * @hide | 
|  | 123 | */ | 
|  | 124 | public void load(Matrix3f src) { | 
|  | 125 | mMat[0] = src.mMat[0]; | 
|  | 126 | mMat[1] = src.mMat[1]; | 
|  | 127 | mMat[2] = src.mMat[2]; | 
|  | 128 | mMat[3] = 0; | 
|  | 129 |  | 
|  | 130 | mMat[4] = src.mMat[3]; | 
|  | 131 | mMat[5] = src.mMat[4]; | 
|  | 132 | mMat[6] = src.mMat[5]; | 
|  | 133 | mMat[7] = 0; | 
|  | 134 |  | 
|  | 135 | mMat[8] = src.mMat[6]; | 
|  | 136 | mMat[9] = src.mMat[7]; | 
|  | 137 | mMat[10] = src.mMat[8]; | 
|  | 138 | mMat[11] = 0; | 
|  | 139 |  | 
|  | 140 | mMat[12] = 0; | 
|  | 141 | mMat[13] = 0; | 
|  | 142 | mMat[14] = 0; | 
|  | 143 | mMat[15] = 1; | 
|  | 144 | } | 
|  | 145 |  | 
|  | 146 | /** | 
| Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 147 | * Sets current values to be a rotation matrix of certain angle | 
|  | 148 | * about a given axis | 
|  | 149 | * | 
|  | 150 | * @param rot angle of rotation | 
|  | 151 | * @param x rotation axis x | 
|  | 152 | * @param y rotation axis y | 
|  | 153 | * @param z rotation axis z | 
|  | 154 | */ | 
| Jason Sams | 0826a6f | 2009-06-15 19:04:56 -0700 | [diff] [blame] | 155 | public void loadRotate(float rot, float x, float y, float z) { | 
|  | 156 | float c, s; | 
|  | 157 | mMat[3] = 0; | 
|  | 158 | mMat[7] = 0; | 
|  | 159 | mMat[11]= 0; | 
|  | 160 | mMat[12]= 0; | 
|  | 161 | mMat[13]= 0; | 
|  | 162 | mMat[14]= 0; | 
|  | 163 | mMat[15]= 1; | 
|  | 164 | rot *= (float)(java.lang.Math.PI / 180.0f); | 
|  | 165 | c = (float)java.lang.Math.cos(rot); | 
|  | 166 | s = (float)java.lang.Math.sin(rot); | 
| Jason Sams | 25430d0 | 2010-02-02 15:26:40 -0800 | [diff] [blame] | 167 |  | 
| Jason Sams | 0826a6f | 2009-06-15 19:04:56 -0700 | [diff] [blame] | 168 | float len = (float)java.lang.Math.sqrt(x*x + y*y + z*z); | 
|  | 169 | if (!(len != 1)) { | 
|  | 170 | float recipLen = 1.f / len; | 
|  | 171 | x *= recipLen; | 
|  | 172 | y *= recipLen; | 
|  | 173 | z *= recipLen; | 
|  | 174 | } | 
|  | 175 | float nc = 1.0f - c; | 
|  | 176 | float xy = x * y; | 
|  | 177 | float yz = y * z; | 
|  | 178 | float zx = z * x; | 
|  | 179 | float xs = x * s; | 
|  | 180 | float ys = y * s; | 
| Jason Sams | 25430d0 | 2010-02-02 15:26:40 -0800 | [diff] [blame] | 181 | float zs = z * s; | 
| Jason Sams | 0826a6f | 2009-06-15 19:04:56 -0700 | [diff] [blame] | 182 | mMat[ 0] = x*x*nc +  c; | 
|  | 183 | mMat[ 4] =  xy*nc - zs; | 
|  | 184 | mMat[ 8] =  zx*nc + ys; | 
|  | 185 | mMat[ 1] =  xy*nc + zs; | 
|  | 186 | mMat[ 5] = y*y*nc +  c; | 
|  | 187 | mMat[ 9] =  yz*nc - xs; | 
|  | 188 | mMat[ 2] =  zx*nc - ys; | 
|  | 189 | mMat[ 6] =  yz*nc + xs; | 
|  | 190 | mMat[10] = z*z*nc +  c; | 
|  | 191 | } | 
|  | 192 |  | 
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 193 | /** | 
| Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 194 | * Sets current values to be a scale matrix of given dimensions | 
|  | 195 | * | 
|  | 196 | * @param x scale component x | 
|  | 197 | * @param y scale component y | 
|  | 198 | * @param z scale component z | 
|  | 199 | */ | 
| Jason Sams | 0826a6f | 2009-06-15 19:04:56 -0700 | [diff] [blame] | 200 | public void loadScale(float x, float y, float z) { | 
|  | 201 | loadIdentity(); | 
|  | 202 | mMat[0] = x; | 
|  | 203 | mMat[5] = y; | 
|  | 204 | mMat[10] = z; | 
|  | 205 | } | 
| Jason Sams | 25430d0 | 2010-02-02 15:26:40 -0800 | [diff] [blame] | 206 |  | 
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 207 | /** | 
| Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 208 | * Sets current values to be a translation matrix of given | 
|  | 209 | * dimensions | 
|  | 210 | * | 
|  | 211 | * @param x translation component x | 
|  | 212 | * @param y translation component y | 
|  | 213 | * @param z translation component z | 
|  | 214 | */ | 
| Jason Sams | 0826a6f | 2009-06-15 19:04:56 -0700 | [diff] [blame] | 215 | public void loadTranslate(float x, float y, float z) { | 
|  | 216 | loadIdentity(); | 
|  | 217 | mMat[12] = x; | 
|  | 218 | mMat[13] = y; | 
|  | 219 | mMat[14] = z; | 
|  | 220 | } | 
|  | 221 |  | 
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 222 | /** | 
| Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 223 | * Sets current values to be the result of multiplying two given | 
|  | 224 | * matrices | 
|  | 225 | * | 
|  | 226 | * @param lhs left hand side matrix | 
|  | 227 | * @param rhs right hand side matrix | 
|  | 228 | */ | 
| Jason Sams | 25430d0 | 2010-02-02 15:26:40 -0800 | [diff] [blame] | 229 | public void loadMultiply(Matrix4f lhs, Matrix4f rhs) { | 
| Jason Sams | 0826a6f | 2009-06-15 19:04:56 -0700 | [diff] [blame] | 230 | for (int i=0 ; i<4 ; i++) { | 
|  | 231 | float ri0 = 0; | 
|  | 232 | float ri1 = 0; | 
|  | 233 | float ri2 = 0; | 
|  | 234 | float ri3 = 0; | 
|  | 235 | for (int j=0 ; j<4 ; j++) { | 
|  | 236 | float rhs_ij = rhs.get(i,j); | 
|  | 237 | ri0 += lhs.get(j,0) * rhs_ij; | 
|  | 238 | ri1 += lhs.get(j,1) * rhs_ij; | 
|  | 239 | ri2 += lhs.get(j,2) * rhs_ij; | 
|  | 240 | ri3 += lhs.get(j,3) * rhs_ij; | 
|  | 241 | } | 
|  | 242 | set(i,0, ri0); | 
|  | 243 | set(i,1, ri1); | 
|  | 244 | set(i,2, ri2); | 
|  | 245 | set(i,3, ri3); | 
|  | 246 | } | 
|  | 247 | } | 
|  | 248 |  | 
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 249 | /** | 
| Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 250 | * Set current values to be an orthographic projection matrix | 
|  | 251 | * | 
|  | 252 | * @param l location of the left vertical clipping plane | 
|  | 253 | * @param r location of the right vertical clipping plane | 
|  | 254 | * @param b location of the bottom horizontal clipping plane | 
|  | 255 | * @param t location of the top horizontal clipping plane | 
|  | 256 | * @param n location of the near clipping plane | 
|  | 257 | * @param f location of the far clipping plane | 
|  | 258 | */ | 
| Jason Sams | 0826a6f | 2009-06-15 19:04:56 -0700 | [diff] [blame] | 259 | public void loadOrtho(float l, float r, float b, float t, float n, float f) { | 
|  | 260 | loadIdentity(); | 
|  | 261 | mMat[0] = 2 / (r - l); | 
|  | 262 | mMat[5] = 2 / (t - b); | 
|  | 263 | mMat[10]= -2 / (f - n); | 
|  | 264 | mMat[12]= -(r + l) / (r - l); | 
| Jason Sams | b37c0a5 | 2009-06-16 17:49:58 -0700 | [diff] [blame] | 265 | mMat[13]= -(t + b) / (t - b); | 
|  | 266 | mMat[14]= -(f + n) / (f - n); | 
| Jason Sams | 0826a6f | 2009-06-15 19:04:56 -0700 | [diff] [blame] | 267 | } | 
|  | 268 |  | 
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 269 | /** | 
| Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 270 | * Set current values to be an orthographic projection matrix | 
|  | 271 | * with the right and bottom clipping planes set to the given | 
|  | 272 | * values. Left and top clipping planes are set to 0. Near and | 
|  | 273 | * far are set to -1, 1 respectively | 
|  | 274 | * | 
|  | 275 | * @param w location of the right vertical clipping plane | 
|  | 276 | * @param h location of the bottom horizontal clipping plane | 
|  | 277 | * | 
|  | 278 | */ | 
| Alex Sakhartchouk | e27cdee | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 279 | public void loadOrthoWindow(int w, int h) { | 
|  | 280 | loadOrtho(0,w, h,0, -1,1); | 
|  | 281 | } | 
|  | 282 |  | 
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 283 | /** | 
| Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 284 | * Sets current values to be a perspective projection matrix | 
|  | 285 | * | 
|  | 286 | * @param l location of the left vertical clipping plane | 
|  | 287 | * @param r location of the right vertical clipping plane | 
|  | 288 | * @param b location of the bottom horizontal clipping plane | 
|  | 289 | * @param t location of the top horizontal clipping plane | 
|  | 290 | * @param n location of the near clipping plane, must be positive | 
|  | 291 | * @param f location of the far clipping plane, must be positive | 
|  | 292 | * | 
|  | 293 | */ | 
| Jason Sams | 0826a6f | 2009-06-15 19:04:56 -0700 | [diff] [blame] | 294 | public void loadFrustum(float l, float r, float b, float t, float n, float f) { | 
|  | 295 | loadIdentity(); | 
|  | 296 | mMat[0] = 2 * n / (r - l); | 
|  | 297 | mMat[5] = 2 * n / (t - b); | 
|  | 298 | mMat[8] = (r + l) / (r - l); | 
|  | 299 | mMat[9] = (t + b) / (t - b); | 
|  | 300 | mMat[10]= -(f + n) / (f - n); | 
|  | 301 | mMat[11]= -1; | 
|  | 302 | mMat[14]= -2*f*n / (f - n); | 
|  | 303 | mMat[15]= 0; | 
|  | 304 | } | 
|  | 305 |  | 
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 306 | /** | 
| Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 307 | * Sets current values to be a perspective projection matrix | 
|  | 308 | * | 
|  | 309 | * @param fovy vertical field of view angle in degrees | 
|  | 310 | * @param aspect aspect ratio of the screen | 
|  | 311 | * @param near near cliping plane, must be positive | 
|  | 312 | * @param far far clipping plane, must be positive | 
|  | 313 | */ | 
| Alex Sakhartchouk | e27cdee | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 314 | public void loadPerspective(float fovy, float aspect, float near, float far) { | 
|  | 315 | float top = near * (float)Math.tan((float) (fovy * Math.PI / 360.0f)); | 
|  | 316 | float bottom = -top; | 
|  | 317 | float left = bottom * aspect; | 
|  | 318 | float right = top * aspect; | 
|  | 319 | loadFrustum(left, right, bottom, top, near, far); | 
|  | 320 | } | 
|  | 321 |  | 
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 322 | /** | 
| Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 323 | * Helper function to set the current values to a perspective | 
|  | 324 | * projection matrix with aspect ratio defined by the parameters | 
|  | 325 | * and (near, far), (bottom, top) mapping to (-1, 1) at z = 0 | 
|  | 326 | * | 
|  | 327 | * @param w screen width | 
|  | 328 | * @param h screen height | 
|  | 329 | */ | 
| Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 330 | public void loadProjectionNormalized(int w, int h) { | 
|  | 331 | // range -1,1 in the narrow axis at z = 0. | 
|  | 332 | Matrix4f m1 = new Matrix4f(); | 
|  | 333 | Matrix4f m2 = new Matrix4f(); | 
|  | 334 |  | 
|  | 335 | if(w > h) { | 
|  | 336 | float aspect = ((float)w) / h; | 
|  | 337 | m1.loadFrustum(-aspect,aspect,  -1,1,  1,100); | 
|  | 338 | } else { | 
|  | 339 | float aspect = ((float)h) / w; | 
|  | 340 | m1.loadFrustum(-1,1, -aspect,aspect, 1,100); | 
|  | 341 | } | 
|  | 342 |  | 
|  | 343 | m2.loadRotate(180, 0, 1, 0); | 
|  | 344 | m1.loadMultiply(m1, m2); | 
|  | 345 |  | 
|  | 346 | m2.loadScale(-2, 2, 1); | 
|  | 347 | m1.loadMultiply(m1, m2); | 
|  | 348 |  | 
|  | 349 | m2.loadTranslate(0, 0, 2); | 
|  | 350 | m1.loadMultiply(m1, m2); | 
|  | 351 |  | 
|  | 352 | load(m1); | 
|  | 353 | } | 
|  | 354 |  | 
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 355 | /** | 
| Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 356 | * Post-multiplies the current matrix by a given parameter | 
|  | 357 | * | 
|  | 358 | * @param rhs right hand side to multiply by | 
|  | 359 | */ | 
| Jason Sams | 25430d0 | 2010-02-02 15:26:40 -0800 | [diff] [blame] | 360 | public void multiply(Matrix4f rhs) { | 
|  | 361 | Matrix4f tmp = new Matrix4f(); | 
| Jason Sams | 0826a6f | 2009-06-15 19:04:56 -0700 | [diff] [blame] | 362 | tmp.loadMultiply(this, rhs); | 
|  | 363 | load(tmp); | 
|  | 364 | } | 
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 365 | /** | 
| Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 366 | * Modifies the current matrix by post-multiplying it with a | 
|  | 367 | * rotation matrix of certain angle about a given axis | 
|  | 368 | * | 
|  | 369 | * @param rot angle of rotation | 
|  | 370 | * @param x rotation axis x | 
|  | 371 | * @param y rotation axis y | 
|  | 372 | * @param z rotation axis z | 
|  | 373 | */ | 
| Jason Sams | 0826a6f | 2009-06-15 19:04:56 -0700 | [diff] [blame] | 374 | public void rotate(float rot, float x, float y, float z) { | 
| Jason Sams | 25430d0 | 2010-02-02 15:26:40 -0800 | [diff] [blame] | 375 | Matrix4f tmp = new Matrix4f(); | 
| Jason Sams | 0826a6f | 2009-06-15 19:04:56 -0700 | [diff] [blame] | 376 | tmp.loadRotate(rot, x, y, z); | 
|  | 377 | multiply(tmp); | 
|  | 378 | } | 
| Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 379 |  | 
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 380 | /** | 
| Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 381 | * Modifies the current matrix by post-multiplying it with a | 
|  | 382 | * scale matrix of given dimensions | 
|  | 383 | * | 
|  | 384 | * @param x scale component x | 
|  | 385 | * @param y scale component y | 
|  | 386 | * @param z scale component z | 
|  | 387 | */ | 
| Jason Sams | 0826a6f | 2009-06-15 19:04:56 -0700 | [diff] [blame] | 388 | public void scale(float x, float y, float z) { | 
| Jason Sams | 25430d0 | 2010-02-02 15:26:40 -0800 | [diff] [blame] | 389 | Matrix4f tmp = new Matrix4f(); | 
| Jason Sams | 0826a6f | 2009-06-15 19:04:56 -0700 | [diff] [blame] | 390 | tmp.loadScale(x, y, z); | 
|  | 391 | multiply(tmp); | 
|  | 392 | } | 
| Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 393 |  | 
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 394 | /** | 
| Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 395 | * Modifies the current matrix by post-multiplying it with a | 
|  | 396 | * translation matrix of given dimensions | 
|  | 397 | * | 
|  | 398 | * @param x translation component x | 
|  | 399 | * @param y translation component y | 
|  | 400 | * @param z translation component z | 
|  | 401 | */ | 
| Jason Sams | 0826a6f | 2009-06-15 19:04:56 -0700 | [diff] [blame] | 402 | public void translate(float x, float y, float z) { | 
| Jason Sams | 25430d0 | 2010-02-02 15:26:40 -0800 | [diff] [blame] | 403 | Matrix4f tmp = new Matrix4f(); | 
| Jason Sams | 0826a6f | 2009-06-15 19:04:56 -0700 | [diff] [blame] | 404 | tmp.loadTranslate(x, y, z); | 
|  | 405 | multiply(tmp); | 
|  | 406 | } | 
| Alex Sakhartchouk | facd6fc | 2010-08-10 17:34:39 -0700 | [diff] [blame] | 407 | private float computeCofactor(int i, int j) { | 
|  | 408 | int c0 = (i+1) % 4; | 
|  | 409 | int c1 = (i+2) % 4; | 
|  | 410 | int c2 = (i+3) % 4; | 
|  | 411 | int r0 = (j+1) % 4; | 
|  | 412 | int r1 = (j+2) % 4; | 
|  | 413 | int r2 = (j+3) % 4; | 
|  | 414 |  | 
|  | 415 | float minor = (mMat[c0 + 4*r0] * (mMat[c1 + 4*r1] * mMat[c2 + 4*r2] - | 
|  | 416 | mMat[c1 + 4*r2] * mMat[c2 + 4*r1])) | 
|  | 417 | - (mMat[c0 + 4*r1] * (mMat[c1 + 4*r0] * mMat[c2 + 4*r2] - | 
|  | 418 | mMat[c1 + 4*r2] * mMat[c2 + 4*r0])) | 
|  | 419 | + (mMat[c0 + 4*r2] * (mMat[c1 + 4*r0] * mMat[c2 + 4*r1] - | 
|  | 420 | mMat[c1 + 4*r1] * mMat[c2 + 4*r0])); | 
|  | 421 |  | 
|  | 422 | float cofactor = ((i+j) & 1) != 0 ? -minor : minor; | 
|  | 423 | return cofactor; | 
|  | 424 | } | 
|  | 425 |  | 
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 426 | /** | 
| Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 427 | * Sets the current matrix to its inverse | 
|  | 428 | */ | 
| Alex Sakhartchouk | facd6fc | 2010-08-10 17:34:39 -0700 | [diff] [blame] | 429 | public boolean inverse() { | 
|  | 430 |  | 
|  | 431 | Matrix4f result = new Matrix4f(); | 
|  | 432 |  | 
|  | 433 | for (int i = 0; i < 4; ++i) { | 
|  | 434 | for (int j = 0; j < 4; ++j) { | 
|  | 435 | result.mMat[4*i + j] = computeCofactor(i, j); | 
|  | 436 | } | 
|  | 437 | } | 
|  | 438 |  | 
|  | 439 | // Dot product of 0th column of source and 0th row of result | 
|  | 440 | float det = mMat[0]*result.mMat[0] + mMat[4]*result.mMat[1] + | 
|  | 441 | mMat[8]*result.mMat[2] + mMat[12]*result.mMat[3]; | 
|  | 442 |  | 
|  | 443 | if (Math.abs(det) < 1e-6) { | 
|  | 444 | return false; | 
|  | 445 | } | 
|  | 446 |  | 
|  | 447 | det = 1.0f / det; | 
|  | 448 | for (int i = 0; i < 16; ++i) { | 
|  | 449 | mMat[i] = result.mMat[i] * det; | 
|  | 450 | } | 
|  | 451 |  | 
|  | 452 | return true; | 
|  | 453 | } | 
|  | 454 |  | 
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 455 | /** | 
| Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 456 | * Sets the current matrix to its inverse transpose | 
|  | 457 | */ | 
| Alex Sakhartchouk | facd6fc | 2010-08-10 17:34:39 -0700 | [diff] [blame] | 458 | public boolean inverseTranspose() { | 
|  | 459 |  | 
|  | 460 | Matrix4f result = new Matrix4f(); | 
|  | 461 |  | 
|  | 462 | for (int i = 0; i < 4; ++i) { | 
|  | 463 | for (int j = 0; j < 4; ++j) { | 
|  | 464 | result.mMat[4*j + i] = computeCofactor(i, j); | 
|  | 465 | } | 
|  | 466 | } | 
|  | 467 |  | 
|  | 468 | float det = mMat[0]*result.mMat[0] + mMat[4]*result.mMat[4] + | 
|  | 469 | mMat[8]*result.mMat[8] + mMat[12]*result.mMat[12]; | 
|  | 470 |  | 
|  | 471 | if (Math.abs(det) < 1e-6) { | 
|  | 472 | return false; | 
|  | 473 | } | 
|  | 474 |  | 
|  | 475 | det = 1.0f / det; | 
|  | 476 | for (int i = 0; i < 16; ++i) { | 
|  | 477 | mMat[i] = result.mMat[i] * det; | 
|  | 478 | } | 
|  | 479 |  | 
|  | 480 | return true; | 
|  | 481 | } | 
|  | 482 |  | 
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 483 | /** | 
| Alex Sakhartchouk | ec0d335 | 2011-01-17 15:23:22 -0800 | [diff] [blame] | 484 | * Sets the current matrix to its transpose | 
|  | 485 | */ | 
| Alex Sakhartchouk | 518f033 | 2010-08-05 10:28:43 -0700 | [diff] [blame] | 486 | public void transpose() { | 
|  | 487 | for(int i = 0; i < 3; ++i) { | 
|  | 488 | for(int j = i + 1; j < 4; ++j) { | 
|  | 489 | float temp = mMat[i*4 + j]; | 
|  | 490 | mMat[i*4 + j] = mMat[j*4 + i]; | 
|  | 491 | mMat[j*4 + i] = temp; | 
|  | 492 | } | 
|  | 493 | } | 
|  | 494 | } | 
| Jason Sams | 0826a6f | 2009-06-15 19:04:56 -0700 | [diff] [blame] | 495 |  | 
| Mathew Inwood | 1532447 | 2018-08-06 11:18:49 +0100 | [diff] [blame] | 496 | @UnsupportedAppUsage | 
| Jason Sams | 25430d0 | 2010-02-02 15:26:40 -0800 | [diff] [blame] | 497 | final float[] mMat; | 
| Jason Sams | 0826a6f | 2009-06-15 19:04:56 -0700 | [diff] [blame] | 498 | } |