public static Vertex RotateArbitrary(Vertex vertex, Vertex axisVertex, double angle) { double axisX2 = axisVertex.X * axisVertex.X; double axisY2 = axisVertex.Y * axisVertex.Y; double axisZ2 = axisVertex.Z * axisVertex.Z; double axisSumOfSquares = axisX2 + axisY2 + axisZ2; Matrix rotationMatrix = new Matrix(4, 4); rotationMatrix[0,0] = (axisX2 + (axisY2 + axisZ2) * Math.Cos(angle)) / axisSumOfSquares; rotationMatrix[0,1] = (axisVertex.X * axisVertex.Y * (1 - Math.Cos(angle)) - axisVertex.Z * Math.Sqrt(axisSumOfSquares) * Math.Sin(angle)) / axisSumOfSquares; rotationMatrix[0,2] = (axisVertex.X * axisVertex.Z * (1 - Math.Cos(angle)) + axisVertex.Y * Math.Sqrt(axisSumOfSquares) * Math.Sin(angle)) / axisSumOfSquares; rotationMatrix[0,3] = 0.0; rotationMatrix[1,0] = (axisVertex.X * axisVertex.Y * (1 - Math.Cos(angle)) + axisVertex.Z * Math.Sqrt(axisSumOfSquares) * Math.Sin(angle)) / axisSumOfSquares; rotationMatrix[1,1] = (axisY2 + (axisX2 + axisZ2) * Math.Cos(angle)) / axisSumOfSquares; rotationMatrix[1,2] = (axisVertex.Y * axisVertex.Z * (1 - Math.Cos(angle)) - axisVertex.X * Math.Sqrt(axisSumOfSquares) * Math.Sin(angle)) / axisSumOfSquares; rotationMatrix[1,3] = 0.0; rotationMatrix[2,0] = (axisVertex.X * axisVertex.Z * (1 - Math.Cos(angle)) - axisVertex.Y * Math.Sqrt(axisSumOfSquares) * Math.Sin(angle)) / axisSumOfSquares; rotationMatrix[2,1] = (axisVertex.Y * axisVertex.Z * (1 - Math.Cos(angle)) + axisVertex.X * Math.Sqrt(axisSumOfSquares) * Math.Sin(angle)) / axisSumOfSquares; rotationMatrix[2,2] = (axisZ2 + (axisX2 + axisY2) * Math.Cos(angle)) / axisSumOfSquares; rotationMatrix[2,3] = 0.0; rotationMatrix[3,0] = 0.0; rotationMatrix[3,1] = 0.0; rotationMatrix[3,2] = 0.0; rotationMatrix[3,3] = 1.0; var outputMatrix = rotationMatrix * vertex.ToSingleRowMatrix(); return new Vertex((float)outputMatrix[0, 0], (float)outputMatrix[1, 0], (float)outputMatrix[2, 0]); }
public void ApplyVertex(Vertex vertex) { for (int i = 0; i < TransformedVertices.Length; i++) { TransformedVertices[i].Add(vertex); } }
public static Vertex RotateZ(Vertex vertex, double angle) { Vertex result = new Vertex(vertex); if (angle != 0) { var sin = Math.Sin(angle); var cos = Math.Cos(angle); result.X = (float)(vertex.X * cos - vertex.Y * sin); result.Y = (float)(vertex.Y * cos + vertex.X * sin); } return result; }
public Box(float width, float height, float depth) { Vertices = new Vertex[] { new Vertex { X = -width/2, Y = height/2, Z = -depth/2 }, new Vertex { X = width/2, Y = height/2, Z = -depth/2 }, new Vertex { X = width/2, Y = -height/2, Z = -depth/2 }, new Vertex { X = -width/2, Y = -height/2, Z = -depth/2 }, new Vertex { X = -width/2, Y = height/2, Z = depth/2 }, new Vertex { X = width/2, Y = height/2, Z = depth/2 }, new Vertex { X = width/2, Y = -height/2, Z = depth/2 }, new Vertex { X = -width/2, Y = -height/2, Z = depth/2 }, }; Triangles = new Triangle[] { // Front new Triangle { A = 0, B = 1, C = 2, EdgeMask = EdgeMask.ABC }, new Triangle { A = 2, B = 3, C = 0, EdgeMask = EdgeMask.ABC }, // Back new Triangle { A = 5, B = 4, C = 7, EdgeMask = EdgeMask.ABC }, new Triangle { A = 7, B = 6, C = 5, EdgeMask = EdgeMask.ABC }, // Top new Triangle { A = 0, B = 4, C = 5, EdgeMask = EdgeMask.ABC }, new Triangle { A = 5, B = 1, C = 0, EdgeMask = EdgeMask.ABC }, // Right side new Triangle { A = 1, B = 5, C = 6, EdgeMask = EdgeMask.ABC }, new Triangle { A = 6, B = 2, C = 1, EdgeMask = EdgeMask.ABC }, // Bottom new Triangle { A = 3, B = 2, C = 6, EdgeMask = EdgeMask.ABC }, new Triangle { A = 6, B = 7, C = 3, EdgeMask = EdgeMask.ABC }, // Right new Triangle { A = 4, B = 0, C = 3, EdgeMask = EdgeMask.ABC }, new Triangle { A = 3, B = 7, C = 4, EdgeMask = EdgeMask.ABC }, }; }
public ShipModel(float width, float height, float depth) { Vertices = new Vertex[] { new Vertex { X = -width/2, Y = -height/2, Z = depth/2 }, new Vertex { X = 0, Y = height/2, Z = depth/2 }, new Vertex { X = width/2, Y = -height/2, Z = depth/2 }, new Vertex { X = 0, Y = -height/2, Z = -depth/2 }, }; Triangles = new Triangle[] { // Back new Triangle { A = 0, B = 1, C = 2, EdgeMask = EdgeMask.ABCA }, // Top right new Triangle { A = 0, B = 3, C = 1, EdgeMask = EdgeMask.ABCA }, // Top left new Triangle { A = 3, B = 2, C = 1, EdgeMask = EdgeMask.ABCA }, // Bottom new Triangle { A = 0, B = 2, C = 3, EdgeMask = EdgeMask.ABCA }, }; }
public void BeginTransformation() { if (TransformedVertices == null || TransformedVertices.Length != Vertices.Length) { TransformedVertices = new Vertex[Vertices.Length]; } for (int i = 0; i < Vertices.Length; i++) { TransformedVertices[i] = new Vertex { X = Vertices[i].X, Y = Vertices[i].Y, Z = Vertices[i].Z }; } }
public void Add(Vertex other) { X += other.X; Y += other.Y; Z += other.Z; }
public Vertex(Vertex copyFrom) { this.X = copyFrom.X; this.Y = copyFrom.Y; this.Z = copyFrom.Z; }
private void TransformObject(WorldObject obj, Vertex eyeVertice) { obj.BeginTransformation(); //obj.DumpVertices("Before"); // Rotate object around it's origin obj.Rotate(); //obj.DumpVertices("After"); // Apply the object's location to its transformed vertices obj.ApplyVertex(obj.Location); // Apply the eye's location obj.ApplyVertex(eyeVertice); // Apply the eye's rotation obj.Rotate(Eye.Rotation); obj.EndTransformation(); //obj.DumpVertices("End"); }