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 #include "rsMatrix.h" 18 19 #include "stdlib.h" 20 #include "string.h" 21 #include "math.h" 22 23 using namespace android; 24 using namespace android::renderscript; 25 26 27 loadIdentity()28 void Matrix::loadIdentity() 29 { 30 set(0, 0, 1); 31 set(1, 0, 0); 32 set(2, 0, 0); 33 set(3, 0, 0); 34 35 set(0, 1, 0); 36 set(1, 1, 1); 37 set(2, 1, 0); 38 set(3, 1, 0); 39 40 set(0, 2, 0); 41 set(1, 2, 0); 42 set(2, 2, 1); 43 set(3, 2, 0); 44 45 set(0, 3, 0); 46 set(1, 3, 0); 47 set(2, 3, 0); 48 set(3, 3, 1); 49 } 50 load(const float * v)51 void Matrix::load(const float *v) 52 { 53 memcpy(m, v, sizeof(m)); 54 } 55 load(const Matrix * v)56 void Matrix::load(const Matrix *v) 57 { 58 memcpy(m, v->m, sizeof(m)); 59 } 60 loadRotate(float rot,float x,float y,float z)61 void Matrix::loadRotate(float rot, float x, float y, float z) 62 { 63 float c, s; 64 m[3] = 0; 65 m[7] = 0; 66 m[11]= 0; 67 m[12]= 0; 68 m[13]= 0; 69 m[14]= 0; 70 m[15]= 1; 71 rot *= float(M_PI / 180.0f); 72 c = cosf(rot); 73 s = sinf(rot); 74 75 const float len = sqrtf(x*x + y*y + z*z); 76 if (!(len != 1)) { 77 const float recipLen = 1.f / len; 78 x *= recipLen; 79 y *= recipLen; 80 z *= recipLen; 81 } 82 const float nc = 1.0f - c; 83 const float xy = x * y; 84 const float yz = y * z; 85 const float zx = z * x; 86 const float xs = x * s; 87 const float ys = y * s; 88 const float zs = z * s; 89 m[ 0] = x*x*nc + c; 90 m[ 4] = xy*nc - zs; 91 m[ 8] = zx*nc + ys; 92 m[ 1] = xy*nc + zs; 93 m[ 5] = y*y*nc + c; 94 m[ 9] = yz*nc - xs; 95 m[ 2] = zx*nc - ys; 96 m[ 6] = yz*nc + xs; 97 m[10] = z*z*nc + c; 98 } 99 loadScale(float x,float y,float z)100 void Matrix::loadScale(float x, float y, float z) 101 { 102 loadIdentity(); 103 m[0] = x; 104 m[5] = y; 105 m[10] = z; 106 } 107 loadTranslate(float x,float y,float z)108 void Matrix::loadTranslate(float x, float y, float z) 109 { 110 loadIdentity(); 111 m[12] = x; 112 m[13] = y; 113 m[14] = z; 114 } 115 loadMultiply(const Matrix * lhs,const Matrix * rhs)116 void Matrix::loadMultiply(const Matrix *lhs, const Matrix *rhs) 117 { 118 for (int i=0 ; i<4 ; i++) { 119 float ri0 = 0; 120 float ri1 = 0; 121 float ri2 = 0; 122 float ri3 = 0; 123 for (int j=0 ; j<4 ; j++) { 124 const float rhs_ij = rhs->get(i,j); 125 ri0 += lhs->get(j,0) * rhs_ij; 126 ri1 += lhs->get(j,1) * rhs_ij; 127 ri2 += lhs->get(j,2) * rhs_ij; 128 ri3 += lhs->get(j,3) * rhs_ij; 129 } 130 set(i,0, ri0); 131 set(i,1, ri1); 132 set(i,2, ri2); 133 set(i,3, ri3); 134 } 135 } 136 loadOrtho(float l,float r,float b,float t,float n,float f)137 void Matrix::loadOrtho(float l, float r, float b, float t, float n, float f) { 138 loadIdentity(); 139 m[0] = 2 / (r - l); 140 m[5] = 2 / (t - b); 141 m[10]= -2 / (f - n); 142 m[12]= -(r + l) / (r - l); 143 m[13]= -(t + b) / (t - b); 144 m[14]= -(f + n) / (f - n); 145 } 146 loadFrustum(float l,float r,float b,float t,float n,float f)147 void Matrix::loadFrustum(float l, float r, float b, float t, float n, float f) { 148 loadIdentity(); 149 m[0] = 2 * n / (r - l); 150 m[5] = 2 * n / (t - b); 151 m[8] = (r + l) / (r - l); 152 m[9] = (t + b) / (t - b); 153 m[10]= -(f + n) / (f - n); 154 m[11]= -1; 155 m[14]= -2*f*n / (f - n); 156 m[15]= 0; 157 } 158 vectorMultiply(float * out,const float * in) const159 void Matrix::vectorMultiply(float *out, const float *in) const { 160 out[0] = (m[0] * in[0]) + (m[4] * in[1]) + (m[8] * in[2]) + m[12]; 161 out[1] = (m[1] * in[0]) + (m[5] * in[1]) + (m[9] * in[2]) + m[13]; 162 out[2] = (m[2] * in[0]) + (m[6] * in[1]) + (m[10] * in[2]) + m[14]; 163 out[3] = (m[3] * in[0]) + (m[7] * in[1]) + (m[11] * in[2]) + m[15]; 164 } 165