How to Create a 3D Cube Mesh inside .NET Application

This Technical tip explains how .NET developers can create a 3D cube mesh inside their .NET applications. A Mesh is defined by a set of control points and the many n-sided polygons as needed. This article explains how to define a Mesh. In order to create a Mesh surface, we need to define control points and polygons as follows: <br/> <br/> • Define the Control Points <br/> • Create Polygons with PolygonBuilder Class <br/> • Create Polygons <br/> <br/> A mesh is composed by a set of control points in space, and polygons to describe the mesh surface, to create a mesh; we need to define the control points. The control points of all geometries in Aspose.3D use homogeneous coordinate, so it’s Vector4 instead of Vector3 in the example code.
//your code here... //Define the Control Points // [C# Code Sample] // For complete examples and data files, please go to https://github.com/aspose3d/Aspose_3D_NET // Initialize control points Vector4[] controlPoints = new Vector4[] { new Vector4( -5.0, 0.0, 5.0, 1.0), new Vector4( 5.0, 0.0, 5.0, 1.0), new Vector4( 5.0, 10.0, 5.0, 1.0), new Vector4( -5.0, 10.0, 5.0, 1.0), new Vector4( -5.0, 0.0, -5.0, 1.0), new Vector4( 5.0, 0.0, -5.0, 1.0), new Vector4( 5.0, 10.0, -5.0, 1.0), new Vector4( -5.0, 10.0, -5.0, 1.0) }; //[VB.NET Code Sample] ' For complete examples and data files, please go to https://github.com/aspose3d/Aspose_3D_NET ' Initialize control points Dim controlPoints As Vector4() = New Vector4() {New Vector4(-5.0, 0.0, 5.0, 1.0), New Vector4(5.0, 0.0, 5.0, 1.0), New Vector4(5.0, 10.0, 5.0, 1.0), New Vector4(-5.0, 10.0, 5.0, 1.0), New Vector4(-5.0, 0.0, -5.0, 1.0), New Vector4(5.0, 0.0, -5.0, 1.0), _ New Vector4(5.0, 10.0, -5.0, 1.0), New Vector4(-5.0, 10.0, -5.0, 1.0)} //Create Polygons The control points are not renderable, to make the cube visible, we need to define polygons in each side: // [C# Code Sample] // For complete examples and data files, please go to https://github.com/aspose3d/Aspose_3D_NET Vector4[] controlPoints = DefineControlPoints(); // Initialize mesh object Mesh mesh = new Mesh(); // Add control points to the mesh mesh.ControlPoints.AddRange(controlPoints); // create polygons to mesh // front face (Z+) mesh.CreatePolygon(new int[] { 0, 1, 2, 3 }); // right side (X+) mesh.CreatePolygon(new int[] { 1, 5, 6, 2 }); // back face (Z-) mesh.CreatePolygon(new int[] { 5, 4, 7, 6 }); // left side (X-) mesh.CreatePolygon(new int[] { 4, 0, 3, 7 }); // bottom face (Y-) mesh.CreatePolygon(new int[] { 0, 4, 5, 1 }); // top face (Y+) mesh.CreatePolygon(new int[] { 3, 2, 6, 7 }); //[VB.NET Code Sample] ' For complete examples and data files, please go to https://github.com/aspose3d/Aspose_3D_NET Dim controlPoints As Vector4() = DefineControlPoints() ' Initialize mesh object Dim mesh As New Mesh() ' Add control points to the mesh mesh.ControlPoints.AddRange(controlPoints) ' create polygons to mesh ' front face (Z+) mesh.CreatePolygon(New Integer() {0, 1, 2, 3}) ' right side (X+) mesh.CreatePolygon(New Integer() {1, 5, 6, 2}) ' back face (Z-) mesh.CreatePolygon(New Integer() {5, 4, 7, 6}) ' left side (X-) mesh.CreatePolygon(New Integer() {4, 0, 3, 7}) ' bottom face (Y-) mesh.CreatePolygon(New Integer() {0, 4, 5, 1}) ' top face (Y+) mesh.CreatePolygon(New Integer() {3, 2, 6, 7}) //Create Polygons with PolygonBuilder Class //We can also define polygon by vertices with PolygonBuilder class: //[C# Code Sample] // For complete examples and data files, please go to https://github.com/aspose3d/Aspose_3D_NET Vector4[] controlPoints = DefineControlPoints(); // Initialize mesh object Mesh mesh = new Mesh(); // Add control points to the mesh mesh.ControlPoints.AddRange(controlPoints); // Indices of the vertices per each polygon int[] indices = new int[] { 0,1,2,3, // front face (Z+) 1,5,6,2, // right side (X+) 5,4,7,6, // back face (Z-) 4,0,3,7, // left side (X-) 0,4,5,1, // bottom face (Y-) 3,2,6,7 // top face (Y+) }; int vertexId = 0; PolygonBuilder builder = new PolygonBuilder(mesh); for (int face = 0; face < 6; face++) { // Start defining a new polygon builder.Begin(); for (int v = 0; v < 4; v++) // The indice of vertice per each polygon builder.AddVertex(indices[vertexId++]); // Finished one polygon builder.End(); } //[VB.NET Code Sample] ' For complete examples and data files, please go to https://github.com/aspose3d/Aspose_3D_NET Dim controlPoints As Vector4() = DefineControlPoints() ' Initialize mesh object Dim mesh As New Mesh() ' Add control points to the mesh mesh.ControlPoints.AddRange(controlPoints) ' Indices of the vertices per each polygon ' 0, 1, 2, 3 -> front face (Z+) ' 1, 5, 6, 2 -> right side (X+) ' 5, 4, 7, 6 -> back face (Z-) ' 4, 0, 3, 7 -> left side (X-) ' 0, 4, 5, 1 -> bottom face (Y-) ' 3, 2, 6, 7 -> top face (Y+) Dim indices = New Integer() {0, 1, 2, 3, 1, 5, 6, 2, 5, 4, 7, 6, 4, 0, 3, 7, 0, 4, 5, 1, 3, 2, 6, 7} Dim vertexId As Integer = 0 Dim builder As New PolygonBuilder(mesh) For face As Integer = 0 To 5 ' Start defining a new polygon builder.Begin() For v As Integer = 0 To 3 ' The indice of vertice per each polygon builder.AddVertex(indices(vertexId)) vertexId = vertexId + 1 Next ' Finished one polygon builder.End() Next

Url: http://www.aspose.com/.net/3d-component.aspx

Language: C# | User: Sheraz Khan | Created: Dec 9, 2015 | Tags: Create 3D Cube Mesh, Define Control Points C#, Create Polygons in .NET, Create Polygons with Polygon Builder, .NET 3D Component, work with 3D File Formats, STL 3D File Formats Support Create 3D Cube Mesh Define Control Points C# Create Polygons in .NET Create Polygons with Polygon Builder .NET 3D Component work with 3D File Formats STL 3D File Formats Support