OSDN Git Service

9f65915d224fdaac19a1b793bd87410627071583
[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 JmfCameraCapture _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                         _capture = new JmfCameraCapture(SCREEN_X, SCREEN_Y, 15f, JmfCameraCapture.PIXEL_FORMAT_RGB);\r
146                         _capture.setCaptureListener(this);\r
147                         //NyARToolkitの準備\r
148                         _ar_param = new NyARParam();\r
149                         _ar_param.loadARParamFromFile(PARAM_FILE);\r
150                         _ar_param.changeScreenSize(SCREEN_X, SCREEN_Y);\r
151 \r
152                         //ARコードを2個ロード\r
153                         double[] width = new double[] { 80.0, 80.0 };\r
154                         NyARCode[] ar_codes = new NyARCode[2];\r
155                         ar_codes[0] = new NyARCode(16, 16);\r
156                         ar_codes[0].loadARPattFromFile(CARCODE_FILE1);\r
157                         ar_codes[1] = new NyARCode(16, 16);\r
158                         ar_codes[1].loadARPattFromFile(CARCODE_FILE2);\r
159                         _nya = new NyARDetectMarker(_ar_param, ar_codes, width, 2);\r
160                         _nya.setContinueMode(false);//ここをtrueにすると、transMatContinueモード(History計算)になります。\r
161                         //NyARToolkit用の支援クラス\r
162                         _glnya = new NyARGLUtil(_gl);\r
163                         //GL対応のRGBラスタオブジェクト\r
164                         _cap_image = new GLNyARRaster_RGB(_ar_param);\r
165                         //キャプチャ開始\r
166                         _capture.start();\r
167                 } catch (Exception e) {\r
168                         e.printStackTrace();\r
169                 }\r
170                 //カメラパラメータの計算\r
171                 _glnya.toCameraFrustumRH(_ar_param,_camera_projection);\r
172                 \r
173                 _animator = new Animator(drawable);\r
174                 _animator.start();\r
175 \r
176         }\r
177 \r
178         public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height)\r
179         {\r
180                 _gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);\r
181                 _gl.glViewport(0, 0, width, height);\r
182 \r
183                 //視体積の設定\r
184                 _gl.glMatrixMode(GL.GL_PROJECTION);\r
185                 _gl.glLoadIdentity();\r
186                 //見る位置\r
187                 _gl.glMatrixMode(GL.GL_MODELVIEW);\r
188                 _gl.glLoadIdentity();\r
189         }\r
190         private NyARTransMatResult __display_transmat_result=new NyARTransMatResult();\r
191         private double[] __display_wk=new double[16];\r
192         \r
193         public void display(GLAutoDrawable drawable)\r
194         {\r
195                 NyARTransMatResult transmat_result=__display_transmat_result;\r
196 \r
197                 try {\r
198                         if (!_cap_image.hasData()) {\r
199                                 return;\r
200                         }\r
201                         _gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); // Clear the buffers for new frame.          \r
202                         //画像チェックしてマーカー探して、背景を書く\r
203                         int found_markers;\r
204                         synchronized (_cap_image) {\r
205                                 found_markers = _nya.detectMarkerLite(_cap_image, 110);\r
206                                 //背景を書く\r
207                                 _glnya.drawBackGround(_cap_image, 1.0);\r
208                         }\r
209                         //あったら立方体を書く\r
210                         for (int i = 0; i < found_markers; i++) {\r
211                                 //1番のマーカーでなければ表示しない。\r
212                                 if (_nya.getARCodeIndex(i) != 0) {\r
213                                         continue;\r
214                                 }\r
215                                 //マーカーの一致度を調査するならば、ここでnya.getConfidence()で一致度を調べて下さい。\r
216                                 // Projection transformation.\r
217                                 _gl.glMatrixMode(GL.GL_PROJECTION);\r
218                                 _gl.glLoadMatrixd(_camera_projection, 0);\r
219                                 _gl.glMatrixMode(GL.GL_MODELVIEW);\r
220                                 // Viewing transformation.\r
221                                 _gl.glLoadIdentity();\r
222                                 //変換行列を取得\r
223                                 _nya.getTransmationMatrix(i,transmat_result);\r
224                                 //変換行列をOpenGL形式に変換\r
225                                 _glnya.toCameraViewRH(transmat_result, __display_wk);\r
226                                 _gl.glLoadMatrixd(__display_wk, 0);\r
227 \r
228                                 // All other lighting and geometry goes here.\r
229                                 drawCube();\r
230                         }\r
231                         Thread.sleep(1);//タスク実行権限を一旦渡す            \r
232                 } catch (Exception e) {\r
233                         e.printStackTrace();\r
234                 }\r
235                 return;\r
236         }\r
237 \r
238         public void onUpdateBuffer(Buffer i_buffer)\r
239         {\r
240                 try {\r
241                         synchronized (_cap_image) {\r
242                                 _cap_image.setBuffer(i_buffer, true);\r
243                         }\r
244                 } catch (Exception e) {\r
245                         e.printStackTrace();\r
246                 }\r
247         }\r
248 \r
249         public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged)\r
250         {\r
251         }\r
252 \r
253         public static void main(String[] args)\r
254         {\r
255                 new JavaSimpleLite2();\r
256         }\r
257 }\r