So I have program which is a Qt App. I have some basic Qt GUI on the outside but then I have a Qt widget that makes use of OpenSceneGraph to render a 3D scene. To make things more complicated inside that screen I have a HUD that toggles on and off. This HUD consists of some graphic elements and then a Qt Widget rendered to a texture.
I basically have that working however I am having some size issues within the HUD/Qt Widget. When I first toggle the HUD to visible the Qt Widget is there but way too big. I can change the size but regardless, the first time I press a key the Qt Widget is auto re-sized to fit in the textured area I give it (which is what I expect) but this auto re-sized widget doesn’t fit the area correctly. Its impossible to read the table that the widget contains.
To help I have two screen shots. The first is before I type a key:
http://cerrnim.com/wp-content/uploads/2012/12/before.png
And the second is after I type a key:
http://cerrnim.com/wp-content/uploads/2012/12/after.png
Additionally here are some code fragments showing how I create the HUD. Its of course a part of a much larger program but hopefully this is enough information.
/* AUX function to create HUD geo. */
osg::Geode* HUDGeometry( int x1, int y1, int x2, int y2,
std::string model, HUDEvents event, NetworkViewer* viewer ) {
osg::Geometry* quad = osg::createTexturedQuadGeometry(osg::Vec3(x1,y1,0),
osg::Vec3(x2-x1,0,0), osg::Vec3(0,y2-y1,0), 1, 1);
osg::Geode* geode = new osg::Geode( ) ;
geode->setName( model ) ;
geode->setUserData( new HUDEvent( event, viewer ) ) ;
osg::Texture2D* HUDTexture = new osg::Texture2D();
HUDTexture->setDataVariance(osg::Object::DYNAMIC);
osg::Image* hudImage = osgDB::readImageFile(model);
HUDTexture->setImage(hudImage);
geode->getOrCreateStateSet()->setTextureAttributeAndModes(
0, HUDTexture, osg::StateAttribute::ON);
geode->addDrawable( quad ) ;
return geode ;
}
/* Creates the HUD but does not display it yet. */
void NetworkViewer::initHUD( ) {
osg::MatrixTransform* mt = new osg::MatrixTransform;
osg::Camera* hudCamera = new osg::Camera;
hudCamera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
hudCamera->setViewMatrix(osg::Matrix::identity());
//hudCamera->setProjectionResizePolicy(osg::Camera::FIXED);
hudCamera->setProjectionMatrixAsOrtho2D(0,100,0,100);
hudCamera->setClearMask(GL_DEPTH_BUFFER_BIT);
hudCamera->setRenderOrder(osg::Camera::POST_RENDER);
hudCamera->setAllowEventFocus(true);
QWidget* widget = new QWidget;
layout = new QVBoxLayout( ) ;
widget->setLayout(layout);
widget->layout()->addWidget(((GUI*)view)->getTabs( ));
//widget->setGeometry(0, 0, 500, 400);
osg::ref_ptr<osgQt::QWidgetImage> widgetImage = new osgQt::QWidgetImage(widget);
osg::Geometry* quad = osg::createTexturedQuadGeometry(osg::Vec3(30,32,0),
osg::Vec3(40,0,0), osg::Vec3(0,35,0), 1, 1);
osg::Geode* geode = new osg::Geode;
geode->addDrawable(quad);
osg::Texture2D* texture = new osg::Texture2D(widgetImage.get());
texture->setResizeNonPowerOfTwoHint(false);
texture->setFilter(osg::Texture::MIN_FILTER,osg::Texture::LINEAR);
texture->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP_TO_EDGE);
texture->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP_TO_EDGE);
mt->getOrCreateStateSet()->setTextureAttributeAndModes(0, texture, osg::StateAttribute::ON);
mt->addChild(hudCamera);
osgViewer::InteractiveImageHandler* handler =
new osgViewer::InteractiveImageHandler(widgetImage.get(), texture, hudCamera);
mt->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
mt->getOrCreateStateSet()->setMode(GL_BLEND, osg::StateAttribute::ON);
mt->getOrCreateStateSet()->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
mt->getOrCreateStateSet()->setAttribute(new osg::Program);
quad->setEventCallback(handler);
quad->setCullCallback(handler);
hudCamera->addChild( geode ) ;
hudCamera->addChild( HUDGeometry(73,73,75,75,
"models/quit.png", EXIT_OBJ, this ));
hudCamera->addChild( HUDGeometry(25,25,75,75,
"models/hud.png", NO_EVENT, this ));
osg::Group* overlay = new osg::Group;
overlay->addChild(mt);
root->addChild(overlay);
HUD = hudCamera ;
disableHUD( ) ;
}
I think the main problem is that you do not adapt the dimensions of the widget to the dimensions of the quad you render it on.
I’m not sure what
QWidgetImageis doing internally, but I guess it’s just rendering the widget onto a canvas of appropriate size and convert the result into an image. In your code you map the complete image (regardless of its dimension or aspect ration) onto that quad. If you want the widget to fit the quad you need to resize the widget before creating an image of it.