[vtkusers] Win32 OffScreen Rendering and Visibility cell selector

David E DeMarle dave.demarle at kitware.com
Fri Mar 23 12:22:32 EDT 2007


Problem 2 probably isn't a bug. The red book says that, "OpenGL is not a
pixel-exact specificaiton" and for this reason the exact pixels and thus
cells returned by the selector may vary. The TestAreaSelection test can
produce slightly different images on Mesa and a graphics card because of
this for example.

Problem 1 probably is a bug and I'll take a look.

On 3/23/07, Lim, Theodore <T.Lim at hw.ac.uk> wrote:
>
>  Hi David, and fellow vtk-er's,
>
> I've made a test to check the changes. What i've done is to create a
> bounding sphere that encompass a test object.  For each long-lat
> intersection of the sphere source, I re-locate the current camera position
> to a long-lat intersection and set the camera's focal point to the center of
> the bounding sphere. Once the camera is in its new position, it is reset so
> as to view the whole object. Then the visible test is performed.
>
> The OffScreen works great but now i have a problem with OnScreen
> rendering. I've attached my code below.
>
> Test subject:
>
> Using STL model - 424000-IDGH.stl (located in vtkData branch)
>
> Problem 1:
>
> When  renWin->SetOffScreenRendering(0) , the first time through the 'for'
> loop (i.e. when i = 0), vtkVisibleCellSelector works fine. The number of
> cells returned is 2114 (based on the window size 800x800).
>
> Then, for any value of i > 0, vtkVisibleCellSelector always returns 1
> cell. Why??
>
> Problem 2:
>
> When  renWin->SetOffScreenRendering(1), and i = 0, the number of cells
> returned is 2116. Shouldn't this be the same as when
> renWin->SetOffScreenRendering(0), i.e. return 2114 cells??
>
> For any value i > 0, there are always cells being returned (Yay!). Some
> results: When i = 1 (3448 cells), i = 2 (2574 cells). No complaints here,
> i'm getting cells back eventhough there are a couple extra (perhaps?).
>
> Would appreciate if you or anyone else can spot the errors i'm getting and
> how to solve it.
>
> Many thanks in advance,
> Theo.
>
>
> //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
>
> // TestVCellSelector.cpp : Defines the entry point for the console
> application.
> //
> #include <stdio.h>
> #include <stdlib.h>
> #include <iostream>
> #include <fstream>
> #include <tchar.h>
> #include "vtkRenderer.h"
> #include "vtkRenderWindow.h"
> #include "vtkRenderWindowInteractor.h"
> #include "vtkSphereSource.h"
> #include "vtkPolyDataMapper.h"
> #include "vtkActor.h"
> #include "vtkInteractorStyleTrackballCamera.h"
> #include "vtkCallbackCommand.h"
> #include "vtkVisibleCellSelector.h"
> #include "vtkSelection.h"
> #include "vtkExtractSelectedPolyDataIds.h"
> #include "vtkIdTypeArray.h"
> #include "vtkCamera.h"
> #include "vtkSmartPointer.h"
> #include "vtkSTLReader.h"
> #include "vtkProperty.h"
> #include "vtkPoints.h"
> #include "vtkWin32OpenGLRenderWindow.h"
> #include "vtkWindowToImageFilter.h"
> #include "vtkJPEGWriter.h"
> #include "vtkUnsignedCharArray.h"
> using namespace std;
> int main()
> {
>  // Standard rendering classes
>  vtkSmartPointer<vtkRenderer> renderer =
> vtkSmartPointer<vtkRenderer>::New();
>  // set up the view
>  renderer->GetActiveCamera()->SetViewUp(0, 1, 0);
>  renderer->SetBackground(0.0,0.0,0.0);
>  // set render window
>  vtkSmartPointer<vtkRenderWindow> renWin =
> vtkSmartPointer<vtkRenderWindow>::New();
>  renWin->AddRenderer(renderer);
>  renWin->SetSize(800,800);
>  /***********************************/
>  // In 'for' loop below:
>  // Works great offscreen with vtkVisibleCellSelector (huzzahhh!)
>  //renWin->SetOffScreenRendering(1);
>  // Works 1st time when set onscreen, faults after that (returns only 1
> cell -- why??!!).
>  renWin->SetOffScreenRendering(0);
>  /***********************************/
>  // set interactor
>  vtkSmartPointer<vtkRenderWindowInteractor> iren =
> vtkSmartPointer<vtkRenderWindowInteractor>::New();
>  iren->SetRenderWindow(renWin);
>  //use the trackball camera interactor style
>  vtkSmartPointer<vtkInteractorStyleTrackballCamera> style =
> vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New();
>  iren->SetInteractorStyle(style);
>  ////////////////////////////////////////////////////////////
>  // load test part
>  vtkSmartPointer<vtkSTLReader> reader =
> vtkSmartPointer<vtkSTLReader>::New();
>  reader->SetFileName("C:/VTKData/Data/42400-IDGH.stl");
>  // set on-screen mapper
>  vtkSmartPointer<vtkPolyDataMapper> map1 =
> vtkSmartPointer<vtkPolyDataMapper>::New();
>  map1->SetInput(reader->GetOutput());
>  map1->Update();
>  // set on-screen actor
>  vtkSmartPointer<vtkActor> act1 = vtkSmartPointer<vtkActor>::New();
>  act1->SetMapper(map1);
>  renderer->AddActor(act1);
>  // make a scaning sphere template
>  int res = 10;
>  double diagonal = act1->GetLength();
>  double *center = act1->GetCenter();
>  vtkSmartPointer<vtkSphereSource> S =
> vtkSmartPointer<vtkSphereSource>::New();
>  S->SetThetaResolution(res);
>  S->SetPhiResolution(res);
>  S->SetRadius(diagonal);
>  S->SetCenter(center);
>  S->Update();
>  // Get longLat intersect points
>  vtkSmartPointer<vtkPoints> pts = S->GetOutput()->GetPoints();
>  // Number of visible cells
>  vtkIdType numCells;
>  // Set visible cell selector
>  vtkSmartPointer<vtkVisibleCellSelector> sel1 =
> vtkSmartPointer<vtkVisibleCellSelector>::New();
>  sel1->SetRenderer(renderer);
>  sel1->SetRenderPasses(0,1,0,1,1);
>  int *size = renderer->GetSize();
>  sel1->SetArea(size[0],size[1],size[2],size[3]);
>  // Get visible cell scan at each long-Lat intersection
>  for (int i = 0; i < pts->GetNumberOfPoints(); i++)
>  {
>   // set camera position at long-lat intersection
>   renderer->GetActiveCamera()->SetPosition(pts->GetPoint(i));
>   renderer->GetActiveCamera()->SetFocalPoint(center);
>   renderer->ResetCamera();
>   // Update view when not using offscreen, so can see orientation of
> camera relative to viewed object
>   if (!renWin->GetOffScreenRendering())
>    renWin->Render();
>   // Test...
>   ////////////////////////////////////////////////////////////
>
>   // grab all visible cells in render window
>   sel1->Select();
>   // get selected ids
>   vtkSelection *res1 = vtkSelection::New();
>   sel1->GetSelectedIds(res1);
>   // extract cell id list
>   vtkSelection *cellids = res1->GetChild(0);
>   // get number of visible cells
>   vtkIdTypeArray *idArray =
> vtkIdTypeArray::SafeDownCast(cellids->GetSelectionList());
>   numCells =
> idArray->GetNumberOfComponents()*idArray->GetNumberOfTuples();
>   ////////////////////////////////////////////////////////////
>   res1->Delete();
>  }
>  // render
>  renWin->Render();
>  // start
>  iren->Initialize();
>  iren->Start();
>  return 0;
> }
>
> ------------------------------
> *From:* David E DeMarle [mailto:dave.demarle at kitware.com]
> *Sent:* Thu 22/03/2007 19:12
> *To:* Lim, Theodore
> *Cc:* vtk-users
> *Subject:* Re: [vtkusers] Win32 OffScreen Rendering and Visibility cell
> selector
>
> I've just committed a change to vtkOpenGLRenderWindow which appears to fix
> it. The change is to return default values if the window isn't mapped like
> getColorDepth does.
>
> cheers
> Dave DeMarle
>
> On 3/22/07, David E DeMarle <dave.demarle at kitware.com> wrote:
> >
> > Hmmm, the program looks ok to me, with the exception of two calls to a
> > "render" instead of "ofsrender".
> > I will try cell selection with offscreen rendering on windows and see if
> > I can reproduce the problem.
> >
> > cheers
> > Dave DeMarle
> >
> >
> >  On 3/20/07, Lim, Theodore < T.Lim at hw.ac.uk> wrote:
> >
> > >   Hi,
> > >
> > > Not sure how to put this but here goes...
> > >
> > > I'm trying to get get visible cell data for a series of camera
> > > positions and would like to do this using the Win32 offscreen rendering
> > > capabilities.
> > >
> > > However, i discovered that the offscreen render window color buffer
> > > size returns all 0 values. This seems to be the reason why the visible cell
> > > selector fails. Is there a way to set (or force) values into it?
> > >
> > > Or rather, can the new class vtkVisibilityCellSelector be used in
> > > Offscreen rendering mode? And if so, how can this be achieved. I've attached
> > > the code which i'm testing below.
> > >
> > > One note: i compiled with cmake option VTK_USE_OFFSCREEN set to 'Off'
> > > as this caused the vtkMFC examples (i.e. vtkDLG, vtkSDI, vtkMDI) to
> > > crash. It also caused the same response to my own VTK-MFC app. Not sure if
> > > this has anything to do with it not being able to use Offscreen. In the test
> > > code below, i use the vtkWindowToImageFilter to grab a snapshot of the
> > > offscreen render window and the results was as expected.
> > >
> > > Any help would be greatly appreciated.
> > >
> > > Many thanks, Theo.
> > >
> > >
> > > /**************************************************************************************************/
> > > int main()
> > > {
> > >  //Load model
> > >  vtkSmartPointer<vtkSTLReader> reader =
> > > vtkSmartPointer<vtkSTLReader>::New();
> > >  reader->SetFileName("C:/Model/Data/MillCut.stl");
> > >  vtkSmartPointer<vtkPolyDataMapper> map1 =
> > > vtkSmartPointer<vtkPolyDataMapper>::New();
> > >  map1->SetInput(reader->GetOutput());
> > >
> > >  // set up offscreen renderer
> > >  vtkSmartPointer<vtkRenderer> ofsrenderer =
> > > vtkSmartPointer<vtkRenderer>::New();
> > >  ofsrenderer->SetBackground(0.0,0.0,0.0);
> > >  vtkSmartPointer<vtkRenderWindow> ofsrenWin =
> > > vtkSmartPointer<vtkRenderWindow>::New();
> > >  ofsrenWin->AddRenderer(ofsrenderer);
> > >  ofsrenWin->SetSize(800,800);
> > >  ofsrenWin->SetOffScreenRendering(1);
> > >  // set interactor
> > >  vtkSmartPointer<vtkRenderWindowInteractor> iren =
> > > vtkSmartPointer<vtkRenderWindowInteractor>::New();
> > >  iren->SetRenderWindow(ofsrenWin);
> > >
> > >  //set up the view
> > >  renderer->GetActiveCamera()->SetViewUp(0, 1, 0);
> > >  renderer->SetBackground(0.0,0.0,0.0);
> > >
> > >
> > >  // set actor
> > >  vtkSmartPointer<vtkActor> act1 = vtkSmartPointer<vtkActor>::New();
> > >  act1->SetMapper(map1);
> > >  // add actor to renderer
> > >  double diagonal = act1->GetLength();
> > >  double *center = act1->GetCenter();
> > >  ofsrenderer->AddActor(act1);
> > >  ofsrenderer->GetActiveCamera()->SetFocalPoint(center);
> > >  ofsrenderer->GetActiveCamera()->SetPosition(20, -20, -27);
> > >  ofsrenderer->ResetCamera();
> > >
> > >  ofsrenWin->Render();
> > >  //-----------------------------------------------
> > >  // check offscreen render window...
> > >  int rgba[4];
> > >  ofsrenWin->GetColorBufferSizes(rgba);
> > >  vtkSmartPointer<vtkWindowToImageFilter> wif =
> > > vtkSmartPointer<vtkWindowToImageFilter>::New();//
> > >  wif->SetInput(ofsrenWin);
> > >  wif->Update();
> > >  vtkSmartPointer<vtkJPEGWriter> jpgw =
> > > vtkSmartPointer<vtkJPEGWriter>::New();
> > >  jpgw->SetInput(wif->GetOutput());
> > >  jpgw->SetQuality(100);
> > >  jpgw->SetFileName("D:/Projects/VTK/testoffscreen.jpg");
> > >  jpgw->Write();
> > >  //-----------------------------------------------
> > >
> > >  // get visible cells
> > >  vtkSmartPointer<vtkVisibleCellSelector> sel1 =
> > > vtkSmartPointer<vtkVisibleCellSelector>::New();
> > >  sel1->SetRenderer(ofsrenderer);
> > >         // grab everything in the render window viewport
> > >  int *size = ofsrenderer->GetRenderWindow()->GetSize();
> > >  sel1->SetRenderPasses(0,1,0,1,1);
> > >  sel1->SetArea(size[0],size[1],size[2],size[3]);
> > >  sel1->Select();
> > >  vtkSmartPointer<vtkSelection> res1 =
> > > vtkSmartPointer<vtkSelection>::New();
> > >  sel1->GetSelectedIds(res1);
> > >  vtkSmartPointer<vtkSelection> cellids = res1->GetChild(0);
> > >  vtkSmartPointer<vtkExtractSelectedPolyDataIds> extr =
> > > vtkSmartPointer<vtkExtractSelectedPolyDataIds>::New();
> > >         if (cellids)
> > >          {
> > >           extr->SetInput(1, act1->GetMapper()->GetInput());
> > >           extr->SetInput(0, cellids);
> > >           extr->Update();
> > >           sMap->SetInput(extr->GetOutput());
> > >          }
> > >         else
> > >          {
> > >           cerr << "Empty color buffer selection -" << endl;
> > >           cerr << "Check display color depth. Must be at least 24
> > > bit." << endl;
> > >           sMap->SetInput(emptyPD);
> > >          }
> > >  ////////////////////////////////////////////////////////////
> > >  ofsrenWin->Render();
> > >
> > >  // Set the user method (bound to key 'u')
> > >  iren->Initialize();
> > >  iren->Start();
> > >
> > >  return 0;
> > > }
> > >
> > >
> > >
> > > _______________________________________________
> > > This is the private VTK discussion list.
> > > Please keep messages on-topic. Check the FAQ at:
> > > http://www.vtk.org/Wiki/VTK_FAQ
> > > Follow this link to subscribe/unsubscribe:
> > > http://www.vtk.org/mailman/listinfo/vtkusers
> > >
> > >
> >
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20070323/fad9bc87/attachment.htm>


More information about the vtkusers mailing list