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