OSDN Git Service

3e33dd0df34f8ee1516931185357388f7a2a7419
[nyartoolkit-and/nyartoolkit-and.git] / trunk / test / jp / nyatla / nyartoolkit / dev / OptimizeView.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.dev;\r
28 \r
29 import java.awt.event.*;\r
30 import java.awt.*;\r
31 import javax.media.Buffer;\r
32 import javax.media.opengl.*;\r
33 import com.sun.opengl.util.*;\r
34 import jp.nyatla.nyartoolkit.*;\r
35 import jp.nyatla.nyartoolkit.core.*;\r
36 import jp.nyatla.nyartoolkit.core.param.*;\r
37 import jp.nyatla.nyartoolkit.core.transmat.*;\r
38 import jp.nyatla.nyartoolkit.detector.*;\r
39 import jp.nyatla.nyartoolkit.jmf.utils.*;\r
40 import jp.nyatla.nyartoolkit.jogl.utils.*;\r
41 class Program implements JmfCaptureListener\r
42 {\r
43         private OptimizeView _view1;\r
44         private OptimizeView _view2;\r
45         public Object _sync_object=new Object();        \r
46         public NyARParam _ar_param;\r
47         public NyARCode _ar_code;\r
48         private final static int SCREEN_X = 640;\r
49         private final static int SCREEN_Y = 480;\r
50         private JmfCaptureDevice _capture;\r
51         public GLNyARRaster_RGB _cap_image;\r
52         public Program(NyARParam i_param, NyARCode i_ar_code) throws NyARException\r
53         {\r
54                 // キャプチャの準備\r
55                 JmfCaptureDeviceList devlist = new JmfCaptureDeviceList();\r
56                 this._capture = devlist.getDevice(0);\r
57                 if (!this._capture.setCaptureFormat(SCREEN_X, SCREEN_Y, 30.0f)) {\r
58                         throw new NyARException();\r
59                 }\r
60                 this._ar_param=i_param;\r
61                 this._ar_code=i_ar_code;\r
62                 this._capture.setOnCapture(this);\r
63                 // GL対応のRGBラスタオブジェクト\r
64                 this._cap_image = new GLNyARRaster_RGB(i_param, this._capture.getCaptureFormat());      \r
65                 this._view1=new OptimizeView(this,NyARSingleDetectMarker.PF_NYARTOOLKIT);\r
66                 this._view2=new OptimizeView(this,NyARSingleDetectMarker.PF_NYARTOOLKIT_ARTOOLKIT_FITTING);\r
67                 this._capture.start();\r
68                 return;\r
69         }\r
70         public void onUpdateBuffer(Buffer i_buffer)\r
71         {\r
72                 try {\r
73                         synchronized (this._sync_object) {\r
74                                 this._cap_image.setBuffer(i_buffer);\r
75                                 this._view1.updateCapture(this._cap_image);\r
76                                 this._view2.updateCapture(this._cap_image);\r
77                         }\r
78                 } catch (Exception e) {\r
79                         e.printStackTrace();\r
80                 }\r
81         }\r
82         //GL API\r
83         void glDrawCube(GL i_gl)\r
84         {\r
85                 // Colour cube data.\r
86                 int polyList = 0;\r
87                 float fSize = 0.5f;// マーカーサイズに対して0.5倍なので、4cmの立方体\r
88                 int f, i;\r
89                 float[][] cube_vertices = new float[][] { { 1.0f, 1.0f, 1.0f }, { 1.0f, -1.0f, 1.0f }, { -1.0f, -1.0f, 1.0f }, { -1.0f, 1.0f, 1.0f }, { 1.0f, 1.0f, -1.0f }, { 1.0f, -1.0f, -1.0f }, { -1.0f, -1.0f, -1.0f }, { -1.0f, 1.0f, -1.0f } };\r
90                 float[][] cube_vertex_colors = new float[][] { { 1.0f, 1.0f, 1.0f }, { 1.0f, 1.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, { 0.0f, 1.0f, 1.0f }, { 1.0f, 0.0f, 1.0f }, { 1.0f, 0.0f, 0.0f }, { 0.0f, 0.0f, 0.0f }, { 0.0f, 0.0f, 1.0f } };\r
91                 int cube_num_faces = 6;\r
92                 short[][] cube_faces = new short[][] { { 3, 2, 1, 0 }, { 2, 3, 7, 6 }, { 0, 1, 5, 4 }, { 3, 0, 4, 7 }, { 1, 2, 6, 5 }, { 4, 5, 6, 7 } };\r
93 \r
94                 if (polyList == 0) {\r
95                         polyList = i_gl.glGenLists(1);\r
96                         i_gl.glNewList(polyList, GL.GL_COMPILE);\r
97                         i_gl.glBegin(GL.GL_QUADS);\r
98                         for (f = 0; f < cube_num_faces; f++)\r
99                                 for (i = 0; i < 4; i++) {\r
100                                         i_gl.glColor3f(cube_vertex_colors[cube_faces[f][i]][0], cube_vertex_colors[cube_faces[f][i]][1], cube_vertex_colors[cube_faces[f][i]][2]);\r
101                                         i_gl.glVertex3f(cube_vertices[cube_faces[f][i]][0] * fSize, cube_vertices[cube_faces[f][i]][1] * fSize, cube_vertices[cube_faces[f][i]][2] * fSize);\r
102                                 }\r
103                         i_gl.glEnd();\r
104                         i_gl.glColor3f(0.0f, 0.0f, 0.0f);\r
105                         for (f = 0; f < cube_num_faces; f++) {\r
106                                 i_gl.glBegin(GL.GL_LINE_LOOP);\r
107                                 for (i = 0; i < 4; i++)\r
108                                         i_gl.glVertex3f(cube_vertices[cube_faces[f][i]][0] * fSize, cube_vertices[cube_faces[f][i]][1] * fSize, cube_vertices[cube_faces[f][i]][2] * fSize);\r
109                                 i_gl.glEnd();\r
110                         }\r
111                         i_gl.glEndList();\r
112                 }\r
113 \r
114                 i_gl.glPushMatrix(); // Save world coordinate system.\r
115                 i_gl.glTranslatef(0.0f, 0.0f, 0.5f); // Place base of cube on marker surface.\r
116                 i_gl.glRotatef(0.0f, 0.0f, 0.0f, 1.0f); // Rotate about z axis.\r
117                 i_gl.glDisable(GL.GL_LIGHTING); // Just use colours.\r
118                 i_gl.glCallList(polyList); // Draw the cube.\r
119                 i_gl.glPopMatrix(); // Restore world coordinate system.\r
120                 return;\r
121         }\r
122         \r
123 }\r
124 /**\r
125  * simpleLiteと同じようなテストプログラム 出来る限りARToolKitのサンプルと似せて作ってあります。 最も一致する"Hiro"マーカーを一つ選択して、その上に立方体を表示します。\r
126  * \r
127  */\r
128 public class OptimizeView implements GLEventListener\r
129 {\r
130         private final static int SCREEN_X = 640;\r
131 \r
132         private final static int SCREEN_Y = 480;\r
133 \r
134         private Animator _animator;\r
135         \r
136         private Program _parent;\r
137 \r
138 \r
139 //      private JmfCaptureDevice _capture;\r
140 \r
141         private GL _gl;\r
142         private NyARGLUtil _glnya;\r
143 \r
144         // NyARToolkit関係\r
145         private NyARSingleDetectMarker _nya;\r
146         private NyARParam _ar_param;\r
147 \r
148         private double[] _camera_projection = new double[16];\r
149 \r
150 \r
151         public OptimizeView(Program i_program,int i_pf) throws NyARException\r
152         {\r
153                 this._parent=i_program;\r
154                 this._ar_param = i_program._ar_param;\r
155 \r
156                 Frame frame = new Frame("["+i_pf+"]");\r
157 \r
158                 // NyARToolkitの準備\r
159                 this._nya = new NyARSingleDetectMarker(this._ar_param, i_program._ar_code, 80.0,i_program._cap_image.getBufferReader().getBufferType(),i_pf);\r
160                 this._nya.setContinueMode(false);// ここをtrueにすると、transMatContinueモード(History計算)になります。\r
161                 \r
162                 // 3Dを描画するコンポーネント\r
163                 GLCanvas canvas = new GLCanvas();\r
164                 frame.add(canvas);\r
165                 canvas.addGLEventListener(this);\r
166                 frame.addWindowListener(new WindowAdapter() {\r
167                         public void windowClosing(WindowEvent e)\r
168                         {\r
169                                 System.exit(0);\r
170                         }\r
171                 });\r
172 \r
173                 frame.setVisible(true);\r
174                 Insets ins = frame.getInsets();\r
175                 frame.setSize(SCREEN_X + ins.left + ins.right, SCREEN_Y + ins.top + ins.bottom);\r
176                 canvas.setBounds(ins.left, ins.top, SCREEN_X, SCREEN_Y);\r
177         }\r
178 \r
179         public void init(GLAutoDrawable drawable)\r
180         {\r
181                 this._gl = drawable.getGL();\r
182                 this._gl.glEnable(GL.GL_DEPTH_TEST);\r
183                 this._gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);\r
184                 // NyARToolkitの準備\r
185                 try {\r
186                         // NyARToolkit用の支援クラス\r
187                         _glnya = new NyARGLUtil(_gl);\r
188                 } catch (Exception e) {\r
189                         e.printStackTrace();\r
190                 }\r
191                 // カメラパラメータの計算\r
192                 this._glnya.toCameraFrustumRH(this._ar_param,this._camera_projection);\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                 _gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);\r
201                 _gl.glViewport(0, 0, width, height);\r
202 \r
203                 // 視体積の設定\r
204                 _gl.glMatrixMode(GL.GL_PROJECTION);\r
205                 _gl.glLoadIdentity();\r
206                 // 見る位置\r
207                 _gl.glMatrixMode(GL.GL_MODELVIEW);\r
208                 _gl.glLoadIdentity();\r
209         }\r
210 \r
211         private boolean _is_marker_exist=false;\r
212         private NyARTransMatResult __display_transmat_result = new NyARTransMatResult();\r
213 \r
214         private double[] __display_wk = new double[16];\r
215 \r
216         public void display(GLAutoDrawable drawable)\r
217         {\r
218                 NyARTransMatResult transmat_result = __display_transmat_result;\r
219                 if (!this._parent._cap_image.hasData()) {\r
220                         return;\r
221                 }\r
222                 // 背景を書く\r
223                 this._gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); // Clear the buffers for new frame.\r
224                 this._glnya.drawBackGround(this._parent._cap_image, 1.0);                       \r
225                 try{\r
226                         synchronized(this._parent._sync_object){\r
227                                 // マーカーがあれば、立方体を描画\r
228                                 if (this._is_marker_exist){\r
229                                         // マーカーの一致度を調査するならば、ここでnya.getConfidence()で一致度を調べて下さい。\r
230                                         // Projection transformation.\r
231                                         _gl.glMatrixMode(GL.GL_PROJECTION);\r
232                                         _gl.glLoadMatrixd(_camera_projection, 0);\r
233                                         _gl.glMatrixMode(GL.GL_MODELVIEW);\r
234                                         // Viewing transformation.\r
235                                         _gl.glLoadIdentity();\r
236                                         // 変換行列を取得\r
237                                         _nya.getTransmationMatrix(transmat_result);\r
238                                         // 変換行列をOpenGL形式に変換\r
239                                         _glnya.toCameraViewRH(transmat_result, __display_wk);\r
240                                         _gl.glLoadMatrixd(__display_wk, 0);\r
241                 \r
242                                         // All other lighting and geometry goes here.\r
243                                         this._parent.glDrawCube(_gl);\r
244                                 }\r
245                         }\r
246                         Thread.sleep(1);// タスク実行権限を一旦渡す\r
247                 }catch(Exception e){\r
248                         e.printStackTrace();\r
249                 }\r
250 \r
251         }\r
252 \r
253         public void updateCapture(GLNyARRaster_RGB i_img)\r
254         {\r
255                 try {\r
256                         synchronized (this._parent._sync_object) {\r
257                                 this._is_marker_exist =this._nya.detectMarkerLite(i_img, 110);\r
258                         }\r
259                 } catch (Exception e) {\r
260                         e.printStackTrace();\r
261                 }\r
262         }\r
263 \r
264         public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged)\r
265         {\r
266         }\r
267 \r
268         private final static String CARCODE_FILE = "../Data/patt.hiro";\r
269 \r
270         private final static String PARAM_FILE = "../Data/camera_para.dat";\r
271 \r
272         public static void main(String[] args)\r
273         {\r
274                 try {\r
275                         NyARParam param = new NyARParam();\r
276                         param.loadARParamFromFile(PARAM_FILE);\r
277                         param.changeScreenSize(SCREEN_X, SCREEN_Y);\r
278 \r
279                         NyARCode code = new NyARCode(16, 16);\r
280                         code.loadARPattFromFile(CARCODE_FILE);\r
281 \r
282                         new Program(param, code);\r
283                 } catch (Exception e) {\r
284                         e.printStackTrace();\r
285                 }\r
286                 return;\r
287         }\r
288 }\r