[vtkusers] I have some problems when deal with key press callback

zhq 15891495523 at 126.com
Mon Nov 17 10:25:57 EST 2014


Deal all :

      What I want to do is : make a transform on a cone when a key
(whatever) is pressed . 

      the problem I met is :  the cone will disappear in the window when a
transform happen . 

      My code is :

#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkJPEGReader.h>
#include <vtkImageData.h>
#include <vtkSmartPointer.h>
#include <vtkImageViewer2.h>
#include <vtkImageBlend.h>
#include <string>
#include <vtkSTLReader.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkPolyDataMapper.h>
#include "vtkActor.h"
#include <vtkCallbackCommand.h>
#include <vtkTransform.h>
#include <vtkTransformPolyDataFilter.h>
#include <vtkSphereSource.h>
#include <vtkConeSource.h>
vtkSmartPointer<vtkConeSource> cone = vtkSmartPointer<vtkConeSource>::New();

vtkSmartPointer<vtkRenderWindowInteractor> iren =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
vtkSmartPointer<vtkTransform> trans = vtkSmartPointer<vtkTransform>::New();
vtkSmartPointer<vtkActor> actor1 = vtkSmartPointer<vtkActor>::New();
vtkSmartPointer<vtkActor> actor2 = vtkSmartPointer<vtkActor>::New();
vtkSmartPointer<vtkPolyDataMapper> mapper1 =
vtkSmartPointer<vtkPolyDataMapper>::New();
vtkSmartPointer<vtkPolyDataMapper> mapper2 =
vtkSmartPointer<vtkPolyDataMapper>::New();

int x=0,y=0,z=0;

void MyCallbackFunc(vtkObject * , unsigned long eid , void * clientdata ,
void * calldata)
{

	x++;

	trans->Translate(x,y,z) ;
	trans->Scale(2, 3, 1);
	trans->RotateX(0);

	vtkSmartPointer<vtkTransformPolyDataFilter> transformFilter =
		vtkSmartPointer<vtkTransformPolyDataFilter>::New();
	transformFilter->SetInputConnection(cone->GetOutputPort());
	transformFilter->SetTransform(trans);
	transformFilter->Update();
	mapper1->SetInputConnection(transformFilter->GetOutputPort());
	iren->Render();
}

void main()
{

	vtkSmartPointer<vtkRenderWindow> renWin =
vtkSmartPointer<vtkRenderWindow>::New();
	
	vtkSmartPointer<vtkSphereSource> sphereSource =
		vtkSmartPointer<vtkSphereSource>::New();
	sphereSource->Update();

	cone->Update();
	

	
	mapper1->SetInputConnection(sphereSource->GetOutputPort());
	mapper1->Update();

	
	mapper2->SetInputConnection(cone->GetOutputPort());
	mapper2->Update();


	actor1->SetMapper(mapper1);
	
	actor2->SetMapper(mapper2);

	vtkSmartPointer<vtkRenderer> ren = vtkSmartPointer<vtkRenderer>::New();
	ren->AddActor(actor1);
	ren->AddActor(actor2);
	

	
	vtkSmartPointer<vtkInteractorStyleTrackballCamera> style =
vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New();
	
	iren->SetInteractorStyle(style);
	renWin->AddRenderer(ren);
	renWin->SetInteractor(iren);
	

	ren->SetBackground(0.3,0.6,0.3);

	vtkSmartPointer<vtkCallbackCommand> KeyPressCallback =
vtkSmartPointer<vtkCallbackCommand>::New();
	KeyPressCallback->SetCallback(&MyCallbackFunc);


	iren->AddObserver(vtkCommand::KeyPressEvent,KeyPressCallback);


	renWin->Render();
	iren->Initialize();
	iren->Start();
}

        Thanks in advance !

Zhq



--
View this message in context: http://vtk.1045678.n5.nabble.com/I-have-some-problems-when-deal-with-key-press-callback-tp5729478.html
Sent from the VTK - Users mailing list archive at Nabble.com.


More information about the vtkusers mailing list