[vtkusers] Problems with vtkFlRenderWindowInteractor.

Chuang Li chuli at cse.psu.edu
Fri Jun 4 21:48:03 EDT 2004


Hello,

I'm working on adding user interface on my VTK application, is using the famous "vtkFlRenderWindowInteractor (0.9)". However, there is problems in saving the image correctly, the saved imaged is always deformed seriously. Following is the Cone3.cpp code. A "save" button is added, everything else is the same as before. The libraries I used are VTK 4.4 and FLTK 1.1.5. The application is running under RedHat 9.0 shrike. One odd thing is if run exactly same code under RH 8.0, everything work fine.

Can anyone help me out? Thanks a lot.

chuang



// Cone3.cxx adapted to illustrate the use of vtkFlRenderWindowInteractor
// $Id: Cone3.cxx,v 1.7 2002/12/30 14:51:26 cpbotha Exp $
// Study this example carefully.  Study it again.  Repeat.

// here we have all the usual VTK stuff that we need for our pipeline
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkConeSource.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkWindowToImageFilter.h>
#include <vtkJPEGWriter.h>
#include <vtkPNGWriter.h>
#include <vtkTIFFWriter.h>
#include <vtkBMPWriter.h>
#include <vtkRendererCollection.h>
#include <vtkRendererSource.h>

// and here we have the famous vtkFlRenderWindowInteractor class
#include <vtkFlRenderWindowInteractor.h>

// and of course some fltk stuff
#include <Fl/Fl_Box.h>
#include <Fl/Fl_Button.h>

// this is a callback for the quit button
void quit_cb(Fl_Widget*, void*)
{
   exit(0);
}

// this callback gets called when the main window is closed
// remember that one should NEVER delete() a vtkFlRenderWindowInteractor,
// since it's a vtk object.  instead, its Delete() method should be used.
// so, in the callback of the containing window, you should either terminate
// your application (if that's applicable) or if you have a multi-window 
// application, call ->Delete() on the vtkFlRWI and then take care of the
// surrounding logic.
void main_window_callback(Fl_Widget *w, void *fl_vtk_window)
{
    exit(0);
}

void save_callback(Fl_Widget *w, void *fl_vtk_window)
{
 vtkFlRenderWindowInteractor *fw = (vtkFlRenderWindowInteractor *)fl_vtk_window;
 vtkRenderWindow *rw = fw->GetRenderWindow();

 vtkRendererCollection *rc = rw->GetRenderers();
 rc->InitTraversal();
 vtkRenderer *rr = rc->GetNextItem();

 vtkRendererSource *rsrc = vtkRendererSource::New();
 rsrc->SetInput(rr);
 rsrc->WholeWindowOn();
 rsrc->RenderFlagOn();
 rsrc->DepthValuesOff(); 
 rsrc->DepthValuesInScalarsOff();

 //vtkWindowToImageFilter *w2i = vtkWindowToImageFilter::New();
 //w2i->SetInput(rsrc->GetOutput()); 

 //vtkPNGWriter *writer = vtkPNGWriter::New();
 vtkJPEGWriter *writer = vtkJPEGWriter::New();
 //vtkTIFFWriter *writer = vtkTIFFWriter::New();
 //vtkBMPWriter *writer = vtkBMPWriter::New();
 writer->SetInput(rsrc->GetOutput());
 //writer->SetFileName("cones.png");
 writer->SetFileName("cones.jpg");
 writer->Write(); 
 writer->Delete();
}

int main( int argc, char *argv[] )
{
   // set up main FLTK window
   Fl_Window* main_window = new Fl_Window(300,330,"Cone3.cxx");
   
   // and instantiate vtkFlRenderWindowInteractor (here it acts like a FLTK window,
   // i.e. you could also instantiate it as child of a Fl_Group in a window)
   vtkFlRenderWindowInteractor* fl_vtk_window = new vtkFlRenderWindowInteractor(5,5,290,260,NULL);
    
   main_window->callback(main_window_callback, fl_vtk_window);

   // this will result in a little message under the rendering
   Fl_Box* box = new Fl_Box(5,261,290,34,"3 = stereo, j = joystick, t = trackball, w = wireframe, s = surface, p = pick; you can also resize the window");
   box->labelsize(10);
   box->align(FL_ALIGN_WRAP);
   
   // we want a button with which the user can quit the application
   Fl_Button* quit_button = new Fl_Button(30,300,100,25,"quit");
   quit_button->callback(quit_cb, NULL);
   
   Fl_Button* save_button = new Fl_Button(160,300,100,25,"save");
   save_button->callback(save_callback, (void *) fl_vtk_window);

   // we're done populating the main_window
   main_window->end();
   // if the main window gets resized, the vtk window should resize with it
   main_window->resizable(fl_vtk_window);
   
   // these two steps are VERY IMPORTANT, you have to show() the fltk window
   // containing the vtkFlRenderWindowInteractor, and then the
   // vtkFlRenderWindowInteractor itself
   main_window->show();
   fl_vtk_window->show();
   
   // now we get to setup our VTK rendering pipeline
   
   // create a rendering window and renderer
   vtkRenderer *ren = vtkRenderer::New();
   vtkRenderWindow *renWindow = vtkRenderWindow::New();
   renWindow->AddRenderer(ren);
   // uncomment the statement below if things aren't rendering 100% on your
   // configuration; the debug output could give you clues as to why
   //renWindow->DebugOn();
   
   // NB: here we treat the vtkFlRenderWindowInteractor just like any other
   // old vtkRenderWindowInteractor
   fl_vtk_window->SetRenderWindow(renWindow);
   // just like with any other vtkRenderWindowInteractor(), you HAVE to call
   // Initialize() before the interactor will function.  See the docs in
   // vtkRenderWindowInteractor.h
   fl_vtk_window->Initialize();

   // create an actor and give it cone geometry
   vtkConeSource *cone = vtkConeSource::New();
   cone->SetResolution(8);
   vtkPolyDataMapper *coneMapper = vtkPolyDataMapper::New();
   coneMapper->SetInput(cone->GetOutput());
   vtkActor *coneActor = vtkActor::New();
   coneActor->SetMapper(coneMapper);

   // assign our actor to the renderer
   ren->AddActor(coneActor);

   // We can now delete all our references to the VTK pipeline (except for
   // our reference to the vtkFlRenderWindowInteractor) as the objects
   // themselves will stick around until we dereference fl_vtk_window
   ren->Delete();
   renWindow->Delete();
   cone->Delete();
   coneMapper->Delete();
   coneActor->Delete();

   // this is the standard way of "starting" a fltk application
   int fl_ret = Fl::run();
 //fl_vtk_window->Start();
   
   // very huge NB: note that we ->Delete() the vtkFlRenderWindowInteractor
   // once we do this, the rest of the vtk pipeline will really disappear
   fl_vtk_window->Delete();
   // and after we've done that, we can delete the main_window
   delete main_window;
   
 return 1;
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20040604/7ecac08f/attachment.htm>


More information about the vtkusers mailing list