[vtkusers] How to render vtkActor2D and vtkActor in different colors?

pnt1614 minpu.code at gmail.com
Fri May 11 21:36:14 EDT 2018


I want to render a 2D line using the vtkActor2D and a 3D sphere using
vtkActor in different colors. After generating a sphere with the default
color, I make a 2D blue line. At the first render, the sphere and the line
are rendered in the different colors but the line's color is changed to the
sphere's color when I rotate the camera.

                vtkSmartPointer<vtkSphereSource> s =
vtkSmartPointer<vtkSphereSource>::New();
		s->SetCenter(0.0, 0.0, 0.0);
		s->SetRadius(2.0);
		s->Update();

		vtkSmartPointer<vtkActor> sphere_actor = make_an_actor(s->GetOutput());
            
		vtkSmartPointer<vtkRenderer> renderer =
m_vtkWindow->GetRenderers()->GetFirstRenderer();
		renderer->AddActor(sphere_actor);


		//-----------------2D line------------------
		int* size = m_vtkWindow->GetSize();
		double w = (double)size[0];
		double h = (double)size[1];

		vtkSmartPointer<vtkCoordinate> coordinate1 = vtkCoordinate::New();
		coordinate1->SetCoordinateSystemToDisplay();
		coordinate1->SetValue(0.0, h / 2);
		double* p1 = coordinate1->GetComputedDoubleDisplayValue(renderer);

		vtkSmartPointer<vtkCoordinate> coordinate2 = vtkCoordinate::New();
		coordinate2->SetCoordinateSystemToDisplay();
		coordinate2->SetValue(w, h / 2);
		double* p2 = coordinate2->GetComputedDoubleDisplayValue(renderer);

		vtkSmartPointer<vtkLineSource> line =
vtkSmartPointer<vtkLineSource>::New();
		line->SetPoint1(p1);
		line->SetPoint2(p2);
		line->Update();

		vtkSmartPointer<vtkPolyDataMapper2D> mapper =
vtkSmartPointer<vtkPolyDataMapper2D>::New();
		mapper->SetInputData(line->GetOutput());

		vtkSmartPointer<vtkActor2D> line_actor =
vtkSmartPointer<vtkActor2D>::New();
		line_actor->SetMapper(mapper);

		line_actor->GetProperty()->SetColor(0, 0, 1);
		line_actor->GetProperty()->SetLineWidth(2.0);

		renderer->AddActor2D(line_actor);



		renderer->ResetCamera();

		vtkSmartPointer<vtkInteractorStyleTrackballCamera> style =
vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New();

		vtkSmartPointer<vtkRenderWindowInteractor> interactor =
m_vtkWindow->GetInteractor();
		interactor->SetInteractorStyle(style);

		m_vtkWindow->Render();

Is there something I missing?



--
Sent from: http://vtk.1045678.n5.nabble.com/VTK-Users-f1224199.html


More information about the vtkusers mailing list