[Closed] sdk – vertex indexes and faces
working on a exporter.
it takes the following format:
vertex pos x y z
and then it has one line for each face,
that specifies the vertex index for that face.
I’m exporting the positions without problems, but I have trouble understanding the indexing, I tried all day yesterday, but I’m missing something.
Also I wonder how one does optimize i afterwards (shared vertices)…
anyways, my current code is this:
Getting vertex positions (this works fine):
for(int f = 0; f < numFaces; ++f)
{
Face* face = &mesh->faces[f];
float Vertpositions[3];
for(int v = 0; v < 3; ++v)
{
DWORD vi = face->v[v];
Point3 vPos = mesh->verts[vi];
Point3 vLocalPos = offsetTM.PointTransform(vPos);
//write out each vertex position insert to array
Vertpositions[0] = vLocalPos.x;
Vertpositions[1] = vLocalPos.y;
Vertpositions[2] = vLocalPos.z;
}
//write out positions
fprintf(file,"<p x=\"%f\" y=\"%f\" z=\"%f\"/>
",Vertpositions[0],Vertpositions[1],Vertpositions[2]);
}
The trouble starts when I need to go through each face, get the indexed vertexes for this face. I cannot get it to work like it should.
fprintf(file,"<set_material sval=\"defaultMat\"/>
");
DWORD vertIndex[3];
for (int f = 0; f < mesh->getNumFaces(); ++f)
{
Face* face = &mesh->faces[f];
for(int v = 0; v < 3; ++v)
{
vertIndex[v] = face->GetVertIndex(f *3 +v);
}
fprintf(file,"<f a=\"%i\" b=\"%i\" c=\"%i\"/>
",vertIndex[0],vertIndex[1],vertIndex[2]);
}
Any hints, tips and so on is greatly appreciated – I hate to be stuck at things like this.
The output i get with a simple cube is this:
<mesh vertices="8" faces="12" has_orco="false" has_uv="false" type="0">
<p x="0.500000" y="0.500000" z="0.000000"/>
<p x="-0.500000" y="-0.500000" z="-0.000000"/>
<p x="0.500000" y="0.500000" z="1.000000"/>
<p x="-0.500000" y="-0.500000" z="1.000000"/>
<p x="0.500000" y="-0.500000" z="1.000000"/>
<p x="-0.500000" y="-0.500000" z="-0.000000"/>
<p x="0.500000" y="0.500000" z="1.000000"/>
<p x="0.500000" y="-0.500000" z="-0.000000"/>
<p x="-0.500000" y="0.500000" z="1.000000"/>
<p x="0.500000" y="0.500000" z="0.000000"/>
<p x="-0.500000" y="-0.500000" z="1.000000"/>
<p x="-0.500000" y="0.500000" z="0.000000"/>
<set_material sval="defaultMat"/>
<f a="0" b="3" c="1"/>
<f a="0" b="3" c="3"/>
<f a="3" b="2" c="3"/>
<f a="3" b="3" c="3"/>
<f a="3" b="3" c="3"/>
<f a="3" b="3" c="3"/>
<f a="3" b="3" c="3"/>
<f a="3" b="3" c="3"/>
<f a="3" b="3" c="3"/>
<f a="3" b="3" c="3"/>
<f a="3" b="3" c="3"/>
<f a="3" b="3" c="3"/>
</mesh>
You can clearly see that the indexing is wrong, no matter what way I tried I never get it right.
a working example is something like this (the output i expect is similar to this):
<mesh vertices="8" faces="6" has_orco="true" has_uv="false" type="0">
<p x="1" y="1" z="0.0166583" ox="1" oy="1" oz="-1"/>
<p x="1" y="-1" z="0.0166583" ox="1" oy="-0.999999" oz="-1"/>
<p x="-1" y="-1" z="0.0166583" ox="-1" oy="-0.999999" oz="-1"/>
<p x="-1" y="1" z="0.0166583" ox="-0.999999" oy="1" oz="-1"/>
<p x="1" y="0.999999" z="2.01666" ox="1" oy="0.999999" oz="1"/>
<p x="0.999999" y="-1" z="2.01666" ox="0.999999" oy="-1" oz="1"/>
<p x="-1" y="-1" z="2.01666" ox="-1" oy="-0.999999" oz="1"/>
<p x="-1" y="1" z="2.01666" ox="-1" oy="1" oz="1"/>
<set_material sval="Material--9223372036841686116"/>
<f a="0" b="1" c="2"/>
<f a="2" b="3" c="0"/>
<f a="4" b="7" c="6"/>
<f a="6" b="5" c="4"/>
<f a="0" b="4" c="5"/>
<f a="5" b="1" c="0"/>
<f a="1" b="5" c="6"/>
<f a="6" b="2" c="1"/>
<f a="2" b="6" c="7"/>
<f a="7" b="3" c="2"/>
<f a="4" b="0" c="3"/>
<f a="3" b="7" c="4"/>
</mesh>
almost got it solved by doing the following:
I did the following:
DWORD vertIndex[3];
for (int f = 0; f < mesh->getNumFaces(); ++f)
{
Face* face = &mesh->faces[f];
for(int v = 0; v < 3; ++v)
{
vertIndex[v] = face ->getAllVerts()[v];
}
fprintf(file,"<f a=\"%i\" b=\"%i\" c=\"%i\"/>
",vertIndex[0],vertIndex[1],vertIndex[2]);
}
gives the following (exported cube):
as you can see the cube is not perfect…
outputted data:
<mesh vertices="8" faces="12" has_orco="false" has_uv="false" type="0">
<p x="0.500000" y="0.500000" z="0.000000"/>
<p x="-0.500000" y="-0.500000" z="-0.000000"/>
<p x="0.500000" y="0.500000" z="1.000000"/>
<p x="-0.500000" y="-0.500000" z="1.000000"/>
<p x="0.500000" y="-0.500000" z="1.000000"/>
<p x="-0.500000" y="-0.500000" z="-0.000000"/>
<p x="0.500000" y="0.500000" z="1.000000"/>
<p x="0.500000" y="-0.500000" z="-0.000000"/>
<p x="-0.500000" y="0.500000" z="1.000000"/>
<p x="0.500000" y="0.500000" z="0.000000"/>
<p x="-0.500000" y="-0.500000" z="1.000000"/>
<p x="-0.500000" y="0.500000" z="0.000000"/>
<set_material sval="defaultMat"/>
<f a="0" b="2" c="3"/>
<f a="3" b="1" c="0"/>
<f a="4" b="5" c="7"/>
<f a="7" b="6" c="4"/>
<f a="0" b="1" c="5"/>
<f a="5" b="4" c="0"/>
<f a="1" b="3" c="7"/>
<f a="7" b="5" c="1"/>
<f a="3" b="2" c="6"/>
<f a="6" b="7" c="3"/>
<f a="2" b="0" c="4"/>
<f a="4" b="6" c="2"/>
</mesh>
i don’t have sdk in front of me to check but as i remenber it has to be more simple:
for (int i = 0; i < mesh.numFaces; i++)
{
Face & f = mesh.faces[i];
fprintf(file,"<f a=\"%i\" b=\"%i\" c=\"%i\"/>
",f.v[0],f.v[1],f.v[2]);
}
hi denisT,
that gave me the exact same result
So now I’m suspecting that it’s my first loop that might be wrong…?
yes. it has to be the same… just shorter. i don’t see anything wrong in the first loop. but it seems like you are in the wrong coordinate system. as i see there is no vertices in the data with 0.5 z coordinate.
got it now!
i fixed my first loop:
for (int i = 0; i < mesh->getNumVerts(); i++)
{
Point3 vert = mesh->getVert(i);
fprintf(file,"<p x=\"%f\" y=\"%f\" z=\"%f\"/>
",vert.x ,vert.y, vert.z);
}
it now works
I’m not sure, (I’m just exporting 1 singe object at this time).
either way if so then I’ll just multiply by object’s transform etc.
you should check out the IGame wrapper in the max sdk, it’s much simpler to use especially for mesh exporters.
I’m running into a new problem with the faceUV.
what I want to do is the following:
Get the UVface from the mesh, get the uv pos, and the uv vertex index.
Currently I’ve got this code:
for (int i = 0; i < mesh->numFaces; i++)
{
for(int v = 0; v < 3; v++)
{
TVFace* tvface = &mesh->tvFace[i];
Point3 uv = mesh->getTVert(tvface->getTVert(v));
VertArrPos[v] = uv[v];
DWORD tVertIndex = tvface->GetVertIndex(v);// .v[v];
tVertIndexArray[v] = tVertIndex;
//write out uv positions.
fprintf(file,"<uv u=\"%f\" v=\"%f\"/>
",uv.x,uv.y);
}
fprintf(file,"<f a=\"%i\" b=\"%i\" c=\"%i\" uv_a=\"%i\" uv_b=\"%i\" uv_c=\"%i\"/>
",f.v[0],f.v[1],f.v[2], tVertIndexArray[0],tVertIndexArray[1],tVertIndexArray[2]);
}
It’s not working as expected, do anyone of you have some hints on things I can try?
thanks in a advance.