程序在短渲染后立即自动停止

时间:2015-06-15 09:43:45

标签: c++ vtk

我对VTK有一个小问题。当我启动我的程序时,它开始渲染,然后自动停止,而它应该实际继续渲染。 我看不出那个错误可能来自哪里。我为完全相同的渲染编写了另一段代码,效果很好。

所以这是我班级的构造函数:

Drawing::Drawing(void)
{
    translation = vtkSmartPointer<vtkTransform>::New();
    ctxView = vtkContextView::New();
    win = ctxView->GetRenderWindow();
    ren = ctxView->GetRenderer();
    cam = ren->GetActiveCamera();

    ren->SetBackground(.0,.0,.0);
}

以下是不起作用的代码:

void Drawing::read(){

    std::string filename = BUNNY;
    // Read all the data from the file
    vtkSmartPointer<vtkXMLPolyDataReader> reader =vtkSmartPointer<vtkXMLPolyDataReader>::New();
    reader->SetFileName(filename.c_str());
    reader->Update();

    cout << "File Found and Loaded : " << filename << endl ;

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

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

    ren->AddActor(mainActor);

    win->PolygonSmoothingOn();
    win->Render();
    win->Start();
}

这是一段有效的代码:

void otherRead(){
    std::string filename = BUNNY;
    // Read all the data from the file
    vtkSmartPointer<vtkXMLPolyDataReader> reader =vtkSmartPointer<vtkXMLPolyDataReader>::New();
    reader->SetFileName(filename.c_str());
    reader->Update();

    cout << "File Found and Loaded : " << filename << endl ;

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

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

    vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
    vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
    renderWindow->AddRenderer(renderer);
    vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
    renderWindowInteractor->SetRenderWindow(renderWindow);

    vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
    iren->SetRenderWindow(renderWindow);

    vtkInteractorStyleMultiTouchCamera *style =
    vtkInteractorStyleMultiTouchCamera::New();
    iren->SetInteractorStyle(style);

    renderer->AddActor(mainActor);
    renderer->SetBackground(0,0,0); // Background color green

    renderWindow->PolygonSmoothingOn();
    renderWindow->Render();
    renderWindowInteractor->Start();
}

我不知道它实际上是vtk还是c ++问题。无效代码的输出只是几毫秒的窗口然后程序存在,而otherRead()给出正常的渲染而不退出程序。

提前感谢您的帮助

1 个答案:

答案 0 :(得分:1)

所以这确实是一个vtk问题。一个非常基本的人可能会说,但是当你从vtk开始时,仍然不容易弄明白。 我发布了答案,文档在vtk上并不大,所以它可能对其他新手有所帮助。所以实际上,必须启动窗口的事件循环。可以通过以下几行来完成:

vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
iren->SetRenderWindow(win);
vtkInteractorStyleMultiTouchCamera *style =
vtkInteractorStyleMultiTouchCamera::New();
iren->SetInteractorStyle(style);

//Start the event loop
iren->Initialize();
iren->Start();

希望这会有所帮助:)