hi Christophe,

> 1) it seems that the aspect ratio used for the rendering is based on the
> ratio of the form size.
About your first problem,when i reuse code of  osgviewerMFC ,
I met the same problem.
I found  in  osgViewer::Viewer's base class osg::view 's constructor ,
it'll new a master camera and set ProjectionMatrix with screen ratio.
and when you add camera with  addSlave() , it will call the updateSlave(),
and set ProjectionMatrix using master camera' projection.

so my solution is set projection matrix to own camera,
and using setCamera() to set master camera  instead of using addSlave().
i am not sure it is a good solution or not, but i works.

by the way, i use  osg 2.2 .
Hope it also works with osg2.0.

some code from osg::View
  View::View(): Object(true)
  {
    ......
   _camera = new osg::Camera;
   _camera->setView(this);

   double height = osg::DisplaySettings::instance()->getScreenHeight();
   double width = osg::DisplaySettings::instance()->getScreenWidth();
   double distance = osg::DisplaySettings::instance()->getScreenDistance();
   double vfov = osg::RadiansToDegrees(atan2(height/2.0f,distance)*2.0);
   _camera->setProjectionMatrixAsPerspective( vfov, width/height, 
1.0f,10000.0f);
   _camera->setClearColor(osg::Vec4f(0.2f, 0.2f, 0.4f, 1.0f));
     ....
  }

  void View::updateSlave(unsigned int i)
  {
   if (i >= _slaves.size() || !_camera) return;

   Slave& slave = _slaves[i];

   if (slave._camera->getReferenceFrame()==osg::Transform::RELATIVE_RF)
   {
    slave._camera->setProjectionMatrix(_camera->getProjectionMatrix() * 
slave._projectionOffset);
    slave._camera->setViewMatrix(_camera->getViewMatrix() * 
slave._viewOffset);
   }

   slave._camera->inheritCullSettings(*_camera);
   if (slave._camera->getInheritanceMask() & osg::CullSettings::CLEAR_COLOR) 
slave._camera->setClearColor(_camera->getClearColor());
  }

Miao

----- Original Message ----- 
From: "LOREK Christophe" <[EMAIL PROTECTED]>
To: <[email protected]>
Sent: Friday, November 30, 2007 12:10 AM
Subject: [osg-users] OsgViewer in form, aspect ratio issue and error on 
close


> hi everyone,
>
> I'm new to this mailing list, although I have been using OpenSceneGraph
> for a couple of monthes.
>
> I'm using the OsgViewer from OSG 2.0 and I would like to have it use a
> .NET form for display (using managed C++, not C#). I followed  the
> advice given by Glenn Waldron in his reply to Romain Blanchais on the
> 20th of November, where he gave the following sample code :
>
> HWND hwnd = (HWND)control->Handle.ToPointer();
>
> osg::ref_ptr<osg::GraphicsContext::Traits> traits =
>        new osg::GraphicsContext::Traits();
>
> traits->inheritedWindowData = new
> osgViewer::GraphicsWindowWin32::WindowData( hwnd );
> traits->setInheritedWindowPixelFormat = true;
> traits->doubleBuffer = true;
> traits->windowDecoration = false;
> traits->sharedContext = 0;
> traits->supportsResize = true;
>
> RECT rect;
> GetWindowRect( hwnd, &rect );
> traits->x = 0;
> traits->y = 0;
> traits->width = rect.right - rect.left;
> traits->height = rect.bottom - rect.top;
>
> osg::ref_ptr<osg::GraphicsContext> gc =
> osg::GraphicsContext::createGraphicsContext(
>        traits.get() );
>
> viewer = new osgViewer::Viewer();
>
> osg::ref_ptr<osg::Camera> cam = new osg::Camera();
> cam->setGraphicsContext( gc.get() );
> cam->setViewport( new osg::Viewport( traits->x, traits->y,
> traits->width, traits->height ) );
> viewer->addSlave( cam.get() );
>
> I have succeeded in obtaining a rendering of the OsgViewer in my form,
> but I have the following issues :
>
> 1) it seems that the aspect ratio used for the rendering is based on the
> ratio of the form size.
> This is troublesome as I would expect to be able to set the aspect ratio
> according to the screen, and not according to the form size. the problem
> is even more obvious when my form is created with a width >> height or
> height >> width.
> In those case, the rendering is squeezed. I tried to override the
> projection matrix of the camera used. I also tried to call windowResize
> on the EventQueue of the Viewer to ensure that it is kept informed of
> the size, but it didn't resolve the aspect ratio problem.
>
> I tried to figure out from the source code of the OsgViewer how it was
> dealing with the FOV settings of the camera, but it seemed a bit
> confusing because I couldn't see the point where the camera actually
> receive a proper projection matrix (I used AddSlave, like in the given
> sample, to add the camera to the viewer, and this mean identity matrices
> are passed as projection matrix and as view matrix, so it must be
> overriden at some point, but I don't know where).
>
> What is the proper way of using OsgViewer in an external window and have
> it set the camera parameters properly, or providing it proper camera
> parameters ?
>
>
> 2) when closing the form, the OsgViewer is informed of the closing of
> the windows, but can't do the things to shut down properly. I get the
> following errors :
>
> Windows Error #6: [Screen #0]
> GraphicsWindowWin32::releaseContextImplementation() - Unable to release
> current OpenGL rendering context. Reason: The handle is invalid.
>
> Windows Error #1400: [Screen #0]
> GraphicsWindowWin32::unregisterWindowProcedure() - Unable to unregister
> window procedure. Reason: Invalid window handle.
>
> what is the proper way of disposing of the OsgViewer and the form, so
> that it can close without errors ?
>
>
> Thanks for any assistance
>
> C.L
>
>
> _______________________________________________
> osg-users mailing list
> [email protected]
> http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
> 

_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to