Commit 7a655383 authored by Michael Migliore's avatar Michael Migliore
Browse files

Add OBJ importer vertex coloring

parent a6e06f43
Pipeline #202536 running with stage
e9b4ce0444e4ff2d6c2ee29632291264c27babca45b25bb36f6bc200ce407f0f5ced1ea562661423ee790559ad5844afb8307826aef7ece1703af00781fc3d5b
8b3ebfdc331161e46be0e968593c183e917b59e0100c79283a660b8dc7c53b0b6dd92e5dba0d5ec77c5fa13193f614685c452975b6a8b03701d5e688bd9396dc
......@@ -169,6 +169,7 @@ struct vtkOBJImportedPolyDataWithMaterial
{ // initialize some structures to store the file contents in
points = vtkSmartPointer<vtkPoints>::New();
tcoords = vtkSmartPointer<vtkFloatArray>::New();
colors = vtkSmartPointer<vtkFloatArray>::New();
normals = vtkSmartPointer<vtkFloatArray>::New();
polys = vtkSmartPointer<vtkCellArray>::New();
tcoord_polys = vtkSmartPointer<vtkCellArray>::New();
......@@ -177,6 +178,7 @@ struct vtkOBJImportedPolyDataWithMaterial
normal_polys = vtkSmartPointer<vtkCellArray>::New();
tcoords->SetNumberOfComponents(2);
normals->SetNumberOfComponents(3);
colors->SetNumberOfComponents(3);
materialName = "";
mtlProperties = nullptr;
......@@ -191,6 +193,7 @@ struct vtkOBJImportedPolyDataWithMaterial
// these are unique per entity
vtkSmartPointer<vtkFloatArray> tcoords;
vtkSmartPointer<vtkFloatArray> colors;
vtkSmartPointer<vtkCellArray> polys;
vtkSmartPointer<vtkCellArray> tcoord_polys;
vtkSmartPointer<vtkCellArray> pointElems;
......@@ -280,9 +283,9 @@ To find a full specification, search the net for "OBJ format", eg.:
We support the following types:
v <x> <y> <z>
v <x> <y> <z> <r> <g> <b>
vertex
vertex position and optionally a vertex color
vn <x> <y> <z>
......@@ -366,21 +369,24 @@ int vtkOBJPolyDataProcessor::RequestData(vtkInformation* vtkNotUsed(request),
if (this->DefaultMTLFileName)
{
mtlname = this->FileName + ".mtl";
}
FILE* defMTL = vtksys::SystemTools::Fopen(mtlname, "r");
if (defMTL == nullptr)
{
if (!this->DefaultMTLFileName)
if (vtksys::SystemTools::FileExists(mtlname))
{
vtkErrorMacro(<< "The MTL file " << mtlname << " could not be found");
fclose(in);
return 0;
this->MTLFileName = mtlname;
}
else
{
mtlname = vtksys::SystemTools::GetFilenamePath(this->FileName) + "/" +
vtksys::SystemTools::GetFilenameWithoutLastExtension(this->FileName) + ".mtl";
if (vtksys::SystemTools::FileExists(mtlname))
{
this->MTLFileName = mtlname;
}
else
{
vtkErrorMacro(<< "The default MTL file could not be found");
return 0;
}
}
}
else
{
this->MTLFileName = mtlname;
fclose(defMTL);
}
int mtlParseResult;
......@@ -418,6 +424,7 @@ int vtkOBJPolyDataProcessor::RequestData(vtkInformation* vtkNotUsed(request),
vtkPoints* points = poly_list.back()->points;
vtkFloatArray* tcoords = poly_list.back()->tcoords;
vtkFloatArray* normals = poly_list.back()->normals;
vtkFloatArray* colors = poly_list.back()->colors;
vtkCellArray* polys = poly_list.back()->polys;
vtkCellArray* tcoord_polys = poly_list.back()->tcoord_polys;
vtkCellArray* pointElems = poly_list.back()->pointElems;
......@@ -430,6 +437,7 @@ int vtkOBJPolyDataProcessor::RequestData(vtkInformation* vtkNotUsed(request),
bool hasTCoords = false; // has vt x y z
bool hasPolysWithTextureIndices = false; // has f i/t/n or f i/t
bool hasNormals = false; // has f i/t/n or f i//n
bool hasColors = false; // has v x y z r g b
bool tcoords_same_as_verts = true;
bool normals_same_as_verts = true;
bool everything_ok = true; // (use of this flag avoids early return and associated memory leak)
......@@ -442,6 +450,7 @@ int vtkOBJPolyDataProcessor::RequestData(vtkInformation* vtkNotUsed(request),
const int MAX_LINE = 100000;
char rawLine[MAX_LINE];
float xyz[3];
float col[3];
int lineNr = 0;
while (everything_ok && fgets(rawLine, MAX_LINE, in) != nullptr)
......@@ -482,8 +491,11 @@ int vtkOBJPolyDataProcessor::RequestData(vtkInformation* vtkNotUsed(request),
static long lastVertexIndex = 0;
if (strcmp(cmd, "v") == 0)
{
// this is a vertex definition, expect three floats, separated by whitespace:
if (sscanf(pLine, "%f %f %f", xyz, xyz + 1, xyz + 2) == 3)
// this is a vertex definition, expect three floats (six if vertex color), separated by
// whitespace:
int nbRead =
sscanf(pLine, "%f %f %f %f %f %f", xyz, xyz + 1, xyz + 2, col, col + 1, col + 2);
if (nbRead >= 3)
{
if (use_scale)
{
......@@ -493,6 +505,12 @@ int vtkOBJPolyDataProcessor::RequestData(vtkInformation* vtkNotUsed(request),
}
points->InsertNextPoint(xyz);
lastVertexIndex++;
if (nbRead == 6)
{
hasColors = true;
colors->InsertNextTypedTuple(col);
}
}
else
{
......@@ -509,7 +527,7 @@ int vtkOBJPolyDataProcessor::RequestData(vtkInformation* vtkNotUsed(request),
// this is a tcoord, expect two floats, separated by whitespace:
if (sscanf(pLine, "%f %f", xyz, xyz + 1) == 2)
{
tcoords->InsertNextTuple(xyz);
tcoords->InsertNextTypedTuple(xyz);
}
else
{
......@@ -522,7 +540,7 @@ int vtkOBJPolyDataProcessor::RequestData(vtkInformation* vtkNotUsed(request),
// this is a normal, expect three floats, separated by whitespace:
if (sscanf(pLine, "%f %f %f", xyz, xyz + 1, xyz + 2) == 3)
{
normals->InsertNextTuple(xyz);
normals->InsertNextTypedTuple(xyz);
hasNormals = true;
}
else
......@@ -907,6 +925,13 @@ int vtkOBJPolyDataProcessor::RequestData(vtkInformation* vtkNotUsed(request),
vtkDebugMacro("generating output polydata .... \n"
<< "tcoords same as verts!? " << tcoords_same_as_verts << " ... hasTCoords?" << hasTCoords
<< " ... numPolysWithTCoords = " << numPolysWithTCoords);
// assign the points color as point data
if (hasColors)
{
output->GetPointData()->SetScalars(colors);
}
// if there are no tcoords or normals or they match exactly
// then we can just copy the data into the output (easy!)
if ((!hasTCoords || tcoords_same_as_verts) && (!hasNormals || normals_same_as_verts))
......
......@@ -43,15 +43,15 @@ int localVerbosity = 0;
void obj_set_material_defaults(vtkOBJImportedMaterial* mtl)
{
mtl->amb[0] = 0.2;
mtl->amb[1] = 0.2;
mtl->amb[2] = 0.2;
mtl->diff[0] = 0.8;
mtl->diff[1] = 0.8;
mtl->diff[2] = 0.8;
mtl->spec[0] = 1.0;
mtl->spec[1] = 1.0;
mtl->spec[2] = 1.0;
mtl->amb[0] = 0.0;
mtl->amb[1] = 0.0;
mtl->amb[2] = 0.0;
mtl->diff[0] = 1.0;
mtl->diff[1] = 1.0;
mtl->diff[2] = 1.0;
mtl->spec[0] = 0.0;
mtl->spec[1] = 0.0;
mtl->spec[2] = 0.0;
mtl->map_Kd_scale[0] = 1.0;
mtl->map_Kd_scale[1] = 1.0;
mtl->map_Kd_scale[2] = 1.0;
......@@ -392,6 +392,7 @@ void bindTexturedPolydataToRenderWindow(
vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputData(objPoly);
mapper->SetColorModeToDirectScalars();
vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
actor->SetMapper(mapper);
......@@ -487,6 +488,8 @@ void bindTexturedPolydataToRenderWindow(
actor->GetTexture()->SetTransform(tf);
}
mapper->ScalarVisibilityOff();
properties->SetDiffuseColor(raw_mtl_data->diff);
properties->SetSpecularColor(raw_mtl_data->spec);
properties->SetAmbientColor(raw_mtl_data->amb);
......
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