1 /*
2  * Copyright (C) 2008 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 package com.example.android.apis.graphics.kube;
18 
19 /**
20  *
21  * A 4x4 float matrix
22  *
23  */
24 public class M4 {
25 	public float[][] m = new float[4][4];
26 
M4()27 	public M4() {
28 	}
29 
M4(M4 other)30 	public M4(M4 other) {
31 		for (int i = 0; i < 4; i++) {
32 			for (int j = 0; j < 4; j++) {
33 				m[i][j] = other.m[i][j];
34 			}
35 		}
36 	}
37 
multiply(GLVertex src, GLVertex dest)38 	public void multiply(GLVertex src, GLVertex dest) {
39 		dest.x = src.x * m[0][0] + src.y * m[1][0] + src.z * m[2][0] + m[3][0];
40 		dest.y = src.x * m[0][1] + src.y * m[1][1] + src.z * m[2][1] + m[3][1];
41 		dest.z = src.x * m[0][2] + src.y * m[1][2] + src.z * m[2][2] + m[3][2];
42 	}
43 
multiply(M4 other)44 	public M4 multiply(M4 other) {
45 		M4 result = new M4();
46 		float[][] m1 = m;
47 		float[][] m2 = other.m;
48 
49 		for (int i = 0; i < 4; i++) {
50 			for (int j = 0; j < 4; j++) {
51 				result.m[i][j] = m1[i][0]*m2[0][j] + m1[i][1]*m2[1][j] + m1[i][2]*m2[2][j] + m1[i][3]*m2[3][j];
52 			}
53 		}
54 
55 		return result;
56 	}
57 
setIdentity()58 	public void setIdentity() {
59 		for (int i = 0; i < 4; i++) {
60 			for (int j = 0; j < 4; j++) {
61 				m[i][j] = (i == j ? 1f : 0f);
62 			}
63 		}
64 	}
65 
66 	@Override
toString()67 	public String toString() {
68 		StringBuilder builder = new StringBuilder("[ ");
69 		for (int i = 0; i < 4; i++) {
70 			for (int j = 0; j < 4; j++) {
71 				builder.append(m[i][j]);
72 				builder.append(" ");
73 			}
74 			if (i < 2)
75 				builder.append("\n  ");
76 		}
77 		builder.append(" ]");
78 		return builder.toString();
79 	}
80 }
81