public Draw3DObject(Panel _panel) { panel = _panel; float oneOverd = oneOverdFactor / (2 * panel.Height / 4); matrix = Matrix3.AzimuthElevation(elevation, azimuth, oneOverd); }
public void TransformNormalize(Matrix3 m) { float[] result = m.VectorMultiply(new float[4] { X, Y, Z, W }); X = result[0] / result[3]; Y = result[1] / result[3]; Z = result[2]; W = 1; }
// Axonometric projection matrix: public static Matrix3 Axonometric(float alpha, float beta) { Matrix3 result = new Matrix3(); float sna = (float)Math.Sin(alpha * Math.PI / 180); float cna = (float)Math.Cos(alpha * Math.PI / 180); float snb = (float)Math.Sin(beta * Math.PI / 180); float cnb = (float)Math.Cos(beta * Math.PI / 180); result.M[0, 0] = cnb; result.M[0, 2] = snb; result.M[1, 0] = sna * snb; result.M[1, 1] = cna; result.M[1, 2] = -sna * cnb; result.M[2, 2] = 0; return result; }
// Multiply two matrices together: public static Matrix3 operator *(Matrix3 m1, Matrix3 m2) { Matrix3 result = new Matrix3(); for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { float element = 0; for (int k = 0; k < 4; k++) { element += m1.M[i, k] * m2.M[k, j]; } result.M[i, j] = element; } } return result; }
public static Matrix3 AzimuthElevation(float elevation, float azimuth, float oneOverd) { Matrix3 result = new Matrix3(); Matrix3 rotate = new Matrix3(); // make sure elevation in the range of [-90, 90]: if (elevation > 90) elevation = 90; else if (elevation < -90) elevation = -90; // Make sure azimuth in the range of [-180, 180]: if (azimuth > 180) azimuth = 180; else if (azimuth < -180) azimuth = -180; elevation = elevation * (float)Math.PI / 180.0f; float sne = (float)Math.Sin(elevation); float cne = (float)Math.Cos(elevation); azimuth = azimuth * (float)Math.PI / 180.0f; float sna = (float)Math.Sin(azimuth); float cna = (float)Math.Cos(azimuth); rotate.M[0, 0] = cna; rotate.M[0, 1] = sna; rotate.M[0, 2] = 0; rotate.M[1, 0] = -sne * sna; rotate.M[1, 1] = sne * cna; rotate.M[1, 2] = cne; rotate.M[2, 0] = cne * sna; rotate.M[2, 1] = -cne * cna; rotate.M[2, 2] = sne; if (oneOverd <= 0) result = rotate; else if (oneOverd > 0) { Matrix3 perspective = Matrix3.Perspective(1 / oneOverd); result = perspective * rotate; } return result; }
// Create a translation matrix public static Matrix3 Translate3(float dx, float dy, float dz) { Matrix3 result = new Matrix3(); result.M[0, 3] = dx; result.M[1, 3] = dy; result.M[2, 3] = dz; return result; }
// Top view projection matrix: public static Matrix3 TopView() { Matrix3 result = new Matrix3(); result.M[1, 1] = 0; result.M[2, 2] = 0; result.M[1, 2] = -1; return result; }
// Side view projection matrix: public static Matrix3 SideView() { Matrix3 result = new Matrix3(); result.M[0, 0] = 0; result.M[2, 2] = 0; result.M[0, 2] = -1; return result; }
// Create a scaling matrix: public static Matrix3 Scale3(float sx, float sy, float sz) { Matrix3 result = new Matrix3(); result.M[0, 0] = sx; result.M[1, 1] = sy; result.M[2, 2] = sz; return result; }
// Create a rotation matrix around the z axis: public static Matrix3 Rotate3Z(float theta) { theta = theta * (float)Math.PI / 180.0f; float sn = (float)Math.Sin(theta); float cn = (float)Math.Cos(theta); Matrix3 result = new Matrix3(); result.M[0, 0] = cn; result.M[0, 1] = -sn; result.M[1, 0] = sn; result.M[1, 1] = cn; return result; }
// Perspective projection matrix: public static Matrix3 Perspective(float d) { Matrix3 result = new Matrix3(); result.M[3, 2] = -1 / d; return result; }
// Oblique projection matrix: public static Matrix3 Oblique(float alpha, float theta) { Matrix3 result = new Matrix3(); float ta = (float)Math.Tan(alpha * Math.PI / 180); float snt = (float)Math.Sin(theta * Math.PI / 180); float cnt = (float)Math.Cos(theta * Math.PI / 180); result.M[0, 2] = -cnt / ta; result.M[1, 2] = -snt / ta; result.M[2, 2] = 0; return result; }
// Front view projection matrix: public static Matrix3 FrontView() { Matrix3 result = new Matrix3(); result.M[2, 2] = 0; return result; }
public static Matrix3 Euler(float alpha, float beta, float gamma) { Matrix3 result = new Matrix3(); alpha = alpha * (float)Math.PI / 180.0f; float sna = (float)Math.Sin(alpha); float cna = (float)Math.Cos(alpha); beta = beta * (float)Math.PI / 180.0f; float snb = (float)Math.Sin(beta); float cnb = (float)Math.Cos(beta); gamma = gamma * (float)Math.PI / 180.0f; float sng = (float)Math.Sin(gamma); float cng = (float)Math.Cos(gamma); result.M[0, 0] = cna * cng - sna * snb * sng; result.M[0, 1] = -snb * sng; result.M[0, 2] = sna * cng - cna * cnb * sng; result.M[1, 0] = -sna * snb; result.M[1, 1] = cnb; result.M[1, 2] = cna * snb; result.M[2, 0] = -cna * sng - sna * cnb * cng; result.M[2, 1] = -snb * cng; result.M[2, 2] = cna * cnb * cng - sna * snb; return result; }