OSDN Git Service

[更新]NyARToolkit
[nyartoolkit-and/nyartoolkit-and.git] / sample / jogl / jp / nyatla / nyartoolkit / jogl / sample / JavaSimpleLite2.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 \r
28 package jp.nyatla.nyartoolkit.jogl.sample;\r
29 \r
30 import java.awt.event.*;\r
31 import java.awt.*;\r
32 import javax.media.*;\r
33 import javax.media.opengl.*;\r
34 import com.sun.opengl.util.Animator;\r
35 import jp.nyatla.nyartoolkit.core.param.*;\r
36 import jp.nyatla.nyartoolkit.core.transmat.NyARTransMatResult;\r
37 import jp.nyatla.nyartoolkit.core.*;\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 /**\r
42  * simpleLiteの複数マーカー同時認識バージョン\r
43  * "Hiro"のマーカーと"人"のマーカーの混在環境で、Hiroのマーカー全てに\r
44  * 立方体を表示します。\r
45  */\r
46 public class JavaSimpleLite2 implements GLEventListener, JmfCaptureListener\r
47 {\r
48         private final String CARCODE_FILE1 = "../../Data/patt.hiro";\r
49 \r
50         private final String CARCODE_FILE2 = "../../Data/patt.kanji";\r
51 \r
52         private final String PARAM_FILE = "../../Data/camera_para.dat";\r
53 \r
54         private final static int SCREEN_X = 640;\r
55 \r
56         private final static int SCREEN_Y = 480;\r
57 \r
58         private Animator _animator;\r
59 \r
60         private GLNyARRaster_RGB _cap_image;\r
61 \r
62         private JmfCaptureDevice _capture;\r
63 \r
64         private GL _gl;\r
65 \r
66         private NyARGLUtil _glnya;\r
67 \r
68         //NyARToolkit関係\r
69         private NyARDetectMarker _nya;\r
70 \r
71         private NyARParam _ar_param;\r
72         private double[] _camera_projection=new double[16];\r
73         /**\r
74          * 立方体を書く\r
75          *\r
76          */\r
77         void drawCube()\r
78         {\r
79                 // Colour cube data.\r
80                 int polyList = 0;\r
81                 float fSize = 0.5f;//マーカーサイズに対して0.5倍なので、4cmの立方体\r
82                 int f, i;\r
83                 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
84                 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
85                 int cube_num_faces = 6;\r
86                 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
87 \r
88                 if (polyList == 0) {\r
89                         polyList = _gl.glGenLists(1);\r
90                         _gl.glNewList(polyList, GL.GL_COMPILE);\r
91                         _gl.glBegin(GL.GL_QUADS);\r
92                         for (f = 0; f < cube_num_faces; f++)\r
93                                 for (i = 0; i < 4; i++) {\r
94                                         _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
95                                         _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
96                                 }\r
97                         _gl.glEnd();\r
98                         _gl.glColor3f(0.0f, 0.0f, 0.0f);\r
99                         for (f = 0; f < cube_num_faces; f++) {\r
100                                 _gl.glBegin(GL.GL_LINE_LOOP);\r
101                                 for (i = 0; i < 4; i++)\r
102                                         _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
103                                 _gl.glEnd();\r
104                         }\r
105                         _gl.glEndList();\r
106                 }\r
107 \r
108                 _gl.glPushMatrix(); // Save world coordinate system.\r
109                 _gl.glTranslatef(0.0f, 0.0f, 0.5f); // Place base of cube on marker surface.\r
110                 _gl.glRotatef(0.0f, 0.0f, 0.0f, 1.0f); // Rotate about z axis.\r
111                 _gl.glDisable(GL.GL_LIGHTING); // Just use colours.\r
112                 _gl.glCallList(polyList); // Draw the cube.\r
113                 _gl.glPopMatrix(); // Restore world coordinate system.\r
114 \r
115         }\r
116 \r
117         public JavaSimpleLite2()\r
118         {\r
119                 Frame frame = new Frame("Java simpleLite with NyARToolkit");\r
120 \r
121                 // 3Dを描画するコンポーネント\r
122                 GLCanvas canvas = new GLCanvas();\r
123                 frame.add(canvas);\r
124                 canvas.addGLEventListener(this);\r
125                 frame.addWindowListener(new WindowAdapter() {\r
126                         public void windowClosing(WindowEvent e)\r
127                         {\r
128                                 System.exit(0);\r
129                         }\r
130                 });\r
131 \r
132                 frame.setVisible(true);\r
133                 Insets ins = frame.getInsets();\r
134                 frame.setSize(SCREEN_X + ins.left + ins.right, SCREEN_Y + ins.top + ins.bottom);\r
135                 canvas.setBounds(ins.left, ins.top, SCREEN_X, SCREEN_Y);\r
136         }\r
137 \r
138         public void init(GLAutoDrawable drawable)\r
139         {\r
140                 _gl = drawable.getGL();\r
141                 _gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);\r
142                 //NyARToolkitの準備\r
143                 try {\r
144                         //キャプチャの準備\r
145                         JmfCaptureDeviceList devlist=new JmfCaptureDeviceList();\r
146                         _capture=devlist.getDevice(0);\r
147                         _capture.setCaptureFormat(JmfCaptureDevice.PIXEL_FORMAT_RGB,SCREEN_X, SCREEN_Y,15f);\r
148                         _capture.setOnCapture(this);                    \r
149                         //NyARToolkitの準備\r
150                         _ar_param = new NyARParam();\r
151                         _ar_param.loadARParamFromFile(PARAM_FILE);\r
152                         _ar_param.changeScreenSize(SCREEN_X, SCREEN_Y);\r
153 \r
154                         //ARコードを2個ロード\r
155                         double[] width = new double[] { 80.0, 80.0 };\r
156                         NyARCode[] ar_codes = new NyARCode[2];\r
157                         ar_codes[0] = new NyARCode(16, 16);\r
158                         ar_codes[0].loadARPattFromFile(CARCODE_FILE1);\r
159                         ar_codes[1] = new NyARCode(16, 16);\r
160                         ar_codes[1].loadARPattFromFile(CARCODE_FILE2);\r
161                         _nya = new NyARDetectMarker(_ar_param, ar_codes, width, 2);\r
162                         _nya.setContinueMode(false);//ここをtrueにすると、transMatContinueモード(History計算)になります。\r
163                         //NyARToolkit用の支援クラス\r
164                         _glnya = new NyARGLUtil(_gl);\r
165                         //GL対応のRGBラスタオブジェクト\r
166                         _cap_image = new GLNyARRaster_RGB(_ar_param);\r
167                         //キャプチャ開始\r
168                         _capture.start();\r
169                 } catch (Exception e) {\r
170                         e.printStackTrace();\r
171                 }\r
172                 //カメラパラメータの計算\r
173                 _glnya.toCameraFrustumRH(_ar_param,_camera_projection);\r
174                 \r
175                 _animator = new Animator(drawable);\r
176                 _animator.start();\r
177 \r
178         }\r
179 \r
180         public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height)\r
181         {\r
182                 _gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);\r
183                 _gl.glViewport(0, 0, width, height);\r
184 \r
185                 //視体積の設定\r
186                 _gl.glMatrixMode(GL.GL_PROJECTION);\r
187                 _gl.glLoadIdentity();\r
188                 //見る位置\r
189                 _gl.glMatrixMode(GL.GL_MODELVIEW);\r
190                 _gl.glLoadIdentity();\r
191         }\r
192         private NyARTransMatResult __display_transmat_result=new NyARTransMatResult();\r
193         private double[] __display_wk=new double[16];\r
194         \r
195         public void display(GLAutoDrawable drawable)\r
196         {\r
197                 NyARTransMatResult transmat_result=__display_transmat_result;\r
198 \r
199                 try {\r
200                         if (!_cap_image.hasData()) {\r
201                                 return;\r
202                         }\r
203                         _gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); // Clear the buffers for new frame.          \r
204                         //画像チェックしてマーカー探して、背景を書く\r
205                         int found_markers;\r
206                         synchronized (_cap_image) {\r
207                                 found_markers = _nya.detectMarkerLite(_cap_image, 110);\r
208                                 //背景を書く\r
209                                 _glnya.drawBackGround(_cap_image, 1.0);\r
210                         }\r
211                         //あったら立方体を書く\r
212                         for (int i = 0; i < found_markers; i++) {\r
213                                 //1番のマーカーでなければ表示しない。\r
214                                 if (_nya.getARCodeIndex(i) != 0) {\r
215                                         continue;\r
216                                 }\r
217                                 //マーカーの一致度を調査するならば、ここでnya.getConfidence()で一致度を調べて下さい。\r
218                                 // Projection transformation.\r
219                                 _gl.glMatrixMode(GL.GL_PROJECTION);\r
220                                 _gl.glLoadMatrixd(_camera_projection, 0);\r
221                                 _gl.glMatrixMode(GL.GL_MODELVIEW);\r
222                                 // Viewing transformation.\r
223                                 _gl.glLoadIdentity();\r
224                                 //変換行列を取得\r
225                                 _nya.getTransmationMatrix(i,transmat_result);\r
226                                 //変換行列をOpenGL形式に変換\r
227                                 _glnya.toCameraViewRH(transmat_result, __display_wk);\r
228                                 _gl.glLoadMatrixd(__display_wk, 0);\r
229 \r
230                                 // All other lighting and geometry goes here.\r
231                                 drawCube();\r
232                         }\r
233                         Thread.sleep(1);//タスク実行権限を一旦渡す            \r
234                 } catch (Exception e) {\r
235                         e.printStackTrace();\r
236                 }\r
237                 return;\r
238         }\r
239 \r
240         public void onUpdateBuffer(Buffer i_buffer)\r
241         {\r
242                 try {\r
243                         synchronized (_cap_image) {\r
244                                 _cap_image.setBuffer(i_buffer, true);\r
245                         }\r
246                 } catch (Exception e) {\r
247                         e.printStackTrace();\r
248                 }\r
249         }\r
250 \r
251         public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged)\r
252         {\r
253         }\r
254 \r
255         public static void main(String[] args)\r
256         {\r
257                 new JavaSimpleLite2();\r
258         }\r
259 }\r