# C++ [MAYA API] Getting all triangles from a polygon

## Recommended Posts

Posted (edited)

Just realized maybe this doesn't fit in this sub-forum but oh well

I'm making an exporter plugin for Maya and I want to export a non-triangulated mesh, while still outputting triangle data, not quad/n-gon data.

Using MItMeshPolygon, I am doing the following:

for (; !polyIter.isDone(); polyIter.next())
{
//Get points and normals from current polygon
MPointArray vts;
polyIter.getPoints(vts);
MVectorArray nmls;
polyIter.getNormals(nmls);

//Get number of triangles in current polygon
int numberOfTriangles;
polyIter.numTriangles(numberOfTriangles);

//Loop through all triangles
for (int i = 0; i < numberOfTriangles; i++)
{
//Get points and vertexList for this triangle.
//vertexList is used to index into the polygon verts and normals.
MPointArray points   = {};
MIntArray vertexList = {};
polyIter.getTriangle(i, points, vertexList, MSpace::kObject);

//For each vertex in this triangle
for (int v = 0; v < 3; v++)
{
//Get point and normal
UINT vi = polyIter.vertexIndex(vertexList[v]);
UINT ni = polyIter.normalIndex(vertexList[v]);

MPoint _v = vts[vi];
MFloatVector _n = nmls[ni];

//Create vertex
Vertex_pos3nor3uv2 vert = {};

vert.posX = _v.x;
vert.posY = _v.y;
vert.posZ = _v.z * -1.0;

vert.norX = _n.x;
vert.norY = _n.y;
vert.norZ = _n.z * -1.0;

vert.u = 0.0;
very.v = 0.0;

verts.push_back(vert);
}
}
}

Doing this only gives me half the triangles I'm supposed to get and the result is very distorted.
Link above is a picture of a cube exported this way.

Edit: I've also tried indexing into the entire mesh vertex array like this:

MPointArray vts;
meshFn.getPoints(vts);
MFloatVectorArray nmls;
meshFn.getNormals(nmls);

//....

UINT vi = polyIter.vertexIndex(vertexList[v]);
UINT ni = polyIter.normalIndex(vertexList[v]);

MPoint       _v = vts[vi];
MFloatVector _n = nmls[vi];

I can't figure out what's wrong with my code.

Any ideas?

Edited by GreenGodDiary
Fixed code

##### Share on other sites

you could treat every face that has more than 3 verts as triangle fan polygon so you could put a vertex inside that polyand split it into triangles?

##### Share on other sites
Posted (edited)
35 minutes ago, WiredCat said:

you could treat every face that has more than 3 verts as triangle fan polygon so you could put a vertex inside that polyand split it into triangles?

While I suppose that would work I, would like to understand how to get it the 'normal' way, i.e. get each triangle through getTriangle()

EDIT:

Doing what @WiredCat said I got it to work, so if no one knows why my original solution didn't work it's not the end of the world.

New code:

for (int i = 0; i < numberOfTriangles; i++)
{
//vN = 0, 1+i, 2+i

//Get positions
Vertex_pos3nor3uv2 v1, v2, v3 = {};
MPoint p1 = vts[polyIter.vertexIndex(0)];
MPoint p2 = vts[polyIter.vertexIndex(1+i)];
MPoint p3 = vts[polyIter.vertexIndex(2+i)];

//Get normals
MVector n1 = nmls[polyIter.normalIndex(0)];
MVector n2 = nmls[polyIter.normalIndex(1 + i)];
MVector n3 = nmls[polyIter.normalIndex(1 + i)];

//First vertex
v1.posX = p1.x;
v1.posY = p1.y;
v1.posZ = p1.z;

v1.norX = n1.x;
v1.norY = n1.y;
v1.norZ = n1.z;

//Second vertex
v2.posX = p2.x;
v2.posY = p2.y;
v2.posZ = p2.z;

v2.norX = n2.x;
v2.norY = n2.y;
v2.norZ = n2.z;

//Third vertex
v3.posX = p3.x;
v3.posY = p3.y;
v3.posZ = p3.z;

v3.norX = n3.x;
v3.norY = n3.y;
v3.norZ = n3.z;

//Changing order of verts for DirectX
verts.push_back(v3);
verts.push_back(v2);
verts.push_back(v1);
}

Edited by GreenGodDiary

## Create an account

Register a new account

1. 1
2. 2
3. 3
Rutin
18
4. 4
JoeJ
14
5. 5

• 14
• 10
• 23
• 9
• 32
• ### Forum Statistics

• Total Topics
632632
• Total Posts
3007524
• ### Who's Online (See full list)

There are no registered users currently online

×