blob: 68b245b7e8226bdaa8c98937e85a5adac5a3d2b3 [file] [log] [blame]
Romain Guy08ae3172010-06-21 19:35:50 -07001/*
2 * Copyright (C) 2010 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#define LOG_TAG "UIMatrix"
18
19#include <math.h>
20#include <stdlib.h>
Romain Guy3e168332010-06-22 13:29:14 -070021#include <string.h>
Romain Guy08ae3172010-06-21 19:35:50 -070022
23#include <utils/Log.h>
24
Romain Guy85bf02f2010-06-22 13:11:24 -070025#include "Matrix.h"
Romain Guy08ae3172010-06-21 19:35:50 -070026
27namespace android {
28
29void Matrix4::loadIdentity() {
30 mMat[0] = 1;
31 mMat[1] = 0;
32 mMat[2] = 0;
33 mMat[3] = 0;
34
35 mMat[4] = 0;
36 mMat[5] = 1;
37 mMat[6] = 0;
38 mMat[7] = 0;
39
40 mMat[8] = 0;
41 mMat[9] = 0;
42 mMat[10] = 1;
43 mMat[11] = 0;
44
45 mMat[12] = 0;
46 mMat[13] = 0;
47 mMat[14] = 0;
48 mMat[15] = 1;
49}
50
51void Matrix4::load(const float* v) {
52 memcpy(mMat, v, sizeof(mMat));
53}
54
55void Matrix4::load(const Matrix4& v) {
56 memcpy(mMat, v.mMat, sizeof(mMat));
57}
58
59void Matrix4::copyTo(float* v) const {
60 memcpy(v, mMat, sizeof(mMat));
61}
62
63void Matrix4::loadTranslate(float x, float y, float z) {
64 loadIdentity();
65 mMat[12] = x;
66 mMat[13] = y;
67 mMat[14] = z;
68}
69
70void Matrix4::loadScale(float sx, float sy, float sz) {
71 loadIdentity();
72 mMat[0] = sx;
73 mMat[5] = sy;
74 mMat[10] = sz;
75}
76
77void Matrix4::loadRotate(float angle, float x, float y, float z) {
78 mMat[3] = 0;
79 mMat[7] = 0;
80 mMat[11] = 0;
81 mMat[12] = 0;
82 mMat[13] = 0;
83 mMat[14] = 0;
84 mMat[15] = 1;
85
86 angle *= float(M_PI / 180.0f);
87 float c = cosf(angle);
88 float s = sinf(angle);
89
90 const float length = sqrtf(x * x + y * y + z * z);
91 const float nc = 1.0f - c;
92 const float xy = x * y;
93 const float yz = y * z;
94 const float zx = z * x;
95 const float xs = x * s;
96 const float ys = y * s;
97 const float zs = z * s;
98
99 mMat[0] = x * x * nc + c;
100 mMat[4] = xy * nc - zs;
101 mMat[8] = zx * nc + ys;
102 mMat[1] = xy * nc + zs;
103 mMat[5] = y * y * nc + c;
104 mMat[9] = yz * nc - xs;
105 mMat[2] = zx * nc - ys;
106 mMat[6] = yz * nc + xs;
107 mMat[10] = z * z * nc + c;
108}
109
110void Matrix4::loadMultiply(const Matrix4& u, const Matrix4& v) {
111 for (int i = 0 ; i < 4 ; i++) {
112 float x = 0;
113 float y = 0;
114 float z = 0;
115 float w = 0;
116
117 for (int j = 0 ; j < 4 ; j++) {
118 const float e = v.get(i,j);
119 x += u.get(j, 0) * e;
120 y += u.get(j, 1) * e;
121 z += u.get(j, 2) * e;
122 w += u.get(j, 3) * e;
123 }
124
125 set(i, 0, x);
126 set(i, 1, y);
127 set(i, 2, z);
128 set(i, 3, w);
129 }
130}
131
132void Matrix4::loadOrtho(float left, float right, float bottom, float top, float near, float far) {
133 loadIdentity();
134 mMat[0] = 2 / (right - left);
135 mMat[5] = 2 / (top - bottom);
136 mMat[10] = -2 / (far - near);
137 mMat[12] = -(right + left) / (right - left);
138 mMat[13] = -(top + bottom) / (top - bottom);
139 mMat[14] = -(far + near) / (far - near);
140}
141
142void Matrix4::dump() const {
143 LOGD("%f %f %f %f", mMat[0], mMat[4], mMat[ 8], mMat[12]);
144 LOGD("%f %f %f %f", mMat[1], mMat[5], mMat[ 9], mMat[13]);
145 LOGD("%f %f %f %f", mMat[2], mMat[6], mMat[10], mMat[14]);
146 LOGD("%f %f %f %f", mMat[3], mMat[7], mMat[11], mMat[15]);
147}
148
149};