官方教程 收藏本版 +发表新主题
查看: 31154|回复: 5
打印 上一主题 下一主题

开发基于FLARToolKit的增强现实(AR)基础教程(一)

[复制链接]

开发基于FLARToolKit的增强现实(AR)基础教程(一)

精华AngleYF 发表于 2013-5-29 10:15:22 浏览:  31154 回复:  5 只看该作者 复制链接

增强现实的基本解释是:在真实环境上实时地附加图形。在Flash中,经常使用一个摄像头和一个标记卡片来实现。当你将有标记的卡片对准摄像头的时候,Flash程序可以检测到这个卡片的基本图形信息并将一个3d模型附加之上。这个技术在flash中实现是非常令人兴奋地。尽管它仍然在开发初期,但随着时间的推移,AR将会很可能在我们体验web的过程中扮演重要的角色。现在是一个踏入这个领域的绝佳时期,最好了解它的工作原理。

围绕着AR有很多的Flash应用,随便在哪都有AR的入门教程(这个在贵国有待发展……)。事实上这篇文章是我用来作为起步学习的,对于初学者的好处在于这个教程已经准备好了一系列封装了FLARToolKit的类文件。同时我们创建了debug模式,当你不用摄像头时用来测试你的标记。对教程的大致浏览可能会令人畏缩,但是我会详细地讲解构建的每一个细节step by step。

然而,如果你不想读过长的教程或者你只想阅读教程的最后部分,我建议下载Final Demo文件来check out。我的代码注释可以使你知道这一切都是如何运做的。

在我们开始之前,有一些术语你需要熟悉:

FLARToolKit:我们将要在我们的工程中使用并实现AR的类库。这个类是由Saqoosha编写,以NyARToolKit 2.0.0为基础。现在它已经是开源的。

pv3d:papervision3d的缩写,这个是实时的3d引擎,也是开源项目。

FLARVision:我们将要创建的Demo的名字。

camera_para.dat是FLARToolKit将引用的一个二进制文件,其中记录了摄像头的设置和配置信息。

Marker or Pattern:FLAR将会分析和计算它的图形信息(orientation)。这个图形需要包含黑色边框和内侧的图形。你需要它来使AR工作起来。

CardEmulator:是一个简易的类,当你无法使用摄像头的时候(或者其他的情况下)用来模仿真实的标记卡片。我们将会通过鼠标移动来仿真一个人拿着卡片向着摄像头来回移动。

现在你已经熟悉了这些术语


安装papervision3d和FLARToolKit(略)

当用svn检出http://www.libspark.org/svn/as3/FLARToolKit/trunk时,lib包内是大量的文件夹形式的文件,如果你建立的是flex工程,请下载.swc格式的papervision和FLARToolKit



Creating our Doc Class(建我们的文件类):



现在我们要创建一个新的ActionScript工程叫做FLARVision。你需要将papervision和FLARToolKit工程添加到FLARVision的Library Path中。

