blob: 94eef136262e2aa49a4b4caac90f450a0627e111 [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
26
27
28void 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
51void Matrix::load(const float *v)
52{
53 memcpy(m, v, sizeof(m));
54}
55
56void Matrix::load(const Matrix *v)
57{
58 memcpy(m, v->m, sizeof(m));
59}
60
61void 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);
Jason Sams41d98252010-08-05 18:11:49 -070076 if (len != 1) {
Jason Samsd19f10d2009-05-22 14:03:28 -070077 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;
Jason Samse9ad9a72009-09-30 17:36:20 -070088 const float zs = z * s;
Jason Samsd19f10d2009-05-22 14:03:28 -070089 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
100void 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
108void 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
116void 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
Jason Sams9c54bdb2009-06-17 16:52:59 -0700137void 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
147void 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
Jason Samse9ad9a72009-09-30 17:36:20 -0700159void 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}