blob: ca41886631f463a780016b32056b550f08178d21 [file] [log] [blame]
Jason Samsd19f10d2009-05-22 14:03:28 -07001/*
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 Palevich81d0a9a2009-05-27 15:26:57 -070020#include "string.h"
Jason Samsd19f10d2009-05-22 14:03:28 -070021#include "math.h"
22
Jason Samsd19f10d2009-05-22 14:03:28 -070023using namespace android;
24using namespace android::renderscript;
25
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080026void Matrix::loadIdentity() {
Jason Samsd19f10d2009-05-22 14:03:28 -070027 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 Sakhartchouked9f2102010-11-09 17:00:54 -080048void Matrix::load(const float *v) {
Jason Samsd19f10d2009-05-22 14:03:28 -070049 memcpy(m, v, sizeof(m));
50}
51
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080052void Matrix::load(const Matrix *v) {
Jason Samsd19f10d2009-05-22 14:03:28 -070053 memcpy(m, v->m, sizeof(m));
54}
55
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080056void Matrix::loadRotate(float rot, float x, float y, float z) {
Jason Samsd19f10d2009-05-22 14:03:28 -070057 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 Sams41d98252010-08-05 18:11:49 -070070 if (len != 1) {
Jason Samsd19f10d2009-05-22 14:03:28 -070071 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 Samse9ad9a72009-09-30 17:36:20 -070082 const float zs = z * s;
Jason Samsd19f10d2009-05-22 14:03:28 -070083 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 Sakhartchouked9f2102010-11-09 17:00:54 -080094void Matrix::loadScale(float x, float y, float z) {
Jason Samsd19f10d2009-05-22 14:03:28 -070095 loadIdentity();
96 m[0] = x;
97 m[5] = y;
98 m[10] = z;
99}
100
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800101void Matrix::loadTranslate(float x, float y, float z) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700102 loadIdentity();
103 m[12] = x;
104 m[13] = y;
105 m[14] = z;
106}
107
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800108void Matrix::loadMultiply(const Matrix *lhs, const Matrix *rhs) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700109 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 Sams9c54bdb2009-06-17 16:52:59 -0700128void 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
138void 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 Samse9ad9a72009-09-30 17:36:20 -0700150void 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}