vtkSphereWidget2 in vtkGenericOpenGLRenderWindow

Hi,
I am trying to use vtkGenericOpenGLRenderWindow instead of vtkRenderWindow in the example given for VtkSphereWidget2(https://lorensen.github.io/VTKExamples/site/Cxx/Widgets/SphereWidget2/) ,I don’t see any renderWindow when I run the executable.

#include <vtkCommand.h>
#include <vtkMath.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkSmartPointer.h>
#include <vtkSphere.h>
#include <vtkSphereRepresentation.h>
#include <vtkSphereWidget2.h>
#include <vtkGenericOpenGLRenderWindow.h>

// Callback that displays the sphere widget's spherical handle postion
// in both sphercial (relative to the widget's center) and cartesian global coordinates
class vtkSphereCallback : public vtkCommand
{
public:
  static vtkSphereCallback *New()
  {
    return new vtkSphereCallback;
  }
virtual void Execute(vtkObject *caller, unsigned long, void*)
  {
      vtkSphereWidget2 *sphereWidget =
      reinterpret_cast<vtkSphereWidget2*>(caller);

    double center[3], handlePosition[3];
    vtkSphereRepresentation* sphereRepresentation =
      dynamic_cast<vtkSphereRepresentation*>( sphereWidget->GetRepresentation() );
    sphereRepresentation->GetHandlePosition( handlePosition );
    sphereRepresentation->GetSphere( this->Sphere );

    this->Sphere->GetCenter( center );

    double radius = sqrt( static_cast<double>(vtkMath::Distance2BetweenPoints( center, handlePosition ) ) );
    radius = (radius <= 0.0 ? 1.0 : radius );
    double theta = vtkMath::DegreesFromRadians( atan2( ( handlePosition[1] - center[1] ), ( handlePosition[0] - center[0] ) ) );
    double phi   = vtkMath::DegreesFromRadians( acos( ( handlePosition[2] - center[2] ) / radius ) );

    std::cout << "r, theta, phi: ("
              << std::setprecision(3)
              << radius << ", " << theta << ", " << phi << ") "
              << "x, y, z: ("
              << handlePosition[0] << ", "
              << handlePosition[1] << ", "
              << handlePosition[2] << ") "
              << std::endl;
  }

  vtkSphereCallback(){ this->Sphere = vtkSphere::New(); }
  ~vtkSphereCallback(){ this->Sphere->Delete(); }

  vtkSphere* Sphere;
};

int main( int, char *[] )
{
  // Create a renderer and a render window
  vtkSmartPointer<vtkRenderer> renderer =
    vtkSmartPointer<vtkRenderer>::New();
  //vtkSmartPointer<vtkRenderWindow> renderWindow =
   // vtkSmartPointer<vtkRenderWindow>::New();
vtkSmartPointer<vtkGenericOpenGLRenderWindow> renderWindow = vtkSmartPointer<vtkGenericOpenGLRenderWindow>::New();

  renderWindow->AddRenderer(renderer);

  // Create an interactor
  vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
    vtkSmartPointer<vtkRenderWindowInteractor>::New();
  renderWindowInteractor->SetRenderWindow(renderWindow);

  // Create a sphere widget
  vtkSmartPointer<vtkSphereWidget2> sphereWidget =
    vtkSmartPointer<vtkSphereWidget2>::New();
  sphereWidget->SetInteractor(renderWindowInteractor);
  sphereWidget->CreateDefaultRepresentation();

  vtkSphereRepresentation* sphereRepresentation =
    dynamic_cast<vtkSphereRepresentation*>( sphereWidget->GetRepresentation() );
  sphereRepresentation->HandleVisibilityOff();
  sphereRepresentation->RadialLineOff();
  sphereRepresentation->HandleTextOff();

  vtkSmartPointer<vtkSphereCallback> sphereCallback =
    vtkSmartPointer<vtkSphereCallback>::New();

  sphereWidget->AddObserver( vtkCommand::InteractionEvent, sphereCallback );

  renderWindow->Render();
  

  renderWindowInteractor->Initialize();
  renderWindow->Render();
  sphereWidget->On();
 
  // Begin mouse interaction
  renderWindowInteractor->Start();

  return EXIT_SUCCESS;
}

Could someone help me with this?

Thank you.

Best Regards,
Mallikarjun

vtkGenericOpenGLRenderWindow provides a skeleton for implementing a render window using one’s own OpenGL context and drawable

This means that vtkGenericOpenGLRenderWindow is not a complete implementation, but only a starting point for implementing your own render window/widget. For example, it is used as a base class for Qt render widgets.

I see…Thank you @lassoan for the reply. I will add this renderwindow to QVTKOpenGLNativeWidget and see.

Best Regards,
Mallikarjun

I am not sure what you mean by not seeing a render window. I just checked and the render window exists and the callback works OK. If you move the little sphere around in the render window, you get an image like this: Screenshot-SphereWidget2

and output on the console like this:

r, theta, phi: (0.25, 96.8, 28.1) x, y, z: (-0.0139, 0.117, 0.221)
r, theta, phi: (0.25, 96.7, 28.5) x, y, z: (-0.014, 0.119, 0.22)
r, theta, phi: (0.25, 97.2, 29.5) x, y, z: (-0.0154, 0.122, 0.218)
r, theta, phi: (0.25, 97.9, 29.5) x, y, z: (-0.0168, 0.122, 0.218)
r, theta, phi: (0.25, 97.8, 29.9) x, y, z: (-0.0168, 0.124, 0.217)```

He replaced vtkRenderWindow by vtkGenericOpenGLRenderWindow, which broke the example (render window did not appear, as expected).

Ok!