Using layer 0 to store a normal to each face
 
 
 

In this section of code, we will assign the normals that we created earlier in a layer element for normals, and we will assign that layer element to Layer 0 of the mesh.

We start by creating layer 0 if it does not already exist:

// Set the normals on Layer 0.
KFbxLayer* lLayer = lMesh->GetLayer(0);
if (lLayer == NULL)
{
    lMesh->CreateLayer();
    lLayer = lMesh->GetLayer(0);
}

Here we assign normals to each vertex of each polygon in our mesh. A normal is a vector that defines which way a face or vertex is pointing. The direction of the normal indicates the front, or outer surface of the face or vertex. Normals are (usually) perpendicular to a polygon.

Since our mesh is a cube:

A layer element is composed of two arrays, called DirectArray and IndexArray:

We start by creating the layer element for the normals.

// We want to have one normal for each control point,
// so we set the mapping mode to eBY_CONTROL_POINT.
KFbxLayerElementNormal* lLayerElementNormal= KFbxLayerElementNormal::Create(lMesh, "");
 
lLayerElementNormal->SetMappingMode(KFbxLayerElement::eBY_CONTROL_POINT);
 
// Set the normal values for every control point.
lLayerElementNormal->SetReferenceMode(KFbxLayerElement::eDIRECT);

The normals for each face of the cube will face the same direction. Accordingly, the first four vertices, which refer to the first face, are assigned the same normal (lNormalZpos); the second four vectors, which refer to the second face, are assigned the same normal (lNormalXpos); etc.

GetDirectArray() returns a pointer to a DirectArray for normals. Add() appends a vector to the end of the array.

lLayerElementNormal->GetDirectArray().Add(lNormalZPos);
lLayerElementNormal->GetDirectArray().Add(lNormalZPos);
lLayerElementNormal->GetDirectArray().Add(lNormalZPos);
lLayerElementNormal->GetDirectArray().Add(lNormalZPos);
 
lLayerElementNormal->GetDirectArray().Add(lNormalXPos);
lLayerElementNormal->GetDirectArray().Add(lNormalXPos);
lLayerElementNormal->GetDirectArray().Add(lNormalXPos);
lLayerElementNormal->GetDirectArray().Add(lNormalXPos);
 
lLayerElementNormal->GetDirectArray().Add(lNormalZNeg);
lLayerElementNormal->GetDirectArray().Add(lNormalZNeg);
lLayerElementNormal->GetDirectArray().Add(lNormalZNeg);
lLayerElementNormal->GetDirectArray().Add(lNormalZNeg);
 
lLayerElementNormal->GetDirectArray().Add(lNormalXNeg);
lLayerElementNormal->GetDirectArray().Add(lNormalXNeg);
lLayerElementNormal->GetDirectArray().Add(lNormalXNeg);
lLayerElementNormal->GetDirectArray().Add(lNormalXNeg);
 
lLayerElementNormal->GetDirectArray().Add(lNormalYPos);
lLayerElementNormal->GetDirectArray().Add(lNormalYPos);
lLayerElementNormal->GetDirectArray().Add(lNormalYPos);
lLayerElementNormal->GetDirectArray().Add(lNormalYPos);
 
lLayerElementNormal->GetDirectArray().Add(lNormalYNeg);
lLayerElementNormal->GetDirectArray().Add(lNormalYNeg);
lLayerElementNormal->GetDirectArray().Add(lNormalYNeg);
lLayerElementNormal->GetDirectArray().Add(lNormalYNeg);

We’ve finished assigning normals to the DirectArray of normals. Now we assign the DirectArray to the layer (layer 0):

lLayer->SetNormals(lLayerElementNormal);

This completes the construction of the mesh for a cube.

See Also