Program Stops automatically right after a short render

81 views Asked by At

I'm having a small issue with VTK. When I launch my program, it starts to render and then automatically stops while it should actually keep on rendering. I cannot see where that error may come from. I wrote an other piece of code for the exact same rendering which works just fine.

So here is the constructor of my class:

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

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

Here is the piece of code not working:

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();
}

And here is the piece of code working:

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();
}

I don't know if it's a vtk or a c++ problem actually. The output of the not-working piece of code is just a few milliseconds window and then the program exists while the otherRead() gives a normal rendering without exiting the program.

Thanks in advance for the help

1

There are 1 answers

0
LBes On BEST ANSWER

So it was indeed a vtk problem. One very basic some might say but still not that easy to figure out when you're starting with vtk. I'm posting the answer, the documentation is not big on vtk so it might help other newbies. So in fact, the event loop for the window has to be started. It can be done thanks to the following lines:

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

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

Hope this helps :)