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 com.sun.opengl.util.j2d.*;
\r
39 import jp.nyatla.nyartoolkit.*;
\r
40 import jp.nyatla.nyartoolkit.core.*;
\r
41 import jp.nyatla.nyartoolkit.core.param.*;
\r
42 import jp.nyatla.nyartoolkit.core.squaredetect.NyARSquare;
\r
43 import jp.nyatla.nyartoolkit.core.transmat.*;
\r
44 import jp.nyatla.nyartoolkit.jmf.utils.*;
\r
45 import jp.nyatla.nyartoolkit.jogl.utils.*;
\r
46 import jp.nyatla.nyartoolkit.processor.*;
\r
54 public class SingleARMarker implements GLEventListener, JmfCaptureListener
\r
58 private TextRenderer _tr;
\r
59 public TextPanel(int i_size)
\r
61 this._tr=new TextRenderer(new Font("SansSerif", Font.BOLD, 36));
\r
64 public void draw(String i_str,float i_scale)
\r
66 this._tr.begin3DRendering();
\r
67 this._tr.setColor(1.0f, 0.2f, 0.2f, 0.8f);
\r
68 this._tr.draw3D(i_str, 0f,0f,0f,i_scale);
\r
69 this._tr.end3DRendering();
\r
74 * 1個のRawBit-Idマーカを認識するロジッククラス。
\r
75 * detectMarker関数の呼び出しに同期して、transmatとcurrent_idパラメタを更新します。
\r
79 class MarkerProcessor extends SingleARMarkerProcesser
\r
81 private NyARGLUtil _glnya;
\r
82 public double[] gltransmat=new double[16];
\r
84 public int current_code=-1;
\r
86 public MarkerProcessor(NyARParam i_cparam,int i_raster_format,NyARGLUtil i_glutil) throws NyARException
\r
88 //アプリケーションフレームワークの初期化
\r
90 initInstance(i_cparam,i_raster_format);
\r
91 this._glnya=i_glutil;
\r
94 protected void onEnterHandler(int i_code)
\r
96 current_code=i_code;
\r
98 protected void onLeaveHandler()
\r
104 * i_square,resultの有効期間は、この関数の終了までです。
\r
106 protected void onUpdateHandler(NyARSquare i_square, NyARTransMatResult result)
\r
109 this._glnya.toCameraViewRH(result, this.gltransmat);
\r
110 }catch(Exception e){
\r
111 e.printStackTrace();
\r
116 private final String CARCODE_FILE1 = "../../Data/patt.hiro";
\r
119 private Animator _animator;
\r
120 private GLNyARRaster_RGB _cap_image;
\r
121 private JmfCaptureDevice _capture;
\r
124 private NyARGLUtil _glnya;
\r
125 private TextPanel _panel;
\r
129 private NyARParam _ar_param;
\r
131 private double[] _camera_projection=new double[16];
\r
133 private Object _sync_object=new Object();
\r
134 private MarkerProcessor _processor;
\r
135 private NyARCode[] _code_table=new NyARCode[1];
\r
137 public SingleARMarker(NyARParam i_cparam) throws NyARException
\r
139 JmfCaptureDeviceList devlist=new JmfCaptureDeviceList();
\r
140 this._ar_param=i_cparam;
\r
143 this._capture=devlist.getDevice(0);
\r
144 if(!this._capture.setCaptureFormat(SCREEN_X, SCREEN_Y,30.0f)){
\r
145 throw new NyARException();
\r
147 this._capture.setOnCapture(this);
\r
148 this._cap_image = new GLNyARRaster_RGB(i_cparam,this._capture.getCaptureFormat());
\r
150 this._code_table[0]=new NyARCode(16,16);
\r
151 this._code_table[0].loadARPattFromFile(CARCODE_FILE1);
\r
153 //OpenGLフレームの準備(OpenGLリソースの初期化、カメラの撮影開始は、initコールバック関数内で実行)
\r
154 Frame frame = new Frame("Java simpleLite with NyARToolkit");
\r
155 GLCanvas canvas = new GLCanvas();
\r
157 canvas.addGLEventListener(this);
\r
158 frame.addWindowListener(new WindowAdapter() {
\r
159 public void windowClosing(WindowEvent e)
\r
166 frame.setVisible(true);
\r
167 Insets ins = frame.getInsets();
\r
168 frame.setSize(SCREEN_X + ins.left + ins.right, SCREEN_Y + ins.top + ins.bottom);
\r
169 canvas.setBounds(ins.left, ins.top, SCREEN_X, SCREEN_Y);
\r
172 public void init(GLAutoDrawable drawable)
\r
174 this._panel = new TextPanel(100);
\r
177 this._gl = drawable.getGL();
\r
178 this._gl.glEnable(GL.GL_DEPTH_TEST);
\r
180 this._gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
\r
183 this._glnya = new NyARGLUtil(this._gl);
\r
185 this._processor=new MarkerProcessor(this._ar_param,this._cap_image.getBufferType(),this._glnya);
\r
186 this._processor.setARCodeTable(_code_table,16,80.0);
\r
189 this._glnya.toCameraFrustumRH(this._ar_param,this._camera_projection);
\r
191 this._capture.start();
\r
192 } catch (Exception e) {
\r
193 e.printStackTrace();
\r
195 this._animator = new Animator(drawable);
\r
196 this._animator.start();
\r
200 public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height)
\r
203 _gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
\r
204 _gl.glViewport(0, 0, width, height);
\r
207 _gl.glMatrixMode(GL.GL_PROJECTION);
\r
208 _gl.glLoadIdentity();
\r
210 _gl.glMatrixMode(GL.GL_MODELVIEW);
\r
211 _gl.glLoadIdentity();
\r
215 public void display(GLAutoDrawable drawable)
\r
217 if (!_cap_image.hasBuffer()) {
\r
221 this._gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); // Clear the buffers for new frame.
\r
222 this._glnya.drawBackGround(this._cap_image, 1.0);
\r
223 //OnEnter,OnUpdateの間に、transmatに初回行列がストアされる実行されることを防ぎます。
\r
224 synchronized(this._sync_object)
\r
226 if(this._processor.current_code<0){
\r
230 // Projection transformation.
\r
231 this._gl.glMatrixMode(GL.GL_PROJECTION);
\r
232 this._gl.glLoadMatrixd(_camera_projection, 0);
\r
233 this._gl.glMatrixMode(GL.GL_MODELVIEW);
\r
234 // Viewing transformation.
\r
235 this._gl.glLoadIdentity();
\r
236 this._gl.glLoadMatrixd(this._processor.gltransmat, 0);
\r
237 // All other lighting and geometry goes here.
\r
238 this._gl.glPushMatrix();
\r
239 this._gl.glDisable(GL.GL_LIGHTING);
\r
242 //マーカのXZ平面をマーカの左上、表示開始位置を10cm上空へ。
\r
244 Date d = new Date();
\r
245 float r=(d.getTime()/50)%360;
\r
246 this._gl.glRotatef(r,0f,0f,1.0f);
\r
247 this._gl.glTranslatef(-1.0f,0f,1.0f);
\r
248 this._gl.glRotatef(90,1.0f,0f,0f);
\r
249 this._panel.draw("MarkerId:"+this._processor.current_code,0.01f);
\r
250 this._gl.glPopMatrix();
\r
251 Thread.sleep(1);// タスク実行権限を一旦渡す
\r
252 }catch(Exception e){
\r
253 e.printStackTrace();
\r
263 public void onUpdateBuffer(Buffer i_buffer)
\r
266 synchronized (this._sync_object) {
\r
267 this._cap_image.setBuffer(i_buffer);
\r
269 this._processor.detectMarker(this._cap_image);
\r
271 } catch (Exception e) {
\r
272 e.printStackTrace();
\r
276 public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged)
\r
281 private final static int SCREEN_X = 640;
\r
282 private final static int SCREEN_Y = 480;
\r
283 private final static String PARAM_FILE = "../../Data/camera_para.dat";
\r
285 public static void main(String[] args)
\r
288 NyARParam cparam= new NyARParam();
\r
289 cparam.loadARParamFromFile(PARAM_FILE);
\r
290 cparam.changeScreenSize(SCREEN_X, SCREEN_Y);
\r
291 new SingleARMarker(cparam);
\r
292 }catch(Exception e){
\r
293 e.printStackTrace();
\r