Develop and Download Open Source Software

Browse Subversion Repository

Contents of /nyar4psg/trunk/src/src/jp/nyatla/nyar4psg/NyARPsgBaseClass.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 824 - (show annotations) (download) (as text)
Sat Apr 23 02:26:56 2011 UTC (12 years, 11 months ago) by nyatla
Original Path: nyar4psg/trunk/src/jp/nyatla/nyar4psg/NyARPsgBaseClass.java
File MIME type: text/x-java
File size: 13168 byte(s)


1 /*
2 * PROJECT: NyARToolkit for proce55ing.
3 * --------------------------------------------------------------------------------
4 * The MIT License
5 * Copyright (c) 2008 nyatla
6 * airmail(at)ebony.plala.or.jp
7 * http://nyatla.jp/nyartoolkit/
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 *
26 */
27
28 package jp.nyatla.nyar4psg;
29
30
31 import javax.media.opengl.*;
32
33 import processing.core.*;
34 import processing.opengl.*;
35
36 import jp.nyatla.nyartoolkit.*;
37 import jp.nyatla.nyartoolkit.core.param.*;
38 import jp.nyatla.nyartoolkit.core.transmat.*;
39 import jp.nyatla.nyartoolkit.core.types.matrix.NyARDoubleMatrix44;
40
41
42
43 /**
44 * ���������������������NyARToolkit for Processing������������������������������
45 * ARToolkit���������������������������������������������������������������������������������������������
46 */
47 class NyARPsgBaseClass
48 {
49 /**
50 * ���������������������������������������������������������������������
51 * RightHand���������������������������������
52 * RightHand���������������ARToolKit������������������������������������������Processing������������������������������������������text���������������������������������������
53 */
54 public final static int CS_RIGHT_HAND=0;
55 /**
56 * ���������������������������������������������������������������������
57 * LeftHand������������������������������
58 * RightHand���������������ARToolKit������������������������������������������Processing������������������������������������������
59 * processing������������������������������������������������������������������������
60 */
61 public final static int CS_LEFT_HAND =1;
62 /**
63 * ���������������������������������
64 * NyAR4psg������������������������������������������
65 */
66 public final static String VERSION = "NyAR4psg/0.9.0;NyARToolkit for java/3.0.0+;ARToolKit/2.72.1";
67 /**���Processing���������������ProjectionMatrix���������*/
68 protected PMatrix3D _ps_projection=new PMatrix3D();
69 /**���������������Applet������������������������������*/
70 protected PApplet _ref_papplet;
71 /**���ARToolkit���������������������������������������������*/
72 protected NyARParam _ar_param;
73 protected NyARFrustum _frustum;
74 protected int _coord_system;
75 /**
76 * ������������������������������
77 */
78 protected NyARPsgBaseClass()
79 {
80 }
81 protected void initInstance(PApplet parent,String i_cparam_file, int i_width,int i_height,int i_coord_system)
82 {
83 checkCoordinateSystemRange(parent,i_coord_system);
84 this._ref_papplet=parent;
85 this._coord_system=i_coord_system;
86 try{
87 this._frustum=new NyARFrustum();
88 this._ar_param=new NyARParam();
89 this._ar_param.loadARParam(this._ref_papplet.createInput(i_cparam_file));
90 this._ar_param.changeScreenSize(i_width, i_height);
91
92 //Processing���projectionMatrix���������������Frustum���������
93 arPerspectiveMat2Projection(this._ar_param.getPerspectiveProjectionMatrix(),i_width,i_height,this._ps_projection,this._frustum);
94 }catch(NyARException e){
95 this._ref_papplet.die("Error while setting up NyARToolkit for java", e);
96 }
97 return;
98 }
99 private final static double view_distance_min = 100;
100 private final static double view_distance_max = 100000;
101 private final static void checkCoordinateSystemRange(PApplet i_pa,int i_cs)
102 {
103 switch(i_cs){
104 case NyARPsgBaseClass.CS_LEFT_HAND:
105 case NyARPsgBaseClass.CS_RIGHT_HAND:
106 return;
107 default:
108 i_pa.die("Please set constant CS_LEFT_HAND or CS_RIGHT_HAND.");
109 }
110 }
111
112 private float[] _tmpf=new float[16];
113
114 /**
115 * ������������������ARToolKit���������ProjectionMatrix���Processing������������������������
116 * ���������������������������Processing���ProjectionMatrix���ARToolKit���������������������������������������������������������������������������������������������������������������������
117 * Processing������������������Frustum���������������������{@link PGraphics3D#perspective()}������������������
118 * Frustum���������������������������{@link PGraphics3D#perspective()}���{@link PGraphics3D#perspective()}���������������������������������
119 * @return
120 * ���������������������������projectionMatrix������������������
121 */
122 public PMatrix3D setARPerspective()
123 {
124 return this.setPerspective(this._ps_projection);
125 }
126 /**
127 * ������������������ProjectionMatrix���Processing������������������������
128 * @param i_projection
129 * ������������ProjectionMatrix���������������������
130 * @return
131 * ���������������������������projectionMatrix������������������
132 */
133 public PMatrix3D setPerspective(PMatrix3D i_projection)
134 {
135 if(!(this._ref_papplet.g instanceof PGraphics3D)){
136 this._ref_papplet.die("NyAR4Psg require PGraphics3D instance.");
137 }
138 PGraphics3D g=(PGraphics3D)this._ref_papplet.g;
139 //���������ProjectionMatrix������������������
140 PMatrix3D ret=new PMatrix3D();
141 ret.set(g.projection);
142 //ProjectionMatrix���������
143 g.projection.set(i_projection);
144 //OpenGL���������������������������
145 if(this._ref_papplet.g instanceof PGraphicsOpenGL)
146 {
147 GL gl=((PGraphicsOpenGL)g).gl;
148 gl.glMatrixMode(GL.GL_PROJECTION);
149 PMatrix2GLProjection(i_projection,_tmpf);
150 gl.glLoadMatrixf(_tmpf,0);
151 }
152 return ret;
153 }
154
155 protected static void PMatrix2GLProjection(PMatrix3D i_in,float[] o_out)
156 {
157 o_out[ 0]=i_in.m00;
158 o_out[ 1]=i_in.m10;
159 o_out[ 2]=i_in.m20;
160 o_out[ 3]=i_in.m30;
161 o_out[ 4]=i_in.m01;
162 o_out[ 5]=i_in.m11;
163 o_out[ 6]=i_in.m21;
164 o_out[ 7]=i_in.m31;
165 o_out[ 8]=i_in.m02;
166 o_out[ 9]=i_in.m12;
167 o_out[10]=i_in.m22;
168 o_out[11]=i_in.m32;
169 o_out[12]=i_in.m03;
170 o_out[13]=i_in.m13;
171 o_out[14]=i_in.m23;
172 o_out[15]=i_in.m33;
173 }
174 protected static void PMatrix2GLProjection(PMatrix3D i_in,double[] o_out)
175 {
176 o_out[ 0]=i_in.m00;
177 o_out[ 1]=i_in.m10;
178 o_out[ 2]=i_in.m20;
179 o_out[ 3]=i_in.m30;
180 o_out[ 4]=i_in.m01;
181 o_out[ 5]=i_in.m11;
182 o_out[ 6]=i_in.m21;
183 o_out[ 7]=i_in.m31;
184 o_out[ 8]=i_in.m02;
185 o_out[ 9]=i_in.m12;
186 o_out[10]=i_in.m22;
187 o_out[11]=i_in.m32;
188 o_out[12]=i_in.m03;
189 o_out[13]=i_in.m13;
190 o_out[14]=i_in.m23;
191 o_out[15]=i_in.m33;
192 }
193
194 private static void arPerspectiveMat2Projection(NyARPerspectiveProjectionMatrix i_prjmat,int i_w,int i_h,PMatrix3D o_projection,NyARFrustum o_frustum)
195 {
196 NyARDoubleMatrix44 tmp=new NyARDoubleMatrix44();
197 i_prjmat.makeCameraFrustumRH(i_w, i_h, view_distance_min, view_distance_max,tmp);
198 o_projection.m00=(float)(tmp.m00);
199 o_projection.m01=(float)(tmp.m01);
200 o_projection.m02=(float)(tmp.m02);
201 o_projection.m03=(float)(tmp.m03);
202 o_projection.m10=(float)(tmp.m10);
203 o_projection.m11=(float)(tmp.m11);
204 o_projection.m12=(float)(tmp.m12);
205 o_projection.m13=(float)(tmp.m13);
206 o_projection.m20=(float)(tmp.m20);
207 o_projection.m21=(float)(tmp.m21);
208 o_projection.m22=(float)(tmp.m22);
209 o_projection.m23=(float)(tmp.m23);
210 o_projection.m30=(float)(tmp.m30);
211 o_projection.m31=(float)(tmp.m31);
212 o_projection.m32=(float)(tmp.m32);
213 o_projection.m33=(float)(tmp.m33);
214 o_frustum.setValue(tmp, i_w, i_h);
215 }
216 protected static void matResult2GLArray(NyARTransMatResult i_src,double[] o_gl_array)
217 {
218 o_gl_array[0 + 0 * 4] = i_src.m00;
219 o_gl_array[0 + 1 * 4] = i_src.m01;
220 o_gl_array[0 + 2 * 4] = i_src.m02;
221 o_gl_array[0 + 3 * 4] = i_src.m03;
222 o_gl_array[1 + 0 * 4] = -i_src.m10;
223 o_gl_array[1 + 1 * 4] = -i_src.m11;
224 o_gl_array[1 + 2 * 4] = -i_src.m12;
225 o_gl_array[1 + 3 * 4] = -i_src.m13;
226 o_gl_array[2 + 0 * 4] = -i_src.m20;
227 o_gl_array[2 + 1 * 4] = -i_src.m21;
228 o_gl_array[2 + 2 * 4] = -i_src.m22;
229 o_gl_array[2 + 3 * 4] = -i_src.m23;
230 o_gl_array[3 + 0 * 4] = 0.0;
231 o_gl_array[3 + 1 * 4] = 0.0;
232 o_gl_array[3 + 2 * 4] = 0.0;
233 o_gl_array[3 + 3 * 4] = 1.0;
234 }
235 /**
236 * ���������������������������
237 */
238 private final static PMatrix3D _lh_mat=new PMatrix3D(-1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1);
239
240 /**
241 * ���������������Processing���Matrix���������������������
242 * @param i_src
243 * @param i_mode
244 * @param o_pmatrix
245 */
246 protected static void matResult2PMatrix3D(NyARTransMatResult i_src,int i_mode,PMatrix3D o_pmatrix)
247 {
248 o_pmatrix.m00 = (float)i_src.m00;
249 o_pmatrix.m01 = (float)i_src.m01;
250 o_pmatrix.m02 = (float)i_src.m02;
251 o_pmatrix.m03 = (float)i_src.m03;
252 o_pmatrix.m10 = (float)i_src.m10;//mirror
253 o_pmatrix.m11 = (float)i_src.m11;//mirror
254 o_pmatrix.m12 = (float)i_src.m12;//mirror
255 o_pmatrix.m13 = (float)i_src.m13;//mirror
256 o_pmatrix.m20 = (float)-i_src.m20;
257 o_pmatrix.m21 = (float)-i_src.m21;
258 o_pmatrix.m22 = (float)-i_src.m22;
259 o_pmatrix.m23 = (float)-i_src.m23;
260 o_pmatrix.m30 = 0.0f;
261 o_pmatrix.m31 = 0.0f;
262 o_pmatrix.m32 = 0.0f;
263 o_pmatrix.m33 = 1.0f;
264 if(i_mode==MultiMarker.CS_LEFT_HAND)
265 {
266 o_pmatrix.apply(_lh_mat);
267 }
268 }
269 /**
270 * ������������������������������������������������������������������������������
271 * ���������������������������������������������������������������
272 * <p>������ -
273 * ������������������gluUnproject������������������������������������������������������������������������������
274 * ���������������������������������������������
275 * http://www.opengl.org/sdk/docs/man/xhtml/gluUnProject.xml
276 * ARToolKit���������������������������������������������OpenGL���unProject������ix,iy���������������������������������������������������������������������������������������
277 * </p>
278 * @param ix
279 * ���������������������������
280 * @param iy
281 * ������������������
282 * @param o_point_on_screen
283 * ���������������
284 */
285 /* public final PVector unProject(double ix,double iy)
286 {
287 double n=(this._frustum_rh.m23/(this._frustum_rh.m22-1));
288 NyARDoubleMatrix44 m44=this._inv_frustum_rh;
289 double v1=(this._screen_size.w-ix-1)*2/this._screen_size.w-1.0;//ARToolKit���Frustram������������������
290 double v2=(this._screen_size.h-iy-1)*2/this._screen_size.h-1.0;
291 double v3=2*n-1.0;
292 double b=1/(m44.m30*v1+m44.m31*v2+m44.m32*v3+m44.m33);
293 o_point_on_screen.x=(m44.m00*v1+m44.m01*v2+m44.m02*v3+m44.m03)*b;
294 o_point_on_screen.y=(m44.m10*v1+m44.m11*v2+m44.m12*v3+m44.m13)*b;
295 o_point_on_screen.z=(m44.m20*v1+m44.m21*v2+m44.m22*v3+m44.m23)*b;
296 return;
297 }
298 */ /**
299 * ���������������������������������������������������������������������������������������������������������������������������������������������������������
300 * ���������������������������������������������
301 * @param ix
302 * ���������������������������
303 * @param iy
304 * ���������������������������
305 * @param i_mat
306 * ������������������������������
307 * @param o_pos
308 * ������������������������������������������������
309 */
310 /* public final PVector unProjectOnCamera(double ix,double iy,PMatrix3D i_mat)
311 {
312 //������������������
313 this.unProject(ix,iy,o_pos);
314 //������������������������������
315 double nx=i_mat.m02;
316 double ny=i_mat.m12;
317 double nz=i_mat.m22;
318 double mx=i_mat.m03;
319 double my=i_mat.m13;
320 double mz=i_mat.m23;
321 double t=(nx*mx+ny*my+nz*mz)/(nx*o_pos.x+ny*o_pos.y+nz*o_pos.z);
322 o_pos.x=t*o_pos.x;
323 o_pos.y=t*o_pos.y;
324 o_pos.z=t*o_pos.z;
325 }
326 */ /**
327 * ������������������������������������������������������������������������������������������������������������������������
328 * ARToolKit������P175���������������������������������
329 * @param ix
330 * ���������������������������
331 * @param iy
332 * ���������������������������
333 * @param i_mat
334 * ������������������������������
335 * @param o_pos
336 * ������������������������������������������������
337 * @return
338 * ���������������������������true������������������
339 */
340 /* public final PVector unProjectOnMatrix(double ix,double iy,PMatrix3D i_mat)
341 {
342 //������������������������������������
343 unProjectOnCamera(ix,iy,i_mat,o_pos);
344 //������������������
345 NyARDoubleMatrix44 m=new NyARDoubleMatrix44();
346 if(!m.inverse(i_mat)){
347 return false;
348 }
349 m.transform3d(o_pos, o_pos);
350 return true;
351 }
352 */ /**
353 * ������������������������������������������������������������������������������
354 * @param i_x
355 * ������������������������
356 * @param i_y
357 * ������������������������
358 * @param i_z
359 * ������������������������
360 * @param o_pos2d
361 * ������������������������������������������������
362 */
363 /* public final PVector project(double i_x,double i_y,double i_z)
364 {
365 NyARDoubleMatrix44 m=this._frustum_rh;
366 double v3_1=1/i_z*m.m32;
367 double w=this._screen_size.w;
368 double h=this._screen_size.h;
369 o_pos2d.x=w-(1+(i_x*m.m00+i_z*m.m02)*v3_1)*w/2;
370 o_pos2d.y=h-(1+(i_y*m.m11+i_z*m.m12)*v3_1)*h/2;
371 return;
372 }
373 */}
374

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26