Adding Normals in OBJ exports

This commit is contained in:
Jean-Samuel Reynaud 2017-02-08 17:09:26 +01:00 committed by wmayer
parent cc46acf10e
commit b77188e35c

View File

@ -1955,6 +1955,21 @@ bool MeshOutput::SaveOBJ (std::ostream &out) const
}
seq.next(true); // allow to cancel
}
// Export normals
MeshFacetIterator clIter(_rclMesh), clEnd(_rclMesh);
const MeshGeomFacet* pclFacet;
clIter.Begin();
clEnd.End();
while (clIter < clEnd) {
pclFacet = &(*clIter);
out << "vn " << pclFacet->GetNormal().x << " "
<< pclFacet->GetNormal().y << " "
<< pclFacet->GetNormal().z << std::endl;
++clIter;
seq.next(true); // allow to cancel
}
if (_groups.empty()) {
if (exportColorPerFace) {
@ -1967,6 +1982,7 @@ bool MeshOutput::SaveOBJ (std::ostream &out) const
std::size_t index = 0;
App::Color prev;
int faceIdx = 1;
const std::vector<App::Color>& Kd = _material->diffuseColor;
for (MeshFacetArray::_TConstIterator it = rFacets.begin(); it != rFacets.end(); ++it, index++) {
if (index == 0 || prev != Kd[index]) {
@ -1976,25 +1992,27 @@ bool MeshOutput::SaveOBJ (std::ostream &out) const
out << "usemtl material_" << (c_it - colors.begin()) << std::endl;
}
}
out << "f " << it->_aulPoints[0]+1 << " "
<< it->_aulPoints[1]+1 << " "
<< it->_aulPoints[2]+1 << std::endl;
out << "f " << it->_aulPoints[0]+1 << "//" << faceIdx << " "
<< it->_aulPoints[1]+1 << "//" << faceIdx << " "
<< it->_aulPoints[2]+1 << "//" << faceIdx << std::endl;
seq.next(true); // allow to cancel
faceIdx++;
}
}
else {
// facet indices (no texture and normal indices)
std::size_t faceIdx = 1;
for (MeshFacetArray::_TConstIterator it = rFacets.begin(); it != rFacets.end(); ++it) {
out << "f " << it->_aulPoints[0]+1 << " "
<< it->_aulPoints[1]+1 << " "
<< it->_aulPoints[2]+1 << std::endl;
out << "f " << it->_aulPoints[0]+1 << "//" << faceIdx << " "
<< it->_aulPoints[1]+1 << "//" << faceIdx << " "
<< it->_aulPoints[2]+1 << "//" << faceIdx << std::endl;
seq.next(true); // allow to cancel
faceIdx++;
}
}
}
else {
if (exportColorPerFace) {
// make sure to use the 'usemtl' statement as less often as possible
std::vector<App::Color> colors = _material->diffuseColor;
std::sort(colors.begin(), colors.end(), Color_Less());
@ -2017,9 +2035,9 @@ bool MeshOutput::SaveOBJ (std::ostream &out) const
}
}
out << "f " << f._aulPoints[0]+1 << " "
<< f._aulPoints[1]+1 << " "
<< f._aulPoints[2]+1 << std::endl;
out << "f " << f._aulPoints[0]+1 << "//" << *it + 1 << " "
<< f._aulPoints[1]+1 << "//" << *it + 1 << " "
<< f._aulPoints[2]+1 << "//" << *it + 1 << std::endl;
seq.next(true); // allow to cancel
}
}
@ -2029,9 +2047,9 @@ bool MeshOutput::SaveOBJ (std::ostream &out) const
out << "g " << Base::Tools::escapedUnicodeFromUtf8(gt->name.c_str()) << std::endl;
for (std::vector<unsigned long>::const_iterator it = gt->indices.begin(); it != gt->indices.end(); ++it) {
const MeshFacet& f = rFacets[*it];
out << "f " << f._aulPoints[0]+1 << " "
<< f._aulPoints[1]+1 << " "
<< f._aulPoints[2]+1 << std::endl;
out << "f " << f._aulPoints[0]+1 << "//" << *it + 1 << " "
<< f._aulPoints[1]+1 << "//" << *it + 1 << " "
<< f._aulPoints[2]+1 << "//" << *it + 1 << std::endl;
seq.next(true); // allow to cancel
}
}