Programmatically set camera position


I want to set camera position programmatically in this way:

int wasModifying = camera->StartModify();
camera->SetPosition(cameraPosition[0], cameraPosition[1], cameraPosition[2]);
camera->SetFocalPoint(cameraFocalPointPosition[0], cameraFocalPointPosition[1], cameraFocalPointPosition[2]);
camera->SetViewUp(0, 0, 1);

which works in some cases. However, when I zoom in or out before the calls manually so that the camera position is significantly different than the position I want to set, the view stays black after the calls. Only when I start to rotate the view afterwards manually, it gets updated and shows the correct angle. Is there any way to force an update?


ViewUp vector must be orthogonal to the line that connects position and focal point. Otherwise the behavior is undefined.

Thanks for your input, however it did not solve the problem. The missing thing was to call ResetClippingRange. The code below does what it should.

camera->SetFocalPoint(cameraFocalPointPosition[0], cameraFocalPointPosition[1], cameraFocalPointPosition[2]);
camera->SetPosition(cameraPosition[0], cameraPosition[1], cameraPosition[2]);
camera->SetViewUp(0, 0, 1);

Yes, ResetClippingRange is a good point, you need that.

OrthogonalizeViewUp discards current camera ViewUp vector value. So, if you care what is up direction in the view then you need to set ViewUp properly (and don’t call OrthogonalizeViewUp); if it does not matter which way is up in the view then just call OrthogonalizeViewUp (and don’t call SetViewUp). See details in vtkCamera source code.

The camera automatically orthogonalizes any given ViewUp vector.
See VTK User’s Guide (page 50):


This is called by setViewUp(...) -> ComputeViewTransform() -> Transform->SetupCamera(...)

The call to OrthogonalizeViewUp() is required to retrieve back the orthogonalized vector from the transform for retrieving it via GetViewUp() and for the convenience methods like Pitch, Roll, Yaw, ...

That’s correct. That’s why you don’t need to call both SetViewUp() and OrthogonalizeViewUp().

Note that the built-in very simplistic orthogonalization method should only be used for small angular deviations. If view up direction is nearly parallel to the view normal then the result is undefined.

In the cited VTK User guide example the OrthogonalizeViewUp call is unnecessary, but since the user guide is not maintained anymore, we cannot submit a PR to fix it. VTK textbook is still maintained, you can find up-to-date version here.

1 Like