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();
}
问题地址:
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
- osg3d模型在iOS中显示
- IOS显示三维模型
- unity中快速调整模型在game视图中显示
- iOS在label中显示表情
- 在iOS视图控制中显示背景图
- iOS - 在NSString中显示%号
- 如何能在浏览器中显示三维模型
- 在AngularJS中显示模型中的数据有两种方式:
- UE4 在UI/UMG中显示三维模型
- 在IOS中 使用模型和字典的区别
- 在IOS中显示HTML的3种选择
- ios中 搜索关键字在结果中高亮显示
- 在IOS中显示HTML的3种选择
- iOS中在UIButton上同时显示文字和图片
- 在iOS中UITableView控件不显示能容
- iOS 开发笔记---在Label中显示图片
- iOS轮询请求并在图表中动态显示
- iOS在一个label中显示不同颜色的字体
- c语言扫雷的游戏续集
- jenkins使用shell命令打IOS包报错:user interaction is not allowed
- 【php基础】PHP分页类
- java PDF文件导出
- 阿里微博双双IPO 最大受益者是新浪
- osg3d模型在iOS中显示
- c语言技巧
- 我的学习日记之JavaScript——表白格
- 设计模式Before-after之享元模式
- 55分钟学会正则表达式(译)
- android中webview调用拨号盘
- 使用C++处理JSON数据交换格式,附已经VC2005编译好的静态lib库
- cocos2dx 代码使用cocoStudio 生成的场景json文件
- NoMoreFixedIps Zero fixed ips available