OSDN Git Service

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