我们要做的第一件事是构建一个简单的文件类。这个类将是你想要利用papervision和FLARToolKit创建的任何工程的基础。我已经构造了它,所以当你建立自己的FLAR工程的时候便会很容易通过重写核心的方法扩展它。让我们来看一看:

  1.     /**
  2.     * Original Author:  Jesse Freeman
  3.     * Class File: FLARVision.as
  4.     *
  5.     * Permission is hereby granted, free of charge, to any person obtaining a copy
  6.     * of this software and associated documentation files (the "Software"), to deal
  7.     * in the Software without restriction, including without limitation the rights
  8.     * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9.     * copies of the Software, and to permit persons to whom the Software is
  10.     * furnished to do so, subject to the following conditions:
  11.     *
  12.     * The above copyright notice and this permission notice shall be included in
  13.     * all copies or substantial portions of the Software.
  14.     *
  15.     * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16.     * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17.     * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18.     * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19.     * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20.     * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21.     * THE SOFTWARE.
  22.     *
  23.     * Licensed under The MIT License
  24.     * Redistributions of files must retain the above copyright notice.
  25.     *
  26.     * Revisions
  27.     *                 1.0  Initial version April 29, 2009
  28.     *
  29.     */

  30.     package
  31.     {
  32.              import flash.display.Sprite;
  33.              import flash.display.StageAlign;
  34.              import flash.display.StageQuality;
  35.              import flash.display.StageScaleMode;
  36.              import flash.events.Event;
  37.             
  38.              import org.papervision3d.cameras.Camera3D;
  39.              import org.papervision3d.materials.WireframeMaterial;
  40.              import org.papervision3d.objects.primitives.Plane;
  41.              import org.papervision3d.render.BasicRenderEngine;
  42.              import org.papervision3d.scenes.Scene3D;
  43.              import org.papervision3d.view.Viewport3D;
  44.             
  45.              /**
  46.               *
  47.               * @author Jesse Freeman
  48.               *  
  49.               */        
  50.              public class FLARVision extends Sprite
  51.              {
  52.                       protected var scene:Scene3D;
  53.                       protected var camera:Camera3D;
  54.                       protected var viewport:Viewport3D;
  55.                       protected var renderer:BasicRenderEngine;
  56.                      
  57.                       /**
  58.                        * Main Constructor for the class.
  59.                        *
  60.                        */               
  61.                       public function FLARVision()
  62.                       {
  63.                                configureStage();
  64.                                init();
  65.                        }
  66.                      
  67.                       /**
  68.                        * Configures the stage and sets the scale mode to keep visuals from
  69.                        * distorting when resizing the browser window.
  70.                        *
  71.                        */                        
  72.                       private function configureStage():void  
  73.               {  
  74.                           stage.quality = StageQuality.HIGH;
  75.                    stage.align = StageAlign.TOP_LEFT;
  76.                    stage.scaleMode = StageScaleMode.NO_SCALE;
  77.                }
  78.             
  79.                       /**
  80.                        * Called at the construction of the class. Calls the setup Papervision
  81.                        * method and adds an event listener to the render the Viewport.
  82.                        *
  83.                        */            
  84.                       protected function init():void {
  85.                                setupPapervision();
  86.                                addEventListener(Event.ENTER_FRAME, renderViewport);
  87.                        }
  88.                      
  89.                       /**
  90.                        * Sets up and configures Papervision. This function can be overridden
  91.                        * to accommodate any custom set ups you may need for your project.
  92.                        *
  93.                        */                        
  94.                       public function setupPapervision():void
  95.                       {
  96.                                scene = new Scene3D();
  97.                                camera = new Camera3D();
  98.       
  99.                                // Create the Viewport
  100.                                viewport = new Viewport3D(stage.stageWidth, stage.stageHeight);
  101.                                addChild(viewport);
  102.       
  103.                                create3dObjects();
  104.                               
  105.                                renderer = new BasicRenderEngine();
  106.                        }
  107.                      
  108.                       /**
  109.                        * This default function is where 3d Objects should be added to PV3D's
  110.                        * scenes.
  111.                        *
  112.                        */                        
  113.                       protected function create3dObjects():void
  114.                       {
  115.                                var plane:Plane = new Plane( new WireframeMaterial(0xff0000) );
  116.                                scene.addChild(plane);
  117.                        }
  118.                      
  119.                       /**
  120.                        * Renders the papervision scene.
  121.                        * @param event
  122.                        *
  123.                        */                                
  124.                       public function renderViewport(event:Event = null):void
  125.                       {        
  126.                                renderer.renderScene(scene, camera, viewport);
  127.                        }
  128.                      
  129.               }
  130.     }
复制代码

(这个类papervision已经构造好了叫做BasicView,相信经常做pv3d的朋友早就知道。而这个逐帧渲染的方法并不是一个很好的方法,由于pv3d非常占用资源,所以我们有时需要手工地stopRendering()和startRendering())。

(关于papervision3d的基础我不翻译,如果不熟悉的童鞋请移步天地会3d应用)。


转载http://miyaonanhai.blogbus.com/logs/70836695.html

分享至:
| 人收藏
回复

使用道具 举报

该用户从未签到

沙发
夏天的冰 发表于 2013-6-30 12:09:24 只看该作者
楼主辛苦了,现在看得人这么少,支持一下
回复 支持 反对

使用道具 举报

该用户从未签到

板凳
AngleYF
 楼主|
发表于 2013-7-3 17:30:11 只看该作者
夏天的冰 发表于 2013-6-30 12:09
楼主辛苦了,现在看得人这么少,支持一下

嗯哪~~~~~~~
回复 支持 反对

使用道具 举报

该用户从未签到

地板
click 发表于 2013-8-26 22:14:23 只看该作者
这个好用吗,这么复杂
回复 支持 反对

使用道具 举报

该用户从未签到

5#
slnose 发表于 2013-11-6 11:40:40 只看该作者
很期待,好东西
回复 支持 反对

使用道具 举报

该用户从未签到

6#
ahstudy 发表于 2013-12-9 16:25:43 只看该作者
这个感觉有点复杂!!先学习一下!!!
回复 支持 反对

使用道具 举报

*滑动验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Copyright © 2013-2017 ARinChina-增强现实中国技术论坛   All Rights Reserved.