public Matrix2D(Matrix1D a, Matrix1D b, Matrix1D c, Matrix1D d) { A = a; B = b; C = c; D = d; }
/// <summary> /// Fully rotate a single point around the origin /// </summary> /// <param name="point">The point to rotate</param> /// <returns>The rotated point</returns> private float4 FullRotateSingle(float4 point) { var x = point.x; var y = point.y; var z = point.z; var w = point.w; //xy Matrix1D matr = new Matrix1D(new float[] { z, w }); Matrix1D rotated = (Matrix1D)(matr * GetRotationMatrix(rotation.A)); z = rotated.values[0]; w = rotated.values[1]; //xz matr = new Matrix1D(new float[] { y, w }); rotated = (Matrix1D)(matr * GetRotationMatrix(rotation.B)); y = rotated.values[0]; w = rotated.values[1]; //xw matr = new Matrix1D(new float[] { y, z }); rotated = (Matrix1D)(matr * GetRotationMatrix(rotation.C)); y = rotated.values[0]; z = rotated.values[1]; //yz matr = new Matrix1D(new float[] { x, w }); rotated = (Matrix1D)(matr * GetRotationMatrix(rotation.D)); x = rotated.values[0]; w = rotated.values[1]; //yw matr = new Matrix1D(new float[] { x, z }); rotated = (Matrix1D)(matr * GetRotationMatrix(rotation.E)); x = rotated.values[0]; z = rotated.values[1]; //zw matr = new Matrix1D(new float[] { x, y }); rotated = (Matrix1D)(matr * GetRotationMatrix(rotation.F)); x = rotated.values[0]; y = rotated.values[1]; return(new float4(x, y, z, w)); }
public Matrix2D(Matrix2DType type, string symbol) { if (type == Matrix2DType.AffineTransformation) { A = new Matrix1D(MatrixType.FourParameter, symbol); B = new Matrix1D(MatrixType.Zero, string.Empty); C = new Matrix1D(MatrixType.Zero, string.Empty); D = A.Transpose().Inverse(); } else if (type == Matrix2DType.ChirpMultiplication) { A = new Matrix1D(MatrixType.Identity, string.Empty); B = new Matrix1D(MatrixType.Zero, string.Empty); C = new Matrix1D(MatrixType.FourParameter, symbol); D = new Matrix1D(MatrixType.Identity, string.Empty); } else { A = new Matrix1D(MatrixType.Zero, string.Empty); B = new Matrix1D(MatrixType.Identity, string.Empty); C = B.Multiply(-1); D = new Matrix1D(MatrixType.Zero, string.Empty); } }
public void Draw(System.Drawing.Graphics graphics) { var rnd = new Random(15); var whitePen = new Pen(Color.White, 1); var screenMid = new Vertex(800 / 2, 600 / 2, 0); var projectMat = Camera.GetProjectionMatrix(); var transLationMat = Camera.GetTranslationMatrix(); var bufferedImage = new BufferedImage(800, 600); var g = bufferedImage.GetGraphics(); bufferedImage.Bitmap.SetPixel((int)screenMid.X, (int)screenMid.Y, Color.Cyan); g.DrawRectangle(new Pen(Color.Cyan), 0, 0, 800 - 1, 600 - 1); foreach (var mesh in Meshes) { foreach (var face in mesh.Faces) { // Static flip var rotTest = Matrix4D.GetXRot((Math.PI * 160) / 180); YROT += .0001; var rotTest2 = Matrix4D.GetYRot((Math.PI * (YROT)) / 180); var scaleTest = Matrix4D.GetScale(600 / 2, 600 / 2, 1); var temp = face.Vertex1; var translatedV1 = Matrix1D.From(mesh.Vertices[face.Vertex1]); var translatedV2 = Matrix1D.From(mesh.Vertices[face.Vertex2]); var translatedV3 = Matrix1D.From(mesh.Vertices[face.Vertex3]); translatedV1 = translatedV1.Multiply(rotTest2); translatedV2 = translatedV2.Multiply(rotTest2); translatedV3 = translatedV3.Multiply(rotTest2); translatedV1 = translatedV1.Multiply(rotTest); translatedV2 = translatedV2.Multiply(rotTest); translatedV3 = translatedV3.Multiply(rotTest); /* * Console.WriteLine(translatedV1); * Console.WriteLine(rotTest); */ translatedV1 = translatedV1.Multiply(scaleTest); translatedV2 = translatedV2.Multiply(scaleTest); translatedV3 = translatedV3.Multiply(scaleTest); translatedV1 = translatedV1.Multiply(transLationMat); translatedV2 = translatedV2.Multiply(transLationMat); translatedV3 = translatedV3.Multiply(transLationMat); translatedV1 = translatedV1.Multiply(projectMat); translatedV2 = translatedV2.Multiply(projectMat); translatedV3 = translatedV3.Multiply(projectMat); var resultV1 = Vertex.From(translatedV1); var resultV2 = Vertex.From(translatedV2); var resultV3 = Vertex.From(translatedV3); if (translatedV1.Z != 0) { resultV1.X /= translatedV1.Z; resultV1.Y /= translatedV1.Z; } if (translatedV2.Z != 0) { resultV2.X /= translatedV2.Z; resultV2.Y /= translatedV2.Z; } if (translatedV3.Z != 0) { resultV3.X /= translatedV3.Z; resultV3.Y /= translatedV3.Z; } resultV1.X += screenMid.X; resultV1.Y += screenMid.Y; resultV2.X += screenMid.X; resultV2.Y += screenMid.Y; resultV3.X += screenMid.X; resultV3.Y += screenMid.Y; if (resultV1.Z <= 0.1 || resultV2.Z <= 0.1 || resultV3.Z <= 0.1) { continue; } /* * g.DrawString($"{Math.Round(test1.X, 3)},{Math.Round(test1.Y, 3)},{Math.Round(test1.Z, 3)}", new Font(FontFamily.GenericSansSerif, 14), Brushes.Cyan, (float)resultV1.X, (float)resultV1.Y); * g.DrawString($"{Math.Round(test2.X, 3)},{Math.Round(test2.Y, 3)},{Math.Round(test2.Z, 3)}", new Font(FontFamily.GenericSansSerif, 14), Brushes.Cyan, (float)resultV2.X, (float)resultV2.Y); * g.DrawString($"{Math.Round(test3.X, 3)},{Math.Round(test3.Y, 3)},{Math.Round(test3.Z, 3)}", new Font(FontFamily.GenericSansSerif, 14), Brushes.Cyan, (float)resultV3.X, (float)resultV3.Y); */ if (ColorfullPolygonDisplayEnabled) { var color = Color.FromArgb(rnd.Next(0, 255), rnd.Next(0, 255), rnd.Next(0, 255), 255); g.FillPolygon( new SolidBrush(color), new PointF[] { new PointF((float)resultV1.X, (float)resultV1.Y), new PointF((float)resultV2.X, (float)resultV2.Y), new PointF((float)resultV3.X, (float)resultV3.Y) }); } var v = resultV2.Clone().Subtract(resultV1); var w = resultV3.Clone().Subtract(resultV1); var n = new Vertex() { X = (v.Y * w.Z) - (v.Z * w.Y), Y = (v.Z * w.X) - (v.X * w.Z), Z = (v.X * w.Y) - (v.Y * w.X) }; if (PointDisplayEnabled) { SetPixel(bufferedImage.Bitmap, (int)resultV1.X, (int)resultV1.Y); SetPixel(bufferedImage.Bitmap, (int)resultV1.X, (int)resultV1.Y); SetPixel(bufferedImage.Bitmap, (int)resultV1.X, (int)resultV1.Y); } if (WireframeDisplayEnabled) { g.DrawLine(whitePen, (float)resultV1.X, (float)resultV1.Y, (float)resultV2.X, (float)resultV2.Y); g.DrawLine(whitePen, (float)resultV2.X, (float)resultV2.Y, (float)resultV3.X, (float)resultV3.Y); g.DrawLine(whitePen, (float)resultV3.X, (float)resultV3.Y, (float)resultV1.X, (float)resultV1.Y); } /* * g.DrawPolygon( * new Pen(Color.FromArgb(rnd.Next(0,255),rnd.Next(0,255),rnd.Next(0,255), 255)), new Point[]{ * new Point((int)translatedV1.X, (int)translatedV1.Y), * new Point((int)translatedV2.X, (int)translatedV2.Y), * new Point((int)translatedV3.X, (int)translatedV3.Y) * }); */ } } Console.WriteLine($"{Camera.Pos.X},{Camera.Pos.Y},{Camera.Pos.Z}"); g.Dispose(); graphics.DrawImage(bufferedImage.Bitmap, 0, 0); }