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