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 /** 24 * @hide 25 * 26 **/ 27 public class Matrix4f { 28 Matrix4f()29 public Matrix4f() { 30 mMat = new float[16]; 31 loadIdentity(); 32 } 33 get(int i, int j)34 public float get(int i, int j) { 35 return mMat[i*4 + j]; 36 } 37 set(int i, int j, float v)38 public void set(int i, int j, float v) { 39 mMat[i*4 + j] = v; 40 } 41 loadIdentity()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; 52 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 load(Matrix4f src)64 public void load(Matrix4f src) { 65 System.arraycopy(mMat, 0, src, 0, 16); 66 } 67 loadRotate(float rot, float x, float y, float z)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); 80 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; 94 float zs = z * s; 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 loadScale(float x, float y, float z)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 } 112 loadTranslate(float x, float y, float z)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 loadMultiply(Matrix4f lhs, Matrix4f rhs)120 public void loadMultiply(Matrix4f lhs, Matrix4f rhs) { 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 loadOrtho(float l, float r, float b, float t, float n, float f)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); 146 mMat[13]= -(t + b) / (t - b); 147 mMat[14]= -(f + n) / (f - n); 148 } 149 loadFrustum(float l, float r, float b, float t, float n, float f)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 multiply(Matrix4f rhs)162 public void multiply(Matrix4f rhs) { 163 Matrix4f tmp = new Matrix4f(); 164 tmp.loadMultiply(this, rhs); 165 load(tmp); 166 } rotate(float rot, float x, float y, float z)167 public void rotate(float rot, float x, float y, float z) { 168 Matrix4f tmp = new Matrix4f(); 169 tmp.loadRotate(rot, x, y, z); 170 multiply(tmp); 171 } scale(float x, float y, float z)172 public void scale(float x, float y, float z) { 173 Matrix4f tmp = new Matrix4f(); 174 tmp.loadScale(x, y, z); 175 multiply(tmp); 176 } translate(float x, float y, float z)177 public void translate(float x, float y, float z) { 178 Matrix4f tmp = new Matrix4f(); 179 tmp.loadTranslate(x, y, z); 180 multiply(tmp); 181 } 182 183 final float[] mMat; 184 } 185 186 187 188 189 190