2 * PROJECT: NyARToolkit JOGL sample program.
\r
3 * --------------------------------------------------------------------------------
\r
5 * Copyright (c) 2008 nyatla
\r
6 * airmail(at)ebony.plala.or.jp
\r
7 * http://nyatla.jp/nyartoolkit/
\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
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
27 package jp.nyatla.nyartoolkit.dev;
\r
29 import java.awt.event.*;
\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 class Program implements JmfCaptureListener
\r
43 private OptimizeView _view1;
\r
44 private OptimizeView _view2;
\r
45 public Object _sync_object=new Object();
\r
46 public NyARParam _ar_param;
\r
47 public NyARCode _ar_code;
\r
48 private final static int SCREEN_X = 640;
\r
49 private final static int SCREEN_Y = 480;
\r
50 private JmfCaptureDevice _capture;
\r
51 public GLNyARRaster_RGB _cap_image;
\r
52 public Program(NyARParam i_param, NyARCode i_ar_code) throws NyARException
\r
55 JmfCaptureDeviceList devlist = new JmfCaptureDeviceList();
\r
56 this._capture = devlist.getDevice(0);
\r
57 if (!this._capture.setCaptureFormat(SCREEN_X, SCREEN_Y, 30.0f)) {
\r
58 throw new NyARException();
\r
60 this._ar_param=i_param;
\r
61 this._ar_code=i_ar_code;
\r
62 this._capture.setOnCapture(this);
\r
63 // GL対応のRGBラスタオブジェクト
\r
64 this._cap_image = new GLNyARRaster_RGB(i_param, this._capture.getCaptureFormat());
\r
65 this._view1=new OptimizeView(this,NyARSingleDetectMarker.PF_NYARTOOLKIT);
\r
66 this._view2=new OptimizeView(this,NyARSingleDetectMarker.PF_NYARTOOLKIT_ARTOOLKIT_FITTING);
\r
67 this._capture.start();
\r
70 public void onUpdateBuffer(Buffer i_buffer)
\r
73 synchronized (this._sync_object) {
\r
74 this._cap_image.setBuffer(i_buffer);
\r
75 this._view1.updateCapture(this._cap_image);
\r
76 this._view2.updateCapture(this._cap_image);
\r
78 } catch (Exception e) {
\r
79 e.printStackTrace();
\r
83 void glDrawCube(GL i_gl)
\r
85 // Colour cube data.
\r
87 float fSize = 0.5f;// マーカーサイズに対して0.5倍なので、4cmの立方体
\r
89 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
90 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
91 int cube_num_faces = 6;
\r
92 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
94 if (polyList == 0) {
\r
95 polyList = i_gl.glGenLists(1);
\r
96 i_gl.glNewList(polyList, GL.GL_COMPILE);
\r
97 i_gl.glBegin(GL.GL_QUADS);
\r
98 for (f = 0; f < cube_num_faces; f++)
\r
99 for (i = 0; i < 4; i++) {
\r
100 i_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
101 i_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
104 i_gl.glColor3f(0.0f, 0.0f, 0.0f);
\r
105 for (f = 0; f < cube_num_faces; f++) {
\r
106 i_gl.glBegin(GL.GL_LINE_LOOP);
\r
107 for (i = 0; i < 4; i++)
\r
108 i_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
114 i_gl.glPushMatrix(); // Save world coordinate system.
\r
115 i_gl.glTranslatef(0.0f, 0.0f, 0.5f); // Place base of cube on marker surface.
\r
116 i_gl.glRotatef(0.0f, 0.0f, 0.0f, 1.0f); // Rotate about z axis.
\r
117 i_gl.glDisable(GL.GL_LIGHTING); // Just use colours.
\r
118 i_gl.glCallList(polyList); // Draw the cube.
\r
119 i_gl.glPopMatrix(); // Restore world coordinate system.
\r
125 * simpleLiteと同じようなテストプログラム 出来る限りARToolKitのサンプルと似せて作ってあります。 最も一致する"Hiro"マーカーを一つ選択して、その上に立方体を表示します。
\r
128 public class OptimizeView implements GLEventListener
\r
130 private final static int SCREEN_X = 640;
\r
132 private final static int SCREEN_Y = 480;
\r
134 private Animator _animator;
\r
136 private Program _parent;
\r
139 // private JmfCaptureDevice _capture;
\r
142 private NyARGLUtil _glnya;
\r
145 private NyARSingleDetectMarker _nya;
\r
146 private NyARParam _ar_param;
\r
148 private double[] _camera_projection = new double[16];
\r
151 public OptimizeView(Program i_program,int i_pf) throws NyARException
\r
153 this._parent=i_program;
\r
154 this._ar_param = i_program._ar_param;
\r
156 Frame frame = new Frame("["+i_pf+"]");
\r
159 this._nya = new NyARSingleDetectMarker(this._ar_param, i_program._ar_code, 80.0,i_program._cap_image.getBufferReader().getBufferType(),i_pf);
\r
160 this._nya.setContinueMode(false);// ここをtrueにすると、transMatContinueモード(History計算)になります。
\r
163 GLCanvas canvas = new GLCanvas();
\r
165 canvas.addGLEventListener(this);
\r
166 frame.addWindowListener(new WindowAdapter() {
\r
167 public void windowClosing(WindowEvent e)
\r
173 frame.setVisible(true);
\r
174 Insets ins = frame.getInsets();
\r
175 frame.setSize(SCREEN_X + ins.left + ins.right, SCREEN_Y + ins.top + ins.bottom);
\r
176 canvas.setBounds(ins.left, ins.top, SCREEN_X, SCREEN_Y);
\r
179 public void init(GLAutoDrawable drawable)
\r
181 this._gl = drawable.getGL();
\r
182 this._gl.glEnable(GL.GL_DEPTH_TEST);
\r
183 this._gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
\r
186 // NyARToolkit用の支援クラス
\r
187 _glnya = new NyARGLUtil(_gl);
\r
188 } catch (Exception e) {
\r
189 e.printStackTrace();
\r
192 this._glnya.toCameraFrustumRH(this._ar_param,this._camera_projection);
\r
193 this._animator = new Animator(drawable);
\r
194 this._animator.start();
\r
198 public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height)
\r
200 _gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
\r
201 _gl.glViewport(0, 0, width, height);
\r
204 _gl.glMatrixMode(GL.GL_PROJECTION);
\r
205 _gl.glLoadIdentity();
\r
207 _gl.glMatrixMode(GL.GL_MODELVIEW);
\r
208 _gl.glLoadIdentity();
\r
211 private boolean _is_marker_exist=false;
\r
212 private NyARTransMatResult __display_transmat_result = new NyARTransMatResult();
\r
214 private double[] __display_wk = new double[16];
\r
216 public void display(GLAutoDrawable drawable)
\r
218 NyARTransMatResult transmat_result = __display_transmat_result;
\r
219 if (!this._parent._cap_image.hasData()) {
\r
223 this._gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); // Clear the buffers for new frame.
\r
224 this._glnya.drawBackGround(this._parent._cap_image, 1.0);
\r
226 synchronized(this._parent._sync_object){
\r
228 if (this._is_marker_exist){
\r
229 // マーカーの一致度を調査するならば、ここでnya.getConfidence()で一致度を調べて下さい。
\r
230 // Projection transformation.
\r
231 _gl.glMatrixMode(GL.GL_PROJECTION);
\r
232 _gl.glLoadMatrixd(_camera_projection, 0);
\r
233 _gl.glMatrixMode(GL.GL_MODELVIEW);
\r
234 // Viewing transformation.
\r
235 _gl.glLoadIdentity();
\r
237 _nya.getTransmationMatrix(transmat_result);
\r
238 // 変換行列をOpenGL形式に変換
\r
239 _glnya.toCameraViewRH(transmat_result, __display_wk);
\r
240 _gl.glLoadMatrixd(__display_wk, 0);
\r
242 // All other lighting and geometry goes here.
\r
243 this._parent.glDrawCube(_gl);
\r
246 Thread.sleep(1);// タスク実行権限を一旦渡す
\r
247 }catch(Exception e){
\r
248 e.printStackTrace();
\r
253 public void updateCapture(GLNyARRaster_RGB i_img)
\r
256 synchronized (this._parent._sync_object) {
\r
257 this._is_marker_exist =this._nya.detectMarkerLite(i_img, 110);
\r
259 } catch (Exception e) {
\r
260 e.printStackTrace();
\r
264 public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged)
\r
268 private final static String CARCODE_FILE = "../Data/patt.hiro";
\r
270 private final static String PARAM_FILE = "../Data/camera_para.dat";
\r
272 public static void main(String[] args)
\r
275 NyARParam param = new NyARParam();
\r
276 param.loadARParamFromFile(PARAM_FILE);
\r
277 param.changeScreenSize(SCREEN_X, SCREEN_Y);
\r
279 NyARCode code = new NyARCode(16, 16);
\r
280 code.loadARPattFromFile(CARCODE_FILE);
\r
282 new Program(param, code);
\r
283 } catch (Exception e) {
\r
284 e.printStackTrace();
\r