OSDN Git Service

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