Xcode4.6 开发 metaio 增强现实 项目(三)--其它函数介绍

下面是其它主要方法的介绍
drawFrame方法主要作用是判断照相机是否拍照,然后加载机器人的跟踪配置信息,使得拍照后机器人的方位不变;
onAnimationEnd主要作用是当机器人的动画播放完后调用此方法播放后续动画;
接着就是覆盖UIResponds类里的Touch方法组了,包括touchsBegin()、touchsMoved()、touchsEnded();
首先来看touchsBegin(),它的作用就是在手指开始在屏幕上选择的的时候确定选择的3D模型是哪个;首先它会获得在屏幕上的触碰对象:

    //获取触碰对象
	UITouch *touch = [touches anyObject];
然后对获取的触碰对象的坐标进行获取:
    //记录下它在屏幕上的坐标
	CGPoint loc = [touch locationInView:glView];
再测算当前屏幕的缩放因子:
    //记录下缩放因子
    float scale = glView.contentScaleFactor;

最后通过get方法返回当前坐标下的3D模型
//如果第三个参数是true,那么将采用定点测定法测定触碰物体,false则为以矩形测定;
    metaio::IGeometry* model = m_metaioSDK->getGeometryFromScreenCoordinates(loc.x * scale, loc.y * scale, false);


当然,如果选中的是机器人的话,那么就播放一段动画;
整个的代码如下:
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    // record the initial states of the geometries with the gesture handler
    //记录开始手势前3D模型的状态
    [m_gestureHandler touchesBegan:touches withEvent:event withView:glView];
    
    // Here's how to pick a geometry
    //获取触碰对象
	UITouch *touch = [touches anyObject];
    //记录下它在屏幕上的坐标
	CGPoint loc = [touch locationInView:glView];
	
    // get the scale factor (will be 2 for retina screens)
    //记录下缩放因子
    float scale = glView.contentScaleFactor;
    
	// ask sdk if the user picked an object(返回在loc坐标下的3D模型)
	// the 'true' flag tells sdk to actually use the vertices for a hit-test, instead of just the bounding box
    	
    if (model == m_metaioMan)
	{
		// we have touched the metaio man
		// let's start an animation
		model->startAnimation( "shock_down" , false);
	}
}


touchesMoved方法是处理手势动作的方法,它是交给了metaioSDK的方法
[m_gestureHandler touchesMoved:touches withEvent:event withView:glView];
来处理;
touchesEnded方法也类似:
[m_gestureHandler touchesEnded:touches withEvent:event withView:glView];


下面是TVButton的方法,它首先用一个if、else来转换它的状态,当然在状态为selected时显示3D模型,unselected时隐藏,显示的时候要重置3D模型的方位,
- (IBAction)onTVButtonClick:(id)sender
get3DPositionFromScreenCoordinates此方法将屏幕的二位坐标转换为真实的三维坐标,最后再通过
setTranslation赋值给3D模型,原始代码如下:
- (IBAction)onTVButtonClick:(id)sender
{
    //这一步很关键,将sender转化为button类型
    UIButton* button = (UIButton*)sender;
    // if the button is already selected, change its state to unselected, and hide the geometry
    //这里是将button设置成为双色跳变的模式,selected是button本身的属性
    if ([button isSelected])
    {
        button.selected = false;
        [self setVisibleTV:false];
    }
    else
    {
        button.selected = true;
        [self setVisibleTV:true];
        
        // reset the location of the geometry
        //重置3D模型的方位
        CGRect screen = self.view.bounds;
        CGFloat width = screen.size.width * [UIScreen mainScreen].scale;
        CGFloat height = screen.size.height * [UIScreen mainScreen].scale;
        metaio::Vector3d translation = m_metaioSDK->get3DPositionFromScreenCoordinates(1, metaio::Vector2d(width/2, height/2));
        m_tv->setTranslation(translation);
        m_screen->setTranslation(translation);
		
		m_screen->startMovieTexture();
        
        // reset the scale of the geometry
        m_tv->setScale(metaio::Vector3d(50.0, 50.0, 50.0));
        m_screen->setScale(metaio::Vector3d(50.0, 50.0, 50.0));
    }
}


后面的也是一样,但是他们都存在一个问题,只是重置了位置和缩放,没有重置旋转角度!


下面就是一些简单的方法实现,
1、拍照方法

- (IBAction)onTakePicture:(id)sender
{
NSString* dir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString* filePath = [NSString stringWithFormat:@"%@/targetImage.jpg", dir];
m_metaioSDK->requestCameraImage([filePath UTF8String]);
// m_metaioSDK->requestCameraImage();
}

2、截屏方法
截屏的图片名字是“时间.jpg”,
方法名:
- (IBAction)onSaveScreen:(id)sender,代码比较简答,就不贴出来了
3、清屏方法
首先重启相机,然后重新加载跟踪配置信息,最后重置按钮到为选择状态,方法名:
- (IBAction)onClearScreen:(id)sender;
4、最后四个方法
onCameraImageSaved、
onNewCameraFrame、
onScreenshot、
onScreenshotImageIOS
它们都是MetaioSdkDelegate的方法,我们需要重写,作用是将拍摄的照片设置为跟踪目标。
ViewDidUnload()方法最后将变量subView释放掉,
总结:至此,增强现实教程7(家居)的所有重点功能都已经涉及到了,下一篇博文要开始对它进行改写了,让它更加精简!!


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值