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