简体   繁体   English

将Obj文件导入到vtk

[英]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. 我正在使用vtk,我想可视化objFile,但是我不知道该怎么做,我认为我应该使用ReadObj.cxx,但是我应该在哪里放置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 . 您的代码是正确的,并且ObjFile的名称是filename You should specify it when you run your program as command line : 当您以命令行运行程序时,应指定它:

ReadObj.exe myobjfile.obj ReadObj.exe myobjfile.obj

Is your file a valid .obj? 您的文件是有效的.obj吗? Can you import it into blender or unity or 3dsmax to validate it? 您可以将其导入Blender或unity或3dsmax进行验证吗? 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. 在hello world示例( Hello World )中,将显示一个多数据,就像您的文件一样,并且设置了颜色。 Also, you didn't reset the camera after adding your actor, so the camera is in an incorrect position. 另外,添加actor后您没有重置相机,因此相机的位置不正确。 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. 您可以在hello world示例中将obj loader的输出连接到vtkPolyDataMapper,它应该可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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