OSDN Git Service

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