Creating a polygon shape

A polygon shape is a plane geometry built from a set of contours. The RED::IMeshShape::Polygon functions lets the user create such a mesh by providing the list of contour. Each contour that has a CCW orientation is triangulated inside. Each contour with a CW orientation defines an outer region, i.e holes in the polygon.

Contours list containing vertex coordinates.

RED::Vector< RED::Vector< double > > contours;  // [[v1_x, v1_y, v1_z, v2_x, v2_y, v2_z, ...][...]...]
// ... Fill this array ...
// contours contains the list of vertices for each contour (grouped by 3 coordinates X, Y and Z).

RED::Object* mesh = RED::Factory::CreateInstance( CID_REDMeshShape );
RED::IMeshShape* imesh = mesh->As< RED::IMeshShape >();

// mapping will retrieve the vertex mapping needed later.
RED::Vector< unsigned int > mapping;
RC_TEST( imesh->Polygon( mapping, contours, iresmgr->GetState() ) );

The RED::IMeshShape::Polygon function only fills the RED::MCL_VERTEX position channel. However, the function returns a mapping array that allows to associate each newly created vertex to its original contour point. If multiple data are associated to the contour points, like texture coordinates, this allows to set them in the mesh.

Vertex mapping array associates the vertex to its contour and point in the contour.

RED::Vector< RED::Vector< double > > texcoords; // [[t1_u, t1_v, t2_u, t2_v, ...][...]...]
// ... Fill this array ...
// texcoords contains the list of texcoords for each contour (grouped by 2 coordinates U and V).

int count;
RC_TEST( imesh->GetVerticesCount( count ) );

// Creates a mesh array to store texture coordinates and retrieves this array.
double* texArray;
RC_TEST( imesh->SetArray( RED::MCL_TEX0, NULL, count, 2, RED::MFT_DOUBLE, iresmgr->GetState() ) );
RC_TEST( imesh->GetArray( (void*&)texArray, RED::MCL_TEX0, iresmgr->GetState() ) );

// For each newly created vertex.
for( int i = 0; i < count; ++i )
{
  // 2 mapping coordinates per vertex.
  unsigned int contourIndex = mapping[ i * 2     ]; // contour
  unsigned int pointIndex   = mapping[ i * 2 + 1 ]; // point

  // Fill the mesh texture coordinates using the returned mapping array.
  // 2 texture coordinates per vertex.
  texArray[ i * 2     ] = texcoords[ contourIndex ][ pointIndex * 2     ]; // tex U
  texArray[ i * 2 + 1 ] = texcoords[ contourIndex ][ pointIndex * 2 + 1 ]; // tex V
}