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