Jason Sams | 2a1cc8f | 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 | |
| 23 | class Matrix { |
| 24 | |
| 25 | public Matrix() { |
| 26 | mMat = new float[16]; |
| 27 | loadIdentity(); |
| 28 | } |
| 29 | |
| 30 | public float get(int i, int j) { |
| 31 | return mMat[i*4 + j]; |
| 32 | } |
| 33 | |
| 34 | public void set(int i, int j, float v) { |
| 35 | mMat[i*4 + j] = v; |
| 36 | } |
| 37 | |
| 38 | public void loadIdentity() { |
| 39 | mMat[0] = 1; |
| 40 | mMat[1] = 0; |
| 41 | mMat[2] = 0; |
| 42 | mMat[3] = 0; |
| 43 | |
| 44 | mMat[4] = 0; |
| 45 | mMat[5] = 1; |
| 46 | mMat[6] = 0; |
| 47 | mMat[7] = 0; |
| 48 | |
| 49 | mMat[8] = 0; |
| 50 | mMat[9] = 0; |
| 51 | mMat[10] = 1; |
| 52 | mMat[11] = 0; |
| 53 | |
| 54 | mMat[12] = 0; |
| 55 | mMat[13] = 0; |
| 56 | mMat[14] = 0; |
| 57 | mMat[15] = 1; |
| 58 | } |
| 59 | |
| 60 | public void load(Matrix src) { |
| 61 | mMat = src.mMat; |
| 62 | } |
| 63 | |
| 64 | public void loadRotate(float rot, float x, float y, float z) { |
| 65 | float c, s; |
| 66 | mMat[3] = 0; |
| 67 | mMat[7] = 0; |
| 68 | mMat[11]= 0; |
| 69 | mMat[12]= 0; |
| 70 | mMat[13]= 0; |
| 71 | mMat[14]= 0; |
| 72 | mMat[15]= 1; |
| 73 | rot *= (float)(java.lang.Math.PI / 180.0f); |
| 74 | c = (float)java.lang.Math.cos(rot); |
| 75 | s = (float)java.lang.Math.sin(rot); |
| 76 | |
| 77 | float len = (float)java.lang.Math.sqrt(x*x + y*y + z*z); |
| 78 | if (!(len != 1)) { |
| 79 | float recipLen = 1.f / len; |
| 80 | x *= recipLen; |
| 81 | y *= recipLen; |
| 82 | z *= recipLen; |
| 83 | } |
| 84 | float nc = 1.0f - c; |
| 85 | float xy = x * y; |
| 86 | float yz = y * z; |
| 87 | float zx = z * x; |
| 88 | float xs = x * s; |
| 89 | float ys = y * s; |
| 90 | float zs = z * s; |
| 91 | mMat[ 0] = x*x*nc + c; |
| 92 | mMat[ 4] = xy*nc - zs; |
| 93 | mMat[ 8] = zx*nc + ys; |
| 94 | mMat[ 1] = xy*nc + zs; |
| 95 | mMat[ 5] = y*y*nc + c; |
| 96 | mMat[ 9] = yz*nc - xs; |
| 97 | mMat[ 2] = zx*nc - ys; |
| 98 | mMat[ 6] = yz*nc + xs; |
| 99 | mMat[10] = z*z*nc + c; |
| 100 | } |
| 101 | |
| 102 | public void loadScale(float x, float y, float z) { |
| 103 | loadIdentity(); |
| 104 | mMat[0] = x; |
| 105 | mMat[5] = y; |
| 106 | mMat[10] = z; |
| 107 | } |
| 108 | |
| 109 | public void loadTranslate(float x, float y, float z) { |
| 110 | loadIdentity(); |
| 111 | mMat[12] = x; |
| 112 | mMat[13] = y; |
| 113 | mMat[14] = z; |
| 114 | } |
| 115 | |
| 116 | public void loadMultiply(Matrix lhs, Matrix rhs) { |
| 117 | for (int i=0 ; i<4 ; i++) { |
| 118 | float ri0 = 0; |
| 119 | float ri1 = 0; |
| 120 | float ri2 = 0; |
| 121 | float ri3 = 0; |
| 122 | for (int j=0 ; j<4 ; j++) { |
| 123 | float rhs_ij = rhs.get(i,j); |
| 124 | ri0 += lhs.get(j,0) * rhs_ij; |
| 125 | ri1 += lhs.get(j,1) * rhs_ij; |
| 126 | ri2 += lhs.get(j,2) * rhs_ij; |
| 127 | ri3 += lhs.get(j,3) * rhs_ij; |
| 128 | } |
| 129 | set(i,0, ri0); |
| 130 | set(i,1, ri1); |
| 131 | set(i,2, ri2); |
| 132 | set(i,3, ri3); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | public void loadOrtho(float l, float r, float b, float t, float n, float f) { |
| 137 | loadIdentity(); |
| 138 | mMat[0] = 2 / (r - l); |
| 139 | mMat[5] = 2 / (t - b); |
| 140 | mMat[10]= -2 / (f - n); |
| 141 | mMat[12]= -(r + l) / (r - l); |
Jason Sams | 56bc1af | 2009-06-16 17:49:58 -0700 | [diff] [blame^] | 142 | mMat[13]= -(t + b) / (t - b); |
| 143 | mMat[14]= -(f + n) / (f - n); |
Jason Sams | 2a1cc8f | 2009-06-15 19:04:56 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | public void loadFrustum(float l, float r, float b, float t, float n, float f) { |
| 147 | loadIdentity(); |
| 148 | mMat[0] = 2 * n / (r - l); |
| 149 | mMat[5] = 2 * n / (t - b); |
| 150 | mMat[8] = (r + l) / (r - l); |
| 151 | mMat[9] = (t + b) / (t - b); |
| 152 | mMat[10]= -(f + n) / (f - n); |
| 153 | mMat[11]= -1; |
| 154 | mMat[14]= -2*f*n / (f - n); |
| 155 | mMat[15]= 0; |
| 156 | } |
| 157 | |
| 158 | public void multiply(Matrix rhs) { |
| 159 | Matrix tmp = new Matrix(); |
| 160 | tmp.loadMultiply(this, rhs); |
| 161 | load(tmp); |
| 162 | } |
| 163 | public void rotate(float rot, float x, float y, float z) { |
| 164 | Matrix tmp = new Matrix(); |
| 165 | tmp.loadRotate(rot, x, y, z); |
| 166 | multiply(tmp); |
| 167 | } |
| 168 | public void scale(float x, float y, float z) { |
| 169 | Matrix tmp = new Matrix(); |
| 170 | tmp.loadScale(x, y, z); |
| 171 | multiply(tmp); |
| 172 | } |
| 173 | public void translate(float x, float y, float z) { |
| 174 | Matrix tmp = new Matrix(); |
| 175 | tmp.loadTranslate(x, y, z); |
| 176 | multiply(tmp); |
| 177 | } |
| 178 | |
| 179 | |
| 180 | |
| 181 | float[] mMat; |
| 182 | |
| 183 | } |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |