OSDN Git Service

[TAG]2.4.1
[nyartoolkit-and/nyartoolkit-and.git] / tags / 2.4.1 / sample / jogl / jp / nyatla / nyartoolkit / jogl / sample / JavaSimpleLite.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.jogl.sample;\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 \r
42 /**\r
43  * simpleLiteと同じようなテストプログラム 出来る限りARToolKitのサンプルと似せて作ってあります。 最も一致する"Hiro"マーカーを一つ選択して、その上に立方体を表示します。\r
44  * \r
45  */\r
46 public class JavaSimpleLite implements GLEventListener, JmfCaptureListener\r
47 {\r
48         private final static int SCREEN_X = 640;\r
49 \r
50         private final static int SCREEN_Y = 480;\r
51 \r
52         private Animator _animator;\r
53 \r
54         private GLNyARRaster_RGB _cap_image;\r
55 \r
56         private JmfCaptureDevice _capture;\r
57 \r
58         private GL _gl;\r
59 \r
60         private NyARGLUtil _glnya;\r
61 \r
62         // NyARToolkit関係\r
63         private NyARSingleDetectMarker _nya;\r
64 \r
65         private NyARParam _ar_param;\r
66 \r
67         private Object _sync_object=new Object();\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 JavaSimpleLite(NyARParam i_param, NyARCode i_ar_code) throws NyARException\r
115         {\r
116                 this._ar_param = i_param;\r
117 \r
118                 Frame frame = new Frame("Java simpleLite with NyARToolkit");\r
119 \r
120                 \r
121                 // キャプチャの準備\r
122                 JmfCaptureDeviceList devlist = new JmfCaptureDeviceList();\r
123                 this._capture = devlist.getDevice(0);\r
124                 if (!this._capture.setCaptureFormat(SCREEN_X, SCREEN_Y, 30.0f)) {\r
125                         throw new NyARException();\r
126                 }\r
127                 this._capture.setOnCapture(this);\r
128                 // GL対応のRGBラスタオブジェクト\r
129                 this._cap_image = new GLNyARRaster_RGB(this._ar_param, this._capture.getCaptureFormat());\r
130                 \r
131                 // NyARToolkitの準備\r
132                 this._nya = new NyARSingleDetectMarker(this._ar_param, i_ar_code, 80.0,this._cap_image.getBufferReader().getBufferType());\r
133                 this._nya.setContinueMode(false);// ここをtrueにすると、transMatContinueモード(History計算)になります。\r
134                 \r
135                 // 3Dを描画するコンポーネント\r
136                 GLCanvas canvas = new GLCanvas();\r
137                 frame.add(canvas);\r
138                 canvas.addGLEventListener(this);\r
139                 frame.addWindowListener(new WindowAdapter() {\r
140                         public void windowClosing(WindowEvent e)\r
141                         {\r
142                                 System.exit(0);\r
143                         }\r
144                 });\r
145 \r
146                 frame.setVisible(true);\r
147                 Insets ins = frame.getInsets();\r
148                 frame.setSize(SCREEN_X + ins.left + ins.right, SCREEN_Y + ins.top + ins.bottom);\r
149                 canvas.setBounds(ins.left, ins.top, SCREEN_X, SCREEN_Y);\r
150         }\r
151 \r
152         public void init(GLAutoDrawable drawable)\r
153         {\r
154                 this._gl = drawable.getGL();\r
155                 this._gl.glEnable(GL.GL_DEPTH_TEST);\r
156                 this._gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);\r
157                 // NyARToolkitの準備\r
158                 try {\r
159                         // NyARToolkit用の支援クラス\r
160                         _glnya = new NyARGLUtil(_gl);\r
161                         // キャプチャ開始\r
162                         _capture.start();\r
163                 } catch (Exception e) {\r
164                         e.printStackTrace();\r
165                 }\r
166                 // カメラパラメータの計算\r
167                 this._glnya.toCameraFrustumRH(this._ar_param,this._camera_projection);\r
168                 this._animator = new Animator(drawable);\r
169                 this._animator.start();\r
170                 return;\r
171         }\r
172 \r
173         public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height)\r
174         {\r
175                 _gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);\r
176                 _gl.glViewport(0, 0, width, height);\r
177 \r
178                 // 視体積の設定\r
179                 _gl.glMatrixMode(GL.GL_PROJECTION);\r
180                 _gl.glLoadIdentity();\r
181                 // 見る位置\r
182                 _gl.glMatrixMode(GL.GL_MODELVIEW);\r
183                 _gl.glLoadIdentity();\r
184         }\r
185 \r
186         private boolean _is_marker_exist=false;\r
187         private NyARTransMatResult __display_transmat_result = new NyARTransMatResult();\r
188 \r
189         private double[] __display_wk = new double[16];\r
190 \r
191         public void display(GLAutoDrawable drawable)\r
192         {\r
193                 NyARTransMatResult transmat_result = __display_transmat_result;\r
194                 if (!_cap_image.hasData()) {\r
195                         return;\r
196                 }\r
197                 // 背景を書く\r
198                 this._gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); // Clear the buffers for new frame.\r
199                 this._glnya.drawBackGround(this._cap_image, 1.0);                       \r
200                 try{\r
201                         synchronized(this._sync_object){\r
202                                 // マーカーがあれば、立方体を描画\r
203                                 if (this._is_marker_exist){\r
204                                         // マーカーの一致度を調査するならば、ここでnya.getConfidence()で一致度を調べて下さい。\r
205                                         // Projection transformation.\r
206                                         _gl.glMatrixMode(GL.GL_PROJECTION);\r
207                                         _gl.glLoadMatrixd(_camera_projection, 0);\r
208                                         _gl.glMatrixMode(GL.GL_MODELVIEW);\r
209                                         // Viewing transformation.\r
210                                         _gl.glLoadIdentity();\r
211                                         // 変換行列を取得\r
212                                         _nya.getTransmationMatrix(transmat_result);\r
213                                         // 変換行列をOpenGL形式に変換\r
214                                         _glnya.toCameraViewRH(transmat_result, __display_wk);\r
215                                         _gl.glLoadMatrixd(__display_wk, 0);\r
216                 \r
217                                         // All other lighting and geometry goes here.\r
218                                         drawCube();\r
219                                 }\r
220                         }\r
221                         Thread.sleep(1);// タスク実行権限を一旦渡す\r
222                 }catch(Exception e){\r
223                         e.printStackTrace();\r
224                 }\r
225 \r
226         }\r
227 \r
228         public void onUpdateBuffer(Buffer i_buffer)\r
229         {\r
230                 try {\r
231                         synchronized (this._sync_object) {\r
232                                 this._cap_image.setBuffer(i_buffer);\r
233                                 this._is_marker_exist =this._nya.detectMarkerLite(this._cap_image, 110);\r
234                         }\r
235                 } catch (Exception e) {\r
236                         e.printStackTrace();\r
237                 }\r
238         }\r
239 \r
240         public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged)\r
241         {\r
242         }\r
243 \r
244         private final static String CARCODE_FILE = "../../Data/patt.hiro";\r
245 \r
246         private final static String PARAM_FILE = "../../Data/camera_para.dat";\r
247 \r
248         public static void main(String[] args)\r
249         {\r
250                 try {\r
251                         NyARParam param = new NyARParam();\r
252                         param.loadARParamFromFile(PARAM_FILE);\r
253                         param.changeScreenSize(SCREEN_X, SCREEN_Y);\r
254 \r
255                         NyARCode code = new NyARCode(16, 16);\r
256                         code.loadARPattFromFile(CARCODE_FILE);\r
257 \r
258                         new JavaSimpleLite(param, code);\r
259                 } catch (Exception e) {\r
260                         e.printStackTrace();\r
261                 }\r
262                 return;\r
263         }\r
264 }\r