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 | 85bf02f | 2010-06-22 13:11:24 -0700 | [diff] [blame] | 17 | #ifndef ANDROID_MATRIX_H |
| 18 | #define ANDROID_MATRIX_H |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 19 | |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 20 | #include <SkMatrix.h> |
| 21 | |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 22 | namespace android { |
| 23 | |
| 24 | /////////////////////////////////////////////////////////////////////////////// |
| 25 | // Classes |
| 26 | /////////////////////////////////////////////////////////////////////////////// |
| 27 | |
| 28 | class Matrix4 { |
| 29 | public: |
| 30 | Matrix4() { |
| 31 | loadIdentity(); |
| 32 | } |
| 33 | |
| 34 | Matrix4(const float* v) { |
| 35 | load(v); |
| 36 | } |
| 37 | |
| 38 | Matrix4(const Matrix4& v) { |
| 39 | load(v); |
| 40 | } |
| 41 | |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 42 | Matrix4(const SkMatrix& v) { |
| 43 | load(v); |
| 44 | } |
| 45 | |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 46 | void loadIdentity(); |
| 47 | |
| 48 | void load(const float* v); |
| 49 | void load(const Matrix4& v); |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 50 | void load(const SkMatrix& v); |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 51 | |
| 52 | void loadTranslate(float x, float y, float z); |
| 53 | void loadScale(float sx, float sy, float sz); |
| 54 | void loadRotate(float angle, float x, float y, float z); |
| 55 | void loadMultiply(const Matrix4& u, const Matrix4& v); |
| 56 | |
| 57 | void loadOrtho(float left, float right, float bottom, float top, float near, float far); |
| 58 | |
| 59 | void multiply(const Matrix4& v) { |
| 60 | Matrix4 u; |
| 61 | u.loadMultiply(*this, v); |
| 62 | load(u); |
| 63 | } |
| 64 | |
| 65 | void translate(float x, float y, float z) { |
| 66 | Matrix4 u; |
| 67 | u.loadTranslate(x, y, z); |
| 68 | multiply(u); |
| 69 | } |
| 70 | |
| 71 | void scale(float sx, float sy, float sz) { |
| 72 | Matrix4 u; |
| 73 | u.loadScale(sx, sy, sz); |
| 74 | multiply(u); |
| 75 | } |
| 76 | |
| 77 | void rotate(float angle, float x, float y, float z) { |
| 78 | Matrix4 u; |
| 79 | u.loadRotate(angle, x, y, z); |
| 80 | multiply(u); |
| 81 | } |
| 82 | |
| 83 | void copyTo(float* v) const; |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 84 | void copyTo(SkMatrix& v) const; |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 85 | |
| 86 | void dump() const; |
| 87 | |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 88 | private: |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 89 | inline float get(int i, int j) const { |
| 90 | return mMat[i * 4 + j]; |
| 91 | } |
| 92 | |
| 93 | inline void set(int i, int j, float v) { |
| 94 | mMat[i * 4 + j] = v; |
| 95 | } |
| 96 | |
| 97 | float mMat[16]; |
| 98 | }; // class Matrix4 |
| 99 | |
| 100 | /////////////////////////////////////////////////////////////////////////////// |
| 101 | // Types |
| 102 | /////////////////////////////////////////////////////////////////////////////// |
| 103 | |
| 104 | typedef Matrix4 mat4; |
| 105 | |
| 106 | }; // namespace android |
| 107 | |
Romain Guy | 85bf02f | 2010-06-22 13:11:24 -0700 | [diff] [blame] | 108 | #endif // ANDROID_MATRIX_H |