OSMesa + vtkHardwareSelector - is that possible ?

Hi everyone,

I am trying to use a vtkHardwareSelector with an OSMesa-based VTK distribution. And it does not work so far … help ! :pray:
Has anyone ever attempted this ?

I tried looking on the Paraview side, but nothing obivous jumped out when trying to get info on how PV does the selection offscreen with OSMesa.

Below are the CMake build & the piece of code that is relevant to the operation. Thank you very much in advance for your help ! :pray:


The CMake build is done like this :

cmake \
-DCMAKE_C_COMPILER=${CC} \
-DCMAKE_CXX_COMPILER=${CXX} \
-DCMAKE_C_FLAGS="-Wno-incompatible-function-pointer-types" \
-DCMAKE_CXX_FLAGS="-Wno-incompatible-function-pointer-types" \
-DCMAKE_EXE_LINKER_FLAGS="-L${LLVM_ROOT}/lib/c++ -Wl,-rpath,${LLVM_ROOT}/lib/c++" \
-DVTK_USE_COCOA=OFF \
-DVTK_USE_X=OFF \
-DVTK_OPENGL_HAS_OSMESA=ON \
-DOSMESA_INCLUDE_DIR=${MESA_ROOT}/include \
-DOSMESA_LIBRARY=${MESA_ROOT}/lib/libOSMesa.${LIB_SUFFIX} \
-DOPENGL_INCLUDE_DIR=IGNORE \
-DOPENGL_gl_LIBRARY=IGNORE \
-DOPENGL_xmesa_INCLUDE_DIR=IGNORE \
-DVTK_DEFAULT_RENDER_WINDOW_OFFSCREEN=OFF \
-DVTK_USE_OFFSCREEN=OFF \
-DBUILD_SHARED_LIBS=ON \
-DCMAKE_INSTALL_PREFIX=${CODE_DIR}/${SUB_DIR}/vtk/build/install \
${@} \
-S .. -B .

And based on multiple examples in the documentation I am using this snippet:

    _graphics_factory = vtkSmartPointer<vtkGraphicsFactory>::New();
    _graphics_factory->SetOffScreenOnlyMode(1);
    _graphics_factory->SetUseMesaClasses(1);

    _renderer = vtkSmartPointer<vtkRenderer>::New();

    _mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
    _mapper->SetInputData(const_cast<vtkPolyData*>(polydata));

    _actor = vtkSmartPointer<vtkActor>::New();
    _actor->SetMapper(_mapper);

    Vertex focal_point = centroid;
    Vertex position = (focal_point - (initial_sight_direction * _bounding_sphere_radius /
                                      std::sin(0.5 * vtkMath::RadiansFromDegrees(_field_of_view))));
    _camera = vtkSmartPointer<vtkCamera>::New();
    _camera->SetPosition(position.x(), position.y(), position.z());
    _camera->SetFocalPoint(focal_point.x(), focal_point.y(), focal_point.z());
    _renderer->SetActiveCamera(_camera);

    _render_window = vtkSmartPointer<vtkRenderWindow>::New();
    _render_window->AddRenderer(_renderer);
    _render_window->SetSize(_window_width, _window_height);
    _render_window->OffScreenRenderingOn();

    _renderer->AddActor(_actor);

    _hardware_selector = vtkSmartPointer<vtkOpenGLHardwareSelector>::New();
    _hardware_selector->SetRenderer(_renderer);
    _hardware_selector->SetArea(0, 0, _window_width - 1, _window_height - 1);
    _hardware_selector->SetFieldAssociation(vtkDataObject::FIELD_ASSOCIATION_CELLS);

    _render_window->Render();

    // Pick the visible cells
    vtkSelection* selection = _hardware_selector->Select();
    vtkSelectionNode* selection_node = selection->GetNode(0);
    vtkIdTypeArray* selection_ids =
            vtkIdTypeArray::SafeDownCast(selection_node->GetSelectionList());

    std::vector<vtkIdType> visible_cells;
    visible_cells.reserve(selection_ids->GetNumberOfTuples());
    for (vtkIdType i = 0; i < selection_ids->GetNumberOfTuples(); ++i) {
        visible_cells.push_back(selection_ids->GetValue(i));
    }

    return visible_cells;

@tbridel As long as you’ve built with osmesa, you don’t need to setup the graphics factory. Try removing the following lines:

Also, you should be fine with using vtkHardwareSelector directly instead of vtkOpenGLHardwareSelector i.e.

_hardware_selector = vtkSmartPointer<vtkHardwareSelector>::New();

Hi @sankhesh

So sorry for the delayed answer, I was on another branch for a while.
I came back to it and did as you suggested (removing the graphics factory, using only a vtkHardwareSelector).
The use case I am playing with works without any problems when vtk is not compiled with Mesa, but when it is, I get that

vtkSelection *selection = _hardware_selector->Select();

is not a nullptr, but all

selection->GetNode(0)
selection->GetNode(1)
etc...

are nullptr. Any idea what could be happening ?


I am building OSMesa with the following attached script - I did not find many pointers online, so maybe something is wrong there too. I am open to any advice about this as well :pray: .

build_mesa_darwin.zsh (3.2 KB)