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
28 package jp.nyatla.nyartoolkit.jogl.sample;
\r
30 import java.awt.event.*;
\r
32 import javax.media.*;
\r
33 import javax.media.opengl.*;
\r
34 import com.sun.opengl.util.Animator;
\r
35 import jp.nyatla.nyartoolkit.core.param.*;
\r
36 import jp.nyatla.nyartoolkit.core.transmat.NyARTransMatResult;
\r
37 import jp.nyatla.nyartoolkit.core.*;
\r
38 import jp.nyatla.nyartoolkit.detector.*;
\r
39 import jp.nyatla.nyartoolkit.jmf.utils.*;
\r
40 import jp.nyatla.nyartoolkit.jogl.utils.*;
\r
42 * simpleLiteの複数マーカー同時認識バージョン
\r
43 * "Hiro"のマーカーと"人"のマーカーの混在環境で、Hiroのマーカー全てに
\r
46 public class JavaSimpleLite2 implements GLEventListener, JmfCaptureListener
\r
48 private final String CARCODE_FILE1 = "../../Data/patt.hiro";
\r
50 private final String CARCODE_FILE2 = "../../Data/patt.kanji";
\r
52 private final String PARAM_FILE = "../../Data/camera_para.dat";
\r
54 private final static int SCREEN_X = 640;
\r
56 private final static int SCREEN_Y = 480;
\r
58 private Animator _animator;
\r
60 private GLNyARRaster_RGB _cap_image;
\r
62 private JmfCaptureDevice _capture;
\r
66 private NyARGLUtil _glnya;
\r
69 private NyARDetectMarker _nya;
\r
71 private NyARParam _ar_param;
\r
72 private double[] _camera_projection=new double[16];
\r
79 // Colour cube data.
\r
81 float fSize = 0.5f;//マーカーサイズに対して0.5倍なので、4cmの立方体
\r
83 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
84 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
85 int cube_num_faces = 6;
\r
86 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
88 if (polyList == 0) {
\r
89 polyList = _gl.glGenLists(1);
\r
90 _gl.glNewList(polyList, GL.GL_COMPILE);
\r
91 _gl.glBegin(GL.GL_QUADS);
\r
92 for (f = 0; f < cube_num_faces; f++)
\r
93 for (i = 0; i < 4; i++) {
\r
94 _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
95 _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
98 _gl.glColor3f(0.0f, 0.0f, 0.0f);
\r
99 for (f = 0; f < cube_num_faces; f++) {
\r
100 _gl.glBegin(GL.GL_LINE_LOOP);
\r
101 for (i = 0; i < 4; i++)
\r
102 _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.glPushMatrix(); // Save world coordinate system.
\r
109 _gl.glTranslatef(0.0f, 0.0f, 0.5f); // Place base of cube on marker surface.
\r
110 _gl.glRotatef(0.0f, 0.0f, 0.0f, 1.0f); // Rotate about z axis.
\r
111 _gl.glDisable(GL.GL_LIGHTING); // Just use colours.
\r
112 _gl.glCallList(polyList); // Draw the cube.
\r
113 _gl.glPopMatrix(); // Restore world coordinate system.
\r
117 public JavaSimpleLite2()
\r
119 Frame frame = new Frame("Java simpleLite with NyARToolkit");
\r
122 GLCanvas canvas = new GLCanvas();
\r
124 canvas.addGLEventListener(this);
\r
125 frame.addWindowListener(new WindowAdapter() {
\r
126 public void windowClosing(WindowEvent e)
\r
132 frame.setVisible(true);
\r
133 Insets ins = frame.getInsets();
\r
134 frame.setSize(SCREEN_X + ins.left + ins.right, SCREEN_Y + ins.top + ins.bottom);
\r
135 canvas.setBounds(ins.left, ins.top, SCREEN_X, SCREEN_Y);
\r
138 public void init(GLAutoDrawable drawable)
\r
140 _gl = drawable.getGL();
\r
141 _gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
\r
145 JmfCaptureDeviceList devlist=new JmfCaptureDeviceList();
\r
146 _capture=devlist.getDevice(0);
\r
147 _capture.setCaptureFormat(JmfCaptureDevice.PIXEL_FORMAT_RGB,SCREEN_X, SCREEN_Y,15f);
\r
148 _capture.setOnCapture(this);
\r
150 _ar_param = new NyARParam();
\r
151 _ar_param.loadARParamFromFile(PARAM_FILE);
\r
152 _ar_param.changeScreenSize(SCREEN_X, SCREEN_Y);
\r
155 double[] width = new double[] { 80.0, 80.0 };
\r
156 NyARCode[] ar_codes = new NyARCode[2];
\r
157 ar_codes[0] = new NyARCode(16, 16);
\r
158 ar_codes[0].loadARPattFromFile(CARCODE_FILE1);
\r
159 ar_codes[1] = new NyARCode(16, 16);
\r
160 ar_codes[1].loadARPattFromFile(CARCODE_FILE2);
\r
161 _nya = new NyARDetectMarker(_ar_param, ar_codes, width, 2);
\r
162 _nya.setContinueMode(false);//ここをtrueにすると、transMatContinueモード(History計算)になります。
\r
163 //NyARToolkit用の支援クラス
\r
164 _glnya = new NyARGLUtil(_gl);
\r
165 //GL対応のRGBラスタオブジェクト
\r
166 _cap_image = new GLNyARRaster_RGB(_ar_param);
\r
169 } catch (Exception e) {
\r
170 e.printStackTrace();
\r
173 _glnya.toCameraFrustumRH(_ar_param,_camera_projection);
\r
175 _animator = new Animator(drawable);
\r
180 public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height)
\r
182 _gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
\r
183 _gl.glViewport(0, 0, width, height);
\r
186 _gl.glMatrixMode(GL.GL_PROJECTION);
\r
187 _gl.glLoadIdentity();
\r
189 _gl.glMatrixMode(GL.GL_MODELVIEW);
\r
190 _gl.glLoadIdentity();
\r
192 private NyARTransMatResult __display_transmat_result=new NyARTransMatResult();
\r
193 private double[] __display_wk=new double[16];
\r
195 public void display(GLAutoDrawable drawable)
\r
197 NyARTransMatResult transmat_result=__display_transmat_result;
\r
200 if (!_cap_image.hasData()) {
\r
203 _gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); // Clear the buffers for new frame.
\r
204 //画像チェックしてマーカー探して、背景を書く
\r
206 synchronized (_cap_image) {
\r
207 found_markers = _nya.detectMarkerLite(_cap_image, 110);
\r
209 _glnya.drawBackGround(_cap_image, 1.0);
\r
212 for (int i = 0; i < found_markers; i++) {
\r
213 //1番のマーカーでなければ表示しない。
\r
214 if (_nya.getARCodeIndex(i) != 0) {
\r
217 //マーカーの一致度を調査するならば、ここでnya.getConfidence()で一致度を調べて下さい。
\r
218 // Projection transformation.
\r
219 _gl.glMatrixMode(GL.GL_PROJECTION);
\r
220 _gl.glLoadMatrixd(_camera_projection, 0);
\r
221 _gl.glMatrixMode(GL.GL_MODELVIEW);
\r
222 // Viewing transformation.
\r
223 _gl.glLoadIdentity();
\r
225 _nya.getTransmationMatrix(i,transmat_result);
\r
227 _glnya.toCameraViewRH(transmat_result, __display_wk);
\r
228 _gl.glLoadMatrixd(__display_wk, 0);
\r
230 // All other lighting and geometry goes here.
\r
233 Thread.sleep(1);//タスク実行権限を一旦渡す
\r
234 } catch (Exception e) {
\r
235 e.printStackTrace();
\r
240 public void onUpdateBuffer(Buffer i_buffer)
\r
243 synchronized (_cap_image) {
\r
244 _cap_image.setBuffer(i_buffer, true);
\r
246 } catch (Exception e) {
\r
247 e.printStackTrace();
\r
251 public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged)
\r
255 public static void main(String[] args)
\r
257 new JavaSimpleLite2();
\r