osg3d模型在iOS中显示

来源:互联网 发布:美国历年cpi数据 编辑:程序博客网 时间:2024/06/18 14:16
由于项目需要osg展示3d模型,小子我可算煞费苦心才找到这个方法,这份提问邮件是一个开发者在提交问题代码时贴出了代码,才解决了小子我的一大问题,不胜感激,鉴于osg的材料比较少,在这里贴出代码,希望对搞osg的小伙伴们能有所帮助!
问题地址:
http://forum.openscenegraph.org/viewtopic.php?t=12566


//.h文件里定义的
//导入的文件
#include <osgDB/ReadFile>
#include <osg/MatrixTransform>
#include <osgViewer/Viewer>


osg::ref_ptr<osgViewer::Viewer> _viewer;
osg::ref_ptr<osg::MatrixTransform> _root;


//这段代码写在uiviewcontroller类.mm文件里的,
// self.osgView 为 iOS的UIView子类


//导入的文件


#include <osgGA/TrackballManipulator>
#include <osgGA/MultiTouchTrackballManipulator>
#include <osg/ShapeDrawable>
#include <osg/DisplaySettings>
#include <osgViewer/api/IOS/GraphicsWindowIOS>


   self.osgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0,self.view.frame.size.width, 300)];
    [self.view addSubview:self.osgView];
    self.osgView.backgroundColor =  [UIColor greenColor];
    
    osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
    CGRect lFrame = [self.osgView convertRect:self.osgView.frame fromView:[UIApplication sharedApplication].keyWindow];
    NSLog(@"[[UIScreen mainScreen] scale] =  %f",[[UIScreen mainScreen] scale]);
    unsigned int w  = lFrame.size.width * [[UIScreen mainScreen] scale];
    unsigned int h  = lFrame.size.height * [[UIScreen mainScreen] scale];
    
    osg::DisplaySettings* settings = osg::DisplaySettings::instance();
    settings->setNumMultiSamples(4);
    
    osg::ref_ptr<osgViewer::GraphicsWindowIOS::WindowData> windata = new osgViewer::GraphicsWindowIOS::WindowData(self.osgView, osgViewer::GraphicsWindowIOS::WindowData::LANDSCAPE_LEFT_ORIENTATION, -1.0f);
    
    // Setup the traits parameters
    traits->x = lFrame.origin.x * [[UIScreen mainScreen] scale];
    traits->y = lFrame.origin.y * [[UIScreen mainScreen] scale];
    traits->width = w;
    traits->height = h;
    
    traits->windowDecoration = false;
    traits->doubleBuffer = true;
    traits->sharedContext = 0;
    traits->setInheritedWindowPixelFormat = false;
    traits->inheritedWindowData = windata;
    
    // Create the Graphics Context
    osg::ref_ptr<osg::GraphicsContext> graphicsContext = osg::GraphicsContext::createGraphicsContext(traits.get());
    
    _root = new osg::MatrixTransform();
    
    osg::ref_ptr<osg::Node> model = (osgDB::readNodeFile("/Users/cnmobi/Desktop/osg-iphone/IPhone_Project/1.ive"));
    _root->addChild(model);
    _viewer = new osgViewer::Viewer();
    
    
    if(graphicsContext)
    {
        _viewer->getCamera()->setGraphicsContext(graphicsContext);
        _viewer->getCamera()->setViewport(new osg::Viewport(traits->x, traits->y, traits->width, traits->height));
    }
    
    _viewer->setSceneData(_root.get());
   osgGA::MultiTouchTrackballManipulator * _cameraManipulator = new osgGA::MultiTouchTrackballManipulator();
    _viewer->setCameraManipulator(_cameraManipulator);
    _viewer->realize();
    
    // draw a frame
    _viewer->frame();
    
    osg::setNotifyLevel(osg::INFO);
    //add
    //旋转和放大缩小,可能与以下代码有关
    [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]


//updateScene 方法


- (void)updateScene {
    _viewer->frame();
}
0 0