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