null in this->GetDefaultRenderer() in virtual void OnRightButtonDown()

Dear all,

I have a doubt of programming. I am trying the examples of picking points from the mouse.

I see in the virtual function of the mouse (OnRightButtonDown()) when i want to get the renderer,
it returns null.

If i do this this->GetDefaultRenderer() i get a an error. So the pointer do not add the renderer info

By now, what i did was a global variable of the mainrenderer and it is working but i want to keep my code clean and i would like to understand why i cannot get this info from this

this->GetDefaultRenderer()

However

int* pos = this->GetInteractor()->GetEventPosition();

this works

it is only the renderer.

I hope you can help me so i can remove this global variable. To continue programming it is fine but i wold like to keep the code as clean as possible.

Thanks

I attach the code i want to replace the global_renderer with this->GetDefaultRenderer().

Thanks

class customMouseInteractorStyle : public vtkInteractorStyleTrackballCamera
{
public:
static customMouseInteractorStyle* New();
vtkTypeMacro(customMouseInteractorStyle, vtkInteractorStyleTrackballCamera);

	virtual void OnRightButtonDown()
	{
		// Get the location of the click (in window coordinates)
		int* pos = this->GetInteractor()->GetEventPosition();

		vtkSmartPointer<vtkCellPicker> picker =
			vtkSmartPointer<vtkCellPicker>::New();
		picker->SetTolerance(0.0005);


		//this->GetDefaultRenderer()->Clear();

		// Pick from this location.
		picker->Pick(pos[0], pos[1], 0, global_renderer);

		double* worldPosition = picker->GetPickPosition();
		std::cout << "Cell id is: " << picker->GetCellId() << std::endl;

		if (picker->GetCellId() != -1)
		{

			std::cout << "Pick position is: " << worldPosition[0] << " " << worldPosition[1]
				<< " " << worldPosition[2] << endl;
		}

		// Forward events
		vtkInteractorStyleTrackballCamera::OnRightButtonDown();
		
	}

};

When you instantiate your custom style, make sure to set the default renderer on it using vtkInteractorStyle::SetDefaultRenderer()

Something like:

vtkNew<customMouseInteractorStyle> style;
style->SetDefaultRenderer(ren);
1 Like