[vtkusers] Re: vtkFLTK eventloop (A J)

Sean McInerney seanm at nmr.mgh.harvard.edu
Sun Jun 13 14:16:40 EDT 2004


AJ,

   1. Look at vtkFLTK/Examples to find the answers you seek.

   2. Just use vtkRenderWindowInteractor (instantiation of
      vtkFLTKRenderWindowInteractor is handled by the ObjectFactory
      which is registered by the Fl_VTK_Window initializer).

   3. Start the FLTK event loop with Fl::run().

   4. Below is a simple example animating via a timeout.
      Be sure to read the comments.

   Note that regular interaction remains enabled.

-Sean

// VTK Graphics
#include "vtkConeSource.h"
// VTK Rendering
#include "vtkPolyDataMapper.h"
#include "vtkActor.h"
#include "vtkCamera.h"
// FLTK (needed for the call to Fl::run())
#include <FL/Fl.H>
// vtkFLTK
#include "Fl_VTK_Window.H"

void
timer_callback (void* a)
{
   Fl_VTK_Window* window = reinterpret_cast<Fl_VTK_Window*>(a);
   vtkCamera*     camera = window->GetDefaultCamera();

   camera->Roll(0.2);
   window->Update();

   // Repeat timeout at 1/30th of a second from when the system call
   // elapsed that caused this timeout.
   Fl::repeat_timeout(1.0 / 30.0, timer_callback, a);
}

int
main (int argc, char* argv[])
{
   // Set up a top level window and a Fl_VTK_Window to be its child.
   Fl_Window*     mainWindow    = new Fl_Window(400,100, 256,256);
   // Create the child window (wrapped within is the RenderWindow).
   Fl_VTK_Window* vtkfltkWindow = new Fl_VTK_Window(8,8, 240,240);

   // If 'mainWindow' is resized, 'vtkfltkWindow' should resize with it.
   mainWindow->resizable(vtkfltkWindow);
   // End populating the mainWindow.
   mainWindow->end();

   // Set up the VTK rendering pipeline creating an actor and giving it
   // some cone geometry. The references to the VTK pipeline can be
   // deleted along the way as the objects themselves will stick around
   // until the Fl_VTK_Window is deleted.
   vtkActor*          coneActor  = vtkActor::New();
   vtkPolyDataMapper* coneMapper = vtkPolyDataMapper::New();
   vtkConeSource*     coneSource = vtkConeSource::New();

   // Connect the Source to the Mapper and decrement its reference count.
   coneMapper->SetInput(coneSource->GetOutput());
   coneSource->Delete();
   // Connect the Mapper to the Actor and decrement its reference count.
   coneActor->SetMapper(coneMapper);
   coneMapper->Delete();
   // Add Actor to default renderer and decrement its reference count.
   vtkfltkWindow->AddProp(coneActor);
   coneActor->Delete();

   // Show the main Fl_Window.
   mainWindow->show();

   // Add a timeout to be called as soon as the event loop starts.
   Fl::add_timeout(0, timer_callback, vtkfltkWindow);

   // Start the FLTK event loop.
   int fl_ret = Fl::run();

   // After the FLTK event loop returns, delete the interface windows.
   //     N.B.: When we delete 'mainWindow', its Fl_VTK_Window
   //           child is also destroyed. When the Fl_VTK_Window is
   //           destroyed, it invokes Delete() on its associated
   //           vtkRenderWindowInteractor. Once this is done, the
   //           rest of the VTK pipeline will also be destroyed.
   delete mainWindow;

   return fl_ret;
}


> 
> Hi,
> 
> Can someone give me a small basic example on how to start the event loop and 
> have a call back associated with the timer that drives the event loop
> and is there a way to set the timer to be called every x number of 
> milliseconds..?
> 
> Right now im on c++ using vtk and just got the vtkFLTK library
> So far I render with vtkRenderWindowInteractor
> 
> but now I guess I should change this to vtkFLTKRenderWindowInteractor..?
> 
> so what do I call to initiate the event loop?
> 
> thanks..
> aj
> 



More information about the vtkusers mailing list