Thanks for quick replies, I'm posting code here with simple example of what I
am doing:
Code:
#include <iostream>
#include <osg/Notify>
#include <osg/io_utils>
#include <osg/ArgumentParser>
#include <osg/MatrixTransform>
#include <osg/AutoTransform>
#include <osg/Camera>
#include <osg/TexMat>
#include <osg/TextureRectangle>
#include <osgDB/ReadFile>
#include <osgGA/TrackballManipulator>
#include <osgGA/StateSetManipulator>
#include <osgViewer/Viewer>
#include <osgViewer/ViewerEventHandlers>
#include <osgWidget/Browser>
#include <QtGui/QGraphicsScene>
#include <QtGui/QGraphicsView>
#include <QtGui/QApplication>
#include <QtGui/QPainter>
#include <QtGui/QtEvents>
#include <QtGui/QDialog>
#include <QtGui/QVBoxLayout>
#include <QtGui/QMainWindow>
#include <QtOpenGL/QtOpenGL>
#include <osgQt/QGraphicsViewAdapter>
#include <osgQt/QWebViewImage>
#include <osgQt/QWidgetImage>
#include <osgViewer/Viewer>
#include <osgViewer/ViewerEventHandlers>
#include <osgGA/StateSetManipulator>
#include <osgGA/EventVisitor>
#include <osgDB/WriteFile>
//------------------------------------------------------------------------
// Thread that runs the viewer's frame loop as we can't run Qt in the
background...
class ViewerFrameThread : public OpenThreads::Thread
{
public:
ViewerFrameThread(osgViewer::ViewerBase* viewerBase, bool
doQApplicationExit):
_viewerBase(viewerBase),
_doQApplicationExit(doQApplicationExit) {}
~ViewerFrameThread()
{
cancel();
while(isRunning())
{
OpenThreads::Thread::YieldCurrentThread();
}
}
int cancel()
{
_viewerBase->setDone(true);
return 0;
}
void run()
{
int result = _viewerBase->run();
if (_doQApplicationExit) QApplication::exit(result);
}
osg::ref_ptr<osgViewer::ViewerBase> _viewerBase;
bool _doQApplicationExit;
};
typedef struct
{
osg::ref_ptr<osg::Group> group;
QPushButton* button;
} qtButton;
// Here is function that creates QPushButton in location x,y with size of
width, height
qtButton createButton (char *buttonText, int x, int y, int width, int height)
{
//creating Qt widget
QWidget* widget = 0;
widget = new QWidget;
widget->setLayout(new QVBoxLayout);
QPushButton* button = new QPushButton(buttonText);
button->setFixedSize(width-30,height-10);
widget->layout()->addWidget(button);
widget->setGeometry(0, 0, 1024,768);
QGraphicsScene* graphicsScene = 0;
osg::ref_ptr<osgQt::QWidgetImage> widgetImage = new
osgQt::QWidgetImage(widget);
#if (QT_VERSION >= QT_VERSION_CHECK(4, 5, 0))
widgetImage->getQWidget()->setAttribute(Qt::WA_TranslucentBackground);
#endif
widgetImage->getQGraphicsViewAdapter()->setBackgroundColor(QColor(0, 0,
0, 0));
widgetImage->getQGraphicsViewAdapter()->resize(800, 600);
graphicsScene = widgetImage->getQGraphicsViewAdapter()->getQGraphicsScene();
//creating OSG stuff for that (don't really know what happens here, taken
from example OsgWidgetsQt)
osg::Camera* camera = 0;
osg::Geometry* quad = osg::createTexturedQuadGeometry(osg::Vec3(0,0,0),
osg::Vec3(1,0,0), osg::Vec3(0,1,0));
osg::StateSet* stateset = quad->getOrCreateStateSet();
stateset->setMode(GL_BLEND,osg::StateAttribute::ON);
osg::Geode* geode = new osg::Geode;
geode->addDrawable(quad);
osg::MatrixTransform* mt = new osg::MatrixTransform;
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);
osgViewer::InteractiveImageHandler* handler;
//creating a camera the same size as button
//the camera is not transparent for mouse events so I can't make it
fullscreen
camera = new osg::Camera;
camera->setProjectionResizePolicy(osg::Camera::FIXED);
camera->setProjectionMatrix(osg::Matrix::ortho2D(0,1,0,1));
camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
camera->setViewMatrix(osg::Matrix::identity());
camera->setClearMask(GL_DEPTH_BUFFER_BIT);
camera->setRenderOrder(osg::Camera::POST_RENDER);
camera->addChild(geode);
camera->setViewport(x,y,width,height);
mt->addChild(camera);
handler = new osgViewer::InteractiveImageHandler(widgetImage.get(),
texture, camera);
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);
osg::Group* overlay = new osg::Group;
overlay->addChild(mt);
quad->setEventCallback(handler);
quad->setCullCallback(handler);
// returning a struct, where .button is the QObject itself
// and .group is OSG group which we will add to root
qtButton result;
result.button = button;
result.group = overlay;
return result;
}
int main(int argc, char **argv)
{
// Qt requires that we construct the global QApplication before creating
any widgets.
QApplication app(argc, argv);
osg::ArgumentParser arguments(&argc,argv);
osg::ref_ptr<osg::Group> root = new osg::Group;
qtButton button = createButton("button",0,60,200,50);
root->addChild(button.group);
//creating a usual OSG viewer
osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer(arguments);
osg::Node* node = osgDB::readNodeFile("cow.osg");
root->addChild(node);
viewer->setSceneData(root);
viewer->setCameraManipulator(new osgGA::TrackballManipulator());
viewer->addEventHandler(new
osgGA::StateSetManipulator(root->getOrCreateStateSet()));
viewer->addEventHandler(new osgViewer::StatsHandler);
viewer->addEventHandler(new osgViewer::WindowSizeHandler);
// running OSG and Qt is diffirent threads
// create a thread to run the viewer's frame loop
ViewerFrameThread viewerThread(viewer.get(), true);
viewerThread.startThread();
// now start the standard Qt event loop, then exists when the
viewerThead sends the QApplication::exit() signal.
return QApplication::exec();
}
I'm sorry, I cant test that code right now, but as I know it should work.
I've tried now to use QString functions to convert my string, but it shows
either black triangles with question marks or just "aeoeao" wrong codec stuff.
------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=50538#50538
_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org