Home > Code > C# > How to Create a 3D Cube Mesh inside .NET Application

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

by sher azam   on Dec 09, 2015   Category: C#   |  Views: 617    |  Points: 25   |  Gold 


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:

• Define the Control Points
• Create Polygons with PolygonBuilder Class
• Create Polygons

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.

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



Overview: Aspose.3D for .NET

Aspose.3D for .NET is a feature-rich component and class library for .NET that empowers Mono and .NET application including ASP.NET, Windows Forms and Web Services to connect with prevalent 3D document formats automatically without the 3D modeling and rendering software being installed on the server. It supports FBX (ASCII, Binary) and STL (ASCII, Binary) file formats and developers can easily create, read, convert, modify and control the substance of these 3D document formats using Aspose.3D API.

- Homepage of Aspose.3D for .NET: http://www.aspose.com/.net/3d-component.aspx

- Download Aspose.3D for .NET: http://www.aspose.com/community/files/51/.net-components/aspose.3d-for-.net/default.aspx



Post Code  |  Code Snippet Home

User Responses


No response found, be the first to review this code snippet.

Submit feedback about this code snippet

Please sign in to post feedback

Latest Posts