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 Object _sync_object=new Object();
\r
82 public NyARTransMatResult transmat=null;
\r
83 public int current_code=-1;
\r
85 public MarkerProcessor(NyARParam i_cparam,int i_raster_format) throws NyARException
\r
87 //アプリケーションフレームワークの初期化
\r
89 initInstance(i_cparam,i_raster_format);
\r
92 protected void onEnterHandler(int i_code)
\r
94 synchronized(this._sync_object){
\r
95 current_code=i_code;
\r
98 protected void onLeaveHandler()
\r
100 synchronized(this._sync_object){
\r
102 this.transmat=null;
\r
107 protected void onUpdateHandler(NyARSquare i_square, NyARTransMatResult result)
\r
109 synchronized(this._sync_object){
\r
110 this.transmat=result;
\r
115 private final String CARCODE_FILE1 = "../../Data/patt.hiro";
\r
118 private Animator _animator;
\r
119 private GLNyARRaster_RGB _cap_image;
\r
120 private JmfCaptureDevice _capture;
\r
123 private NyARGLUtil _glnya;
\r
124 private TextPanel _panel;
\r
128 private NyARParam _ar_param;
\r
130 private double[] _camera_projection=new double[16];
\r
132 private Object _sync_object=new Object();
\r
133 private MarkerProcessor _processor;
\r
134 private NyARCode[] _code_table=new NyARCode[1];
\r
136 public SingleARMarker(NyARParam i_cparam) throws NyARException
\r
138 JmfCaptureDeviceList devlist=new JmfCaptureDeviceList();
\r
139 this._ar_param=i_cparam;
\r
142 this._capture=devlist.getDevice(0);
\r
143 if(!this._capture.setCaptureFormat(SCREEN_X, SCREEN_Y,30.0f)){
\r
144 throw new NyARException();
\r
146 this._capture.setOnCapture(this);
\r
147 this._cap_image = new GLNyARRaster_RGB(i_cparam,this._capture.getCaptureFormat());
\r
149 this._code_table[0]=new NyARCode(16,16);
\r
150 this._code_table[0].loadARPattFromFile(CARCODE_FILE1);
\r
152 this._processor=new MarkerProcessor(i_cparam,this._cap_image.getBufferType());
\r
153 this._processor.setARCodeTable(_code_table,16,80.0);
\r
155 //OpenGLフレームの準備(OpenGLリソースの初期化、カメラの撮影開始は、initコールバック関数内で実行)
\r
156 Frame frame = new Frame("Java simpleLite with NyARToolkit");
\r
157 GLCanvas canvas = new GLCanvas();
\r
159 canvas.addGLEventListener(this);
\r
160 frame.addWindowListener(new WindowAdapter() {
\r
161 public void windowClosing(WindowEvent e)
\r
168 frame.setVisible(true);
\r
169 Insets ins = frame.getInsets();
\r
170 frame.setSize(SCREEN_X + ins.left + ins.right, SCREEN_Y + ins.top + ins.bottom);
\r
171 canvas.setBounds(ins.left, ins.top, SCREEN_X, SCREEN_Y);
\r
174 public void init(GLAutoDrawable drawable)
\r
176 this._panel = new TextPanel(100);
\r
179 this._gl = drawable.getGL();
\r
180 this._gl.glEnable(GL.GL_DEPTH_TEST);
\r
182 this._gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
\r
185 this._glnya = new NyARGLUtil(this._gl);
\r
187 this._glnya.toCameraFrustumRH(this._ar_param,this._camera_projection);
\r
189 this._capture.start();
\r
190 } catch (Exception e) {
\r
191 e.printStackTrace();
\r
193 this._animator = new Animator(drawable);
\r
194 this._animator.start();
\r
198 public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height)
\r
201 _gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
\r
202 _gl.glViewport(0, 0, width, height);
\r
205 _gl.glMatrixMode(GL.GL_PROJECTION);
\r
206 _gl.glLoadIdentity();
\r
208 _gl.glMatrixMode(GL.GL_MODELVIEW);
\r
209 _gl.glLoadIdentity();
\r
211 private double[] __display_wk=new double[16];
\r
214 public void display(GLAutoDrawable drawable)
\r
216 NyARTransMatResult transmat_result = this._processor.transmat;
\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 if(this._processor.current_code<0 || transmat_result==null){
\r
227 synchronized(this._sync_object){
\r
228 // Projection transformation.
\r
229 this._gl.glMatrixMode(GL.GL_PROJECTION);
\r
230 this._gl.glLoadMatrixd(_camera_projection, 0);
\r
231 this._gl.glMatrixMode(GL.GL_MODELVIEW);
\r
232 // Viewing transformation.
\r
233 this._gl.glLoadIdentity();
\r
234 // 変換行列をOpenGL形式に変換
\r
235 this._glnya.toCameraViewRH(transmat_result, __display_wk);
\r
236 this._gl.glLoadMatrixd(__display_wk, 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
252 Thread.sleep(1);// タスク実行権限を一旦渡す
\r
253 }catch(Exception e){
\r
254 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