# vtkSphereWidget2 in vtkGenericOpenGLRenderWindow

**URL:** https://discourse.vtk.org/t/vtkspherewidget2-in-vtkgenericopenglrenderwindow/4379
**Category:** Support
**Created:** [October 8, 2020, 6:35am UTC](https://discourse.vtk.org/t/vtkspherewidget2-in-vtkgenericopenglrenderwindow/4379 "2020-10-08T06:35:47Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![mallikarjun49](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@mallikarjun49](https://discourse.vtk.org/u/mallikarjun49)
#### Post date: [October 8, 2020, 6:35am UTC](https://discourse.vtk.org/t/vtkspherewidget2-in-vtkgenericopenglrenderwindow/4379/1 "2020-10-08T06:35:47Z")

</div>

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/](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

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)
#### Post date: [October 8, 2020, 12:08pm UTC](https://discourse.vtk.org/t/vtkspherewidget2-in-vtkgenericopenglrenderwindow/4379/2 "2020-10-08T12:08:08Z")

</div>

> [vtkGenericOpenGLRenderWindow](https://vtk.org/doc/nightly/html/classvtkGenericOpenGLRenderWindow.html) 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.

---

<div class="post-metadata">

### Author: ![mallikarjun49](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@mallikarjun49](https://discourse.vtk.org/u/mallikarjun49)
#### Post date: [October 8, 2020, 2:49pm UTC](https://discourse.vtk.org/t/vtkspherewidget2-in-vtkgenericopenglrenderwindow/4379/3 "2020-10-08T14:49:41Z")

</div>

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

Best Regards,  
Mallikarjun

---

<div class="post-metadata">

### Author: ![amaclean](https://discourse.vtk.org/user_avatar/discourse.vtk.org/amaclean/32/224_2.png) [@amaclean](https://discourse.vtk.org/u/amaclean)
#### Post date: [October 8, 2020, 8:47pm UTC](https://discourse.vtk.org/t/vtkspherewidget2-in-vtkgenericopenglrenderwindow/4379/4 "2020-10-08T20:47:39Z")

</div>

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](https://discourse.vtk.org/uploads/default/original/2X/2/27b2e5f7813add7e412bd72b69307ac3db9b1e71.png)

and output on the console like this:

````nohighlight
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)```
````

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)
#### Post date: [October 8, 2020, 11:51pm UTC](https://discourse.vtk.org/t/vtkspherewidget2-in-vtkgenericopenglrenderwindow/4379/5 "2020-10-08T23:51:08Z")

</div>

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

---

<div class="post-metadata">

### Author: ![amaclean](https://discourse.vtk.org/user_avatar/discourse.vtk.org/amaclean/32/224_2.png) [@amaclean](https://discourse.vtk.org/u/amaclean)
#### Post date: [October 9, 2020, 5:02am UTC](https://discourse.vtk.org/t/vtkspherewidget2-in-vtkgenericopenglrenderwindow/4379/6 "2020-10-09T05:02:51Z")

</div>

Ok!
