Commit 55e29142 authored by Peter Karasev's avatar Peter Karasev

fix importing to work for no mtl file + no warnings for empty texture image

parent bc011dbf
......@@ -62,8 +62,12 @@ int TestOBJImporter( int argc, char * argv [] )
std::string tmppath(argv[lastArg]);
vtkNew<vtkOBJImporter> importer;
if(argc > 8)
{
s_interactive = 1;
importer->DebugOn();
}
importer->SetFileName(filenameOBJ.data());
importer->SetFileNameMTL(filenameMTL.data());
......
......@@ -68,11 +68,15 @@ int CanReadFile( vtkObject* that, const std::string& fname )
int vtkOBJImporter::ImportBegin()
{
if (CanReadFile(this, this->GetFileName()) && CanReadFile(this, this->GetFileNameMTL()))
if (!CanReadFile(this, this->GetFileName()))
{
return 1;
return 0;
}
if( !std::string(GetFileNameMTL()).empty() && !CanReadFile(this,this->GetFileNameMTL()))
{
return 0;
}
return 0;
return 1;
}
//----------------------------------------------------------------------------
......@@ -315,6 +319,11 @@ int vtkOBJPolyDataProcessor::RequestData(
int mtlParseResult;
std::vector<vtkOBJImportedMaterial*> parsedMTLs = ParseOBJandMTL(MTLFileName,mtlParseResult);
if(parsedMTLs.empty())
{ // construct a default material to define the single polydata's actor.
parsedMTLs.push_back( new vtkOBJImportedMaterial );
}
vtkDebugMacro("vtkOBJPolyDataProcessor parsed " << parsedMTLs.size()
<< " materials from " << MTLFileName);
......@@ -327,12 +336,15 @@ int vtkOBJPolyDataProcessor::RequestData(
{
// Since we read the MTL file, we already know how many actors we need.
// So, pre-allocate instead of trying to do it on the fly.
while(poly_list.size() != parsedMTLs.size() )
if(!parsedMTLs.empty())
{
vtkOBJImportedPolyDataWithMaterial* newMaterial = new vtkOBJImportedPolyDataWithMaterial;
newMaterial->SetSharedPoints(shared_vertexs);
newMaterial->SetSharedNormals(shared_normals);
poly_list.push_back(newMaterial);
while(poly_list.size() != parsedMTLs.size() )
{
vtkOBJImportedPolyDataWithMaterial* newMaterial = new vtkOBJImportedPolyDataWithMaterial;
newMaterial->SetSharedPoints(shared_vertexs);
newMaterial->SetSharedNormals(shared_normals);
poly_list.push_back(newMaterial);
}
}
for( size_t k = 0; k<parsedMTLs.size(); ++k )
{
......@@ -357,12 +369,6 @@ int vtkOBJPolyDataProcessor::RequestData(
vtkCellArray* lineElems = poly_list.back()->lineElems;
vtkCellArray* normal_polys = poly_list.back()->normal_polys;
// ????
// Implementation warning: this seems to assume that
// the MTL definitions are in "apperance in .obj order" within
// the actual MTL file. So, can't have "use_mtl M0" before
// "use_mtl M1" if the .mtl file lists them in order M1, M0
outVector_of_textureFilnames.resize( parsedMTLs.size() );
for( int i = 0; i < (int)parsedMTLs.size(); ++i )
{
......@@ -782,6 +788,11 @@ int vtkOBJPolyDataProcessor::RequestData(
// we have finished with the file
fclose(in);
if(!gotFirstUseMaterialTag)
{
known_materials[parsedMTLs[0]->name] = poly_list[0];
}
{ /** based on how many named materials are present,
set the number of output ports of vtkPolyData */
this->SetNumberOfOutputPorts( known_materials.size() );
......
......@@ -268,8 +268,11 @@ void bindTexturedPolydataToRenderWindow( vtkRenderWindow* renderWindow,
}
else
{
vtkErrorWithObjectMacro(reader, "Nonexistant texture image type!? imagefile: "
<<textureFilename);
if(!textureFilename.empty()) // OK to have no texture image, but if its not empty it ought to exist.
{
vtkErrorWithObjectMacro(reader, "Nonexistant texture image type!? imagefile: "
<<textureFilename);
}
}
vtk_texture->InterpolateOff(); // Faster?? (yes clearly faster for largish texture)
......@@ -310,3 +313,11 @@ void bindTexturedPolydataToRenderWindow( vtkRenderWindow* renderWindow,
}
/** post-condition of this function: the renderer has had a bunch of actors added to it */
}
vtkOBJImportedMaterial::vtkOBJImportedMaterial()
{
name[0] = 'x';
name[1] = '\0';
obj_set_material_defaults(this);
}
......@@ -50,6 +50,7 @@ struct vtkOBJImportedMaterial
return sqrt( spec[0]*spec[0]+spec[1]*spec[1]+spec[2]*spec[2] );
}
const char *GetClassName() {return "vtkOBJImportedMaterial";}
vtkOBJImportedMaterial();
};
......@@ -78,6 +79,8 @@ public:
void SetTexturePath( const char* arg )
{
TexturePath = std::string(arg);
if(TexturePath.empty())
return;
char sep = '/';
#if defined(_WIN32)
sep = '\\';
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment