From Marc Helbling, Clément Léger, Aurélien Chatelain, Paul Cheyrou-Lagrèze :

"a last submission for the obj plugin
* supports vertex color definition after vertex position by Clément Léger
* supports zbrush vertex color definition (as #MRGB comment) by Aurélien Chatelain
* adds a noReverseFace option to not mess with face definition by Aurélien Chatelain
* makes material parsing case insensitive (by Paul Cheyrou-Lagrèze and me)
* makes the plugin resilient to faulty vertex uv/normal definition (i.e. when a too big index is referenced) by Aurélien hatelain
"


git-svn-id: http://svn.openscenegraph.org/osg/OpenSceneGraph/trunk@14641 16af8721-9629-0410-8352-f15c8da7e697
This commit is contained in:
Robert Osfield
2014-12-24 11:25:46 +00:00
parent 50203023c7
commit 78729e28fd
3 changed files with 135 additions and 34 deletions

View File

@@ -61,6 +61,7 @@ public:
supportsOption("noTesselateLargePolygons","Do not do the default tesselation of large polygons");
supportsOption("noTriStripPolygons","Do not do the default tri stripping of polygons");
supportsOption("generateFacetNormals","generate facet normals for verticies without normals");
supportsOption("noReverseFaces","avoid to reverse faces when normals and triangles orientation are reversed");
supportsOption("DIFFUSE=<unit>", "Set texture unit for diffuse texture");
supportsOption("AMBIENT=<unit>", "Set texture unit for ambient texture");
@@ -138,6 +139,7 @@ protected:
bool noTriStripPolygons;
bool generateFacetNormals;
bool fixBlackMaterials;
bool noReverseFaces;
// This is the order in which the materials will be assigned to texture maps, unless
// otherwise overriden
typedef std::vector< std::pair<int,obj::Material::Map::TextureMapType> > TextureAllocationMap;
@@ -486,10 +488,13 @@ osg::Geometry* ReaderWriterOBJ::convertElementListToGeometry(obj::Model& model,
osg::Vec3Array* vertices = numVertexIndices ? new osg::Vec3Array : 0;
osg::Vec3Array* normals = numNormalIndices ? new osg::Vec3Array : 0;
osg::Vec2Array* texcoords = numTexCoordIndices ? new osg::Vec2Array : 0;
osg::Vec4Array* colors = (!model.colors.empty()) ? new osg::Vec4Array : 0;
if (vertices) vertices->reserve(numVertexIndices);
if (normals) normals->reserve(numNormalIndices);
if (texcoords) texcoords->reserve(numTexCoordIndices);
if (colors) colors->reserve(numVertexIndices);
osg::Geometry* geometry = new osg::Geometry;
if (vertices) geometry->setVertexArray(vertices);
@@ -497,11 +502,17 @@ osg::Geometry* ReaderWriterOBJ::convertElementListToGeometry(obj::Model& model,
{
geometry->setNormalArray(normals, osg::Array::BIND_PER_VERTEX);
}
if (texcoords)
{
geometry->setTexCoordArray(0,texcoords);
}
if (colors)
{
geometry->setColorArray(colors);
geometry->setColorBinding(osg::Geometry::BIND_PER_VERTEX);
}
if (numPointElements>0)
{
@@ -518,6 +529,11 @@ osg::Geometry* ReaderWriterOBJ::convertElementListToGeometry(obj::Model& model,
index_itr != element.vertexIndices.end();
++index_itr)
{
// if use color extension ( not standard but used by meshlab)
if (colors)
{
colors->push_back(model.colors[*index_itr]);
}
vertices->push_back(transformVertex(model.vertices[*index_itr],localOptions.rotate));
++numPoints;
}
@@ -564,6 +580,12 @@ osg::Geometry* ReaderWriterOBJ::convertElementListToGeometry(obj::Model& model,
index_itr != element.vertexIndices.end();
++index_itr)
{
// if use color extension ( not standard but used by meshlab)
if (colors)
{
colors->push_back(model.colors[*index_itr]);
}
vertices->push_back(transformVertex(model.vertices[*index_itr],localOptions.rotate));
}
if (numNormalIndices)
@@ -592,7 +614,7 @@ osg::Geometry* ReaderWriterOBJ::convertElementListToGeometry(obj::Model& model,
}
// #define USE_DRAWARRAYLENGTHS
bool hasReversedFaces = false ;
if (numPolygonElements>0)
{
unsigned int startPos = vertices->size();
@@ -633,13 +655,20 @@ osg::Geometry* ReaderWriterOBJ::convertElementListToGeometry(obj::Model& model,
#endif
if (model.needReverse(element))
if (model.needReverse(element) && !localOptions.noReverseFaces)
{
hasReversedFaces = true;
// need to reverse so add to OSG arrays in same order as in OBJ, as OSG assume anticlockwise ordering.
for(obj::Element::IndexList::reverse_iterator index_itr = element.vertexIndices.rbegin();
index_itr != element.vertexIndices.rend();
++index_itr)
{
// if use color extension ( not standard but used by meshlab)
if (colors)
{
colors->push_back(model.colors[*index_itr]);
}
vertices->push_back(transformVertex(model.vertices[*index_itr],localOptions.rotate));
}
if (numNormalIndices)
@@ -670,6 +699,12 @@ osg::Geometry* ReaderWriterOBJ::convertElementListToGeometry(obj::Model& model,
index_itr != element.vertexIndices.end();
++index_itr)
{
// if use color extension ( not standard but used by meshlab)
if (colors)
{
colors->push_back(model.colors[*index_itr]);
}
vertices->push_back(transformVertex(model.vertices[*index_itr],localOptions.rotate));
}
if (numNormalIndices)
@@ -693,8 +728,11 @@ osg::Geometry* ReaderWriterOBJ::convertElementListToGeometry(obj::Model& model,
}
}
}
}
if(hasReversedFaces)
{
OSG_WARN << "Warning: [ReaderWriterOBJ::convertElementListToGeometry] Some faces from geometry '" << geometry->getName() << "' were reversed by the plugin" << std::endl;
}
return geometry;
@@ -787,6 +825,7 @@ ReaderWriterOBJ::ObjOptionsStruct ReaderWriterOBJ::parseOptions(const osgDB::Rea
localOptions.noTriStripPolygons = false;
localOptions.generateFacetNormals = false;
localOptions.fixBlackMaterials = true;
localOptions.noReverseFaces = false;
if (options!=NULL)
{
@@ -825,6 +864,10 @@ ReaderWriterOBJ::ObjOptionsStruct ReaderWriterOBJ::parseOptions(const osgDB::Rea
{
localOptions.generateFacetNormals = true;
}
else if (pre_equals == "noReverseFaces")
{
localOptions.noReverseFaces = true;
}
else if (post_equals.length()>0)
{
obj::Material::Map::TextureMapType type = obj::Material::Map::UNKNOWN;