Hi,
I am trying to learn OSG for iPhone.
For this I am using the simpleExample on https://github.com/stmh/osg.
Instead of the blue background color I want to load an Image.
I already look at the osgHud.cpp example and I try to use some code lines.
Now, I can see the child-node with geode but the background image doesn´t
appear.
> struct SnapImage : public osg::Camera::DrawCallback
> {
> SnapImage(const std::string&
> filename):_filename(filename),_snapImage(false)
> {
> _image = new osg::Image;
> }
>
> virtual void operator () (osg::RenderInfo& renderInfo) const
> {
>
> if (!_snapImage) return;
>
> osg::notify(osg::NOTICE)<<"Camera callback"<<std::endl;
>
> osg::Camera* camera = renderInfo.getCurrentCamera();
> osg::Viewport* viewport = camera ? camera->getViewport() : 0;
>
> osg::notify(osg::NOTICE)<<"Camera callback "<<camera<<"
> "<<viewport<<std::endl;
>
> if (viewport && _image.valid())
> {
>
> _image->readPixels(int(viewport->x()),int(viewport->y()),int(viewport->width()),int(viewport->height()),
> GL_RGBA,
> GL_UNSIGNED_BYTE);
> osgDB::writeImageFile(*_image, _filename);
>
> osg::notify(osg::NOTICE)<<"Taken screenshot, and written to
> '"<<_filename<<"'"<<std::endl;
> }
>
> _snapImage = false;
> }
>
> std::string _filename;
> mutable bool _snapImage;
> mutable osg::ref_ptr<osg::Image> _image;
> };
>
> struct SnapeImageHandler : public osgGA::GUIEventHandler
> {
> SnapeImageHandler(int key,SnapImage* si):
> _key(key),
> _snapImage(si) {}
>
> bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter&)
> {
> if (ea.getHandled()) return false;
>
> switch(ea.getEventType())
> {
> case(osgGA::GUIEventAdapter::KEYUP):
> {
> if (ea.getKey() == _key)
> {
> osg::notify(osg::NOTICE)<<"event handler"<<std::endl;
> _snapImage->_snapImage = true;
> return true;
> }
>
> break;
> }
> default:
> break;
> }
>
> return false;
> }
>
> int _key;
> osg::ref_ptr<SnapImage> _snapImage;
> };
>
>
> //
> //Called once app has finished launching, create the viewer then realize.
> Can't call viewer->run as will
> //block the final inialization of the windowing system
> //
> - (void)applicationDidFinishLaunching:(UIApplication *)application {
>
> osg::setNotifyLevel(osg::DEBUG_INFO);
>
> _viewer = new osgViewer::Viewer();
>
> _root = new osg::Group;
>
> osg::ref_ptr<osg::ShapeDrawable> shape1 = new osg::ShapeDrawable;
> shape1->setShape(new osg::Box(osg::Vec3(-3.0f, 0.0f, 0.0f), 2.0f, 2.0f,
> 2.0f));
> shape1->setColor(osg::Vec4(1.0f, 0.0f, 0.0f, 0.0f));
>
> osg::ref_ptr<osg::ShapeDrawable> shape2 = new osg::ShapeDrawable;
> shape2->setShape(new osg::Sphere(osg::Vec3(3.0f, 0.0f, 0.0f), 1.0f));
> shape2->setColor(osg::Vec4(0.0f, 0.0f, 1.0f, 1.0f));
>
> osg::ref_ptr<osg::ShapeDrawable> shape3 = new osg::ShapeDrawable;
> shape3->setShape(new osg::Cone(osg::Vec3(0.0f, 0.0f, 0.0f), 1.0f, 1.0f));
> shape3->setColor(osg::Vec4(0.0f, 1.0f, 0.0f, 1.0f));
>
> osg::ref_ptr<osg::Geode> geode = new osg::Geode;
> geode->addDrawable(shape1.get());
> geode->addDrawable(shape2.get());
> geode->addDrawable(shape3.get());
>
>
> // try msaa. available for iOS >= 4.0
> osg::DisplaySettings* settings = osg::DisplaySettings::instance();
> settings->setNumMultiSamples(4);
>
> OpenSceneGraph_iOSViewController* touch_handler = new
> OpenSceneGraph_iOSViewController();
> {
> unsigned int w(640);
> unsigned int h(480);
> osg::GraphicsContext::WindowingSystemInterface* wsi =
> osg::GraphicsContext::getWindowingSystemInterface();
> if (wsi) {
> wsi->getScreenResolution(0, w, h);
> }
> //create and attach ortho camera for hud text
> osg::ref_ptr<osg::Camera> hudCamera = new osg::Camera;
>
> // set the projection matrix
> hudCamera->setProjectionMatrix(osg::Matrix::ortho2D(0,w,0,h));
>
> // set the view matrix
> hudCamera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
> hudCamera->setViewMatrix(osg::Matrix::identity());
>
> // only clear the depth buffer
> hudCamera->setClearMask(GL_DEPTH_BUFFER_BIT);
>
> // draw subgraph after main camera view.
> hudCamera->setRenderOrder(osg::Camera::POST_RENDER);;
>
> _root->addChild(hudCamera.get());
> hudCamera->addChild(touch_handler->getDebugNode());
> hudCamera->getOrCreateStateSet()->setRenderBinDetails(1,
> "RenderBin");
> }
>
> geode->getOrCreateStateSet()->setRenderBinDetails(2, "RenderBin");
> geode->getOrCreateStateSet()->setMode(GL_DEPTH_TEST,
> osg::StateAttribute::ON);
>
> _root->addChild(geode.get());
>
> _root->getOrCreateStateSet()->setMode(GL_DEPTH_TEST,
> osg::StateAttribute::OFF);
>
> _viewer->addEventHandler(touch_handler);
> _viewer->setSceneData(_root.get());
> _viewer->setCameraManipulator(new osgGA::MultiTouchTrackballManipulator);
> _viewer->setThreadingModel(osgViewer::Viewer::SingleThreaded);
>
> SnapImage* finalDrawCallback = new SnapImage("file.jpg");
> _viewer->getCamera()->setFinalDrawCallback(finalDrawCallback);
> _viewer->addEventHandler(new SnapeImageHandler('f', postDrawCallback));
>
>
> //SingleThreaded DrawThreadPerContext
> _viewer->realize();
> _viewer->frame();
> osg::setNotifyLevel(osg::INFO);
>
> [NSTimer scheduledTimerWithTimeInterval:1.0/30.0 target:self
> selector:@selector(updateScene) userInfo:nil repeats:YES];
>
> //Configure and start accelerometer
> [[UIAccelerometer sharedAccelerometer] setUpdateInterval:(1.0 /
> kAccelerometerFrequency)];
> [[UIAccelerometer sharedAccelerometer] setDelegate:self];
> }
Have you any idea, what the reason could be? Any advice is really appreciated.
Thank you!
Cheers,
Büsra[/code]
------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=41349#41349
_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org