[vtkusers] JPEG OR BMPWRITER VTK Animating Help

Bryan Lee Perez brylee at fumbo.com
Tue Apr 6 20:32:45 EDT 2004


Hi , thanks for the info, Im a newbie im a little bit lost no this, i
tryed to implement it on my code here is the code.
the main idea is to save screenshots using my C Code every time the
window
is redered or every time the object in this case is moved froma
different
posicion.

thanks
Bryan Lee




//**//
// First include the required header files for the VTK classes we are
using.

#include "vtkSphereSource.h"
#include "vtkCylinderSource.h"
#include "vtkConeSource.h"
#include "vtkPolyDataMapper.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkCamera.h"
#include "vtkActor.h"
#include "vtkRenderer.h"
#include "vtkProperty.h"
#include "vtkWindowToImageFilter.h"
#include "vtkJPEGWriter.h"
//#include ".h"

int main( int argc, char *argv[] )
{
  //
  // Next we create an instance of vtkConeSource and set some of its //
properties. The instance of vtkConeSource "cone" is part of a //
visualization pipeline (it is a source process object); it produces data
// (output type is vtkPolyData) which other filters may process. //
int sides=10;

// defining the first source (Cone)
  vtkConeSource *cone = vtkConeSource::New();
  cone->SetHeight( 2.0 );
  cone->SetRadius( 1.0 );
  cone->SetResolution( sides );

  // defining the second source (cylinder)
  vtkCylinderSource *cylinder = vtkCylinderSource::New();
  cylinder->SetHeight( 4.0 );
  cylinder->SetRadius( 1.0 );
  cylinder->SetResolution(sides);

  // defining the trird source (Sphere)
  vtkSphereSource *sphere = vtkSphereSource::New();
  sphere->SetPhiResolution( sides);
  sphere->SetRadius( 0.5 );

  //defining engines
  vtkCylinderSource *cylengines = vtkCylinderSource::New();
  cylengines->SetHeight( 1.5 );
  cylengines->SetRadius( 0.5 );
  cylengines->SetResolution(sides);


  //figure 1 Cone to map the polygonal data into graphics primitives

  vtkPolyDataMapper *coneMapper = vtkPolyDataMapper::New();
  coneMapper->SetInput( cone->GetOutput() );

  //figure 2 cylinder to map the polygonal data into graphics primitives

  vtkPolyDataMapper *cylinderMapper = vtkPolyDataMapper::New();
  cylinderMapper->SetInput( cylinder->GetOutput() );


  //figure 3 Sphere to map the polygonal data into graphics primitives

  vtkPolyDataMapper *sphereMapper = vtkPolyDataMapper::New();
  sphereMapper->SetInput( sphere->GetOutput() );

  //figure 4 cylinder engines to map the polygonal data into graphics
primitives

  vtkPolyDataMapper *cylenginesMapper = vtkPolyDataMapper::New();
  cylenginesMapper->SetInput( cylengines->GetOutput() );

  // Create an actor to represent the figures (cone.cilinder,sphere). //
The actor orchestrates rendering
  // of the mapper's graphics primitives. An actor also refers to
properties // via a vtkProperty instance, and includes an internal
transformation // matrix. We set this actor's mapper to be coneMapper
which we created // above.

  // first Actor of the Cone Which orchestrates rendering of
  // the mapper's graphics primitives
  vtkActor *coneActor = vtkActor::New();
  coneActor->SetMapper( coneMapper );
  /// extra
  coneActor->GetProperty()->SetColor (0.2,0.63,0.79); //(rgb Color)
coneActor->GetProperty()->SetDiffuse(0.7);  //Diffuseness
  coneActor->GetProperty()->SetSpecular(0.9);
  coneActor->GetProperty()->SetSpecularPower(20);
  coneActor->RotateX(0.0);
  coneActor->RotateY(0.0);
  coneActor->RotateZ(90.0);
  coneActor ->SetPosition(0, 3, 0);

  // Second Actor of the Cylinder Which orchestrates rendering of
  // the mapper's graphics primitives

  vtkActor *cylinderActor = vtkActor::New();
  cylinderActor ->SetMapper( cylinderMapper );


  // third Actor the Sphere Which orchestrates rendering of
  // the mapper's graphics primitives

  vtkActor *sphereActor = vtkActor::New();
  sphereActor ->SetMapper( sphereMapper );
  sphereActor ->SetPosition(1, -1, 0);

  vtkActor *sphereActor2 = vtkActor::New();
  sphereActor2 ->SetMapper( sphereMapper );
  sphereActor2 ->SetPosition(-1, -1, 0);

  // Engines Actor of Class Cylinder Which orchestrates rendering of //
the mapper's graphics primitives

  vtkActor *cylenginesActor = vtkActor::New();
  cylenginesActor ->SetMapper( cylenginesMapper );
  cylenginesActor ->SetPosition(1, -1.75, 0);

  vtkActor *cylenginesActor2 = vtkActor::New();
  cylenginesActor2 ->SetMapper( cylenginesMapper );
  cylenginesActor2 ->SetPosition(-1, -1.75, 0);


  //
  // Create the Renderer and assign actors to it. A renderer is like a
//
viewport. It is part or all of a window on the screen and it is //
responsible for drawing the actors it has.  We also set the background
// color here.
  //
  vtkRenderer *ren1= vtkRenderer::New();
  ren1->AddActor( coneActor );
  ren1->AddActor( sphereActor );
  ren1->AddActor( sphereActor2 );
  ren1->AddActor( cylinderActor );
  ren1->AddActor( cylenginesActor );
  ren1->AddActor( cylenginesActor2 );
  ren1->SetBackground( 0.2, 0.3, 0.5 );
//  ren1->SetViewport  ( 0.0, 0.0, 0.5, 0.5 ); //(Xini,Yini,Xend,Yend)


  // Finally we create the render window which will show up on the
screen.
// We put our renderer into the render window using AddRenderer. We also
// set the size to be 500 pixels by 500.
  //
  vtkRenderWindow *renWin = vtkRenderWindow::New();
  renWin->AddRenderer( ren1 );
  renWin->SetSize( 500, 500 );  //size of the window

  //adding the interactor
  vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
iren->SetRenderWindow(renWin);



  // This starts the event loop and as a side effect causes an initial
render.
	vtkWindowToImageFilter *w2i = vtkWindowToImageFilter::New();
	vtkJPEGWriter *jpegw = vtkJPEGWriter::New();

		w2i->SetInput(renWin);
		w2i->Update();

		jpegw->SetInput(w2i->GetOutput());
		jpegw->SetFileName("whatever.jpg");
		jpegw->ProgressiveOn();
		jpegw->SetQuality(100);
		// render the images
		renWin->Render();

		jpegw->Write();

  iren->Start();

  // Free up any objects we created. All instances in VTK are deleted by
// using the Delete() method.


  cone->Delete();
  cylinder->Delete();

  coneMapper->Delete();
  cylinderMapper->Delete();

  coneActor->Delete();
  cylinderActor->Delete();

  //ren1->Delete();

  //renWin->Delete();

  return 0;
}










More information about the vtkusers mailing list