I have a mesh model and, using VTK, have rendered a view of it from a given camera position (x,y,z). I can save this to an RGB image (640x480) but I also want to save a depth map where each pixel stores the value of the depth from the camera.
I have tried using the Zbuffer
values given by the render window by following this example. The problem is that the Zbufer
only stores values in the range [0,1]. Instead I am trying to create synthetic range image, where I store the depth/distance of each pixel from the camera. Analogous to the image produced by the Kinect, I am trying to create one from a specific viewpoint of a mesh model.
EDIT - adding some code
My current code:
Load the mesh
string mesh_filename = "mesh.ply";
vtkSmartPointer<vtkPLYReader> mesh_reader = read_mesh_ply(mesh_filename);
vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputConnection(mesh_reader->GetOutputPort());
vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
actor->SetMapper(mapper);
vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(renderer);
renderWindow->SetSize(640, 480);
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderWindowInteractor->SetRenderWindow(renderWindow);
//Add the actors to the scene
renderer->AddActor(actor);
renderer->SetBackground(1, 1, 1);
Create a camera and place it somewhere
vtkSmartPointer<vtkCamera> camera = vtkSmartPointer<vtkCamera>::New();
renderer->SetActiveCamera(camera);
camera->SetPosition(0,0,650);
//Render and interact
renderWindow->Render();
Get result from the z buffer
double b = renderer->GetZ(320, 240);
In this example, this gives 0.999995. As the values are between [0,1] I don't know how to interpret this, as you can see I have set the camera to be 650 units away on the z-axis so I assume the z distance at this pixel (which is on the object in the rendered RGB) should be near to 650.