--- /dev/null
+/* \r
+ * PROJECT: NyARToolkit JOGL sample program.\r
+ * --------------------------------------------------------------------------------\r
+ * The MIT License\r
+ * Copyright (c) 2008-2011 nyatla\r
+ * airmail(at)ebony.plala.or.jp\r
+ * http://nyatla.jp/nyartoolkit/\r
+ * \r
+ * Permission is hereby granted, free of charge, to any person obtaining a copy\r
+ * of this software and associated documentation files (the "Software"), to deal\r
+ * in the Software without restriction, including without limitation the rights\r
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\r
+ * copies of the Software, and to permit persons to whom the Software is\r
+ * furnished to do so, subject to the following conditions:\r
+ * The above copyright notice and this permission notice shall be included in\r
+ * all copies or substantial portions of the Software.\r
+ * \r
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\r
+ * THE SOFTWARE.\r
+ * \r
+ */\r
+package jp.nyatla.nyartoolkit.rpf.sample;\r
+\r
+import java.awt.Color;\r
+import java.awt.Font;\r
+import java.awt.Frame;\r
+import java.awt.Insets;\r
+import java.awt.event.WindowAdapter;\r
+import java.awt.event.WindowEvent;\r
+\r
+import javax.media.Buffer;\r
+import javax.media.opengl.*;\r
+\r
+\r
+import jp.nyatla.nyartoolkit.NyARException;\r
+import jp.nyatla.nyartoolkit.core.param.NyARParam;\r
+\r
+import jp.nyatla.nyartoolkit.core.types.NyARDoublePoint2d;\r
+import jp.nyatla.nyartoolkit.core.types.matrix.NyARDoubleMatrix44;\r
+import jp.nyatla.nyartoolkit.rpf.mklib.ARTKMarkerTable;\r
+import jp.nyatla.nyartoolkit.rpf.reality.nyartk.NyARRealityTarget;\r
+import jp.nyatla.nyartoolkit.rpf.reality.nyartk.NyARRealityTargetList;\r
+import jp.nyatla.nyartoolkit.rpf.realitysource.nyartk.NyARRealitySource_Jmf;\r
+import jp.nyatla.nyartoolkit.jmf.utils.JmfCaptureDevice;\r
+import jp.nyatla.nyartoolkit.jmf.utils.JmfCaptureDeviceList;\r
+import jp.nyatla.nyartoolkit.jmf.utils.JmfCaptureListener;\r
+import jp.nyatla.nyartoolkit.jogl.utils.NyARGLDrawUtil;\r
+import jp.nyatla.nyartoolkit.rpf.reality.nyartk.gl.NyARRealityGl;\r
+\r
+import com.sun.opengl.util.Animator;\r
+\r
+/**\r
+ * NyARRealityシステムのサンプル。\r
+ * ARToolkitスタイルのマーカの上に、立方体を表示します。\r
+ * 取り扱うマーカの種類は2種類(Hiro,Kanji)です。\r
+ * パターンの種類によっては、内側のパターンを誤認識する場合があります。\r
+ * @author nyatla\r
+ *\r
+ */\r
+public class Test_NyARRealityGl_ARMarker implements GLEventListener, JmfCaptureListener\r
+{\r
+\r
+ private final static int SCREEN_X = 640;\r
+ private final static int SCREEN_Y = 480;\r
+\r
+ private Animator _animator;\r
+ private JmfCaptureDevice _capture;\r
+\r
+ private GL _gl;\r
+\r
+ private Object _sync_object=new Object();\r
+\r
+ NyARRealityGl _reality;\r
+ NyARRealitySource_Jmf _src;\r
+ ARTKMarkerTable _mklib;\r
+\r
+ public Test_NyARRealityGl_ARMarker(NyARParam i_param) throws NyARException\r
+ {\r
+ Frame frame = new Frame("NyARToolkit+RPF["+this.getClass().getName()+"]");\r
+ \r
+ // キャプチャの準備\r
+ JmfCaptureDeviceList devlist = new JmfCaptureDeviceList();\r
+ this._capture = devlist.getDevice(0);\r
+ if (!this._capture.setCaptureFormat(SCREEN_X, SCREEN_Y, 30.0f)) {\r
+ throw new NyARException();\r
+ }\r
+ this._capture.setOnCapture(this);\r
+ //Realityの構築\r
+ i_param.changeScreenSize(SCREEN_X, SCREEN_Y); \r
+ //キャプチャ画像と互換性のあるRealitySourceを構築(樽型歪みの少ないカメラの時は、distortionFactorをnullにすること。)\r
+// this._src=new NyARRealitySource_Jmf(this._capture.getCaptureFormat(),i_param.getDistortionFactor(),2,100);\r
+ this._src=new NyARRealitySource_Jmf(this._capture.getCaptureFormat(),null,2,100);\r
+ //OpenGL互換のRealityを構築 \r
+ this._reality=new NyARRealityGl(i_param.getPerspectiveProjectionMatrix(),i_param.getScreenSize(),10,10000,3,10);\r
+ //マーカライブラリ(ARTKId)の構築\r
+ this._mklib= new ARTKMarkerTable(10,16,16,25,25,4);\r
+ //マーカテーブルの作成(2種類)\r
+ this._mklib.addMarkerFromARPattFile(PATT_HIRO,0,"HIRO",80,80);\r
+ this._mklib.addMarkerFromARPattFile(PATT_KANJI,1,"KANJI",80,80);\r
+ \r
+ // 3Dを描画するコンポーネント\r
+ GLCanvas canvas = new GLCanvas();\r
+ frame.add(canvas);\r
+ canvas.addGLEventListener(this);\r
+ frame.addWindowListener(new WindowAdapter() {\r
+ public void windowClosing(WindowEvent e)\r
+ {\r
+ System.exit(0);\r
+ }\r
+ });\r
+\r
+ frame.setVisible(true);\r
+ Insets ins = frame.getInsets();\r
+ frame.setSize(SCREEN_X + ins.left + ins.right, SCREEN_Y + ins.top + ins.bottom);\r
+ canvas.setBounds(ins.left, ins.top, SCREEN_X, SCREEN_Y);\r
+ }\r
+\r
+ public void init(GLAutoDrawable drawable)\r
+ {\r
+ this._gl = drawable.getGL();\r
+ this._gl.glEnable(GL.GL_DEPTH_TEST);\r
+ this._gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);\r
+ NyARGLDrawUtil.setFontStyle("SansSerif",Font.BOLD,24);\r
+ // NyARToolkitの準備\r
+ try {\r
+ // キャプチャ開始\r
+ _capture.start();\r
+ } catch (Exception e) {\r
+ e.printStackTrace();\r
+ }\r
+ this._animator = new Animator(drawable);\r
+ this._animator.start();\r
+ return;\r
+ }\r
+\r
+ public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height)\r
+ {\r
+ _gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);\r
+ _gl.glViewport(0, 0, width, height);\r
+\r
+ // 視体積の設定\r
+ _gl.glMatrixMode(GL.GL_PROJECTION);\r
+ _gl.glLoadIdentity();\r
+ // 見る位置\r
+ _gl.glMatrixMode(GL.GL_MODELVIEW);\r
+ _gl.glLoadIdentity();\r
+ }\r
+\r
+ public void display(GLAutoDrawable drawable)\r
+ {\r
+ //RealitySourceにデータが処理する。\r
+ if(!this._src.isReady())\r
+ {\r
+ return;\r
+ }\r
+ \r
+ // 背景を書く\r
+ this._gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); // Clear the buffers for new frame.\r
+ try{\r
+ synchronized(this._sync_object){\r
+ \r
+ this._reality.glDrawRealitySource(this._gl,this._src);\r
+ // Projection transformation.\r
+ this._gl.glMatrixMode(GL.GL_PROJECTION);\r
+ this._reality.glLoadCameraFrustum(this._gl);\r
+ //ターゲットリストを走査して、画面に内容を反映\r
+ NyARRealityTargetList tl=this._reality.refTargetList();\r
+ for(int i=tl.getLength()-1;i>=0;i--){\r
+ NyARRealityTarget t=tl.getItem(i);\r
+ switch(t.getTargetType())\r
+ {\r
+ case NyARRealityTarget.RT_KNOWN:\r
+ //立方体の描画\r
+ this._gl.glMatrixMode(GL.GL_MODELVIEW);\r
+ this._gl.glLoadIdentity();\r
+ NyARDoubleMatrix44 m=t.refTransformMatrix();\r
+ this._reality.glLoadModelViewMatrix(this._gl,m);\r
+ _gl.glPushMatrix(); // Save world coordinate system.\r
+ _gl.glTranslatef(0f,0,20f); // Place base of cube on marker surface.\r
+ _gl.glDisable(GL.GL_LIGHTING); // Just use colours.\r
+ NyARGLDrawUtil.drawColorCube(this._gl,40f);\r
+ _gl.glPopMatrix(); // Restore world coordinate system.\r
+ //マーカ情報の描画\r
+ _gl.glPushMatrix(); // Save world coordinate system.\r
+ this._reality.glLoadModelViewMatrix(this._gl,m);\r
+ _gl.glTranslatef(0,0,90f); // Place base of cube on marker surface.\r
+ _gl.glRotatef(90,1.0f,0.0f,0.0f); // Place base of cube on marker surface.\r
+ NyARGLDrawUtil.setFontColor(t.getGrabbRate()<50?Color.RED:Color.BLUE);\r
+ ARTKMarkerTable.GetBestMatchTargetResult d=((ARTKMarkerTable.GetBestMatchTargetResult)(t.tag));\r
+ NyARGLDrawUtil.drawText("Name:"+d.name+" GRUB:"+t.grab_rate+"%",0.5f);\r
+ _gl.glPopMatrix();\r
+ \r
+ break;\r
+ case NyARRealityTarget.RT_UNKNOWN:\r
+ NyARDoublePoint2d[] p=t.refTargetVertex(); \r
+ NyARGLDrawUtil.beginScreenCoordinateSystem(this._gl,SCREEN_X,SCREEN_Y,true);\r
+ _gl.glLineWidth(2);\r
+ _gl.glColor3f(1.0f,0.0f,0.0f);\r
+ _gl.glBegin(GL.GL_LINE_LOOP);\r
+ _gl.glVertex2d(p[0].x,p[0].y);\r
+ _gl.glVertex2d(p[1].x,p[1].y);\r
+ _gl.glVertex2d(p[2].x,p[2].y);\r
+ _gl.glVertex2d(p[3].x,p[3].y);\r
+ _gl.glEnd();\r
+ NyARGLDrawUtil.endScreenCoordinateSystem(this._gl);\r
+ \r
+ break;\r
+ }\r
+ }\r
+ }\r
+ Thread.sleep(1);// タスク実行権限を一旦渡す\r
+ }catch(Exception e){\r
+ e.printStackTrace();\r
+ }\r
+\r
+ }\r
+\r
+ /**\r
+ * カメラのキャプチャした画像を非同期に受け取る関数。\r
+ * 画像を受け取ると、同期を取ってRealityを1サイクル進めます。\r
+ */\r
+ public void onUpdateBuffer(Buffer i_buffer)\r
+ {\r
+ try {\r
+ synchronized (this._sync_object)\r
+ {\r
+ this._src.setImage(i_buffer);\r
+ this._reality.progress(this._src);\r
+ //UnknownTargetを1個取得して、遷移を試す。\r
+ NyARRealityTarget t=this._reality.selectSingleUnknownTarget();\r
+ if(t==null){\r
+ return;\r
+ }\r
+ //ターゲットに一致するデータを検索\r
+ ARTKMarkerTable.GetBestMatchTargetResult r=new ARTKMarkerTable.GetBestMatchTargetResult();\r
+ if(this._mklib.getBestMatchTarget(t,this._src,r)){\r
+ if(r.confidence<0.6)\r
+ { //一致率が低すぎる。\r
+ return;\r
+ }\r
+ //既に認識しているターゲットの内側のものでないか確認する?(この処理をすれば、二重認識は無くなる。)\r
+ \r
+ //一致度を確認して、80%以上ならKnownターゲットへ遷移\r
+ if(!this._reality.changeTargetToKnown(t,r.artk_direction,r.marker_width)){\r
+ //遷移の成功チェック\r
+ return;//失敗\r
+ }\r
+ //遷移に成功したので、tagにResult情報をコピーしておく。(後で表示に使う)\r
+ t.tag=r;\r
+ }else{\r
+ //一致しないので、このターゲットは捨てる。\r
+ this._reality.changeTargetToDead(t,15);\r
+ }\r
+ }\r
+ } catch (Exception e) {\r
+ e.printStackTrace();\r
+ }\r
+ }\r
+\r
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged)\r
+ {\r
+ }\r
+ \r
+ private final static String PARAM_FILE = "../../Data/camera_para.dat";\r
+ private final static String PATT_HIRO = "../../Data/patt.hiro";\r
+ private final static String PATT_KANJI = "../../Data/patt.kanji";\r
+\r
+ public static void main(String[] args)\r
+ {\r
+ try {\r
+ NyARParam param = new NyARParam();\r
+ param.loadARParamFromFile(PARAM_FILE);\r
+ new Test_NyARRealityGl_ARMarker(param);\r
+ } catch (Exception e) {\r
+ e.printStackTrace();\r
+ }\r
+ return;\r
+ }\r
+\r
+}\r
--- /dev/null
+/* \r
+ * PROJECT: NyARToolkit JOGL sample program.\r
+ * --------------------------------------------------------------------------------\r
+ * The MIT License\r
+ * Copyright (c) 2008-2011 nyatla\r
+ * airmail(at)ebony.plala.or.jp\r
+ * http://nyatla.jp/nyartoolkit/\r
+ * \r
+ * Permission is hereby granted, free of charge, to any person obtaining a copy\r
+ * of this software and associated documentation files (the "Software"), to deal\r
+ * in the Software without restriction, including without limitation the rights\r
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\r
+ * copies of the Software, and to permit persons to whom the Software is\r
+ * furnished to do so, subject to the following conditions:\r
+ * The above copyright notice and this permission notice shall be included in\r
+ * all copies or substantial portions of the Software.\r
+ * \r
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\r
+ * THE SOFTWARE.\r
+ * \r
+ */\r
+package jp.nyatla.nyartoolkit.rpf.sample;\r
+\r
+import java.awt.Color;\r
+import java.awt.Font;\r
+import java.awt.Frame;\r
+import java.awt.Insets;\r
+import java.awt.event.WindowAdapter;\r
+import java.awt.event.WindowEvent;\r
+\r
+import javax.media.Buffer;\r
+import javax.media.opengl.*;\r
+\r
+import jp.nyatla.nyartoolkit.NyARException;\r
+import jp.nyatla.nyartoolkit.core.param.NyARParam;\r
+import jp.nyatla.nyartoolkit.core.types.NyARIntPoint2d;\r
+import jp.nyatla.nyartoolkit.core.types.matrix.NyARDoubleMatrix44;\r
+import jp.nyatla.nyartoolkit.rpf.mklib.ASyncIdMarkerTable;\r
+import jp.nyatla.nyartoolkit.rpf.mklib.ASyncIdMarkerTable.IResultListener;\r
+import jp.nyatla.nyartoolkit.rpf.reality.nyartk.NyARRealityTarget;\r
+import jp.nyatla.nyartoolkit.rpf.reality.nyartk.NyARRealityTargetList;\r
+import jp.nyatla.nyartoolkit.rpf.realitysource.nyartk.NyARRealitySource_Jmf;\r
+import jp.nyatla.nyartoolkit.jmf.utils.JmfCaptureDevice;\r
+import jp.nyatla.nyartoolkit.jmf.utils.JmfCaptureDeviceList;\r
+import jp.nyatla.nyartoolkit.jmf.utils.JmfCaptureListener;\r
+import jp.nyatla.nyartoolkit.jogl.utils.NyARGLDrawUtil;\r
+import jp.nyatla.nyartoolkit.rpf.reality.nyartk.gl.NyARRealityGl;\r
+\r
+import com.sun.opengl.util.Animator;\r
+\r
+/**\r
+ * NyARRealityシステムのサンプル。\r
+ * このプログラムは、非同期マーカー認識の実験プログラムです。\r
+ * リアルタイムにマーカ一致を行わずに、外部プログラムやサーバなどで非同期にマーカ認識処理を行うテンプレートになります。\r
+ * \r
+ * このプログラムでは、IDマーカの非同期認識(別スレッドで認識)を行い、認識が成功したらその上に立方体を表示します。\r
+ * \r
+ * マーカには、IDマーカを使ってください。\r
+ * @author nyatla\r
+ *\r
+ */\r
+public class Test_NyARRealityGl_AsyncIdMarker implements GLEventListener, JmfCaptureListener,IResultListener\r
+{\r
+\r
+ private final static int SCREEN_X = 320;\r
+ private final static int SCREEN_Y = 240;\r
+\r
+ private Animator _animator;\r
+ private JmfCaptureDevice _capture;\r
+\r
+ private GL _gl;\r
+\r
+ private Object _sync_object=new Object();\r
+\r
+ NyARRealityGl _reality;\r
+ NyARRealitySource_Jmf _src;\r
+ ASyncIdMarkerTable _mklib;\r
+\r
+ public Test_NyARRealityGl_AsyncIdMarker(NyARParam i_param) throws NyARException\r
+ {\r
+ Frame frame = new Frame("NyARToolkit+RPF["+this.getClass().getName()+"]");\r
+ \r
+ // キャプチャの準備\r
+ JmfCaptureDeviceList devlist = new JmfCaptureDeviceList();\r
+ this._capture = devlist.getDevice(0);\r
+ if (!this._capture.setCaptureFormat(SCREEN_X, SCREEN_Y, 30.0f)) {\r
+ throw new NyARException();\r
+ }\r
+ this._capture.setOnCapture(this);\r
+ //Realityの構築\r
+ i_param.changeScreenSize(SCREEN_X, SCREEN_Y); \r
+ //キャプチャ画像と互換性のあるRealitySourceを構築\r
+// this._src=new NyARRealitySource_Jmf(this._capture.getCaptureFormat(),i_param.getDistortionFactor(),2,100);\r
+ this._src=new NyARRealitySource_Jmf(this._capture.getCaptureFormat(),null,2,100);\r
+ //OpenGL互換のRealityを構築 \r
+ this._reality=new NyARRealityGl(i_param.getPerspectiveProjectionMatrix(),i_param.getScreenSize(),10,10000,3,3);\r
+ //非同期マーカライブラリ(NyId)の構築\r
+ this._mklib= new ASyncIdMarkerTable(this);\r
+ \r
+ // 3Dを描画するコンポーネント\r
+ GLCanvas canvas = new GLCanvas();\r
+ frame.add(canvas);\r
+ canvas.addGLEventListener(this);\r
+ frame.addWindowListener(new WindowAdapter() {\r
+ public void windowClosing(WindowEvent e)\r
+ {\r
+ System.exit(0);\r
+ }\r
+ });\r
+\r
+ frame.setVisible(true);\r
+ Insets ins = frame.getInsets();\r
+ frame.setSize(SCREEN_X + ins.left + ins.right, SCREEN_Y + ins.top + ins.bottom);\r
+ canvas.setBounds(ins.left, ins.top, SCREEN_X, SCREEN_Y);\r
+ }\r
+\r
+ public void init(GLAutoDrawable drawable)\r
+ {\r
+ this._gl = drawable.getGL();\r
+ this._gl.glEnable(GL.GL_DEPTH_TEST);\r
+ this._gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);\r
+ NyARGLDrawUtil.setFontStyle("SansSerif",Font.BOLD,24);\r
+ // NyARToolkitの準備\r
+ try {\r
+ // キャプチャ開始\r
+ _capture.start();\r
+ } catch (Exception e) {\r
+ e.printStackTrace();\r
+ }\r
+ this._animator = new Animator(drawable);\r
+ this._animator.start();\r
+ return;\r
+ }\r
+\r
+ public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height)\r
+ {\r
+ _gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);\r
+ _gl.glViewport(0, 0, width, height);\r
+\r
+ // 視体積の設定\r
+ _gl.glMatrixMode(GL.GL_PROJECTION);\r
+ _gl.glLoadIdentity();\r
+ // 見る位置\r
+ _gl.glMatrixMode(GL.GL_MODELVIEW);\r
+ _gl.glLoadIdentity();\r
+ }\r
+\r
+ public void display(GLAutoDrawable drawable)\r
+ {\r
+ //RealitySourceにデータが処理する。\r
+ if(!this._src.isReady())\r
+ {\r
+ return;\r
+ }\r
+ \r
+ // 背景を書く\r
+ this._gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); // Clear the buffers for new frame.\r
+ try{\r
+ synchronized(this._sync_object){\r
+ \r
+ this._reality.glDrawRealitySource(this._gl,this._src);\r
+ // Projection transformation.\r
+ this._gl.glMatrixMode(GL.GL_PROJECTION);\r
+ this._reality.glLoadCameraFrustum(this._gl);\r
+ //ターゲットリストを走査して、画面に内容を反映\r
+ NyARRealityTargetList tl=this._reality.refTargetList();\r
+ for(int i=tl.getLength()-1;i>=0;i--){\r
+ NyARRealityTarget t=tl.getItem(i);\r
+ switch(t.getTargetType())\r
+ {\r
+ case NyARRealityTarget.RT_KNOWN:\r
+ //立方体の描画\r
+ this._gl.glMatrixMode(GL.GL_MODELVIEW);\r
+ this._gl.glLoadIdentity();\r
+ NyARDoubleMatrix44 m=t.refTransformMatrix();\r
+ this._reality.glLoadModelViewMatrix(this._gl,m);\r
+ _gl.glPushMatrix(); // Save world coordinate system.\r
+ //_gl.glRotatef(90,0.0f,0.0f,1.0f); // Place base of cube on marker surface.\r
+ \r
+ _gl.glTranslatef(0,0,10f); // Place base of cube on marker surface.\r
+ _gl.glDisable(GL.GL_LIGHTING); // Just use colours.\r
+ NyARGLDrawUtil.drawColorCube(this._gl,20f);\r
+ _gl.glPopMatrix(); // Restore world coordinate system.\r
+ //マーカ情報の描画\r
+ _gl.glPushMatrix(); // Save world coordinate system.\r
+ this._reality.glLoadModelViewMatrix(this._gl,m);\r
+ _gl.glTranslatef(-30,0,40f); // Place base of cube on marker surface.\r
+ _gl.glRotatef(90,1.0f,0.0f,0.0f); // Place base of cube on marker surface.\r
+ //マーカ情報の表示\r
+ NyARGLDrawUtil.setFontColor(t.getGrabbRate()<50?Color.RED:Color.BLUE);\r
+ NyARGLDrawUtil.drawText("ID:"+(Long)(t.tag)+" GRUB:"+t.grab_rate+"%",0.5f);\r
+ _gl.glPopMatrix();\r
+ \r
+ break;\r
+ case NyARRealityTarget.RT_UNKNOWN:\r
+ NyARGLDrawUtil.beginScreenCoordinateSystem(this._gl,SCREEN_X,SCREEN_Y,false);\r
+ NyARGLDrawUtil.setFontColor(t.getGrabbRate()<50?Color.RED:Color.BLUE);\r
+ NyARIntPoint2d cp=new NyARIntPoint2d();\r
+ t.getTargetCenter(cp);\r
+ _gl.glTranslated(cp.x,SCREEN_Y-cp.y,1);\r
+ NyARGLDrawUtil.drawText("now matching marker.",1f);\r
+ NyARGLDrawUtil.endScreenCoordinateSystem(this._gl); \r
+ break;\r
+ }\r
+ }\r
+ }\r
+ Thread.sleep(1);// タスク実行権限を一旦渡す\r
+ }catch(Exception e){\r
+ e.printStackTrace();\r
+ }\r
+\r
+ }\r
+ public void OnDetect(boolean i_result,long i_serial,int i_dir,double i_width,long i_id)\r
+ {\r
+ try{\r
+ //Realityを触るのでロック\r
+ synchronized (this._sync_object)\r
+ {\r
+ NyARRealityTarget t=this._reality.refTargetList().getItemBySerial(i_serial);\r
+ if(t==null){\r
+ return;\r
+ }\r
+ if(t.getTargetType()!=NyARRealityTarget.RT_UNKNOWN){\r
+ return;\r
+ }\r
+ if(i_result){\r
+ this._reality.changeTargetToKnown(t, i_dir, i_width);\r
+ t.tag=new Long(i_id);\r
+ }else{\r
+ this._reality.changeTargetToDead(t);\r
+ }\r
+ }\r
+ }catch(Exception e){\r
+ //握りつぶす\r
+ e.printStackTrace();\r
+ }\r
+ }\r
+\r
+ /**\r
+ * カメラのキャプチャした画像を非同期に受け取る関数。\r
+ * 画像を受け取ると、同期を取ってRealityを1サイクル進めます。\r
+ */\r
+ public void onUpdateBuffer(Buffer i_buffer)\r
+ {\r
+ try {\r
+ synchronized (this._sync_object)\r
+ {\r
+ this._src.setImage(i_buffer);\r
+ this._reality.progress(this._src);\r
+ //UnknownTargetを1個取得して、遷移を試す。\r
+ NyARRealityTarget t=this._reality.selectSingleUnknownTarget();\r
+ if(t==null){\r
+ return;\r
+ }\r
+ //tagに何か入ってたらなにかやっている。\r
+ if(t.tag!=null){\r
+ return;\r
+ }\r
+ this._mklib.requestAsyncMarkerDetect(this._reality,this._src,t);\r
+ t.tag=new Long(-1);//-1は認識中ということで。\r
+ }\r
+ } catch (Exception e) {\r
+ e.printStackTrace();\r
+ }\r
+ }\r
+\r
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged)\r
+ {\r
+ }\r
+ \r
+ private final static String PARAM_FILE = "../../Data/camera_para.dat";\r
+\r
+ public static void main(String[] args)\r
+ {\r
+ try {\r
+ NyARParam param = new NyARParam();\r
+ param.loadARParamFromFile(PARAM_FILE);\r
+ new Test_NyARRealityGl_AsyncIdMarker(param);\r
+ } catch (Exception e) {\r
+ e.printStackTrace();\r
+ }\r
+ return;\r
+ }\r
+\r
+}\r
--- /dev/null
+/* \r
+ * PROJECT: NyARToolkit JOGL sample program.\r
+ * --------------------------------------------------------------------------------\r
+ * The MIT License\r
+ * Copyright (c) 2008-2011 nyatla\r
+ * airmail(at)ebony.plala.or.jp\r
+ * http://nyatla.jp/nyartoolkit/\r
+ * \r
+ * Permission is hereby granted, free of charge, to any person obtaining a copy\r
+ * of this software and associated documentation files (the "Software"), to deal\r
+ * in the Software without restriction, including without limitation the rights\r
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\r
+ * copies of the Software, and to permit persons to whom the Software is\r
+ * furnished to do so, subject to the following conditions:\r
+ * The above copyright notice and this permission notice shall be included in\r
+ * all copies or substantial portions of the Software.\r
+ * \r
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\r
+ * THE SOFTWARE.\r
+ * \r
+ */\r
+package jp.nyatla.nyartoolkit.rpf.sample;\r
+\r
+import java.awt.Color;\r
+import java.awt.Font;\r
+import java.awt.Frame;\r
+import java.awt.Insets;\r
+import java.awt.event.WindowAdapter;\r
+import java.awt.event.WindowEvent;\r
+\r
+import javax.media.Buffer;\r
+import javax.media.opengl.*;\r
+\r
+import jp.nyatla.nyartoolkit.NyARException;\r
+import jp.nyatla.nyartoolkit.core.param.NyARParam;\r
+import jp.nyatla.nyartoolkit.core.types.NyARDoublePoint2d;\r
+import jp.nyatla.nyartoolkit.core.types.NyARIntPoint2d;\r
+import jp.nyatla.nyartoolkit.core.types.matrix.NyARDoubleMatrix44;\r
+import jp.nyatla.nyartoolkit.rpf.mklib.CardDetect;\r
+import jp.nyatla.nyartoolkit.rpf.reality.nyartk.NyARRealityTarget;\r
+import jp.nyatla.nyartoolkit.rpf.reality.nyartk.NyARRealityTargetList;\r
+import jp.nyatla.nyartoolkit.rpf.realitysource.nyartk.NyARRealitySource_Jmf;\r
+import jp.nyatla.nyartoolkit.jmf.utils.JmfCaptureDevice;\r
+import jp.nyatla.nyartoolkit.jmf.utils.JmfCaptureDeviceList;\r
+import jp.nyatla.nyartoolkit.jmf.utils.JmfCaptureListener;\r
+import jp.nyatla.nyartoolkit.jogl.utils.NyARGLDrawUtil;\r
+import jp.nyatla.nyartoolkit.rpf.reality.nyartk.gl.NyARRealityGl;\r
+\r
+import com.sun.opengl.util.Animator;\r
+\r
+/**\r
+ * NyARRealityシステムのサンプル。\r
+ * このサンプルは、定型以外のマーカを認識する実験プログラムです。\r
+ *\r
+ * 未知の四角形の比率推定をして、立方体を表示します。\r
+ * このプログラムでは、未知の四角形を正面から撮影したときに比率を推定して、それがクレジットカードの比率に近ければ、\r
+ * クレジットカードと判定して、その上に立方体を表示します。\r
+ * \r
+ * マーカーには適当なクレジットカードを使ってください。\r
+ * \r
+ * エッジ検出の性能が十分でないため、カード検出には十分なコントラストが必要です。\r
+ * (白色のカードの場合は黒色の背景、黒色のカードなら白色の背景など。)\r
+ * \r
+ * @author nyatla\r
+ */\r
+public class Test_NyARRealityGl_CreditCardDetect implements GLEventListener, JmfCaptureListener\r
+{\r
+ long clock;\r
+ private final static int SCREEN_X = 640;\r
+ private final static int SCREEN_Y = 480;\r
+\r
+ private Animator _animator;\r
+ private JmfCaptureDevice _capture;\r
+\r
+ private GL _gl;\r
+\r
+ private Object _sync_object=new Object();\r
+\r
+ NyARRealityGl _reality;\r
+ NyARRealitySource_Jmf _src;\r
+ CardDetect _mklib;\r
+\r
+ public Test_NyARRealityGl_CreditCardDetect(NyARParam i_param) throws NyARException\r
+ {\r
+ clock=0;\r
+ Frame frame = new Frame("NyARToolkit+RPF["+this.getClass().getName()+"]");\r
+ \r
+ // キャプチャの準備\r
+ JmfCaptureDeviceList devlist = new JmfCaptureDeviceList();\r
+ this._capture = devlist.getDevice(0);\r
+ if (!this._capture.setCaptureFormat(SCREEN_X, SCREEN_Y, 15.0f)) {\r
+ throw new NyARException();\r
+ }\r
+ this._capture.setOnCapture(this);\r
+ //Realityの構築\r
+ i_param.changeScreenSize(SCREEN_X, SCREEN_Y); \r
+ //キャプチャ画像と互換性のあるRealitySourceを構築\r
+// this._src=new NyARRealitySource_Jmf(this._capture.getCaptureFormat(),i_param.getDistortionFactor(),2,100);\r
+ this._src=new NyARRealitySource_Jmf(this._capture.getCaptureFormat(),null,2,100);\r
+ //OpenGL互換のRealityを構築 \r
+ this._reality=new NyARRealityGl(i_param.getPerspectiveProjectionMatrix(),i_param.getScreenSize(),10,10000,3,10);\r
+ //マーカライブラリ(比率推定)の構築\r
+ this._mklib= new CardDetect();\r
+\r
+ \r
+ // 3Dを描画するコンポーネント\r
+ GLCanvas canvas = new GLCanvas();\r
+ frame.add(canvas);\r
+ canvas.addGLEventListener(this);\r
+ frame.addWindowListener(new WindowAdapter() {\r
+ public void windowClosing(WindowEvent e)\r
+ {\r
+ System.exit(0);\r
+ }\r
+ });\r
+\r
+ frame.setVisible(true);\r
+ Insets ins = frame.getInsets();\r
+ frame.setSize(SCREEN_X + ins.left + ins.right, SCREEN_Y + ins.top + ins.bottom);\r
+ canvas.setBounds(ins.left, ins.top, SCREEN_X, SCREEN_Y);\r
+ }\r
+\r
+ public void init(GLAutoDrawable drawable)\r
+ {\r
+ this._gl = drawable.getGL();\r
+ this._gl.glEnable(GL.GL_DEPTH_TEST);\r
+ this._gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);\r
+ NyARGLDrawUtil.setFontStyle("SansSerif",Font.BOLD,24);\r
+ // NyARToolkitの準備\r
+ try {\r
+ // キャプチャ開始\r
+ _capture.start();\r
+ } catch (Exception e) {\r
+ e.printStackTrace();\r
+ }\r
+ this._animator = new Animator(drawable);\r
+ this._animator.start();\r
+ return;\r
+ }\r
+\r
+ public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height)\r
+ {\r
+ _gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);\r
+ _gl.glViewport(0, 0, width, height);\r
+\r
+ // 視体積の設定\r
+ _gl.glMatrixMode(GL.GL_PROJECTION);\r
+ _gl.glLoadIdentity();\r
+ // 見る位置\r
+ _gl.glMatrixMode(GL.GL_MODELVIEW);\r
+ _gl.glLoadIdentity();\r
+ }\r
+\r
+ \r
+ public void display(GLAutoDrawable drawable)\r
+ {\r
+ //RealitySourceにデータが処理する。\r
+ if(!this._src.isReady())\r
+ {\r
+ return;\r
+ }\r
+ \r
+ // 背景を書く\r
+ this._gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); // Clear the buffers for new frame.\r
+ try{\r
+ synchronized(this._sync_object){\r
+ //背景を描画\r
+ this._reality.glDrawRealitySource(this._gl,this._src);\r
+ // Projection transformation.\r
+ this._gl.glMatrixMode(GL.GL_PROJECTION);\r
+ this._reality.glLoadCameraFrustum(this._gl);\r
+ //ターゲットリストを走査して、画面に内容を反映\r
+ NyARRealityTargetList tl=this._reality.refTargetList();\r
+ for(int i=tl.getLength()-1;i>=0;i--){\r
+ NyARRealityTarget t=tl.getItem(i);\r
+ CardDetect.UnknownRectInfo tag=((CardDetect.UnknownRectInfo)(t.tag));\r
+ switch(t.getTargetType())\r
+ {\r
+ case NyARRealityTarget.RT_KNOWN:\r
+ //立方体の描画\r
+ this._gl.glMatrixMode(GL.GL_MODELVIEW);\r
+ this._gl.glLoadIdentity();\r
+ NyARDoubleMatrix44 m=t.refTransformMatrix();\r
+ this._reality.glLoadModelViewMatrix(this._gl,m);\r
+ //カード番号を消す。\r
+ _gl.glColor3d(0.0,0.0, 0.0);\r
+ _gl.glBegin(GL.GL_POLYGON);\r
+ _gl.glVertex2d(-35,-20);\r
+ _gl.glVertex2d(35,-20);\r
+ _gl.glVertex2d(35,20);\r
+ _gl.glVertex2d(-35,20);\r
+ _gl.glEnd();\r
+ \r
+ _gl.glPushMatrix(); // Save world coordinate system.\r
+ _gl.glTranslatef(0,0,20f); // Place base of cube on marker surface.\r
+ _gl.glDisable(GL.GL_LIGHTING); // Just use colours.\r
+ _gl.glLineWidth(1);\r
+ NyARGLDrawUtil.drawColorCube(this._gl,40f);\r
+ _gl.glPopMatrix(); // Restore world coordinate system.\r
+ break;\r
+ case NyARRealityTarget.RT_UNKNOWN:\r
+ if(tag==null){\r
+ break;\r
+ }\r
+ if(t.getGrabbRate()<20){\r
+ break;\r
+ }\r
+ if(tag.last_status==CardDetect.MORE_FRONT_CENTER)\r
+ {\r
+ NyARDoublePoint2d[] p=t.refTargetVertex();\r
+ //もっと真ん中から写せメッセージ\r
+ double c=Math.sin((clock%45*4)*Math.PI/180);\r
+ _gl.glColor3d(c,c, 0.0);\r
+ _gl.glLineWidth(2);\r
+ NyARGLDrawUtil.beginScreenCoordinateSystem(this._gl,SCREEN_X,SCREEN_Y,true);\r
+ _gl.glBegin(GL.GL_LINE_LOOP);\r
+ _gl.glVertex2d(p[0].x,p[0].y);\r
+ _gl.glVertex2d(p[1].x,p[1].y);\r
+ _gl.glVertex2d(p[2].x,p[2].y);\r
+ _gl.glVertex2d(p[3].x,p[3].y);\r
+ _gl.glEnd();\r
+ NyARGLDrawUtil.endScreenCoordinateSystem(this._gl);\r
+ NyARGLDrawUtil.beginScreenCoordinateSystem(this._gl,SCREEN_X,SCREEN_Y,false);\r
+ NyARGLDrawUtil.setFontColor(t.getGrabbRate()<50?Color.RED:Color.BLUE);\r
+ NyARIntPoint2d cp=new NyARIntPoint2d();\r
+ t.getTargetCenter(cp);\r
+ _gl.glTranslated(cp.x,SCREEN_Y-cp.y,1);\r
+ NyARGLDrawUtil.drawText("Please view the card from the front. "+t.getGrabbRate(),1f);\r
+ NyARGLDrawUtil.endScreenCoordinateSystem(this._gl);\r
+ \r
+ }else if(tag.last_status==CardDetect.ESTIMATE_NOW)\r
+ {\r
+ NyARDoublePoint2d[] p=t.refTargetVertex();\r
+ //もっと真ん中から写せメッセージ\r
+ double c=Math.sin((clock%45*8)*Math.PI/180);\r
+ _gl.glColor3d(0,c, 0.0);\r
+ _gl.glLineWidth(2);\r
+ NyARGLDrawUtil.beginScreenCoordinateSystem(this._gl,SCREEN_X,SCREEN_Y,true);\r
+ _gl.glBegin(GL.GL_LINE_LOOP);\r
+ _gl.glVertex2d(p[0].x,p[0].y);\r
+ _gl.glVertex2d(p[1].x,p[1].y);\r
+ _gl.glVertex2d(p[2].x,p[2].y);\r
+ _gl.glVertex2d(p[3].x,p[3].y);\r
+ _gl.glEnd();\r
+ NyARGLDrawUtil.endScreenCoordinateSystem(this._gl);\r
+ NyARGLDrawUtil.beginScreenCoordinateSystem(this._gl,SCREEN_X,SCREEN_Y,false);\r
+ NyARGLDrawUtil.setFontColor(t.getGrabbRate()<50?Color.RED:Color.BLUE);\r
+ NyARIntPoint2d cp=new NyARIntPoint2d();\r
+ t.getTargetCenter(cp);\r
+ _gl.glTranslated(cp.x,SCREEN_Y-cp.y,1);\r
+ NyARGLDrawUtil.drawText("Check card size...",1f);\r
+ NyARGLDrawUtil.endScreenCoordinateSystem(this._gl);\r
+ \r
+ }\r
+ break;\r
+ }\r
+ }\r
+ }\r
+ Thread.sleep(1);// タスク実行権限を一旦渡す\r
+ }catch(Exception e){\r
+ e.printStackTrace();\r
+ }\r
+\r
+ }\r
+ /**\r
+ * カメラのキャプチャした画像を非同期に受け取る関数。\r
+ * 画像を受け取ると、同期を取ってRealityを1サイクル進めます。\r
+ */\r
+ public void onUpdateBuffer(Buffer i_buffer)\r
+ {\r
+ //クロック進行\r
+ clock++;\r
+ try {\r
+ synchronized (this._sync_object)\r
+ {\r
+ this._src.setImage(i_buffer);\r
+ this._reality.progress(this._src);\r
+ for(int i=this._reality.refTargetList().getLength()-1;i>=0;i--)\r
+ {\r
+ NyARRealityTarget t=this._reality.refTargetList().getItem(i);\r
+ switch(t.getTargetType())\r
+ {\r
+ case NyARRealityTarget.RT_UNKNOWN:\r
+ //tagに推定用オブジェクトが割り当てられていなければ割り当てる。\r
+ if(t.tag==null){\r
+ t.tag=new CardDetect.UnknownRectInfo();\r
+ }\r
+ CardDetect.UnknownRectInfo r=(CardDetect.UnknownRectInfo)t.tag;\r
+ //推定\r
+ this._mklib.detectCardDirection(t, r);\r
+ switch(r.last_status){\r
+ case CardDetect.ESTIMATE_COMPLETE:\r
+ //レートチェック(17/11(1.54)くらいならクレジットカードサイズ。)\r
+ if(1.35<r.rate && r.rate<1.75){\r
+ //クレジットカードサイズをセット\r
+ if(!this._reality.changeTargetToKnown(t,r.artk_direction,85,55)){\r
+ //遷移の成功チェック\r
+ break;//失敗\r
+ }\r
+ }else{\r
+ //サイズ違う?\r
+ this._reality.changeTargetToDead(t,50);\r
+ }\r
+ break;\r
+ case CardDetect.ESTIMATE_NOW:\r
+ break;\r
+ case CardDetect.FAILED_ESTIMATE:\r
+ this._reality.changeTargetToDead(t,15);\r
+ break;\r
+ }\r
+ default:\r
+ //他の種類は特にやることは無い。\r
+ }\r
+ }\r
+ }\r
+ } catch (Exception e) {\r
+ e.printStackTrace();\r
+ }\r
+ }\r
+\r
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged)\r
+ {\r
+ }\r
+ \r
+ private final static String PARAM_FILE = "../../Data/camera_para.dat";\r
+\r
+ public static void main(String[] args)\r
+ {\r
+ try {\r
+ NyARParam param = new NyARParam();\r
+ param.loadARParamFromFile(PARAM_FILE);\r
+ new Test_NyARRealityGl_CreditCardDetect(param);\r
+ } catch (Exception e) {\r
+ e.printStackTrace();\r
+ }\r
+ return;\r
+ }\r
+\r
+}\r
--- /dev/null
+/* \r
+ * PROJECT: NyARToolkit JOGL sample program.\r
+ * --------------------------------------------------------------------------------\r
+ * The MIT License\r
+ * Copyright (c) 2008-2011 nyatla\r
+ * airmail(at)ebony.plala.or.jp\r
+ * http://nyatla.jp/nyartoolkit/\r
+ * \r
+ * Permission is hereby granted, free of charge, to any person obtaining a copy\r
+ * of this software and associated documentation files (the "Software"), to deal\r
+ * in the Software without restriction, including without limitation the rights\r
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\r
+ * copies of the Software, and to permit persons to whom the Software is\r
+ * furnished to do so, subject to the following conditions:\r
+ * The above copyright notice and this permission notice shall be included in\r
+ * all copies or substantial portions of the Software.\r
+ * \r
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\r
+ * THE SOFTWARE.\r
+ * \r
+ */\r
+package jp.nyatla.nyartoolkit.rpf.sample;\r
+\r
+import java.awt.Color;\r
+import java.awt.Font;\r
+import java.awt.Frame;\r
+import java.awt.Insets;\r
+import java.awt.event.WindowAdapter;\r
+import java.awt.event.WindowEvent;\r
+\r
+import javax.media.Buffer;\r
+import javax.media.opengl.*;\r
+\r
+import jp.nyatla.nyartoolkit.NyARException;\r
+import jp.nyatla.nyartoolkit.core.param.NyARParam;\r
+import jp.nyatla.nyartoolkit.core.types.NyARDoublePoint2d;\r
+import jp.nyatla.nyartoolkit.core.types.matrix.NyARDoubleMatrix44;\r
+import jp.nyatla.nyartoolkit.rpf.mklib.RawbitSerialIdTable;\r
+import jp.nyatla.nyartoolkit.rpf.reality.nyartk.NyARRealityTarget;\r
+import jp.nyatla.nyartoolkit.rpf.reality.nyartk.NyARRealityTargetList;\r
+import jp.nyatla.nyartoolkit.rpf.realitysource.nyartk.NyARRealitySource_Jmf;\r
+import jp.nyatla.nyartoolkit.jmf.utils.JmfCaptureDevice;\r
+import jp.nyatla.nyartoolkit.jmf.utils.JmfCaptureDeviceList;\r
+import jp.nyatla.nyartoolkit.jmf.utils.JmfCaptureListener;\r
+import jp.nyatla.nyartoolkit.jogl.utils.NyARGLDrawUtil;\r
+import jp.nyatla.nyartoolkit.rpf.reality.nyartk.gl.NyARRealityGl;\r
+\r
+import com.sun.opengl.util.Animator;\r
+\r
+/**\r
+ * NyARRealityシステムのサンプル。\r
+ * IDマーカの上に立方体を表示するサンプルプログラムです。\r
+ * RealitySourceにはJMFを使用し、RealityにはRealityGLを使います。\r
+ * @author nyatla\r
+ *\r
+ */\r
+public class Test_NyARRealityGl_IdMarker implements GLEventListener, JmfCaptureListener\r
+{\r
+\r
+ private final static int SCREEN_X = 640;\r
+ private final static int SCREEN_Y = 480;\r
+\r
+ private Animator _animator;\r
+ private JmfCaptureDevice _capture;\r
+\r
+ private GL _gl;\r
+\r
+ private Object _sync_object=new Object();\r
+\r
+ NyARRealityGl _reality;\r
+ NyARRealitySource_Jmf _src;\r
+ RawbitSerialIdTable _mklib;\r
+\r
+ public Test_NyARRealityGl_IdMarker(NyARParam i_param) throws NyARException\r
+ {\r
+ Frame frame = new Frame("NyARToolkit+RPF["+this.getClass().getName()+"]");\r
+ \r
+ // キャプチャの準備\r
+ JmfCaptureDeviceList devlist = new JmfCaptureDeviceList();\r
+ this._capture = devlist.getDevice(0);\r
+ if (!this._capture.setCaptureFormat(SCREEN_X, SCREEN_Y, 30.0f)) {\r
+ throw new NyARException();\r
+ }\r
+ this._capture.setOnCapture(this);\r
+ //Realityの構築\r
+ i_param.changeScreenSize(SCREEN_X, SCREEN_Y); \r
+ //キャプチャ画像と互換性のあるRealitySourceを構築\r
+// this._src=new NyARRealitySource_Jmf(this._capture.getCaptureFormat(),i_param.getDistortionFactor(),2,100);\r
+ this._src=new NyARRealitySource_Jmf(this._capture.getCaptureFormat(),null,2,100);\r
+ //OpenGL互換のRealityを構築 \r
+ this._reality=new NyARRealityGl(i_param.getPerspectiveProjectionMatrix(),i_param.getScreenSize(),10,10000,3,10);\r
+ //マーカライブラリ(NyId)の構築\r
+ this._mklib= new RawbitSerialIdTable(10);\r
+ //マーカサイズテーブルの作成(とりあえず全部8cm)\r
+ this._mklib.addAnyItem("any id",80);\r
+ \r
+ // 3Dを描画するコンポーネント\r
+ GLCanvas canvas = new GLCanvas();\r
+ frame.add(canvas);\r
+ canvas.addGLEventListener(this);\r
+ frame.addWindowListener(new WindowAdapter() {\r
+ public void windowClosing(WindowEvent e)\r
+ {\r
+ System.exit(0);\r
+ }\r
+ });\r
+\r
+ frame.setVisible(true);\r
+ Insets ins = frame.getInsets();\r
+ frame.setSize(SCREEN_X + ins.left + ins.right, SCREEN_Y + ins.top + ins.bottom);\r
+ canvas.setBounds(ins.left, ins.top, SCREEN_X, SCREEN_Y);\r
+ }\r
+\r
+ public void init(GLAutoDrawable drawable)\r
+ {\r
+ this._gl = drawable.getGL();\r
+ this._gl.glEnable(GL.GL_DEPTH_TEST);\r
+ this._gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);\r
+ NyARGLDrawUtil.setFontStyle("SansSerif",Font.BOLD,24);\r
+ // NyARToolkitの準備\r
+ try {\r
+ // キャプチャ開始\r
+ _capture.start();\r
+ } catch (Exception e) {\r
+ e.printStackTrace();\r
+ }\r
+ this._animator = new Animator(drawable);\r
+ this._animator.start();\r
+ return;\r
+ }\r
+\r
+ public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height)\r
+ {\r
+ _gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);\r
+ _gl.glViewport(0, 0, width, height);\r
+\r
+ // 視体積の設定\r
+ _gl.glMatrixMode(GL.GL_PROJECTION);\r
+ _gl.glLoadIdentity();\r
+ // 見る位置\r
+ _gl.glMatrixMode(GL.GL_MODELVIEW);\r
+ _gl.glLoadIdentity();\r
+ }\r
+\r
+ public void display(GLAutoDrawable drawable)\r
+ {\r
+ //RealitySourceにデータが処理する。\r
+ if(!this._src.isReady())\r
+ {\r
+ return;\r
+ }\r
+ \r
+ // 背景を書く\r
+ this._gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); // Clear the buffers for new frame.\r
+ try{\r
+ synchronized(this._sync_object){\r
+ \r
+ this._reality.glDrawRealitySource(this._gl,this._src);\r
+ // Projection transformation.\r
+ this._gl.glMatrixMode(GL.GL_PROJECTION);\r
+ this._reality.glLoadCameraFrustum(this._gl);\r
+ //ターゲットリストを走査して、画面に内容を反映\r
+ NyARRealityTargetList tl=this._reality.refTargetList();\r
+ for(int i=tl.getLength()-1;i>=0;i--){\r
+ NyARRealityTarget t=tl.getItem(i);\r
+ switch(t.getTargetType())\r
+ {\r
+ case NyARRealityTarget.RT_KNOWN:\r
+ //立方体の描画\r
+ this._gl.glMatrixMode(GL.GL_MODELVIEW);\r
+ this._gl.glLoadIdentity();\r
+ NyARDoubleMatrix44 m=t.refTransformMatrix();\r
+ this._reality.glLoadModelViewMatrix(this._gl,m);\r
+ _gl.glPushMatrix(); // Save world coordinate system.\r
+ _gl.glTranslatef(0,0,40f); // Place base of cube on marker surface.\r
+ _gl.glDisable(GL.GL_LIGHTING); // Just use colours.\r
+ NyARGLDrawUtil.drawColorCube(this._gl,80f);\r
+ _gl.glPopMatrix(); // Restore world coordinate system.\r
+ //マーカ情報の描画\r
+ _gl.glPushMatrix(); // Save world coordinate system.\r
+ this._reality.glLoadModelViewMatrix(this._gl,m);\r
+ _gl.glTranslatef(-30,0,90f); // Place base of cube on marker surface.\r
+ _gl.glRotatef(90,1.0f,0.0f,0.0f); // Place base of cube on marker surface.\r
+ //マーカ情報の表示\r
+ NyARGLDrawUtil.setFontColor(t.getGrabbRate()<50?Color.RED:Color.BLUE);\r
+ NyARGLDrawUtil.drawText("ID:"+(Long)(t.tag)+" GRUB:"+t.grab_rate+"%",0.5f);\r
+ _gl.glPopMatrix();\r
+ \r
+ break;\r
+ case NyARRealityTarget.RT_UNKNOWN:\r
+ NyARDoublePoint2d[] p=t.refTargetVertex(); \r
+ NyARGLDrawUtil.beginScreenCoordinateSystem(this._gl,SCREEN_X,SCREEN_Y,true);\r
+ _gl.glLineWidth(2);\r
+ _gl.glColor3f(1.0f,0.0f,0.0f);\r
+ _gl.glBegin(GL.GL_LINE_LOOP);\r
+ _gl.glVertex2d(p[0].x,p[0].y);\r
+ _gl.glVertex2d(p[1].x,p[1].y);\r
+ _gl.glVertex2d(p[2].x,p[2].y);\r
+ _gl.glVertex2d(p[3].x,p[3].y);\r
+ _gl.glEnd();\r
+ NyARGLDrawUtil.endScreenCoordinateSystem(this._gl);\r
+ \r
+ break;\r
+ }\r
+ }\r
+ }\r
+ Thread.sleep(1);// タスク実行権限を一旦渡す\r
+ }catch(Exception e){\r
+ e.printStackTrace();\r
+ }\r
+\r
+ }\r
+\r
+ /**\r
+ * カメラのキャプチャした画像を非同期に受け取る関数。\r
+ * 画像を受け取ると、同期を取ってRealityを1サイクル進めます。\r
+ */\r
+ public void onUpdateBuffer(Buffer i_buffer)\r
+ {\r
+ try {\r
+ synchronized (this._sync_object)\r
+ {\r
+ this._src.setImage(i_buffer);\r
+ this._reality.progress(this._src);\r
+ //UnknownTargetを1個取得して、遷移を試す。\r
+ NyARRealityTarget t=this._reality.selectSingleUnknownTarget();\r
+ if(t==null){\r
+ return;\r
+ }\r
+ //ターゲットに一致するデータを検索\r
+ RawbitSerialIdTable.IdentifyIdResult r=new RawbitSerialIdTable.IdentifyIdResult();\r
+ if(this._mklib.identifyId(t,this._src,r)){\r
+ //テーブルにターゲットが見つかったので遷移する。\r
+ if(!this._reality.changeTargetToKnown(t,r.artk_direction,r.marker_width)){\r
+ //遷移の成功チェック\r
+ return;//失敗\r
+ }\r
+ //遷移に成功したので、tagにユーザ定義情報を書きこむ。\r
+ t.tag=new Long(r.id);\r
+ }else{\r
+ //一致しないので、このターゲットは捨てる。(15フレーム無視)\r
+ this._reality.changeTargetToDead(t,15);\r
+ }\r
+ }\r
+ } catch (Exception e) {\r
+ e.printStackTrace();\r
+ }\r
+ }\r
+\r
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged)\r
+ {\r
+ }\r
+ \r
+ private final static String PARAM_FILE = "../../Data/camera_para.dat";\r
+\r
+ public static void main(String[] args)\r
+ {\r
+ try {\r
+ NyARParam param = new NyARParam();\r
+ param.loadARParamFromFile(PARAM_FILE);\r
+ new Test_NyARRealityGl_IdMarker(param);\r
+ } catch (Exception e) {\r
+ e.printStackTrace();\r
+ }\r
+ return;\r
+ }\r
+\r
+}\r
--- /dev/null
+/* \r
+ * PROJECT: NyARToolkit JOGL sample program.\r
+ * --------------------------------------------------------------------------------\r
+ * The MIT License\r
+ * Copyright (c) 2008-2011 nyatla\r
+ * airmail(at)ebony.plala.or.jp\r
+ * http://nyatla.jp/nyartoolkit/\r
+ * \r
+ * Permission is hereby granted, free of charge, to any person obtaining a copy\r
+ * of this software and associated documentation files (the "Software"), to deal\r
+ * in the Software without restriction, including without limitation the rights\r
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\r
+ * copies of the Software, and to permit persons to whom the Software is\r
+ * furnished to do so, subject to the following conditions:\r
+ * The above copyright notice and this permission notice shall be included in\r
+ * all copies or substantial portions of the Software.\r
+ * \r
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\r
+ * THE SOFTWARE.\r
+ * \r
+ */\r
+package jp.nyatla.nyartoolkit.rpf.sample;\r
+\r
+import java.awt.Frame;\r
+import java.awt.Insets;\r
+import java.awt.event.MouseEvent;\r
+import java.awt.event.MouseListener;\r
+import java.awt.event.WindowAdapter;\r
+import java.awt.event.WindowEvent;\r
+\r
+import javax.media.Buffer;\r
+import javax.media.opengl.*;\r
+\r
+import jp.nyatla.nyartoolkit.NyARException;\r
+import jp.nyatla.nyartoolkit.core.param.NyARParam;\r
+import jp.nyatla.nyartoolkit.core.types.NyARDoublePoint3d;\r
+import jp.nyatla.nyartoolkit.rpf.mklib.RawbitSerialIdTable;\r
+import jp.nyatla.nyartoolkit.rpf.reality.nyartk.NyARRealityTarget;\r
+import jp.nyatla.nyartoolkit.rpf.reality.nyartk.NyARRealityTargetList;\r
+import jp.nyatla.nyartoolkit.rpf.realitysource.nyartk.NyARRealitySource_Jmf;\r
+import jp.nyatla.nyartoolkit.jmf.utils.JmfCaptureDevice;\r
+import jp.nyatla.nyartoolkit.jmf.utils.JmfCaptureDeviceList;\r
+import jp.nyatla.nyartoolkit.jmf.utils.JmfCaptureListener;\r
+import jp.nyatla.nyartoolkit.jogl.utils.NyARGLDrawUtil;\r
+import jp.nyatla.nyartoolkit.rpf.reality.nyartk.gl.NyARRealityGl;\r
+\r
+import com.sun.opengl.util.Animator;\r
+\r
+/**\r
+ * このプログラムは、NyARRealityシステムのデバックプログラムです。\r
+ * ピッキングの実験ができます。\r
+ * 画面上のクリックした位置に、立方体を移動することができます。\r
+ * マーカは、idマーカを使います。\r
+ * @author nyatla\r
+ *\r
+ */\r
+public class Test_NyARRealityGl_ScreenPos implements GLEventListener, JmfCaptureListener,MouseListener\r
+{\r
+\r
+ private final static int SCREEN_X = 640;\r
+ private final static int SCREEN_Y = 480;\r
+\r
+ private Animator _animator;\r
+ private JmfCaptureDevice _capture;\r
+\r
+ private GL _gl;\r
+\r
+\r
+ private Object _sync_object=new Object();\r
+\r
+ NyARRealityGl _reality;\r
+ NyARRealitySource_Jmf _src;\r
+ RawbitSerialIdTable _mklib;\r
+ public void mouseClicked(MouseEvent e)\r
+ {\r
+ int x=e.getX();\r
+ int y=e.getY();\r
+// System.out.println(x+":"+y);\r
+ \r
+ synchronized(this._sync_object)\r
+ {\r
+ for(int i=this._reality.refTargetList().getLength()-1;i>=0;i--)\r
+ {\r
+ NyARRealityTarget rt=this._reality.refTargetList().getItem(i);\r
+ if(rt._target_type!=NyARRealityTarget.RT_KNOWN){\r
+ continue;\r
+ }\r
+ //ヒットするKnownターゲットを探す\r
+\r
+ if(e.getButton()==MouseEvent.BUTTON1){\r
+ NyARDoublePoint3d p=new NyARDoublePoint3d();\r
+ this._reality.refFrustum().unProjectOnMatrix(x,y,rt._transform_matrix,p);\r
+ NyARDoublePoint3d tag=(NyARDoublePoint3d)rt.tag;\r
+ tag.setValue(p); \r
+ //タグの現在位置を変えてみる。\r
+\r
+ }\r
+ }\r
+ }\r
+ }\r
+ public void mouseEntered(MouseEvent e){}\r
+ public void mouseExited(MouseEvent e){}\r
+ public void mousePressed(MouseEvent e){}\r
+ public void mouseReleased(MouseEvent e){}\r
+ public Test_NyARRealityGl_ScreenPos(NyARParam i_param) throws NyARException\r
+ {\r
+ Frame frame = new Frame("NyARToolkit+RPF["+this.getClass().getName()+"]");\r
+ \r
+ // キャプチャの準備\r
+ JmfCaptureDeviceList devlist = new JmfCaptureDeviceList();\r
+ this._capture = devlist.getDevice(0);\r
+ if (!this._capture.setCaptureFormat(SCREEN_X, SCREEN_Y, 30.0f)) {\r
+ throw new NyARException();\r
+ }\r
+ this._capture.setOnCapture(this);\r
+ //Realityの構築\r
+ i_param.changeScreenSize(SCREEN_X, SCREEN_Y); \r
+ //キャプチャ画像と互換性のあるRealitySourceを構築\r
+// this._src=new NyARRealitySource_Jmf(this._capture.getCaptureFormat(),i_param.getDistortionFactor(),2,100);\r
+ this._src=new NyARRealitySource_Jmf(this._capture.getCaptureFormat(),null,2,100);\r
+ //OpenGL互換のRealityを構築\r
+ this._reality=new NyARRealityGl(i_param.getPerspectiveProjectionMatrix(),i_param.getScreenSize(),10,10000,3,3);\r
+ //マーカライブラリ(NyId)の構築\r
+ this._mklib= new RawbitSerialIdTable(10);\r
+ //マーカサイズテーブルの作成(とりあえず全部4cm)\r
+ this._mklib.addAnyItem("ANY_ITEM",80);\r
+ \r
+ // 3Dを描画するコンポーネント\r
+ GLCanvas canvas = new GLCanvas();\r
+ frame.add(canvas);\r
+ canvas.addGLEventListener(this);\r
+ frame.addWindowListener(new WindowAdapter() {\r
+ public void windowClosing(WindowEvent e)\r
+ {\r
+ System.exit(0);\r
+ }\r
+ });\r
+ canvas.addMouseListener(this);\r
+\r
+ frame.setVisible(true);\r
+ Insets ins = frame.getInsets();\r
+ frame.setSize(SCREEN_X + ins.left + ins.right, SCREEN_Y + ins.top + ins.bottom);\r
+ canvas.setBounds(ins.left, ins.top, SCREEN_X, SCREEN_Y);\r
+ }\r
+\r
+ public void init(GLAutoDrawable drawable)\r
+ {\r
+ this._gl = drawable.getGL();\r
+ this._gl.glEnable(GL.GL_DEPTH_TEST);\r
+ this._gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);\r
+ // NyARToolkitの準備\r
+ try {\r
+ // キャプチャ開始\r
+ _capture.start();\r
+ } catch (Exception e) {\r
+ e.printStackTrace();\r
+ }\r
+ this._animator = new Animator(drawable);\r
+ this._animator.start();\r
+ return;\r
+ }\r
+\r
+ public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height)\r
+ {\r
+ _gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);\r
+ _gl.glViewport(0, 0, width, height);\r
+\r
+ // 視体積の設定\r
+ _gl.glMatrixMode(GL.GL_PROJECTION);\r
+ _gl.glLoadIdentity();\r
+ // 見る位置\r
+ _gl.glMatrixMode(GL.GL_MODELVIEW);\r
+ _gl.glLoadIdentity();\r
+ }\r
+ private static float TICK=0;\r
+ public void display(GLAutoDrawable drawable)\r
+ {\r
+ TICK+=0.1;\r
+ //RealitySourceにデータが処理する。\r
+ if(!this._src.isReady())\r
+ {\r
+ return;\r
+ }\r
+ \r
+ // 背景を書く\r
+ this._gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); // Clear the buffers for new frame.\r
+ try{\r
+ synchronized(this._sync_object)\r
+ {\r
+ //背景描画\r
+ this._reality.glDrawRealitySource(this._gl,this._src); \r
+ // Projection transformation.\r
+ this._gl.glMatrixMode(GL.GL_PROJECTION);\r
+ this._reality.glLoadCameraFrustum(this._gl);\r
+ //ターゲットリストを走査して、画面に内容を反映\r
+ NyARRealityTargetList tl=this._reality.refTargetList();\r
+ for(int i=tl.getLength()-1;i>=0;i--){\r
+ NyARRealityTarget t=tl.getItem(i);\r
+ switch(t.getTargetType())\r
+ {\r
+ case NyARRealityTarget.RT_KNOWN:\r
+ this._gl.glMatrixMode(GL.GL_MODELVIEW);\r
+ // 変換行列をOpenGL形式に変換(ここ少し変えるかも)\r
+ this._reality.glLoadModelViewMatrix(this._gl,t.refTransformMatrix());\r
+\r
+ NyARDoublePoint3d tag=(NyARDoublePoint3d)t.tag; \r
+ // All other lighting and geometry goes here.\r
+ _gl.glPushMatrix(); // Save world coordinate system.\r
+ _gl.glTranslatef((float)tag.x,(float)tag.y,(float)tag.z+20f);\r
+ _gl.glDisable(GL.GL_LIGHTING); // Just use colours.\r
+ NyARGLDrawUtil.drawColorCube(this._gl,40f);\r
+ _gl.glPopMatrix(); // Restore world coordinate system.\r
+\r
+ \r
+ break;\r
+ case NyARRealityTarget.RT_UNKNOWN:\r
+ \r
+ break;\r
+ }\r
+ }\r
+ }\r
+ Thread.sleep(1);// タスク実行権限を一旦渡す\r
+ }catch(Exception e){\r
+ e.printStackTrace();\r
+ }\r
+\r
+ }\r
+\r
+ /**\r
+ * カメラのキャプチャした画像を非同期に受け取る関数。\r
+ * 画像を受け取ると、同期を取ってRealityを1サイクル進めます。\r
+ */\r
+ public void onUpdateBuffer(Buffer i_buffer)\r
+ {\r
+ try {\r
+ synchronized (this._sync_object)\r
+ {\r
+ this._src.setImage(i_buffer);\r
+ this._reality.progress(this._src);\r
+ //Deadターゲットの後片付け\r
+ //省略\r
+ \r
+ //Knownターゲットは1個だけね。\r
+ if(this._reality.getNumberOfKnown()>0){\r
+ return;\r
+ }\r
+ //UnknownTargetを1個取得して、遷移を試す。\r
+ NyARRealityTarget t=this._reality.selectSingleUnknownTarget();\r
+ if(t==null){\r
+ return;\r
+ }\r
+\r
+\r
+ //ターゲットに一致するデータを検索\r
+ RawbitSerialIdTable.IdentifyIdResult r=new RawbitSerialIdTable.IdentifyIdResult();\r
+ if(this._mklib.identifyId(t,this._src,r)){\r
+ //テーブルにターゲットが見つかったので遷移する。\r
+ if(!this._reality.changeTargetToKnown(t,r.artk_direction,r.marker_width)){\r
+ //遷移の成功チェック\r
+ return;//失敗\r
+ }\r
+ //遷移に成功したので、tagにユーザ定義情報を書きこむ。\r
+ t.tag=new NyARDoublePoint3d();\r
+ }else{\r
+ //一致しないので、このターゲットは捨てる。\r
+ this._reality.changeTargetToDead(t);\r
+ }\r
+ }\r
+ } catch (Exception e) {\r
+ e.printStackTrace();\r
+ }\r
+ }\r
+\r
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged)\r
+ {\r
+ }\r
+ \r
+ private final static String PARAM_FILE = "../../Data/camera_para.dat";\r
+\r
+ public static void main(String[] args)\r
+ {\r
+ try {\r
+ NyARParam param = new NyARParam();\r
+ param.loadARParamFromFile(PARAM_FILE);\r
+ new Test_NyARRealityGl_ScreenPos(param);\r
+ } catch (Exception e) {\r
+ e.printStackTrace();\r
+ }\r
+ return;\r
+ }\r
+\r
+}\r