| 1 |
nyatla |
420 |
/* |
| 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 |
|
|
import javax.media.opengl.GL; |
| 30 |
|
|
|
| 31 |
|
|
import processing.core.*; |
| 32 |
|
|
import processing.opengl.PGraphicsOpenGL; |
| 33 |
|
|
|
| 34 |
|
|
import jp.nyatla.nyartoolkit.*; |
| 35 |
|
|
import jp.nyatla.nyartoolkit.core.*; |
| 36 |
|
|
import jp.nyatla.nyartoolkit.core.squaredetect.*; |
| 37 |
|
|
import jp.nyatla.nyartoolkit.core.param.*; |
| 38 |
|
|
import jp.nyatla.nyartoolkit.core.transmat.NyARTransMatResult; |
| 39 |
|
|
import jp.nyatla.nyartoolkit.core.types.*; |
| 40 |
|
|
|
| 41 |
|
|
|
| 42 |
|
|
|
| 43 |
|
|
/** |
| 44 |
|
|
* NyARToolkit for Processing������������������������ |
| 45 |
|
|
* ������������������������������������������������������������������������������������������������������������������������������ |
| 46 |
|
|
* @author nyatla |
| 47 |
|
|
* |
| 48 |
|
|
*/ |
| 49 |
|
|
class NyARPsgBaseClass |
| 50 |
|
|
{ |
| 51 |
|
|
/** |
| 52 |
|
|
* RightHand������������������������������������������ |
| 53 |
|
|
* ��������������������������������������������������� |
| 54 |
|
|
* <br/>EN: - |
| 55 |
|
|
*/ |
| 56 |
|
|
public final static int CS_RIGHT=0; |
| 57 |
|
|
/** |
| 58 |
|
|
* LeftHand������������������������������������������ |
| 59 |
|
|
* ��������������������������������������������������� |
| 60 |
|
|
* <br/>EN: - |
| 61 |
|
|
*/ |
| 62 |
|
|
public final static int CS_LEFT =1; |
| 63 |
|
|
/** |
| 64 |
|
|
* ��������������������������������� |
| 65 |
|
|
* <br/>EN: |
| 66 |
|
|
* version string. |
| 67 |
|
|
*/ |
| 68 |
|
|
public final String VERSION = "NyAR4psg/0.3.0;NyARToolkit for java/2.5.0+;ARToolKit/2.72.1"; |
| 69 |
|
|
/** |
| 70 |
|
|
* OpenGL���������������ProjectionMatrix��������� |
| 71 |
|
|
* <br/>EN: |
| 72 |
|
|
* OpenGL form projection matrix. |
| 73 |
|
|
*/ |
| 74 |
|
|
public final double[] projection=new double[16]; |
| 75 |
|
|
|
| 76 |
|
|
protected PMatrix3D _ps_projection=new PMatrix3D(); |
| 77 |
|
|
protected PApplet _pa; |
| 78 |
|
|
protected NyARParam _ar_param; |
| 79 |
|
|
protected NyARPsgBaseClass(PApplet parent,String i_cparam_file, int i_width,int i_htight,int i_projection_coord_system) |
| 80 |
|
|
{ |
| 81 |
|
|
checkCoordinateSystemRange(parent,i_projection_coord_system); |
| 82 |
|
|
this._pa=parent; |
| 83 |
|
|
try{ |
| 84 |
|
|
this._ar_param=new NyARParam(); |
| 85 |
|
|
this._ar_param.loadARParam(this._pa.createInput(i_cparam_file)); |
| 86 |
|
|
this._ar_param.changeScreenSize(i_width, i_htight); |
| 87 |
|
|
initProjection(parent,this._ar_param,i_projection_coord_system); |
| 88 |
|
|
}catch(NyARException e){ |
| 89 |
|
|
this._pa.die("Error while setting up NyARToolkit for java", e); |
| 90 |
|
|
} |
| 91 |
|
|
return; |
| 92 |
|
|
} |
| 93 |
|
|
private void initProjection(PApplet i_pa, NyARParam i_param,int i_coord_system) |
| 94 |
|
|
{ |
| 95 |
|
|
NyARMat trans_mat = new NyARMat(3, 4); |
| 96 |
|
|
NyARMat icpara_mat = new NyARMat(3, 4); |
| 97 |
|
|
double[][] p = new double[3][3], q = new double[4][4]; |
| 98 |
|
|
int i, j; |
| 99 |
|
|
|
| 100 |
|
|
final NyARIntSize size=i_param.getScreenSize(); |
| 101 |
|
|
final int width = size.w; |
| 102 |
|
|
final int height = size.h; |
| 103 |
|
|
|
| 104 |
|
|
i_param.getPerspectiveProjectionMatrix().decompMat(icpara_mat, trans_mat); |
| 105 |
|
|
|
| 106 |
|
|
double[][] icpara = icpara_mat.getArray(); |
| 107 |
|
|
double[][] trans = trans_mat.getArray(); |
| 108 |
|
|
for (i = 0; i < 4; i++) { |
| 109 |
|
|
icpara[1][i] = (height - 1) * (icpara[2][i]) - icpara[1][i]; |
| 110 |
|
|
} |
| 111 |
|
|
|
| 112 |
|
|
for (i = 0; i < 3; i++) { |
| 113 |
|
|
for (j = 0; j < 3; j++) { |
| 114 |
|
|
p[i][j] = icpara[i][j] / icpara[2][2]; |
| 115 |
|
|
} |
| 116 |
|
|
} |
| 117 |
|
|
q[0][0] = (2.0 * p[0][0] / (width - 1)); |
| 118 |
|
|
q[0][1] = (2.0 * p[0][1] / (width - 1)); |
| 119 |
|
|
q[0][2] = -((2.0 * p[0][2] / (width - 1)) - 1.0); |
| 120 |
|
|
q[0][3] = 0.0; |
| 121 |
|
|
|
| 122 |
|
|
q[1][0] = 0.0; |
| 123 |
|
|
q[1][1] = -(2.0 * p[1][1] / (height - 1)); |
| 124 |
|
|
q[1][2] = -((2.0 * p[1][2] / (height - 1)) - 1.0); |
| 125 |
|
|
q[1][3] = 0.0; |
| 126 |
|
|
|
| 127 |
|
|
q[2][0] = 0.0; |
| 128 |
|
|
q[2][1] = 0.0; |
| 129 |
|
|
q[2][2] = (view_distance_max + view_distance_min) / (view_distance_min - view_distance_max); |
| 130 |
|
|
q[2][3] = 2.0 * view_distance_max * view_distance_min / (view_distance_min - view_distance_max); |
| 131 |
|
|
|
| 132 |
|
|
q[3][0] = 0.0; |
| 133 |
|
|
q[3][1] = 0.0; |
| 134 |
|
|
q[3][2] = -1.0; |
| 135 |
|
|
q[3][3] = 0.0; |
| 136 |
|
|
|
| 137 |
|
|
switch(i_coord_system){ |
| 138 |
|
|
case NyARPsgBaseClass.CS_LEFT: |
| 139 |
|
|
break; |
| 140 |
|
|
case NyARPsgBaseClass.CS_RIGHT: |
| 141 |
|
|
q[2][2] = q[2][2]* -1; |
| 142 |
|
|
q[2][3] = q[2][3]* -1; |
| 143 |
|
|
break; |
| 144 |
|
|
default: |
| 145 |
|
|
i_pa.die("Please set NyARBoard.CS_LEFT or NyARBoard.CS_RIGHT."); |
| 146 |
|
|
} |
| 147 |
|
|
|
| 148 |
|
|
for (i = 0; i < 4; i++) { // Row. |
| 149 |
|
|
// First 3 columns of the current row. |
| 150 |
|
|
for (j = 0; j < 3; j++) { // Column. |
| 151 |
|
|
this.projection[i + j * 4] = q[i][0] * trans[0][j] + q[i][1] * trans[1][j] + q[i][2] * trans[2][j]; |
| 152 |
|
|
} |
| 153 |
|
|
// Fourth column of the current row. |
| 154 |
|
|
this.projection[i + 3 * 4] = q[i][0] * trans[0][3] + q[i][1] * trans[1][3] + q[i][2] * trans[2][3] + q[i][3]; |
| 155 |
|
|
} |
| 156 |
|
|
|
| 157 |
|
|
//processing���ProjectionMatrix������������������������������ |
| 158 |
|
|
this._ps_projection.m00=(float)this.projection[0]; |
| 159 |
|
|
this._ps_projection.m01=(float)this.projection[1]; |
| 160 |
|
|
this._ps_projection.m02=(float)this.projection[2]; |
| 161 |
|
|
this._ps_projection.m03=(float)this.projection[3]; |
| 162 |
|
|
this._ps_projection.m10=(float)this.projection[4]; |
| 163 |
|
|
this._ps_projection.m11=(float)this.projection[5]; |
| 164 |
|
|
this._ps_projection.m12=(float)this.projection[6]; |
| 165 |
|
|
this._ps_projection.m13=(float)this.projection[7]; |
| 166 |
|
|
this._ps_projection.m20=(float)this.projection[8]; |
| 167 |
|
|
this._ps_projection.m21=(float)this.projection[9]; |
| 168 |
|
|
this._ps_projection.m22=(float)this.projection[10]; |
| 169 |
|
|
this._ps_projection.m23=(float)this.projection[11]; |
| 170 |
|
|
this._ps_projection.m30=(float)this.projection[12]; |
| 171 |
|
|
this._ps_projection.m31=(float)this.projection[13]; |
| 172 |
|
|
this._ps_projection.m32=(float)this.projection[14]; |
| 173 |
|
|
this._ps_projection.m33=(float)this.projection[15]; |
| 174 |
|
|
this._ps_projection.transpose(); |
| 175 |
|
|
|
| 176 |
|
|
return; |
| 177 |
|
|
} |
| 178 |
|
|
|
| 179 |
|
|
|
| 180 |
|
|
private final static double view_distance_min = 100;//#define VIEW_DISTANCE_MIN 0.1 // Objects closer to the camera than this will not be displayed. |
| 181 |
|
|
private final static double view_distance_max = 100000;//#define VIEW_DISTANCE_MAX 100.0 // Objects further away from the camera than this will not be displayed. |
| 182 |
|
|
private final static void checkCoordinateSystemRange(PApplet i_pa,int i_cs) |
| 183 |
|
|
{ |
| 184 |
|
|
switch(i_cs){ |
| 185 |
|
|
case NyARPsgBaseClass.CS_LEFT: |
| 186 |
|
|
case NyARPsgBaseClass.CS_RIGHT: |
| 187 |
|
|
return; |
| 188 |
|
|
default: |
| 189 |
|
|
i_pa.die("Please set constant CS_LEFT or CS_RIGHT."); |
| 190 |
|
|
} |
| 191 |
|
|
} |
| 192 |
|
|
private static double view_scale_factor = 1.0; |
| 193 |
|
|
protected static void matResult2GLArray(NyARTransMatResult i_src,double[] o_gl_array) |
| 194 |
|
|
{ |
| 195 |
|
|
o_gl_array[0 + 0 * 4] = i_src.m00; |
| 196 |
|
|
o_gl_array[0 + 1 * 4] = i_src.m01; |
| 197 |
|
|
o_gl_array[0 + 2 * 4] = i_src.m02; |
| 198 |
|
|
o_gl_array[0 + 3 * 4] = i_src.m03; |
| 199 |
|
|
o_gl_array[1 + 0 * 4] = -i_src.m10; |
| 200 |
|
|
o_gl_array[1 + 1 * 4] = -i_src.m11; |
| 201 |
|
|
o_gl_array[1 + 2 * 4] = -i_src.m12; |
| 202 |
|
|
o_gl_array[1 + 3 * 4] = -i_src.m13; |
| 203 |
|
|
o_gl_array[2 + 0 * 4] = -i_src.m20; |
| 204 |
|
|
o_gl_array[2 + 1 * 4] = -i_src.m21; |
| 205 |
|
|
o_gl_array[2 + 2 * 4] = -i_src.m22; |
| 206 |
|
|
o_gl_array[2 + 3 * 4] = -i_src.m23; |
| 207 |
|
|
o_gl_array[3 + 0 * 4] = 0.0; |
| 208 |
|
|
o_gl_array[3 + 1 * 4] = 0.0; |
| 209 |
|
|
o_gl_array[3 + 2 * 4] = 0.0; |
| 210 |
|
|
o_gl_array[3 + 3 * 4] = 1.0; |
| 211 |
|
|
if (view_scale_factor != 0.0) { |
| 212 |
|
|
o_gl_array[12] *= view_scale_factor; |
| 213 |
|
|
o_gl_array[13] *= view_scale_factor; |
| 214 |
|
|
o_gl_array[14] *= view_scale_factor; |
| 215 |
|
|
} |
| 216 |
|
|
} |
| 217 |
|
|
} |
| 218 |
|
|
|
| 219 |
|
|
/** |
| 220 |
|
|
* ������������������������������������������������������������������������������������������������������������������������ |
| 221 |
|
|
* ������������������������������������������������������������������������������ |
| 222 |
|
|
* @author nyatla |
| 223 |
|
|
* |
| 224 |
|
|
*/ |
| 225 |
|
|
class SingleMarkerBaseClass extends NyARPsgBaseClass |
| 226 |
|
|
{ |
| 227 |
|
|
/** |
| 228 |
|
|
* ������������x,y,z������������������������ |
| 229 |
|
|
* <br/>EN: |
| 230 |
|
|
* The angle value in radian unit of "x,y,z" . |
| 231 |
|
|
*/ |
| 232 |
|
|
public PVector angle; |
| 233 |
|
|
/** |
| 234 |
|
|
* ������������x,y,z��������������������������� |
| 235 |
|
|
* <br/>EN: |
| 236 |
|
|
* The translation value in radian unit of "x,y,z". |
| 237 |
|
|
*/ |
| 238 |
|
|
public PVector trans; |
| 239 |
|
|
/** |
| 240 |
|
|
* ������������������������4������������������������������������������ |
| 241 |
|
|
* <br/>EN: |
| 242 |
|
|
* The position of 4 corner of marker. |
| 243 |
|
|
*/ |
| 244 |
|
|
public int[][] pos2d; |
| 245 |
|
|
/** |
| 246 |
|
|
* ��������������������������������������������� |
| 247 |
|
|
* <br/>EN: |
| 248 |
|
|
* The transform matrix of detected marker. |
| 249 |
|
|
*/ |
| 250 |
|
|
public double[] transmat; |
| 251 |
|
|
/** |
| 252 |
|
|
* ���������������������������Matrix��������������������� |
| 253 |
|
|
* ���������������������������������processing������������������������������������������������������ |
| 254 |
|
|
* ������������������������������endTransform������������������������������������������������������������ |
| 255 |
|
|
* <br/>EN: |
| 256 |
|
|
* This function sets corresponding transform matrix to the surface of the marker to OpenGL. |
| 257 |
|
|
* The coordinate system of processing moves to the surface of the marker when this function is executed. |
| 258 |
|
|
* Must return the coordinate system by using endTransform function at the end. |
| 259 |
|
|
* @param i_pgl |
| 260 |
|
|
* PGraphicsOpenGL���������������������������������������processing���g��������������������������������������������������������� |
| 261 |
|
|
* <br/>EN: |
| 262 |
|
|
* Specify PGraphicsOpenGL instance. |
| 263 |
|
|
* Set cast "g" member of processing graphics object. |
| 264 |
|
|
*/ |
| 265 |
|
|
public void beginTransform(PGraphicsOpenGL i_pgl) |
| 266 |
|
|
{ |
| 267 |
|
|
if(this._gl!=null){ |
| 268 |
|
|
this._pa.die("The function beginTransform is already called.", null); |
| 269 |
|
|
} |
| 270 |
|
|
this._pgl=i_pgl; |
| 271 |
|
|
this._gl=this._pgl.beginGL(); |
| 272 |
|
|
|
| 273 |
|
|
|
| 274 |
|
|
this._gl=i_pgl.gl; |
| 275 |
|
|
this._gl.glMatrixMode(GL.GL_PROJECTION); |
| 276 |
|
|
this._pa.pushMatrix(); |
| 277 |
|
|
this._pa.resetMatrix(); |
| 278 |
|
|
//PGraphicsOpenGL���updateProjection������������������������������ |
| 279 |
|
|
this._gl.glLoadMatrixd(this.projection,0); |
| 280 |
|
|
this._old_matrix=this._pgl.projection; |
| 281 |
|
|
this._pgl.projection=this._ps_projection; |
| 282 |
|
|
|
| 283 |
|
|
|
| 284 |
|
|
this._gl.glMatrixMode(GL.GL_MODELVIEW); |
| 285 |
|
|
this._pa.pushMatrix(); |
| 286 |
|
|
this._pa.resetMatrix(); |
| 287 |
|
|
this._gl.glLoadMatrixd(this.transmat,0); |
| 288 |
|
|
|
| 289 |
|
|
this._pa.pushMatrix(); |
| 290 |
|
|
return; |
| 291 |
|
|
} |
| 292 |
|
|
/** |
| 293 |
|
|
* beginTransform��������������������������������������������������������� |
| 294 |
|
|
* <br/>EN: |
| 295 |
|
|
* This function recover coordinate system that was changed by beginTransform function. |
| 296 |
|
|
*/ |
| 297 |
|
|
public void endTransform() |
| 298 |
|
|
{ |
| 299 |
|
|
if(this._gl==null){ |
| 300 |
|
|
this._pa.die("The function beginTransform is never called.", null); |
| 301 |
|
|
} |
| 302 |
|
|
this._pgl.projection=this._old_matrix; |
| 303 |
|
|
this._pa.popMatrix(); |
| 304 |
|
|
this._pa.popMatrix(); |
| 305 |
|
|
this._gl.glMatrixMode(GL.GL_PROJECTION); |
| 306 |
|
|
this._pa.popMatrix(); |
| 307 |
|
|
this._gl.glMatrixMode(GL.GL_MODELVIEW); |
| 308 |
|
|
if(this._pgl!=null){ |
| 309 |
|
|
this._pgl.endGL(); |
| 310 |
|
|
} |
| 311 |
|
|
this._gl=null; |
| 312 |
|
|
this._pgl=null; |
| 313 |
|
|
return; |
| 314 |
|
|
} |
| 315 |
|
|
protected void updateTransmat(NyARSquare i_square,NyARTransMatResult i_src) |
| 316 |
|
|
{ |
| 317 |
|
|
matResult2GLArray(i_src,this.transmat); |
| 318 |
|
|
//angle |
| 319 |
|
|
i_src.getZXYAngle(this._tmp_d3p); |
| 320 |
|
|
|
| 321 |
|
|
this.angle.x=(float)this._tmp_d3p.x; |
| 322 |
|
|
this.angle.y=(float)this._tmp_d3p.y; |
| 323 |
|
|
this.angle.z=(float)this._tmp_d3p.z; |
| 324 |
|
|
//trans |
| 325 |
|
|
this.trans.x=(float)i_src.m03; |
| 326 |
|
|
this.trans.y=(float)i_src.m13; |
| 327 |
|
|
this.trans.z=(float)i_src.m23; |
| 328 |
|
|
|
| 329 |
|
|
//pos������ |
| 330 |
|
|
final NyARDoublePoint2d[] pts=i_square.sqvertex; |
| 331 |
|
|
for(int i=0;i<4;i++){ |
| 332 |
|
|
this.pos2d[i][0]=(int)pts[i].x; |
| 333 |
|
|
this.pos2d[i][1]=(int)pts[i].y; |
| 334 |
|
|
} |
| 335 |
|
|
|
| 336 |
|
|
return; |
| 337 |
|
|
} |
| 338 |
|
|
/******** |
| 339 |
|
|
* protected/private |
| 340 |
|
|
*******/ |
| 341 |
|
|
private final NyARDoublePoint3d _tmp_d3p=new NyARDoublePoint3d(); |
| 342 |
|
|
|
| 343 |
|
|
|
| 344 |
|
|
//��������������������� |
| 345 |
|
|
private GL _gl=null; |
| 346 |
|
|
private PGraphicsOpenGL _pgl=null; |
| 347 |
|
|
private PMatrix3D _old_matrix; |
| 348 |
|
|
protected SingleMarkerBaseClass(PApplet parent,String i_cparam_file, int i_width,int i_htight,int i_projection_coord_system) |
| 349 |
|
|
{ |
| 350 |
|
|
super(parent,i_cparam_file,i_width,i_htight,i_projection_coord_system); |
| 351 |
|
|
} |
| 352 |
|
|
|
| 353 |
|
|
|
| 354 |
|
|
|
| 355 |
|
|
|
| 356 |
|
|
|
| 357 |
|
|
|
| 358 |
|
|
} |