Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 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 | |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 17 | #define LOG_TAG "OpenGLRenderer" |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 18 | |
| 19 | #include <math.h> |
| 20 | #include <stdlib.h> |
Romain Guy | 3e16833 | 2010-06-22 13:29:14 -0700 | [diff] [blame] | 21 | #include <string.h> |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 22 | |
| 23 | #include <utils/Log.h> |
| 24 | |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 25 | #include <SkMatrix.h> |
| 26 | |
Romain Guy | 4bb9420 | 2010-10-12 15:59:26 -0700 | [diff] [blame] | 27 | #include "utils/Compare.h" |
Romain Guy | 85bf02f | 2010-06-22 13:11:24 -0700 | [diff] [blame] | 28 | #include "Matrix.h" |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 29 | |
| 30 | namespace android { |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 31 | namespace uirenderer { |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 32 | |
| 33 | void Matrix4::loadIdentity() { |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 34 | data[kScaleX] = 1.0f; |
| 35 | data[kSkewY] = 0.0f; |
| 36 | data[2] = 0.0f; |
| 37 | data[kPerspective0] = 0.0f; |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 38 | |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 39 | data[kSkewX] = 0.0f; |
| 40 | data[kScaleY] = 1.0f; |
| 41 | data[6] = 0.0f; |
| 42 | data[kPerspective1] = 0.0f; |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 43 | |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 44 | data[8] = 0.0f; |
| 45 | data[9] = 0.0f; |
| 46 | data[kScaleZ] = 1.0f; |
| 47 | data[11] = 0.0f; |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 48 | |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 49 | data[kTranslateX] = 0.0f; |
| 50 | data[kTranslateY] = 0.0f; |
| 51 | data[kTranslateZ] = 0.0f; |
| 52 | data[kPerspective2] = 1.0f; |
| 53 | |
Romain Guy | 302a9df | 2011-08-16 13:55:02 -0700 | [diff] [blame] | 54 | mIsIdentity = true; |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 55 | mSimpleMatrix = true; |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Romain Guy | e8cb9c14 | 2010-10-04 14:14:11 -0700 | [diff] [blame] | 58 | bool Matrix4::changesBounds() { |
Romain Guy | 03750a0 | 2010-10-18 14:06:08 -0700 | [diff] [blame] | 59 | return !(ALMOST_EQUAL(data[0], 1.0f) && ALMOST_EQUAL(data[1], 0.0f) && |
| 60 | ALMOST_EQUAL(data[2], 0.0f) && ALMOST_EQUAL(data[4], 0.0f) && |
| 61 | ALMOST_EQUAL(data[5], 1.0f) && ALMOST_EQUAL(data[6], 0.0f) && |
| 62 | ALMOST_EQUAL(data[8], 0.0f) && ALMOST_EQUAL(data[9], 0.0f) && |
| 63 | ALMOST_EQUAL(data[10], 1.0f)); |
Romain Guy | e8cb9c14 | 2010-10-04 14:14:11 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 66 | bool Matrix4::isPureTranslate() { |
| 67 | return mSimpleMatrix && |
| 68 | ALMOST_EQUAL(data[kScaleX], 1.0f) && ALMOST_EQUAL(data[kScaleY], 1.0f); |
| 69 | } |
| 70 | |
Romain Guy | 181d0a6 | 2011-06-09 18:52:38 -0700 | [diff] [blame] | 71 | bool Matrix4::isSimple() { |
| 72 | return mSimpleMatrix; |
| 73 | } |
| 74 | |
Romain Guy | 302a9df | 2011-08-16 13:55:02 -0700 | [diff] [blame] | 75 | bool Matrix4::isIdentity() { |
| 76 | return mIsIdentity; |
| 77 | } |
| 78 | |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 79 | void Matrix4::load(const float* v) { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 80 | memcpy(data, v, sizeof(data)); |
Romain Guy | 302a9df | 2011-08-16 13:55:02 -0700 | [diff] [blame] | 81 | // TODO: Do something smarter here |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 82 | mSimpleMatrix = false; |
Romain Guy | 302a9df | 2011-08-16 13:55:02 -0700 | [diff] [blame] | 83 | mIsIdentity = false; |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | void Matrix4::load(const Matrix4& v) { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 87 | memcpy(data, v.data, sizeof(data)); |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 88 | mSimpleMatrix = v.mSimpleMatrix; |
Romain Guy | 302a9df | 2011-08-16 13:55:02 -0700 | [diff] [blame] | 89 | mIsIdentity = v.mIsIdentity; |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 92 | void Matrix4::load(const SkMatrix& v) { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 93 | memset(data, 0, sizeof(data)); |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 94 | |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 95 | data[kScaleX] = v[SkMatrix::kMScaleX]; |
| 96 | data[kSkewX] = v[SkMatrix::kMSkewX]; |
| 97 | data[kTranslateX] = v[SkMatrix::kMTransX]; |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 98 | |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 99 | data[kSkewY] = v[SkMatrix::kMSkewY]; |
| 100 | data[kScaleY] = v[SkMatrix::kMScaleY]; |
| 101 | data[kTranslateY] = v[SkMatrix::kMTransY]; |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 102 | |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 103 | data[kPerspective0] = v[SkMatrix::kMPersp0]; |
| 104 | data[kPerspective1] = v[SkMatrix::kMPersp1]; |
| 105 | data[kPerspective2] = v[SkMatrix::kMPersp2]; |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 106 | |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 107 | data[kScaleZ] = 1.0f; |
| 108 | |
Romain Guy | e5ebcb0 | 2010-10-15 13:57:28 -0700 | [diff] [blame] | 109 | mSimpleMatrix = (v.getType() <= (SkMatrix::kScale_Mask | SkMatrix::kTranslate_Mask)); |
Romain Guy | 302a9df | 2011-08-16 13:55:02 -0700 | [diff] [blame] | 110 | mIsIdentity = v.isIdentity(); |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | void Matrix4::copyTo(SkMatrix& v) const { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 114 | v.reset(); |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 115 | |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 116 | v.set(SkMatrix::kMScaleX, data[kScaleX]); |
| 117 | v.set(SkMatrix::kMSkewX, data[kSkewX]); |
| 118 | v.set(SkMatrix::kMTransX, data[kTranslateX]); |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 119 | |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 120 | v.set(SkMatrix::kMSkewY, data[kSkewY]); |
| 121 | v.set(SkMatrix::kMScaleY, data[kScaleY]); |
| 122 | v.set(SkMatrix::kMTransY, data[kTranslateY]); |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 123 | |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 124 | v.set(SkMatrix::kMPersp0, data[kPerspective0]); |
| 125 | v.set(SkMatrix::kMPersp1, data[kPerspective1]); |
| 126 | v.set(SkMatrix::kMPersp2, data[kPerspective2]); |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 129 | void Matrix4::loadInverse(const Matrix4& v) { |
| 130 | double scale = 1.0 / |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 131 | (v.data[kScaleX] * ((double) v.data[kScaleY] * v.data[kPerspective2] - |
| 132 | (double) v.data[kTranslateY] * v.data[kPerspective1]) + |
| 133 | v.data[kSkewX] * ((double) v.data[kTranslateY] * v.data[kPerspective0] - |
| 134 | (double) v.data[kSkewY] * v.data[kPerspective2]) + |
| 135 | v.data[kTranslateX] * ((double) v.data[kSkewY] * v.data[kPerspective1] - |
| 136 | (double) v.data[kScaleY] * v.data[kPerspective0])); |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 137 | |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 138 | data[kScaleX] = (v.data[kScaleY] * v.data[kPerspective2] - |
| 139 | v.data[kTranslateY] * v.data[kPerspective1]) * scale; |
| 140 | data[kSkewX] = (v.data[kTranslateX] * v.data[kPerspective1] - |
| 141 | v.data[kSkewX] * v.data[kPerspective2]) * scale; |
| 142 | data[kTranslateX] = (v.data[kSkewX] * v.data[kTranslateY] - |
| 143 | v.data[kTranslateX] * v.data[kScaleY]) * scale; |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 144 | |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 145 | data[kSkewY] = (v.data[kTranslateY] * v.data[kPerspective0] - |
| 146 | v.data[kSkewY] * v.data[kPerspective2]) * scale; |
| 147 | data[kScaleY] = (v.data[kScaleX] * v.data[kPerspective2] - |
| 148 | v.data[kTranslateX] * v.data[kPerspective0]) * scale; |
| 149 | data[kTranslateY] = (v.data[kTranslateX] * v.data[kSkewY] - |
| 150 | v.data[kScaleX] * v.data[kTranslateY]) * scale; |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 151 | |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 152 | data[kPerspective0] = (v.data[kSkewY] * v.data[kPerspective1] - |
| 153 | v.data[kScaleY] * v.data[kPerspective0]) * scale; |
| 154 | data[kPerspective1] = (v.data[kSkewX] * v.data[kPerspective0] - |
| 155 | v.data[kScaleX] * v.data[kPerspective1]) * scale; |
| 156 | data[kPerspective2] = (v.data[kScaleX] * v.data[kScaleY] - |
| 157 | v.data[kSkewX] * v.data[kSkewY]) * scale; |
| 158 | |
| 159 | mSimpleMatrix = v.mSimpleMatrix; |
Romain Guy | 302a9df | 2011-08-16 13:55:02 -0700 | [diff] [blame] | 160 | mIsIdentity = v.mIsIdentity; |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 163 | void Matrix4::copyTo(float* v) const { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 164 | memcpy(v, data, sizeof(data)); |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 167 | float Matrix4::getTranslateX() { |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 168 | return data[kTranslateX]; |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | float Matrix4::getTranslateY() { |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 172 | return data[kTranslateY]; |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Romain Guy | ddb80be | 2010-09-20 19:04:33 -0700 | [diff] [blame] | 175 | void Matrix4::multiply(float v) { |
| 176 | for (int i = 0; i < 16; i++) { |
| 177 | data[i] *= v; |
| 178 | } |
Romain Guy | 302a9df | 2011-08-16 13:55:02 -0700 | [diff] [blame] | 179 | mIsIdentity = false; |
Romain Guy | ddb80be | 2010-09-20 19:04:33 -0700 | [diff] [blame] | 180 | } |
| 181 | |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 182 | void Matrix4::loadTranslate(float x, float y, float z) { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 183 | loadIdentity(); |
Romain Guy | 302a9df | 2011-08-16 13:55:02 -0700 | [diff] [blame] | 184 | |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 185 | data[kTranslateX] = x; |
| 186 | data[kTranslateY] = y; |
| 187 | data[kTranslateZ] = z; |
Romain Guy | 302a9df | 2011-08-16 13:55:02 -0700 | [diff] [blame] | 188 | |
| 189 | mIsIdentity = false; |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | void Matrix4::loadScale(float sx, float sy, float sz) { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 193 | loadIdentity(); |
Romain Guy | 302a9df | 2011-08-16 13:55:02 -0700 | [diff] [blame] | 194 | |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 195 | data[kScaleX] = sx; |
| 196 | data[kScaleY] = sy; |
| 197 | data[kScaleZ] = sz; |
Romain Guy | 302a9df | 2011-08-16 13:55:02 -0700 | [diff] [blame] | 198 | |
| 199 | mIsIdentity = false; |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Romain Guy | 807daf7 | 2011-01-18 11:19:19 -0800 | [diff] [blame] | 202 | void Matrix4::loadSkew(float sx, float sy) { |
| 203 | loadIdentity(); |
| 204 | |
| 205 | data[kScaleX] = 1.0f; |
| 206 | data[kSkewX] = sx; |
| 207 | data[kTranslateX] = 0.0f; |
| 208 | |
| 209 | data[kSkewY] = sy; |
| 210 | data[kScaleY] = 1.0f; |
| 211 | data[kTranslateY] = 0.0f; |
| 212 | |
| 213 | data[kPerspective0] = 0.0f; |
| 214 | data[kPerspective1] = 0.0f; |
| 215 | data[kPerspective2] = 1.0f; |
| 216 | |
| 217 | mSimpleMatrix = false; |
Romain Guy | 302a9df | 2011-08-16 13:55:02 -0700 | [diff] [blame] | 218 | mIsIdentity = false; |
Romain Guy | 807daf7 | 2011-01-18 11:19:19 -0800 | [diff] [blame] | 219 | } |
| 220 | |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 221 | void Matrix4::loadRotate(float angle, float x, float y, float z) { |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 222 | data[kPerspective0] = 0.0f; |
| 223 | data[kPerspective1] = 0.0f; |
| 224 | data[11] = 0.0f; |
| 225 | data[kTranslateX] = 0.0f; |
| 226 | data[kTranslateY] = 0.0f; |
| 227 | data[kTranslateZ] = 0.0f; |
| 228 | data[kPerspective2] = 1.0f; |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 229 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 230 | angle *= float(M_PI / 180.0f); |
| 231 | float c = cosf(angle); |
| 232 | float s = sinf(angle); |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 233 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 234 | const float length = sqrtf(x * x + y * y + z * z); |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 235 | float recipLen = 1.0f / length; |
| 236 | x *= recipLen; |
| 237 | y *= recipLen; |
| 238 | z *= recipLen; |
| 239 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 240 | const float nc = 1.0f - c; |
| 241 | const float xy = x * y; |
| 242 | const float yz = y * z; |
| 243 | const float zx = z * x; |
| 244 | const float xs = x * s; |
| 245 | const float ys = y * s; |
| 246 | const float zs = z * s; |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 247 | |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 248 | data[kScaleX] = x * x * nc + c; |
| 249 | data[kSkewX] = xy * nc - zs; |
| 250 | data[8] = zx * nc + ys; |
| 251 | data[kSkewY] = xy * nc + zs; |
| 252 | data[kScaleY] = y * y * nc + c; |
| 253 | data[9] = yz * nc - xs; |
| 254 | data[2] = zx * nc - ys; |
| 255 | data[6] = yz * nc + xs; |
| 256 | data[kScaleZ] = z * z * nc + c; |
| 257 | |
| 258 | mSimpleMatrix = false; |
Romain Guy | 302a9df | 2011-08-16 13:55:02 -0700 | [diff] [blame] | 259 | mIsIdentity = false; |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | void Matrix4::loadMultiply(const Matrix4& u, const Matrix4& v) { |
| 263 | for (int i = 0 ; i < 4 ; i++) { |
| 264 | float x = 0; |
| 265 | float y = 0; |
| 266 | float z = 0; |
| 267 | float w = 0; |
| 268 | |
| 269 | for (int j = 0 ; j < 4 ; j++) { |
Romain Guy | c7d5349 | 2010-06-25 13:41:57 -0700 | [diff] [blame] | 270 | const float e = v.get(i, j); |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 271 | x += u.get(j, 0) * e; |
| 272 | y += u.get(j, 1) * e; |
| 273 | z += u.get(j, 2) * e; |
| 274 | w += u.get(j, 3) * e; |
| 275 | } |
| 276 | |
| 277 | set(i, 0, x); |
| 278 | set(i, 1, y); |
| 279 | set(i, 2, z); |
| 280 | set(i, 3, w); |
| 281 | } |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 282 | |
| 283 | mSimpleMatrix = u.mSimpleMatrix && v.mSimpleMatrix; |
Romain Guy | 302a9df | 2011-08-16 13:55:02 -0700 | [diff] [blame] | 284 | mIsIdentity = false; |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | void Matrix4::loadOrtho(float left, float right, float bottom, float top, float near, float far) { |
| 288 | loadIdentity(); |
Romain Guy | 302a9df | 2011-08-16 13:55:02 -0700 | [diff] [blame] | 289 | |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 290 | data[kScaleX] = 2.0f / (right - left); |
| 291 | data[kScaleY] = 2.0f / (top - bottom); |
| 292 | data[kScaleZ] = -2.0f / (far - near); |
| 293 | data[kTranslateX] = -(right + left) / (right - left); |
| 294 | data[kTranslateY] = -(top + bottom) / (top - bottom); |
| 295 | data[kTranslateZ] = -(far + near) / (far - near); |
Romain Guy | 302a9df | 2011-08-16 13:55:02 -0700 | [diff] [blame] | 296 | |
| 297 | mIsIdentity = false; |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 298 | } |
| 299 | |
Romain Guy | 0ba681b | 2010-08-12 15:37:00 -0700 | [diff] [blame] | 300 | #define MUL_ADD_STORE(a, b, c) a = (a) * (b) + (c) |
| 301 | |
| 302 | void Matrix4::mapPoint(float& x, float& y) const { |
| 303 | if (mSimpleMatrix) { |
| 304 | MUL_ADD_STORE(x, data[kScaleX], data[kTranslateX]); |
| 305 | MUL_ADD_STORE(y, data[kScaleY], data[kTranslateY]); |
| 306 | return; |
| 307 | } |
| 308 | |
| 309 | float dx = x * data[kScaleX] + y * data[kSkewX] + data[kTranslateX]; |
| 310 | float dy = x * data[kSkewY] + y * data[kScaleY] + data[kTranslateY]; |
| 311 | float dz = x * data[kPerspective0] + y * data[kPerspective1] + data[kPerspective2]; |
| 312 | if (dz) dz = 1.0f / dz; |
| 313 | |
| 314 | x = dx * dz; |
| 315 | y = dy * dz; |
| 316 | } |
| 317 | |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 318 | void Matrix4::mapRect(Rect& r) const { |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 319 | if (mSimpleMatrix) { |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 320 | MUL_ADD_STORE(r.left, data[kScaleX], data[kTranslateX]); |
| 321 | MUL_ADD_STORE(r.right, data[kScaleX], data[kTranslateX]); |
| 322 | MUL_ADD_STORE(r.top, data[kScaleY], data[kTranslateY]); |
| 323 | MUL_ADD_STORE(r.bottom, data[kScaleY], data[kTranslateY]); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 324 | |
| 325 | if (r.left > r.right) { |
| 326 | float x = r.left; |
| 327 | r.left = r.right; |
| 328 | r.right = x; |
| 329 | } |
| 330 | |
| 331 | if (r.top > r.bottom) { |
| 332 | float y = r.top; |
| 333 | r.top = r.bottom; |
| 334 | r.bottom = y; |
| 335 | } |
| 336 | |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 337 | return; |
| 338 | } |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 339 | |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 340 | float vertices[] = { |
Romain Guy | 1d83e19 | 2010-08-17 11:37:00 -0700 | [diff] [blame] | 341 | r.left, r.top, |
| 342 | r.right, r.top, |
| 343 | r.right, r.bottom, |
| 344 | r.left, r.bottom |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 345 | }; |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 346 | |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 347 | float x, y, z; |
| 348 | |
| 349 | for (int i = 0; i < 8; i+= 2) { |
| 350 | float px = vertices[i]; |
| 351 | float py = vertices[i + 1]; |
| 352 | |
| 353 | x = px * data[kScaleX] + py * data[kSkewX] + data[kTranslateX]; |
| 354 | y = px * data[kSkewY] + py * data[kScaleY] + data[kTranslateY]; |
| 355 | z = px * data[kPerspective0] + py * data[kPerspective1] + data[kPerspective2]; |
| 356 | if (z) z = 1.0f / z; |
| 357 | |
| 358 | vertices[i] = x * z; |
| 359 | vertices[i + 1] = y * z; |
| 360 | } |
| 361 | |
| 362 | r.left = r.right = vertices[0]; |
| 363 | r.top = r.bottom = vertices[1]; |
| 364 | |
| 365 | for (int i = 2; i < 8; i += 2) { |
| 366 | x = vertices[i]; |
| 367 | y = vertices[i + 1]; |
| 368 | |
| 369 | if (x < r.left) r.left = x; |
| 370 | else if (x > r.right) r.right = x; |
| 371 | if (y < r.top) r.top = y; |
| 372 | else if (y > r.bottom) r.bottom = y; |
| 373 | } |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 374 | } |
| 375 | |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 376 | void Matrix4::dump() const { |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 377 | LOGD("Matrix4[simple=%d", mSimpleMatrix); |
Romain Guy | 1d83e19 | 2010-08-17 11:37:00 -0700 | [diff] [blame] | 378 | LOGD(" %f %f %f %f", data[kScaleX], data[kSkewX], data[8], data[kTranslateX]); |
| 379 | LOGD(" %f %f %f %f", data[kSkewY], data[kScaleY], data[9], data[kTranslateY]); |
Romain Guy | af28b51 | 2010-08-12 14:34:44 -0700 | [diff] [blame] | 380 | LOGD(" %f %f %f %f", data[2], data[6], data[kScaleZ], data[kTranslateZ]); |
| 381 | LOGD(" %f %f %f %f", data[kPerspective0], data[kPerspective1], data[11], data[kPerspective2]); |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 382 | LOGD("]"); |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 383 | } |
| 384 | |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 385 | }; // namespace uirenderer |
| 386 | }; // namespace android |