Hello all,

I am trying to implement a HUD with a crosshair in a QT window.
I have used the osghud.cpp and QTViewer examples for this.
It seems like I am not setting the correct projection matrix in my HUD camera.

In my project I have 2 cameras (as in the osghud.cpp example):
The first, looking at a scene with some objects in it (a surface with a 
projected image on it)
And the second, for the HUD.

Here is my code:


Code:
// somewhere in the code I create a Qt dialog, it will be the holder for the 
graphics window
QWidget* qWidget = new QDialog;
qWidget->resize(640, 480);
qWidget->setLayout(new QVBoxLayout());

...
...

Then, In the constructor of QtViewerWidget (QtViewerWidget is in the example)
I do:

QLayout* parentLayout = parent->layout(); // this is the layout of the dialog 
from above
parentLayout->addWidget(this);

// now I create a HUD camera:
hudCamera = new osg::Camera();

// These numbers are arbitrary, I wasn't sure what value I should put here.
// In the example, they put the size of the screen, such that every location in 
the world seen
// by the hudCamera will be the corresponding pixel on the screen.
// Since I am using a QtViewerWidget, which is a dialog, I am not sure what 
size to set the projection for the viewing volume
hudCamera->setProjectionMatrix(osg::Matrix::ortho2D(0,640,0,480));

// now I set the HUD camera to overlay and be rendered last
hudCamera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
hudCamera->setViewMatrix(osg::Matrix::identity());
hudCamera->setClearMask(GL_DEPTH_BUFFER_BIT);
hudCamera->setRenderOrder(osg::Camera::POST_RENDER);
hudCamera->setAllowEventFocus(false);

//Now I create my crosshair:

osg::Geode* xhairGeode = new osg::Geode;
osg::Geometry* xhairGeometry    = new osg::Geometry;
osg::Vec3Array* xhairVertices   = new osg::Vec3Array;
// horizontal line
xhairVertices->push_back(osg::Vec3(0.0, 240.0, 0.0));
xhairVertices->push_back(osg::Vec3(640.0, 240.0, 0.0));
// vertical line
xhairVertices->push_back(osg::Vec3(320.0, 0.0, 0.0));
xhairVertices->push_back(osg::Vec3(320.0, 480.0, 0.0));

...
...
// more boring code here for drawing the crosshair lines, and at the end I do:
hudCamera->addChild(xhairGeode);


// Now I go and create a camera for my 3D scene:

// continuing code from QtViewer example
osg::DisplaySettings* ds = osg::DisplaySettings::instance().get();
osg::ref_ptr<osg::GraphicsContext::Traits> traits = new 
osg::GraphicsContext::Traits;
traits->windowName = name;
traits->windowDecoration = windowDecoration;
traits->x = x; // these arguments are passed in as x = y = 100, width = 
width(), height = height(), as in the example
traits->y = y;
traits->width = w;
traits->height = h;
traits->doubleBuffer = true;
traits->alpha = ds->getMinimumNumAlphaBits();
traits->stencil = ds->getMinimumNumStencilBits();
traits->sampleBuffers = ds->getMultiSamples();
traits->samples = ds->getNumMultiSamples();

osg::ref_ptr<osg::Camera> camera = new osg::Camera;
osgQt::GraphicsWindowQt* graphicsWndQt = new 
osgQt::GraphicsWindowQt(traits.get());
camera->setGraphicsContext( graphicsWndQt );
camera->setViewport( new osg::Viewport(0, 0, traits->width, traits->height) );

// this is different from the example since I want to view my world scene also 
in orthographic
camera->setProjectionMatrixAsOrtho(-1400.0, 1400.0, -1400.0, 1400.0, 320.0, 
0.0);
camera->setViewMatrixAsLookAt( osg::Vec3(0.0, 0.0, 320.0), // camera position
                                                           osg::Vec3(0.0, 0.0, 
0.0),   // center vector 
                                                           osg::Vec3(0.0, 1.0, 
0.0));  // up vector

// I am using the same traits and graphicsWndQt object for my hud camera
hudCamera->setGraphicsContext(graphicsWndQt);
hudCamera->setViewport(0, 0, traits->width, traits->height);
// finally
osgQt::GraphicsWindowQt* gw = 
dynamic_cast<osgQt::GraphicsWindowQt*>(camera->getGraphicsContext());
QWidget* qw =  gw->getGLWidget();

// this code does not change from the example
QWidget* widget = addViewWidget(worldCamera, hudCamera, worldGraph);

// and then
QGridLayout* grid = new QGridLayout;
grid->setSpacing(1);
grid->addWidget( widget, 0, 0 ); // I do not need 4 views




/////////////////////////////////////////////////////////////////////////////////////////



The result can be seen in the attached images.
The second image is zooming out the world (not hud) camera.
Notice that the dialog can be resized, picture 3.

I need to find a way that when I say "draw a pixel on the hud at (5, 5) it will 
draw it there in the viewport window.
I asked to draw the left side of the horizontal line at (0, 240), and it seems 
to be shifted to the right .


Thanks! 

Thank you!

Cheers,
Ron

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=45552#45552




Attachments: 
http://forum.openscenegraph.org//files/3_551.png
http://forum.openscenegraph.org//files/2_992.png
http://forum.openscenegraph.org//files/1_225.png


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

Reply via email to