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