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
osg-...@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
> 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
_camera->setClearColor(osg::Vec4f(0.2f, 0.2f, 0.4f , 1.0f));
I need to fix this ASAP. I am using OSG 2.4+ with old Producer Config
files. OSG creates slave cameras for my multiple window configuration (one
for each). The resulting settings are not quite like specified in my
configuration file. Field of Views are mostly OK, but it is altering the
aspect ratios of the slave cams and providing me with an image slightly
off from how it should be. I've tried setting the Slave cams' projection
matrix, (setProjectionMatrixAsPerspective) with particular aspect ratios
etc, but the aspect ratio value is being overriden somewhere and adjusted
a little bit. However, the Master cam's aspect ratio is correct. How do I
correctly set the Slave cam's aspect ratios?? and Why are the Producer
Configuration files not doing what they used to in OSG 1.2 ?? Anyone
please let me know soon, THANKS!
-JCOLON
Could you provide a Producer .cfg that you are having problem with.
Could you also please specify which versions of the OSG you've tried.
Robert.
Here is the Producer config file I am using:
*******************************************************
Camera "Camera-1"
{
RenderSurface "Window-1"
{
Visual { SetSimple }
Screen 0;
WindowRect 0 30 426 341;
Border on;
InputRectangle -1.0 1.0 -1.0 1.0;
}
Offset {
Rotate 42.4 0 1 0;
}
}
Camera "Camera-2"
{
RenderSurface "Window-2"
{
Visual { SetSimple }
Screen 0;
WindowRect 426 30 426 341;
Border on;
InputRectangle -1.0 1.0 -1.0 1.0;
}
Offset {
Rotate 0.0 0 1 0;
}
}
Camera "Camera-3"
{
RenderSurface "Window-3"
{
Visual { SetSimple }
Screen 0;
WindowRect 852 30 426 341;
Border on;
InputRectangle -1.0 1.0 -1.0 1.0;
}
Offset {
Rotate -42.4 0 1 0;
}
}
InputArea
{
RenderSurface "Window-1" ;
RenderSurface "Window-2" ;
RenderSurface "Window-3" ;
}
*******************************************************
I have tested and compared it with OSG 2.4 and OSG 1.2 with (very)
slightly different results. I need the results to be exactly the same as
in OSG 1.2.
My understanding is that the "InputArea" is no longer required, but is is
still there for backwards compatibility with OSG 1.2 . For each
RenderSurface block I used to have a Lens configuration block like this
one:
Lens {
Perspective 42.4 45.0 1.0 100.0;
}
but I no longer use it because I set the parameters in my code as:
vwr.getCamera()->setProjectionMatrixAsPerspective(VerticalFOV, AspectRatio,
NearClip, FarClip); //for the master cam.
I've tried doing it for each of the Slave cams as well:
vwr.getSlave(int i)._camera->setProjectionMat..... (same thing) . but it
doesn't change anything.
In this example:
AspectRatio = 1.33
VerticalFOV = 42.4/AspectRatio
NearClip = doesn't matter right now
FarClip = doesn't matter right now
Whe I run my program (version OSG 2.4) I get the following runtime values:
Master cam: (fov, aspect, near, far)31.807951987997, 1.333, 0.02, 199.9999
Slave cam # 0 (fov, aspect, near, far): 31.8079530956935,
1.31249230769231, 305.702400933255, 70223.0343790709
Slave cam # 1 (fov, aspect, near, far): 31.8079530956935,
1.31249230769231, 443.807876894962, 43394.6435810508
Slave cam # 2 (fov, aspect, near, far): 31.8079530956935,
1.31249230769231, 292.024441391996, 54427.4475917436
You may ignore the Near/Far values. You can notice how the slave cams
manipulated the aspect ratio slightly. The VerticalFOV seems right.
When I run OSG 1.2 I get this:
(no master cam concept)
camera 0 FOV(h,v) = 42.4,31.808
aspect_ratio : 1.36129
camera 1 FOV(h,v) = 42.4,31.808
aspect_ratio : 1.36129
camera 2 FOV(h,v) = 42.4,31.808
aspect_ratio : 1.36129
I don't know how the aspect ration became that value either, because the
aspect ratio should have been forced to 1.33. Anyways this works fine.
I'd like to get the OSG 1.2 results, although none of these numbers quite
make sense to me, and the corresponding values of each version are very
close. I'll also provide the projection matrices which are a bit off as
well:
OSG2.4:
Slave cam 0:
projectionMatrix : {
2.67399874028772 0 0 0
0 3.50960277740655 0 0
0 0 -1.00874468153415 -1
0 0 -614.078072006898 0
}
Slave cam 1:
projectionMatrix : {
2.67399874028772 0 0 0
0 3.50960277740655 0 0
0 0 -1.02066585525608 -1
0 0 -896.787423135345 0
}
Slave cam2:
projectionMatrix : {
2.67399874028772 0 0 0
0 3.50960277740655 0 0
0 0 -1.01078866385069 -1
0 0 -587.199436318357 0
}
What they should be: (OSG 1.2):
Cam 0:
2.57815,0,0,0
0,3.5096,0,0
0,0,-1,-1
0,0,-20,0
Cam 1:
2.57815,0,0,0
0,3.5096,0,0
0,0,-1,-1
0,0,-20,0
Cam2:
2.57815,0,0,0
0,3.5096,0,0
0,0,-1,-1
0,0,-20,0
(all the same). In OSG 2.4, if I hard-code my Master cam's
projectionMatrix to be like the one printed out in OSG 1.2 I get the good
image result.
The big question is: What parameter is off? Why? and How do I fix it?
Any help with anything here mentioned will be VERY welcomed!!
Thanks,
-Jose
I tried the .cfg out and it works, but since osgviewer cow.osg -c
your.cfg doesn't contain your specific master projection matrix nor
have your profiling code I can't really test what your are seeing at
your end.
Given I'm busy working getting a 2.6 rc1 out the door, and am away
from tomorrow there is little time I have to further chase this up.
As a general note, the slave projection matrices are update on each
frame by multiplying the master camera's projection matrix by the
slave projection offset matrix. This means updates to the slaves
projection matrix will be overwritten, which is why you aren't seeing
these changes.
As to why you are seeing different values, I'm afraid I can't give a
specific reason for this, one would have to look at your specific code
against osgProducer and against osgViewer, and these both cases so you
can see it first hand and profile the values.
Robert.
On Thu, Jul 24, 2008 at 10:57 PM, Joseanibal Colon Ramos
Thanks, you are right. I tested my config file separately and it worked
fine. I re-included the
Lens {
Perspective 42.4 45.0 1.0 100.0;
}
in the configuration file and avoided messing with the FOV, aspect ratio
parameters inside my code by avoiding making calls to
"setProjectionMatrixAsPerspective(*)" with conflicting values. I allow OSG
to do its job now and it is doing the right thing. I am getting the same
results I used to get on OSG 1.2 Perfect. Thanks for the OSG viewer hint.
-J