2 * PROJECT: NyARToolkit JOGL sample program.
\r
3 * --------------------------------------------------------------------------------
\r
5 * Copyright (c) 2008 nyatla
\r
6 * airmail(at)ebony.plala.or.jp
\r
7 * http://nyatla.jp/nyartoolkit/
\r
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
\r
10 * of this software and associated documentation files (the "Software"), to deal
\r
11 * in the Software without restriction, including without limitation the rights
\r
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
\r
13 * copies of the Software, and to permit persons to whom the Software is
\r
14 * furnished to do so, subject to the following conditions:
\r
15 * The above copyright notice and this permission notice shall be included in
\r
16 * all copies or substantial portions of the Software.
\r
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
\r
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
\r
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
\r
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
\r
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
\r
27 package jp.nyatla.nyartoolkit.jogl.sample;
\r
30 import java.awt.event.*;
\r
32 import java.util.Date;
\r
34 import javax.media.Buffer;
\r
35 import javax.media.opengl.*;
\r
37 import com.sun.opengl.util.*;
\r
38 import jp.nyatla.nyartoolkit.*;
\r
39 import jp.nyatla.nyartoolkit.core.*;
\r
40 import jp.nyatla.nyartoolkit.core.param.*;
\r
41 import jp.nyatla.nyartoolkit.core.squaredetect.NyARSquare;
\r
42 import jp.nyatla.nyartoolkit.core.transmat.*;
\r
43 import jp.nyatla.nyartoolkit.jmf.utils.*;
\r
44 import jp.nyatla.nyartoolkit.jogl.utils.*;
\r
45 import jp.nyatla.nyartoolkit.processor.*;
\r
49 * SingleARMarkerProcesserを使ったサンプルです。
\r
50 * 登録した複数種類マーカのうちから、同時に1個を認識するサンプルプログラムです。
\r
54 public class SingleARMarker implements GLEventListener, JmfCaptureListener
\r
57 * 1個のRawBit-Idマーカを認識するロジッククラス。
\r
58 * detectMarker関数の呼び出しに同期して、transmatとcurrent_idパラメタを更新します。
\r
62 class MarkerProcessor extends SingleARMarkerProcesser
\r
64 public double[] gltransmat=new double[16];
\r
66 public int current_code=-1;
\r
68 public MarkerProcessor(NyARParam i_cparam,int i_raster_format,NyARGLUtil i_glutil) throws NyARException
\r
70 //アプリケーションフレームワークの初期化
\r
72 initInstance(i_cparam,i_raster_format);
\r
75 protected void onEnterHandler(int i_code)
\r
77 current_code=i_code;
\r
79 protected void onLeaveHandler()
\r
85 * i_square,resultの有効期間は、この関数の終了までです。
\r
87 protected void onUpdateHandler(NyARSquare i_square, NyARTransMatResult result)
\r
90 NyARGLUtil.toCameraViewRH(result,1.0, this.gltransmat);
\r
91 }catch(Exception e){
\r
92 e.printStackTrace();
\r
97 private final String CARCODE_FILE1 = "../../Data/patt.hiro";
\r
98 private final String CARCODE_FILE2 = "../../Data/patt.kanji";
\r
101 private Animator _animator;
\r
102 private JmfNyARRaster_RGB _cap_image;
\r
103 private JmfCaptureDevice _capture;
\r
106 private NyARGLUtil _glnya;
\r
110 private NyARParam _ar_param;
\r
112 private double[] _camera_projection=new double[16];
\r
114 private Object _sync_object=new Object();
\r
115 private MarkerProcessor _processor;
\r
116 private NyARCode[] _code_table=new NyARCode[2];
\r
118 public SingleARMarker(NyARParam i_cparam) throws NyARException
\r
120 JmfCaptureDeviceList devlist=new JmfCaptureDeviceList();
\r
121 this._ar_param=i_cparam;
\r
124 this._capture=devlist.getDevice(0);
\r
125 if(!this._capture.setCaptureFormat(SCREEN_X, SCREEN_Y,30.0f)){
\r
126 throw new NyARException();
\r
128 this._capture.setOnCapture(this);
\r
129 this._cap_image = new JmfNyARRaster_RGB(this._capture.getCaptureFormat());
\r
131 this._code_table[0]=new NyARCode(16,16);
\r
132 this._code_table[0].loadARPattFromFile(CARCODE_FILE1);
\r
133 this._code_table[1]=new NyARCode(16,16);
\r
134 this._code_table[1].loadARPattFromFile(CARCODE_FILE2);
\r
136 //OpenGLフレームの準備(OpenGLリソースの初期化、カメラの撮影開始は、initコールバック関数内で実行)
\r
137 Frame frame = new Frame("NyARToolkit["+this.getClass().getName()+"]");
\r
138 GLCanvas canvas = new GLCanvas();
\r
140 canvas.addGLEventListener(this);
\r
141 frame.addWindowListener(new WindowAdapter() {
\r
142 public void windowClosing(WindowEvent e)
\r
149 frame.setVisible(true);
\r
150 Insets ins = frame.getInsets();
\r
151 frame.setSize(SCREEN_X + ins.left + ins.right, SCREEN_Y + ins.top + ins.bottom);
\r
152 canvas.setBounds(ins.left, ins.top, SCREEN_X, SCREEN_Y);
\r
155 public void init(GLAutoDrawable drawable)
\r
157 this._gl = drawable.getGL();
\r
158 this._gl.glEnable(GL.GL_DEPTH_TEST);
\r
159 NyARGLDrawUtil.setFontStyle("SansSerif",Font.BOLD,36);
\r
160 NyARGLDrawUtil.setFontColor(Color.RED);
\r
161 this._gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
\r
165 this._processor=new MarkerProcessor(this._ar_param,this._cap_image.getBufferType(),this._glnya);
\r
166 this._processor.setARCodeTable(_code_table,16,80.0);
\r
169 NyARGLUtil.toCameraFrustumRH(this._ar_param,1.0,10,10000,this._camera_projection);
\r
171 this._capture.start();
\r
172 } catch (Exception e) {
\r
173 e.printStackTrace();
\r
175 this._animator = new Animator(drawable);
\r
176 this._animator.start();
\r
180 public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height)
\r
183 _gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
\r
184 _gl.glViewport(0, 0, width, height);
\r
187 _gl.glMatrixMode(GL.GL_PROJECTION);
\r
188 _gl.glLoadIdentity();
\r
190 _gl.glMatrixMode(GL.GL_MODELVIEW);
\r
191 _gl.glLoadIdentity();
\r
195 public void display(GLAutoDrawable drawable)
\r
197 if (!_cap_image.hasBuffer()) {
\r
201 this._gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); // Clear the buffers for new frame.
\r
202 //OnEnter,OnUpdateの間に、transmatに初回行列がストアされる実行されることを防ぎます。
\r
204 NyARGLDrawUtil.drawBackGround(this._gl,this._cap_image, 1.0);
\r
205 synchronized(this._sync_object)
\r
207 if(this._processor.current_code<0){
\r
210 // Projection transformation.
\r
211 this._gl.glMatrixMode(GL.GL_PROJECTION);
\r
212 this._gl.glLoadMatrixd(_camera_projection, 0);
\r
213 this._gl.glMatrixMode(GL.GL_MODELVIEW);
\r
214 // Viewing transformation.
\r
215 this._gl.glLoadIdentity();
\r
216 this._gl.glLoadMatrixd(this._processor.gltransmat, 0);
\r
217 // All other lighting and geometry goes here.
\r
218 this._gl.glPushMatrix();
\r
219 this._gl.glDisable(GL.GL_LIGHTING);
\r
222 //マーカのXZ平面をマーカの左上、表示開始位置を10cm上空へ。
\r
224 Date d = new Date();
\r
225 float r=(d.getTime()/50)%360;
\r
226 this._gl.glRotatef(r,0f,0f,1.0f);
\r
227 this._gl.glTranslatef(-70f,0f,1.0f);
\r
228 this._gl.glRotatef(90,1.0f,0f,0f);
\r
229 NyARGLDrawUtil.drawText("MarkerId:"+this._processor.current_code,1f);
\r
230 this._gl.glPopMatrix();
\r
231 Thread.sleep(1);// タスク実行権限を一旦渡す
\r
234 }catch(Exception e){
\r
235 e.printStackTrace();
\r
243 public void onUpdateBuffer(Buffer i_buffer)
\r
246 synchronized (this._sync_object) {
\r
247 this._cap_image.setBuffer(i_buffer);
\r
249 this._processor.detectMarker(this._cap_image);
\r
251 } catch (Exception e) {
\r
252 e.printStackTrace();
\r
256 public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged)
\r
261 private final static int SCREEN_X = 640;
\r
262 private final static int SCREEN_Y = 480;
\r
263 private final static String PARAM_FILE = "../../Data/camera_para.dat";
\r
265 public static void main(String[] args)
\r
268 NyARParam cparam= new NyARParam();
\r
269 cparam.loadARParamFromFile(PARAM_FILE);
\r
270 cparam.changeScreenSize(SCREEN_X, SCREEN_Y);
\r
271 new SingleARMarker(cparam);
\r
272 }catch(Exception e){
\r
273 e.printStackTrace();
\r