OSDN Git Service

[TAG]NyARToolkit/2.5.0
[nyartoolkit-and/nyartoolkit-and.git] / tags / 2.5.0 / sample / jogl / jp / nyatla / nyartoolkit / jogl / sample / SingleARMarker.java
1 /* \r
2  * PROJECT: NyARToolkit JOGL sample program.\r
3  * --------------------------------------------------------------------------------\r
4  * The MIT License\r
5  * Copyright (c) 2008 nyatla\r
6  * airmail(at)ebony.plala.or.jp\r
7  * http://nyatla.jp/nyartoolkit/\r
8  * \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
17  * \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
24  * THE SOFTWARE.\r
25  * \r
26  */\r
27 package jp.nyatla.nyartoolkit.jogl.sample;\r
28 \r
29 \r
30 import java.awt.event.*;\r
31 import java.awt.*;\r
32 import java.util.Date;\r
33 \r
34 import javax.media.Buffer;\r
35 import javax.media.opengl.*;\r
36 \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
47 \r
48 /*\r
49 \r
50 \r
51 */\r
52 \r
53 \r
54 public class SingleARMarker implements GLEventListener, JmfCaptureListener\r
55 {\r
56         class TextPanel\r
57         {\r
58                 private TextRenderer _tr;\r
59                 public TextPanel(int i_size)\r
60                 {\r
61                         this._tr=new TextRenderer(new Font("SansSerif", Font.BOLD, 36));\r
62 \r
63                 }\r
64                 public void draw(String i_str,float i_scale)\r
65                 {\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
70                         return;\r
71                 }\r
72         }       \r
73         /**\r
74          * 1個のRawBit-Idマーカを認識するロジッククラス。\r
75          * detectMarker関数の呼び出しに同期して、transmatとcurrent_idパラメタを更新します。\r
76          * \r
77          *\r
78          */\r
79         class MarkerProcessor extends SingleARMarkerProcesser\r
80         {       \r
81                 private Object _sync_object=new Object();\r
82                 public NyARTransMatResult transmat=null;\r
83                 public int current_code=-1;\r
84 \r
85                 public MarkerProcessor(NyARParam i_cparam,int i_raster_format) throws NyARException\r
86                 {\r
87                         //アプリケーションフレームワークの初期化\r
88                         super();\r
89                         initInstance(i_cparam,i_raster_format);\r
90                         return;\r
91                 }\r
92                 protected void onEnterHandler(int i_code)\r
93                 {\r
94                         synchronized(this._sync_object){\r
95                                 current_code=i_code;\r
96                         }\r
97                 }\r
98                 protected void onLeaveHandler()\r
99                 {\r
100                         synchronized(this._sync_object){\r
101                                 current_code=-1;\r
102                                 this.transmat=null;\r
103                         }\r
104                         return;                 \r
105                 }\r
106 \r
107                 protected void onUpdateHandler(NyARSquare i_square, NyARTransMatResult result)\r
108                 {\r
109                         synchronized(this._sync_object){\r
110                                 this.transmat=result;\r
111                         }                       \r
112                 }\r
113         }\r
114         \r
115         private final String CARCODE_FILE1 = "../../Data/patt.hiro";\r
116         \r
117         \r
118         private Animator _animator;\r
119         private GLNyARRaster_RGB _cap_image;\r
120         private JmfCaptureDevice _capture;\r
121 \r
122         private GL _gl;\r
123         private NyARGLUtil _glnya;\r
124         private TextPanel _panel;\r
125 \r
126 \r
127         //NyARToolkit関係\r
128         private NyARParam _ar_param;\r
129 \r
130         private double[] _camera_projection=new double[16];\r
131         \r
132         private Object _sync_object=new Object();\r
133         private MarkerProcessor _processor;\r
134         private NyARCode[] _code_table=new NyARCode[1];\r
135 \r
136         public SingleARMarker(NyARParam i_cparam) throws NyARException\r
137         {\r
138                 JmfCaptureDeviceList devlist=new JmfCaptureDeviceList();\r
139                 this._ar_param=i_cparam;\r
140 \r
141                 //キャプチャリソースの準備\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
145                 }\r
146                 this._capture.setOnCapture(this);\r
147                 this._cap_image = new GLNyARRaster_RGB(i_cparam,this._capture.getCaptureFormat());      \r
148 \r
149                 this._code_table[0]=new NyARCode(16,16);\r
150                 this._code_table[0].loadARPattFromFile(CARCODE_FILE1);\r
151                 //プロセッサの準備\r
152                 this._processor=new MarkerProcessor(i_cparam,this._cap_image.getBufferType());\r
153                 this._processor.setARCodeTable(_code_table,16,80.0);\r
154                 \r
155                 //OpenGLフレームの準備(OpenGLリソースの初期化、カメラの撮影開始は、initコールバック関数内で実行)\r
156                 Frame frame = new Frame("Java simpleLite with NyARToolkit");\r
157                 GLCanvas canvas = new GLCanvas();\r
158                 frame.add(canvas);\r
159                 canvas.addGLEventListener(this);\r
160                 frame.addWindowListener(new WindowAdapter() {\r
161                         public void windowClosing(WindowEvent e)\r
162                         {\r
163                                 System.exit(0);\r
164                         }\r
165                 });\r
166                 \r
167                 //ウインドウサイズの調整\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
172                 return;\r
173         }\r
174         public void init(GLAutoDrawable drawable)\r
175         {\r
176                 this._panel = new TextPanel(100);\r
177 \r
178 \r
179                 this._gl = drawable.getGL();\r
180                 this._gl.glEnable(GL.GL_DEPTH_TEST);\r
181 \r
182                 this._gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);\r
183                 //NyARToolkitの準備\r
184                 try {\r
185                         this._glnya = new NyARGLUtil(this._gl);\r
186                         //カメラパラメータの計算\r
187                         this._glnya.toCameraFrustumRH(this._ar_param,this._camera_projection);\r
188                         //キャプチャ開始\r
189                         this._capture.start();\r
190                 } catch (Exception e) {\r
191                         e.printStackTrace();\r
192                 }\r
193                 this._animator = new Animator(drawable);\r
194                 this._animator.start();\r
195                 return;\r
196         }\r
197 \r
198         public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height)\r
199         {\r
200                 \r
201                 _gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);\r
202                 _gl.glViewport(0, 0, width, height);\r
203 \r
204                 //視体積の設定\r
205                 _gl.glMatrixMode(GL.GL_PROJECTION);\r
206                 _gl.glLoadIdentity();\r
207                 //見る位置\r
208                 _gl.glMatrixMode(GL.GL_MODELVIEW);\r
209                 _gl.glLoadIdentity();\r
210         }\r
211         private double[] __display_wk=new double[16];\r
212         \r
213         \r
214         public void display(GLAutoDrawable drawable)\r
215         {\r
216                 NyARTransMatResult transmat_result = this._processor.transmat;\r
217                 if (!_cap_image.hasBuffer()) {\r
218                         return;\r
219                 }\r
220                 // 背景を書く\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
224                         \r
225                 }else{\r
226                         try{\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
240 \r
241                                         \r
242                                         //マーカのXZ平面をマーカの左上、表示開始位置を10cm上空へ。\r
243                                         //くるーんくるん\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                                 }\r
252                                 Thread.sleep(1);// タスク実行権限を一旦渡す\r
253                         }catch(Exception e){\r
254                                 e.printStackTrace();\r
255                         }\r
256                 }               \r
257                 return;\r
258 \r
259         }\r
260         /**\r
261          * カメラデバイスからのコールバック\r
262          */\r
263         public void onUpdateBuffer(Buffer i_buffer)\r
264         {\r
265                 try {\r
266                         synchronized (this._sync_object) {\r
267                                 this._cap_image.setBuffer(i_buffer);\r
268                                 //フレームワークに画像を転送\r
269                                 this._processor.detectMarker(this._cap_image);\r
270                         }\r
271                 } catch (Exception e) {\r
272                         e.printStackTrace();\r
273                 }\r
274         }\r
275 \r
276         public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged)\r
277         {\r
278         }\r
279 \r
280 \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
284         //エントリポイント\r
285         public static void main(String[] args)\r
286         {\r
287                 try{\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
294                 }\r
295                 return;\r
296         }\r
297 }\r
298 \r