简体   繁体   中英

Importing an Obj File into vtk

I'm using vtk and I want to visualise an objFile but i don't know how to do it I think I should use ReadObj.cxx but where should I put the name of my ObjFile.

int main(int argc, char* argv[])
{
 // Parse command line arguments
 if(argc != 2)
 {
 std::cout << "Usage: " << argv[0] << " Filename(.obj)" << std::endl;
 return EXIT_FAILURE;
 }

 std::string filename = argv[1];
 vtkSmartPointer<vtkOBJReader> reader =
 vtkSmartPointer<vtkOBJReader>::New();
  reader->SetFileName(filename.c_str());
 reader->Update();

 // Visualize
 vtkSmartPointer<vtkPolyDataMapper> mapper =
 vtkSmartPointer<vtkPolyDataMapper>::New();
 mapper->SetInputConnection(reader->GetOutputPort());

 vtkSmartPointer<vtkActor> actor =
 vtkSmartPointer<vtkActor>::New();
 actor->SetMapper(mapper);

 vtkSmartPointer<vtkRenderer> renderer =
 vtkSmartPointer<vtkRenderer>::New();
 renderer->AddActor(actor);
 renderer->SetBackground(.3, .6, .3); // Background color green

 vtkSmartPointer<vtkRenderWindow> renderWindow =
  vtkSmartPointer<vtkRenderWindow>::New();
  renderWindow->AddRenderer(renderer);

  vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
   vtkSmartPointer<vtkRenderWindowInteractor>::New();
  renderWindowInteractor->SetRenderWindow(renderWindow);

  renderWindowInteractor->Start();

 return EXIT_SUCCESS;
 }

can anyone help me ? Thanks.

Not sure what your question is. Your code is correct and the name of your ObjFile is filename . You should specify it when you run your program as command line :

ReadObj.exe myobjfile.obj

Is your file a valid .obj? Can you import it into blender or unity or 3dsmax to validate it? You didn't set the color. In the hello world example ( Hello World ) a polydata, just like your file, is shown and it's color is set. Also, you didn't reset the camera after adding your actor, so the camera is in an incorrect position. The hello world exemple also shows how to do reset the camera. You could just connect your obj loader's output to the vtkPolyDataMapper in the hello world example and it should just work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM