// a(matrix_t) * f(float) public static matrix_t matrix_scale(matrix_t a, float f) { matrix_t _tmp = a; for (int i = 0; i < _tmp.row; i++) { for (int j = 0; j < _tmp.column; j++) { _tmp.m[i, j] *= f; } } return(_tmp); }
public void DrawBox() { matrix_t ma = new matrix_t(4, 4); ma.SetRotate(mRotateVector); mTransformer.world = ma; mTransformer.UpdateTransform(); DrawPlane(3, 7, 6, 2); DrawPlane(1, 2, 6, 5); DrawPlane(4, 5, 6, 7); DrawPlane(0, 4, 5, 1); DrawPlane(0, 1, 2, 3); DrawPlane(0, 4, 7, 3); }
// v(vector_t) * m(matrix_t) 这里默认矩阵row = 4 public static vector_t matrix_apply(vector_t v, matrix_t m) { vector_t _tmp = v; if (m.row == 4) { float[] _val = new float[4]; for (int i = 0; i < 4; ++i) { _val[i] = v.x * m.m[0, i] + v.y * m.m[1, i] + v.z * m.m[2, i] + v.w * m.m[3, i]; } _tmp.x = _val[0]; _tmp.y = _val[1]; _tmp.z = _val[2]; _tmp.w = _val[3]; } else { System.Diagnostics.Debug.Assert(false, "matrix_t Operation Error : matrix_apply"); } return(_tmp); }
// a + b public static matrix_t matrix_add(matrix_t a, matrix_t b) { matrix_t _tmp = a; if (a.row == b.row && a.column == b.column) { for (int i = 0; i < _tmp.row; i++) { for (int j = 0; j < _tmp.column; j++) { _tmp.m[i, j] += b.m[i, j]; } } } else { System.Diagnostics.Debug.Assert(false, "matrix_t Operation Error : matrix_add"); } return(_tmp); }
// a * b public static matrix_t matrix_mul(matrix_t a, matrix_t b) { matrix_t _tmp = new matrix_t(a.row, b.column); if (a.column == b.row) { for (int i = 0; i < a.row; i++) { for (int j = 0; j < b.column; j++) { for (int k = 0; k < b.column; k++) { _tmp.m[i, j] += a.m[i, k] * b.m[k, j]; } } } } else { System.Diagnostics.Debug.Assert(false, "matrix_t Operation Error : matrix_mul"); } return(_tmp); }
public matrix_t transform; // transform = world * view * projection public void Init() { world = new matrix_t(4, 4); world.SetIdentity(); view = new matrix_t(4, 4); view.SetIdentity(); //Init Projection projection = new matrix_t(4, 4); float fovy = 3.1415926f * 0.5f; float aspect = (float)Def.DeviceWidth / ((float)Def.DeviceHeight); float zn = 1f; float zf = 500f; float fax = 1.0f / (float)Math.Tan(fovy * 0.5f); projection.SetZero(); projection.m[0, 0] = (float)(fax / aspect); projection.m[1, 1] = (float)(fax); projection.m[2, 2] = zf / (zf - zn); projection.m[3, 2] = -zn * zf / (zf - zn); projection.m[2, 3] = 1; }
public void UpdateTransform() { matrix_t _tmp = Tools.matrix_mul(world, view); transform = Tools.matrix_mul(_tmp, projection); }