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.sandbox.quadx2;
\r
29 import java.awt.event.WindowAdapter;
\r
30 import java.awt.event.WindowEvent;
\r
33 import javax.media.Buffer;
\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
40 import com.sun.opengl.util.Animator;
\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.JmfCameraCapture;
\r
46 import jp.nyatla.nyartoolkit.jmf.utils.JmfCaptureListener;
\r
47 import jp.nyatla.nyartoolkit.jogl.utils.*;
\r
49 * simpleLiteと同じようなテストプログラム
\r
50 * 出来る限りARToolKitのサンプルと似せて作ってあります。
\r
51 * 最も一致する"Hiro"マーカーを一つ選択して、その上に立方体を表示します。
\r
54 public class JavaSimpleLite_Quad implements GLEventListener, JmfCaptureListener
\r
56 private final String CARCODE_FILE = "../../Data/patt.hiro";
\r
58 private final String PARAM_FILE = "../../Data/camera_para.dat";
\r
60 private final static int SCREEN_X = 320;
\r
62 private final static int SCREEN_Y = 240;
\r
64 private Animator _animator;
\r
66 private GLNyARRaster_RGB _cap_image;
\r
68 private JmfCameraCapture _capture;
\r
72 private NyARGLUtil _glnya;
\r
75 private NyARSingleDetectMarker_Quad _nya;
\r
77 private NyARParam _ar_param;
\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 = _gl.glGenLists(1);
\r
96 _gl.glNewList(polyList, GL.GL_COMPILE);
\r
97 _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 _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 _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 _gl.glColor3f(0.0f, 0.0f, 0.0f);
\r
105 for (f = 0; f < cube_num_faces; f++) {
\r
106 _gl.glBegin(GL.GL_LINE_LOOP);
\r
107 for (i = 0; i < 4; i++)
\r
108 _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 _gl.glPushMatrix(); // Save world coordinate system.
\r
115 _gl.glTranslatef(0.0f, 0.0f, 0.5f); // Place base of cube on marker surface.
\r
116 _gl.glRotatef(0.0f, 0.0f, 0.0f, 1.0f); // Rotate about z axis.
\r
117 _gl.glDisable(GL.GL_LIGHTING); // Just use colours.
\r
118 _gl.glCallList(polyList); // Draw the cube.
\r
119 _gl.glPopMatrix(); // Restore world coordinate system.
\r
123 public JavaSimpleLite_Quad()
\r
125 Frame frame = new Frame("Java simpleLite with NyARToolkit");
\r
128 GLCanvas canvas = new GLCanvas();
\r
130 canvas.addGLEventListener(this);
\r
131 frame.addWindowListener(new WindowAdapter() {
\r
132 public void windowClosing(WindowEvent e)
\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
144 public void init(GLAutoDrawable drawable)
\r
146 _gl = drawable.getGL();
\r
147 _gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
\r
151 _capture = new JmfCameraCapture(SCREEN_X, SCREEN_Y, 15f, JmfCameraCapture.PIXEL_FORMAT_RGB);
\r
152 _capture.setCaptureListener(this);
\r
154 _ar_param = new NyARParam();
\r
155 NyARCode ar_code = new NyARCode(16, 16);
\r
156 _ar_param.loadARParamFromFile(PARAM_FILE);
\r
157 _ar_param.changeScreenSize(SCREEN_X, SCREEN_Y);
\r
158 _nya = new NyARSingleDetectMarker_Quad(_ar_param, ar_code, 80.0);
\r
159 _nya.setContinueMode(false);//ここをtrueにすると、transMatContinueモード(History計算)になります。
\r
160 ar_code.loadARPattFromFile(CARCODE_FILE);
\r
161 //NyARToolkit用の支援クラス
\r
162 _glnya = new NyARGLUtil(_gl);
\r
163 //GL対応のRGBラスタオブジェクト
\r
164 _cap_image = new GLNyARRaster_RGB(_ar_param);
\r
167 } catch (Exception e) {
\r
168 e.printStackTrace();
\r
171 _glnya.toCameraFrustumRH(_ar_param,_camera_projection);
\r
172 _animator = new Animator(drawable);
\r
178 public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height)
\r
180 _gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
\r
181 _gl.glViewport(0, 0, width, height);
\r
184 _gl.glMatrixMode(GL.GL_PROJECTION);
\r
185 _gl.glLoadIdentity();
\r
187 _gl.glMatrixMode(GL.GL_MODELVIEW);
\r
188 _gl.glLoadIdentity();
\r
190 private double[] _camera_projection=new double[16];
\r
191 private NyARTransMatResult __display_transmat_result=new NyARTransMatResult();
\r
192 private double[] __display_wk=new double[16];
\r
193 public void display(GLAutoDrawable drawable)
\r
195 NyARTransMatResult transmat_result=__display_transmat_result;
\r
197 if (!_cap_image.hasData()) {
\r
200 _gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); // Clear the buffers for new frame.
\r
201 //画像チェックしてマーカー探して、背景を書く
\r
202 boolean is_marker_exist;
\r
203 synchronized (_cap_image) {
\r
204 is_marker_exist = _nya.detectMarkerLite(_cap_image, 110);
\r
206 _glnya.drawBackGround(_cap_image, 1.0);
\r
209 if (is_marker_exist) {
\r
210 //マーカーの一致度を調査するならば、ここでnya.getConfidence()で一致度を調べて下さい。
\r
211 // Projection transformation.
\r
212 _gl.glMatrixMode(GL.GL_PROJECTION);
\r
213 _gl.glLoadMatrixd(_camera_projection, 0);
\r
214 _gl.glMatrixMode(GL.GL_MODELVIEW);
\r
215 // Viewing transformation.
\r
216 _gl.glLoadIdentity();
\r
218 _nya.getTransmationMatrix(transmat_result);
\r
220 _glnya.toCameraViewRH(transmat_result, __display_wk);
\r
221 _gl.glLoadMatrixd(__display_wk, 0);
\r
223 // All other lighting and geometry goes here.
\r
226 Thread.sleep(1);//タスク実行権限を一旦渡す
\r
227 } catch (Exception e) {
\r
228 e.printStackTrace();
\r
232 public void onUpdateBuffer(Buffer i_buffer)
\r
235 synchronized (_cap_image) {
\r
236 _cap_image.setBuffer(i_buffer, true);
\r
238 } catch (Exception e) {
\r
239 e.printStackTrace();
\r
243 public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged)
\r
247 public static void main(String[] args)
\r
249 new JavaSimpleLite_Quad();
\r