Jason Sams | 0826a6f | 2009-06-15 19:04:56 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 The Android Open Source Project |
| 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 | |
| 19 | import java.lang.Math; |
| 20 | import android.util.Log; |
| 21 | |
| 22 | |
Jason Sams | e29d471 | 2009-07-23 15:19:03 -0700 | [diff] [blame] | 23 | /** |
Jason Sams | a23d4e7 | 2011-01-04 18:59:12 -0800 | [diff] [blame] | 24 | * Class for exposing the rs_matrix4x4 type back to java applications. |
Jason Sams | e29d471 | 2009-07-23 15:19:03 -0700 | [diff] [blame] | 25 | * |
| 26 | **/ |
Jason Sams | 25430d0 | 2010-02-02 15:26:40 -0800 | [diff] [blame] | 27 | public class Matrix4f { |
Jason Sams | 0826a6f | 2009-06-15 19:04:56 -0700 | [diff] [blame] | 28 | |
Jason Sams | 25430d0 | 2010-02-02 15:26:40 -0800 | [diff] [blame] | 29 | public Matrix4f() { |
Jason Sams | 0826a6f | 2009-06-15 19:04:56 -0700 | [diff] [blame] | 30 | mMat = new float[16]; |
| 31 | loadIdentity(); |
| 32 | } |
| 33 | |
Alex Sakhartchouk | e27cdee | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 34 | public Matrix4f(float[] dataArray) { |
| 35 | mMat = new float[16]; |
| 36 | System.arraycopy(dataArray, 0, mMat, 0, mMat.length); |
| 37 | } |
| 38 | |
| 39 | public float[] getArray() { |
| 40 | return mMat; |
| 41 | } |
| 42 | |
Jason Sams | 0826a6f | 2009-06-15 19:04:56 -0700 | [diff] [blame] | 43 | public float get(int i, int j) { |
| 44 | return mMat[i*4 + j]; |
| 45 | } |
| 46 | |
| 47 | public void set(int i, int j, float v) { |
| 48 | mMat[i*4 + j] = v; |
| 49 | } |
| 50 | |
| 51 | public void loadIdentity() { |
| 52 | mMat[0] = 1; |
| 53 | mMat[1] = 0; |
| 54 | mMat[2] = 0; |
| 55 | mMat[3] = 0; |
| 56 | |
| 57 | mMat[4] = 0; |
| 58 | mMat[5] = 1; |
| 59 | mMat[6] = 0; |
| 60 | mMat[7] = 0; |
Jason Sams | 25430d0 | 2010-02-02 15:26:40 -0800 | [diff] [blame] | 61 | |
Jason Sams | 0826a6f | 2009-06-15 19:04:56 -0700 | [diff] [blame] | 62 | mMat[8] = 0; |
| 63 | mMat[9] = 0; |
| 64 | mMat[10] = 1; |
| 65 | mMat[11] = 0; |
| 66 | |
| 67 | mMat[12] = 0; |
| 68 | mMat[13] = 0; |
| 69 | mMat[14] = 0; |
| 70 | mMat[15] = 1; |
| 71 | } |
| 72 | |
Jason Sams | 25430d0 | 2010-02-02 15:26:40 -0800 | [diff] [blame] | 73 | public void load(Matrix4f src) { |
Alex Sakhartchouk | b3b89f6 | 2010-12-29 08:43:49 -0800 | [diff] [blame] | 74 | System.arraycopy(src.getArray(), 0, mMat, 0, mMat.length); |
Jason Sams | 0826a6f | 2009-06-15 19:04:56 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | public void loadRotate(float rot, float x, float y, float z) { |
| 78 | float c, s; |
| 79 | mMat[3] = 0; |
| 80 | mMat[7] = 0; |
| 81 | mMat[11]= 0; |
| 82 | mMat[12]= 0; |
| 83 | mMat[13]= 0; |
| 84 | mMat[14]= 0; |
| 85 | mMat[15]= 1; |
| 86 | rot *= (float)(java.lang.Math.PI / 180.0f); |
| 87 | c = (float)java.lang.Math.cos(rot); |
| 88 | s = (float)java.lang.Math.sin(rot); |
Jason Sams | 25430d0 | 2010-02-02 15:26:40 -0800 | [diff] [blame] | 89 | |
Jason Sams | 0826a6f | 2009-06-15 19:04:56 -0700 | [diff] [blame] | 90 | float len = (float)java.lang.Math.sqrt(x*x + y*y + z*z); |
| 91 | if (!(len != 1)) { |
| 92 | float recipLen = 1.f / len; |
| 93 | x *= recipLen; |
| 94 | y *= recipLen; |
| 95 | z *= recipLen; |
| 96 | } |
| 97 | float nc = 1.0f - c; |
| 98 | float xy = x * y; |
| 99 | float yz = y * z; |
| 100 | float zx = z * x; |
| 101 | float xs = x * s; |
| 102 | float ys = y * s; |
Jason Sams | 25430d0 | 2010-02-02 15:26:40 -0800 | [diff] [blame] | 103 | float zs = z * s; |
Jason Sams | 0826a6f | 2009-06-15 19:04:56 -0700 | [diff] [blame] | 104 | mMat[ 0] = x*x*nc + c; |
| 105 | mMat[ 4] = xy*nc - zs; |
| 106 | mMat[ 8] = zx*nc + ys; |
| 107 | mMat[ 1] = xy*nc + zs; |
| 108 | mMat[ 5] = y*y*nc + c; |
| 109 | mMat[ 9] = yz*nc - xs; |
| 110 | mMat[ 2] = zx*nc - ys; |
| 111 | mMat[ 6] = yz*nc + xs; |
| 112 | mMat[10] = z*z*nc + c; |
| 113 | } |
| 114 | |
| 115 | public void loadScale(float x, float y, float z) { |
| 116 | loadIdentity(); |
| 117 | mMat[0] = x; |
| 118 | mMat[5] = y; |
| 119 | mMat[10] = z; |
| 120 | } |
Jason Sams | 25430d0 | 2010-02-02 15:26:40 -0800 | [diff] [blame] | 121 | |
Jason Sams | 0826a6f | 2009-06-15 19:04:56 -0700 | [diff] [blame] | 122 | public void loadTranslate(float x, float y, float z) { |
| 123 | loadIdentity(); |
| 124 | mMat[12] = x; |
| 125 | mMat[13] = y; |
| 126 | mMat[14] = z; |
| 127 | } |
| 128 | |
Jason Sams | 25430d0 | 2010-02-02 15:26:40 -0800 | [diff] [blame] | 129 | public void loadMultiply(Matrix4f lhs, Matrix4f rhs) { |
Jason Sams | 0826a6f | 2009-06-15 19:04:56 -0700 | [diff] [blame] | 130 | for (int i=0 ; i<4 ; i++) { |
| 131 | float ri0 = 0; |
| 132 | float ri1 = 0; |
| 133 | float ri2 = 0; |
| 134 | float ri3 = 0; |
| 135 | for (int j=0 ; j<4 ; j++) { |
| 136 | float rhs_ij = rhs.get(i,j); |
| 137 | ri0 += lhs.get(j,0) * rhs_ij; |
| 138 | ri1 += lhs.get(j,1) * rhs_ij; |
| 139 | ri2 += lhs.get(j,2) * rhs_ij; |
| 140 | ri3 += lhs.get(j,3) * rhs_ij; |
| 141 | } |
| 142 | set(i,0, ri0); |
| 143 | set(i,1, ri1); |
| 144 | set(i,2, ri2); |
| 145 | set(i,3, ri3); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | public void loadOrtho(float l, float r, float b, float t, float n, float f) { |
| 150 | loadIdentity(); |
| 151 | mMat[0] = 2 / (r - l); |
| 152 | mMat[5] = 2 / (t - b); |
| 153 | mMat[10]= -2 / (f - n); |
| 154 | mMat[12]= -(r + l) / (r - l); |
Jason Sams | b37c0a5 | 2009-06-16 17:49:58 -0700 | [diff] [blame] | 155 | mMat[13]= -(t + b) / (t - b); |
| 156 | mMat[14]= -(f + n) / (f - n); |
Jason Sams | 0826a6f | 2009-06-15 19:04:56 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Alex Sakhartchouk | e27cdee | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 159 | public void loadOrthoWindow(int w, int h) { |
| 160 | loadOrtho(0,w, h,0, -1,1); |
| 161 | } |
| 162 | |
Jason Sams | 0826a6f | 2009-06-15 19:04:56 -0700 | [diff] [blame] | 163 | public void loadFrustum(float l, float r, float b, float t, float n, float f) { |
| 164 | loadIdentity(); |
| 165 | mMat[0] = 2 * n / (r - l); |
| 166 | mMat[5] = 2 * n / (t - b); |
| 167 | mMat[8] = (r + l) / (r - l); |
| 168 | mMat[9] = (t + b) / (t - b); |
| 169 | mMat[10]= -(f + n) / (f - n); |
| 170 | mMat[11]= -1; |
| 171 | mMat[14]= -2*f*n / (f - n); |
| 172 | mMat[15]= 0; |
| 173 | } |
| 174 | |
Alex Sakhartchouk | e27cdee | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 175 | public void loadPerspective(float fovy, float aspect, float near, float far) { |
| 176 | float top = near * (float)Math.tan((float) (fovy * Math.PI / 360.0f)); |
| 177 | float bottom = -top; |
| 178 | float left = bottom * aspect; |
| 179 | float right = top * aspect; |
| 180 | loadFrustum(left, right, bottom, top, near, far); |
| 181 | } |
| 182 | |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 183 | public void loadProjectionNormalized(int w, int h) { |
| 184 | // range -1,1 in the narrow axis at z = 0. |
| 185 | Matrix4f m1 = new Matrix4f(); |
| 186 | Matrix4f m2 = new Matrix4f(); |
| 187 | |
| 188 | if(w > h) { |
| 189 | float aspect = ((float)w) / h; |
| 190 | m1.loadFrustum(-aspect,aspect, -1,1, 1,100); |
| 191 | } else { |
| 192 | float aspect = ((float)h) / w; |
| 193 | m1.loadFrustum(-1,1, -aspect,aspect, 1,100); |
| 194 | } |
| 195 | |
| 196 | m2.loadRotate(180, 0, 1, 0); |
| 197 | m1.loadMultiply(m1, m2); |
| 198 | |
| 199 | m2.loadScale(-2, 2, 1); |
| 200 | m1.loadMultiply(m1, m2); |
| 201 | |
| 202 | m2.loadTranslate(0, 0, 2); |
| 203 | m1.loadMultiply(m1, m2); |
| 204 | |
| 205 | load(m1); |
| 206 | } |
| 207 | |
| 208 | |
Jason Sams | 25430d0 | 2010-02-02 15:26:40 -0800 | [diff] [blame] | 209 | public void multiply(Matrix4f rhs) { |
| 210 | Matrix4f tmp = new Matrix4f(); |
Jason Sams | 0826a6f | 2009-06-15 19:04:56 -0700 | [diff] [blame] | 211 | tmp.loadMultiply(this, rhs); |
| 212 | load(tmp); |
| 213 | } |
| 214 | public void rotate(float rot, float x, float y, float z) { |
Jason Sams | 25430d0 | 2010-02-02 15:26:40 -0800 | [diff] [blame] | 215 | Matrix4f tmp = new Matrix4f(); |
Jason Sams | 0826a6f | 2009-06-15 19:04:56 -0700 | [diff] [blame] | 216 | tmp.loadRotate(rot, x, y, z); |
| 217 | multiply(tmp); |
| 218 | } |
| 219 | public void scale(float x, float y, float z) { |
Jason Sams | 25430d0 | 2010-02-02 15:26:40 -0800 | [diff] [blame] | 220 | Matrix4f tmp = new Matrix4f(); |
Jason Sams | 0826a6f | 2009-06-15 19:04:56 -0700 | [diff] [blame] | 221 | tmp.loadScale(x, y, z); |
| 222 | multiply(tmp); |
| 223 | } |
| 224 | public void translate(float x, float y, float z) { |
Jason Sams | 25430d0 | 2010-02-02 15:26:40 -0800 | [diff] [blame] | 225 | Matrix4f tmp = new Matrix4f(); |
Jason Sams | 0826a6f | 2009-06-15 19:04:56 -0700 | [diff] [blame] | 226 | tmp.loadTranslate(x, y, z); |
| 227 | multiply(tmp); |
| 228 | } |
Alex Sakhartchouk | facd6fc | 2010-08-10 17:34:39 -0700 | [diff] [blame] | 229 | private float computeCofactor(int i, int j) { |
| 230 | int c0 = (i+1) % 4; |
| 231 | int c1 = (i+2) % 4; |
| 232 | int c2 = (i+3) % 4; |
| 233 | int r0 = (j+1) % 4; |
| 234 | int r1 = (j+2) % 4; |
| 235 | int r2 = (j+3) % 4; |
| 236 | |
| 237 | float minor = (mMat[c0 + 4*r0] * (mMat[c1 + 4*r1] * mMat[c2 + 4*r2] - |
| 238 | mMat[c1 + 4*r2] * mMat[c2 + 4*r1])) |
| 239 | - (mMat[c0 + 4*r1] * (mMat[c1 + 4*r0] * mMat[c2 + 4*r2] - |
| 240 | mMat[c1 + 4*r2] * mMat[c2 + 4*r0])) |
| 241 | + (mMat[c0 + 4*r2] * (mMat[c1 + 4*r0] * mMat[c2 + 4*r1] - |
| 242 | mMat[c1 + 4*r1] * mMat[c2 + 4*r0])); |
| 243 | |
| 244 | float cofactor = ((i+j) & 1) != 0 ? -minor : minor; |
| 245 | return cofactor; |
| 246 | } |
| 247 | |
| 248 | public boolean inverse() { |
| 249 | |
| 250 | Matrix4f result = new Matrix4f(); |
| 251 | |
| 252 | for (int i = 0; i < 4; ++i) { |
| 253 | for (int j = 0; j < 4; ++j) { |
| 254 | result.mMat[4*i + j] = computeCofactor(i, j); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | // Dot product of 0th column of source and 0th row of result |
| 259 | float det = mMat[0]*result.mMat[0] + mMat[4]*result.mMat[1] + |
| 260 | mMat[8]*result.mMat[2] + mMat[12]*result.mMat[3]; |
| 261 | |
| 262 | if (Math.abs(det) < 1e-6) { |
| 263 | return false; |
| 264 | } |
| 265 | |
| 266 | det = 1.0f / det; |
| 267 | for (int i = 0; i < 16; ++i) { |
| 268 | mMat[i] = result.mMat[i] * det; |
| 269 | } |
| 270 | |
| 271 | return true; |
| 272 | } |
| 273 | |
| 274 | public boolean inverseTranspose() { |
| 275 | |
| 276 | Matrix4f result = new Matrix4f(); |
| 277 | |
| 278 | for (int i = 0; i < 4; ++i) { |
| 279 | for (int j = 0; j < 4; ++j) { |
| 280 | result.mMat[4*j + i] = computeCofactor(i, j); |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | float det = mMat[0]*result.mMat[0] + mMat[4]*result.mMat[4] + |
| 285 | mMat[8]*result.mMat[8] + mMat[12]*result.mMat[12]; |
| 286 | |
| 287 | if (Math.abs(det) < 1e-6) { |
| 288 | return false; |
| 289 | } |
| 290 | |
| 291 | det = 1.0f / det; |
| 292 | for (int i = 0; i < 16; ++i) { |
| 293 | mMat[i] = result.mMat[i] * det; |
| 294 | } |
| 295 | |
| 296 | return true; |
| 297 | } |
| 298 | |
Alex Sakhartchouk | 518f033 | 2010-08-05 10:28:43 -0700 | [diff] [blame] | 299 | public void transpose() { |
| 300 | for(int i = 0; i < 3; ++i) { |
| 301 | for(int j = i + 1; j < 4; ++j) { |
| 302 | float temp = mMat[i*4 + j]; |
| 303 | mMat[i*4 + j] = mMat[j*4 + i]; |
| 304 | mMat[j*4 + i] = temp; |
| 305 | } |
| 306 | } |
| 307 | } |
Jason Sams | 0826a6f | 2009-06-15 19:04:56 -0700 | [diff] [blame] | 308 | |
Jason Sams | 25430d0 | 2010-02-02 15:26:40 -0800 | [diff] [blame] | 309 | final float[] mMat; |
Jason Sams | 0826a6f | 2009-06-15 19:04:56 -0700 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | |
| 313 | |
| 314 | |
| 315 | |