public void BufferAddShadowHull(ShadowHull hull) { // Why do we need all of these again? (hint: we don't) var vertexMatrix = Matrix.Identity; var normalMatrix = Matrix.Identity; // Create the matrices (3X speed boost versus prior version) var cos = (float)Math.Cos(hull.Angle); var sin = (float)Math.Sin(hull.Angle); // vertexMatrix = scale * rotation * translation; vertexMatrix.M11 = hull.Scale.X * cos; vertexMatrix.M12 = hull.Scale.X * sin; vertexMatrix.M21 = hull.Scale.Y * -sin; vertexMatrix.M22 = hull.Scale.Y * cos; vertexMatrix.M41 = hull.Position.X; vertexMatrix.M42 = hull.Position.Y; // normalMatrix = scaleInv * rotation; normalMatrix.M11 = 1f / hull.Scale.X * cos; normalMatrix.M12 = 1f / hull.Scale.X * sin; normalMatrix.M21 = 1f / hull.Scale.Y * -sin; normalMatrix.M22 = 1f / hull.Scale.Y * cos; // Where are we in the buffer? var vertexCount = ShadowHullVertices.Count; // Add the vertices to the buffer for (var i = 0; i < hull.NumPoints; i++) { // Transform the vertices to screen coordinates var point = hull.Points[i]; ShadowHullVertex hullVertex; Vector2.Transform( ref point.Position, ref vertexMatrix, out hullVertex.Position); Vector2.TransformNormal( ref point.Normal, ref normalMatrix, out hullVertex.Normal); hullVertex.Color = new Color( r: 0, g: 0, b: 0, a: 1 - hull.Opacity); ShadowHullVertices.Add(hullVertex); } foreach (var index in hull.Indicies) { ShadowHullIndicies.Add(vertexCount + index); } }
public void BufferAddBoundOutline(BoundingRect boundingRect) { var vertexCount = ShadowHullVertices.Count; ShadowHullVertices.Add( new ShadowHullVertex { Color = Color.Black, Normal = Vector2.Zero, Position = new Vector2( boundingRect.Left, boundingRect.Top) }); ShadowHullVertices.Add( new ShadowHullVertex { Color = Color.Black, Normal = Vector2.Zero, Position = new Vector2( boundingRect.Right, boundingRect.Top) }); ShadowHullVertices.Add( new ShadowHullVertex { Color = Color.Black, Normal = Vector2.Zero, Position = new Vector2( boundingRect.Right, boundingRect.Bottom) }); ShadowHullVertices.Add( new ShadowHullVertex { Color = Color.Black, Normal = Vector2.Zero, Position = new Vector2( boundingRect.Left, boundingRect.Bottom) }); ShadowHullIndicies.Add(vertexCount + 0); ShadowHullIndicies.Add(vertexCount + 1); ShadowHullIndicies.Add(vertexCount + 2); ShadowHullIndicies.Add(vertexCount + 0); ShadowHullIndicies.Add(vertexCount + 2); ShadowHullIndicies.Add(vertexCount + 3); }
public void BufferDraw() { if (ShadowHullIndicies.Count >= 3) { GraphicsDevice.DrawUserIndexedPrimitives( primitiveType: PrimitiveType.TriangleList, vertexData: ShadowHullVertices.ToArray(), vertexOffset: 0, numVertices: ShadowHullVertices.Count, indexData: ShadowHullIndicies.ToArray(), indexOffset: 0, primitiveCount: ShadowHullIndicies.Count / 3); } }