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.jogl.sample;
\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
43 * simpleLiteと同じようなテストプログラム 出来る限りARToolKitのサンプルと似せて作ってあります。 最も一致する"Hiro"マーカーを一つ選択して、その上に立方体を表示します。
\r
46 public class JavaSimpleLite implements GLEventListener, JmfCaptureListener
\r
48 private final static int SCREEN_X = 640;
\r
50 private final static int SCREEN_Y = 480;
\r
52 private Animator _animator;
\r
54 private GLNyARRaster_RGB _cap_image;
\r
56 private JmfCaptureDevice _capture;
\r
60 private NyARGLUtil _glnya;
\r
63 private NyARSingleDetectMarker _nya;
\r
65 private NyARParam _ar_param;
\r
67 private Object _sync_object=new Object();
\r
68 private double[] _camera_projection = new double[16];
\r
76 // Colour cube data.
\r
78 float fSize = 0.5f;// マーカーサイズに対して0.5倍なので、4cmの立方体
\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
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
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
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
114 public JavaSimpleLite(NyARParam i_param, NyARCode i_ar_code) throws NyARException
\r
116 this._ar_param = i_param;
\r
118 Frame frame = new Frame("Java simpleLite with NyARToolkit");
\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
127 this._capture.setOnCapture(this);
\r
128 // GL対応のRGBラスタオブジェクト
\r
129 this._cap_image = new GLNyARRaster_RGB(this._ar_param, this._capture.getCaptureFormat());
\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
136 GLCanvas canvas = new GLCanvas();
\r
138 canvas.addGLEventListener(this);
\r
139 frame.addWindowListener(new WindowAdapter() {
\r
140 public void windowClosing(WindowEvent e)
\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
152 public void init(GLAutoDrawable drawable)
\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
159 // NyARToolkit用の支援クラス
\r
160 _glnya = new NyARGLUtil(_gl);
\r
163 } catch (Exception e) {
\r
164 e.printStackTrace();
\r
167 this._glnya.toCameraFrustumRH(this._ar_param,this._camera_projection);
\r
168 this._animator = new Animator(drawable);
\r
169 this._animator.start();
\r
173 public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height)
\r
175 _gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
\r
176 _gl.glViewport(0, 0, width, height);
\r
179 _gl.glMatrixMode(GL.GL_PROJECTION);
\r
180 _gl.glLoadIdentity();
\r
182 _gl.glMatrixMode(GL.GL_MODELVIEW);
\r
183 _gl.glLoadIdentity();
\r
186 private boolean _is_marker_exist=false;
\r
187 private NyARTransMatResult __display_transmat_result = new NyARTransMatResult();
\r
189 private double[] __display_wk = new double[16];
\r
191 public void display(GLAutoDrawable drawable)
\r
193 NyARTransMatResult transmat_result = __display_transmat_result;
\r
194 if (!_cap_image.hasData()) {
\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
201 synchronized(this._sync_object){
\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
212 _nya.getTransmationMatrix(transmat_result);
\r
213 // 変換行列をOpenGL形式に変換
\r
214 _glnya.toCameraViewRH(transmat_result, __display_wk);
\r
215 _gl.glLoadMatrixd(__display_wk, 0);
\r
217 // All other lighting and geometry goes here.
\r
221 Thread.sleep(1);// タスク実行権限を一旦渡す
\r
222 }catch(Exception e){
\r
223 e.printStackTrace();
\r
228 public void onUpdateBuffer(Buffer i_buffer)
\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
235 } catch (Exception e) {
\r
236 e.printStackTrace();
\r
240 public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged)
\r
244 private final static String CARCODE_FILE = "../../Data/patt.hiro";
\r
246 private final static String PARAM_FILE = "../../Data/camera_para.dat";
\r
248 public static void main(String[] args)
\r
251 NyARParam param = new NyARParam();
\r
252 param.loadARParamFromFile(PARAM_FILE);
\r
253 param.changeScreenSize(SCREEN_X, SCREEN_Y);
\r
255 NyARCode code = new NyARCode(16, 16);
\r
256 code.loadARPattFromFile(CARCODE_FILE);
\r
258 new JavaSimpleLite(param, code);
\r
259 } catch (Exception e) {
\r
260 e.printStackTrace();
\r