public static Mesh Compute(RectangleD rectangle, int numberOfPartitionsX, int numberOfPartitionsY) { if (numberOfPartitionsX < 0) { throw new ArgumentOutOfRangeException("numberOfPartitionsX"); } if (numberOfPartitionsY < 0) { throw new ArgumentOutOfRangeException("numberOfPartitionsY"); } Mesh mesh = new Mesh(); mesh.PrimitiveType = PrimitiveType.Triangles; mesh.FrontFaceWindingOrder = WindingOrder.Counterclockwise; int numberOfPositions = (numberOfPartitionsX + 1) * (numberOfPartitionsY + 1); VertexAttributeFloatVector2 positionsAttribute = new VertexAttributeFloatVector2("position", numberOfPositions); IList <Vector2F> positions = positionsAttribute.Values; mesh.Attributes.Add(positionsAttribute); int numberOfIndices = (numberOfPartitionsX * numberOfPartitionsY) * 6; IndicesUnsignedInt indices = new IndicesUnsignedInt(numberOfIndices); mesh.Indices = indices; // // Positions // Vector2D lowerLeft = rectangle.LowerLeft; Vector2D toUpperRight = rectangle.UpperRight - lowerLeft; for (int y = 0; y <= numberOfPartitionsY; ++y) { double deltaY = y / (double)numberOfPartitionsY; double currentY = lowerLeft.Y + (deltaY * toUpperRight.Y); for (int x = 0; x <= numberOfPartitionsX; ++x) { double deltaX = x / (double)numberOfPartitionsX; double currentX = lowerLeft.X + (deltaX * toUpperRight.X); positions.Add(new Vector2D(currentX, currentY).ToVector2F()); } } // // Indices // int rowDelta = numberOfPartitionsX + 1; int i = 0; for (int y = 0; y < numberOfPartitionsY; ++y) { for (int x = 0; x < numberOfPartitionsX; ++x) { indices.AddTriangle(new TriangleIndicesUnsignedInt(i, i + 1, rowDelta + (i + 1))); indices.AddTriangle(new TriangleIndicesUnsignedInt(i, rowDelta + (i + 1), rowDelta + i)); i += 1; } i += 1; } return(mesh); }
public void MeshVertexAttributes() { Mesh mesh = new Mesh(); VertexAttributeHalfFloat halfFloatAttribute = new VertexAttributeHalfFloat("halfFloatAttribute"); mesh.Attributes.Add(halfFloatAttribute); Assert.AreEqual("halfFloatAttribute", mesh.Attributes["halfFloatAttribute"].Name); Assert.AreEqual(VertexAttributeType.HalfFloat, mesh.Attributes["halfFloatAttribute"].Datatype); VertexAttributeHalfFloatVector2 halfFloatAttribute2 = new VertexAttributeHalfFloatVector2("halfFloatAttribute2"); mesh.Attributes.Add(halfFloatAttribute2); Assert.AreEqual("halfFloatAttribute2", mesh.Attributes["halfFloatAttribute2"].Name); Assert.AreEqual(VertexAttributeType.HalfFloatVector2, mesh.Attributes["halfFloatAttribute2"].Datatype); VertexAttributeHalfFloatVector3 halfFloatAttribute3 = new VertexAttributeHalfFloatVector3("halfFloatAttribute3"); mesh.Attributes.Add(halfFloatAttribute3); Assert.AreEqual("halfFloatAttribute3", mesh.Attributes["halfFloatAttribute3"].Name); Assert.AreEqual(VertexAttributeType.HalfFloatVector3, mesh.Attributes["halfFloatAttribute3"].Datatype); VertexAttributeHalfFloatVector4 halfFloatAttribute4 = new VertexAttributeHalfFloatVector4("halfFloatAttribute4"); mesh.Attributes.Add(halfFloatAttribute4); Assert.AreEqual("halfFloatAttribute4", mesh.Attributes["halfFloatAttribute4"].Name); Assert.AreEqual(VertexAttributeType.HalfFloatVector4, mesh.Attributes["halfFloatAttribute4"].Datatype); /////////////////////////////////////////////////////////////////// VertexAttributeFloat floatAttribute = new VertexAttributeFloat("floatAttribute"); mesh.Attributes.Add(floatAttribute); Assert.AreEqual("floatAttribute", mesh.Attributes["floatAttribute"].Name); Assert.AreEqual(VertexAttributeType.Float, mesh.Attributes["floatAttribute"].Datatype); VertexAttributeFloatVector2 floatAttribute2 = new VertexAttributeFloatVector2("floatAttribute2"); mesh.Attributes.Add(floatAttribute2); Assert.AreEqual("floatAttribute2", mesh.Attributes["floatAttribute2"].Name); Assert.AreEqual(VertexAttributeType.FloatVector2, mesh.Attributes["floatAttribute2"].Datatype); VertexAttributeFloatVector3 floatAttribute3 = new VertexAttributeFloatVector3("floatAttribute3"); mesh.Attributes.Add(floatAttribute3); Assert.AreEqual("floatAttribute3", mesh.Attributes["floatAttribute3"].Name); Assert.AreEqual(VertexAttributeType.FloatVector3, mesh.Attributes["floatAttribute3"].Datatype); VertexAttributeFloatVector4 floatAttribute4 = new VertexAttributeFloatVector4("floatAttribute4"); mesh.Attributes.Add(floatAttribute4); Assert.AreEqual("floatAttribute4", mesh.Attributes["floatAttribute4"].Name); Assert.AreEqual(VertexAttributeType.FloatVector4, mesh.Attributes["floatAttribute4"].Datatype); /////////////////////////////////////////////////////////////////// VertexAttributeByte byteAttribute = new VertexAttributeByte("byteAttribute"); mesh.Attributes.Add(byteAttribute); Assert.AreEqual("byteAttribute", mesh.Attributes["byteAttribute"].Name); Assert.AreEqual(VertexAttributeType.UnsignedByte, mesh.Attributes["byteAttribute"].Datatype); VertexAttributeRGBA colorAttribute = new VertexAttributeRGBA("colorAttribute"); mesh.Attributes.Add(colorAttribute); Assert.AreEqual("colorAttribute", mesh.Attributes["colorAttribute"].Name); Assert.AreEqual(VertexAttributeType.UnsignedByte, mesh.Attributes["colorAttribute"].Datatype); colorAttribute.AddColor(Color.FromArgb(3, 0, 1, 2)); Assert.AreEqual(0, colorAttribute.Values[0]); Assert.AreEqual(1, colorAttribute.Values[1]); Assert.AreEqual(2, colorAttribute.Values[2]); Assert.AreEqual(3, colorAttribute.Values[3]); }