Matrix.cpp revision 5cbbce535744b89df5ecea95de21ee3733298260
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 "OpenGLRenderer" 18 19#include <math.h> 20#include <stdlib.h> 21#include <string.h> 22 23#include <utils/Log.h> 24 25#include <SkMatrix.h> 26 27#include "Matrix.h" 28 29namespace android { 30namespace uirenderer { 31 32void Matrix4::loadIdentity() { 33 data[0] = 1.0f; 34 data[1] = 0.0f; 35 data[2] = 0.0f; 36 data[3] = 0.0f; 37 38 data[4] = 0.0f; 39 data[5] = 1.0f; 40 data[6] = 0.0f; 41 data[7] = 0.0f; 42 43 data[8] = 0.0f; 44 data[9] = 0.0f; 45 data[10] = 1.0f; 46 data[11] = 0.0f; 47 48 data[12] = 0.0f; 49 data[13] = 0.0f; 50 data[14] = 0.0f; 51 data[15] = 1.0f; 52} 53 54void Matrix4::load(const float* v) { 55 memcpy(data, v, sizeof(data)); 56} 57 58void Matrix4::load(const Matrix4& v) { 59 memcpy(data, v.data, sizeof(data)); 60} 61 62void Matrix4::load(const SkMatrix& v) { 63 memset(data, 0, sizeof(data)); 64 65 data[0] = v[SkMatrix::kMScaleX]; 66 data[4] = v[SkMatrix::kMSkewX]; 67 data[12] = v[SkMatrix::kMTransX]; 68 69 data[1] = v[SkMatrix::kMSkewY]; 70 data[5] = v[SkMatrix::kMScaleY]; 71 data[13] = v[SkMatrix::kMTransY]; 72 73 data[3] = v[SkMatrix::kMPersp0]; 74 data[7] = v[SkMatrix::kMPersp1]; 75 data[15] = v[SkMatrix::kMPersp2]; 76 77 data[10] = 1.0f; 78} 79 80void Matrix4::copyTo(SkMatrix& v) const { 81 v.reset(); 82 83 v.set(SkMatrix::kMScaleX, data[0]); 84 v.set(SkMatrix::kMSkewX, data[4]); 85 v.set(SkMatrix::kMTransX, data[12]); 86 87 v.set(SkMatrix::kMSkewY, data[1]); 88 v.set(SkMatrix::kMScaleY, data[5]); 89 v.set(SkMatrix::kMTransY, data[13]); 90 91 v.set(SkMatrix::kMPersp0, data[3]); 92 v.set(SkMatrix::kMPersp1, data[7]); 93 v.set(SkMatrix::kMPersp2, data[15]); 94} 95 96void Matrix4::copyTo(float* v) const { 97 memcpy(v, data, sizeof(data)); 98} 99 100float Matrix4::getTranslateX() { 101 return data[12]; 102} 103 104float Matrix4::getTranslateY() { 105 return data[13]; 106} 107 108void Matrix4::loadTranslate(float x, float y, float z) { 109 loadIdentity(); 110 data[12] = x; 111 data[13] = y; 112 data[14] = z; 113} 114 115void Matrix4::loadScale(float sx, float sy, float sz) { 116 loadIdentity(); 117 data[0] = sx; 118 data[5] = sy; 119 data[10] = sz; 120} 121 122void Matrix4::loadRotate(float angle, float x, float y, float z) { 123 data[3] = 0.0f; 124 data[7] = 0.0f; 125 data[11] = 0.0f; 126 data[12] = 0.0f; 127 data[13] = 0.0f; 128 data[14] = 0.0f; 129 data[15] = 1.0f; 130 131 angle *= float(M_PI / 180.0f); 132 float c = cosf(angle); 133 float s = sinf(angle); 134 135 const float length = sqrtf(x * x + y * y + z * z); 136 const float nc = 1.0f - c; 137 const float xy = x * y; 138 const float yz = y * z; 139 const float zx = z * x; 140 const float xs = x * s; 141 const float ys = y * s; 142 const float zs = z * s; 143 144 data[0] = x * x * nc + c; 145 data[4] = xy * nc - zs; 146 data[8] = zx * nc + ys; 147 data[1] = xy * nc + zs; 148 data[5] = y * y * nc + c; 149 data[9] = yz * nc - xs; 150 data[2] = zx * nc - ys; 151 data[6] = yz * nc + xs; 152 data[10] = z * z * nc + c; 153} 154 155void Matrix4::loadMultiply(const Matrix4& u, const Matrix4& v) { 156 for (int i = 0 ; i < 4 ; i++) { 157 float x = 0; 158 float y = 0; 159 float z = 0; 160 float w = 0; 161 162 for (int j = 0 ; j < 4 ; j++) { 163 const float e = v.get(i, j); 164 x += u.get(j, 0) * e; 165 y += u.get(j, 1) * e; 166 z += u.get(j, 2) * e; 167 w += u.get(j, 3) * e; 168 } 169 170 set(i, 0, x); 171 set(i, 1, y); 172 set(i, 2, z); 173 set(i, 3, w); 174 } 175} 176 177void Matrix4::loadOrtho(float left, float right, float bottom, float top, float near, float far) { 178 loadIdentity(); 179 data[0] = 2.0f / (right - left); 180 data[5] = 2.0f / (top - bottom); 181 data[10] = -2.0f / (far - near); 182 data[12] = -(right + left) / (right - left); 183 data[13] = -(top + bottom) / (top - bottom); 184 data[14] = -(far + near) / (far - near); 185} 186 187#define MUL_ADD_STORE(a, b, c) a = (a) * (b) + (c) 188 189void Matrix4::mapRect(Rect& r) const { 190 const float sx = data[0]; 191 const float sy = data[5]; 192 193 const float tx = data[12]; 194 const float ty = data[13]; 195 196 MUL_ADD_STORE(r.left, sx, tx); 197 MUL_ADD_STORE(r.right, sx, tx); 198 MUL_ADD_STORE(r.top, sy, ty); 199 MUL_ADD_STORE(r.bottom, sy, ty); 200} 201 202void Matrix4::dump() const { 203 LOGD("Matrix4["); 204 LOGD(" %f %f %f %f", data[0], data[4], data[ 8], data[12]); 205 LOGD(" %f %f %f %f", data[1], data[5], data[ 9], data[13]); 206 LOGD(" %f %f %f %f", data[2], data[6], data[10], data[14]); 207 LOGD(" %f %f %f %f", data[3], data[7], data[11], data[15]); 208 LOGD("]"); 209} 210 211}; // namespace uirenderer 212}; // namespace android 213