| 28 |
package jp.nyatla.nyar4psg; |
package jp.nyatla.nyar4psg; |
| 29 |
|
|
| 30 |
|
|
| 31 |
|
import javax.media.opengl.GL; |
| 32 |
|
|
| 33 |
import processing.core.*; |
import processing.core.*; |
| 34 |
|
import processing.opengl.PGraphicsOpenGL; |
| 35 |
|
|
| 36 |
import jp.nyatla.nyartoolkit.*; |
import jp.nyatla.nyartoolkit.*; |
| 37 |
import jp.nyatla.nyartoolkit.core.param.*; |
import jp.nyatla.nyartoolkit.core.param.*; |
| 38 |
import jp.nyatla.nyartoolkit.core.transmat.*; |
import jp.nyatla.nyartoolkit.core.transmat.*; |
| 39 |
import jp.nyatla.nyartoolkit.core.types.matrix.NyARDoubleMatrix44; |
import jp.nyatla.nyartoolkit.core.types.matrix.NyARDoubleMatrix44; |
| 40 |
|
|
| 41 |
class NyPsUtils |
|
|
{ |
|
|
public static void dumpObject(PMatrix3D i_mat) |
|
|
{ |
|
|
PApplet.println("PMatrix3D"); |
|
|
PApplet.println(String.format("%f %f %f %f",i_mat.m00,i_mat.m01,i_mat.m02,i_mat.m03)); |
|
|
PApplet.println(String.format("%f %f %f %f",i_mat.m10,i_mat.m11,i_mat.m12,i_mat.m13)); |
|
|
PApplet.println(String.format("%f %f %f %f",i_mat.m20,i_mat.m21,i_mat.m22,i_mat.m23)); |
|
|
PApplet.println(String.format("%f %f %f %f",i_mat.m30,i_mat.m31,i_mat.m32,i_mat.m33)); |
|
|
} |
|
|
public static void dumpObject(double[] i_val) |
|
|
{ |
|
|
PApplet.println("double[]"); |
|
|
for(int i=0;i<i_val.length;i++){ |
|
|
PApplet.println(i_val[i]); |
|
|
} |
|
|
} |
|
|
} |
|
| 42 |
|
|
| 43 |
/** |
/** |
| 44 |
* このクラスは、NyARToolkit for Processingのベースクラスです。 |
* このクラスは、NyARToolkit for Processingのベースクラスです。 |
| 49 |
/** |
/** |
| 50 |
* 定数値です。この値はコンストラクタで使います。 |
* 定数値です。この値はコンストラクタで使います。 |
| 51 |
* RightHand系の座標を構築します。 |
* RightHand系の座標を構築します。 |
| 52 |
* RightHand座標系は、ARToolKitと互換性のある座標系ですが、Processingの座標系と互換性がありません。(そのため、text等の出力が鏡像になります。) |
* RightHand座標系は、ARToolKitと互換性のある座標系ですが、Processingの座標系と互換性がないため、text等の出力が鏡像になります。 |
|
* <br/>EN: - |
|
| 53 |
*/ |
*/ |
| 54 |
public final static int CS_RIGHT_HAND=0; |
public final static int CS_RIGHT_HAND=0; |
| 55 |
/** |
/** |
| 57 |
* LeftHand座標系を構築します。 |
* LeftHand座標系を構築します。 |
| 58 |
* RightHand座標系は、ARToolKitと互換性のない座標系ですが、Processingの座標系と互換性があります。 |
* RightHand座標系は、ARToolKitと互換性のない座標系ですが、Processingの座標系と互換性があります。 |
| 59 |
* processing関数で描画する場合は、こちらを選択してください。 |
* processing関数で描画する場合は、こちらを選択してください。 |
|
* <br/>EN: - |
|
| 60 |
*/ |
*/ |
| 61 |
public final static int CS_LEFT_HAND =1; |
public final static int CS_LEFT_HAND =1; |
| 62 |
/** |
/** |
| 63 |
* バージョン文字列です。 |
* バージョン文字列です。 |
| 64 |
* NyAR4psgのバージョン情報を示します。 |
* NyAR4psgのバージョン情報を示します。 |
|
* <br/>EN: |
|
|
* version string. |
|
|
*/ |
|
|
public final String VERSION = "NyAR4psg/0.4.1;NyARToolkit for java/3.0.0;ARToolKit/2.72.1"; |
|
|
/** |
|
|
* OpenGLスタイルのProjectionMatrixです。 |
|
|
* <br/>EN: |
|
|
* OpenGL form projection matrix. |
|
|
*/ |
|
|
public final double[] projection=new double[16]; |
|
|
/** |
|
|
* ProcessingスタイルのProjectionMatrixです。 |
|
| 65 |
*/ |
*/ |
| 66 |
|
public final static String VERSION = "NyAR4psg/0.5.1;NyARToolkit for java/3.0.0+;ARToolKit/2.72.1"; |
| 67 |
|
/** ProcessingスタイルのProjectionMatrixです。*/ |
| 68 |
protected PMatrix3D _ps_projection=new PMatrix3D(); |
protected PMatrix3D _ps_projection=new PMatrix3D(); |
| 69 |
/** |
/** 参照するAppletのインスタンスです。*/ |
| 70 |
* 参照するAppletのインスタンスです。 |
protected PApplet _ref_papplet; |
| 71 |
*/ |
/** ARToolkitパラメータのインスタンスです。*/ |
|
protected PApplet _ref_papplet; |
|
|
/** |
|
|
* ARToolkitパラメータのインスタンスです。 |
|
|
*/ |
|
| 72 |
protected NyARParam _ar_param; |
protected NyARParam _ar_param; |
| 73 |
protected NyARFrustum _frustum; |
protected NyARFrustum _frustum; |
| 74 |
protected int _coord_system; |
protected int _coord_system; |
| 75 |
protected NyARPsgBaseClass(PApplet parent,String i_cparam_file, int i_width,int i_height,int i_coord_system) |
/** |
| 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); |
checkCoordinateSystemRange(parent,i_coord_system); |
| 84 |
this._ref_papplet=parent; |
this._ref_papplet=parent; |
| 90 |
this._ar_param.changeScreenSize(i_width, i_height); |
this._ar_param.changeScreenSize(i_width, i_height); |
| 91 |
|
|
| 92 |
//ProcessingのprojectionMatrixの計算と、Frustumの計算 |
//ProcessingのprojectionMatrixの計算と、Frustumの計算 |
| 93 |
arPerspectiveMat2Projection(this._ar_param.getPerspectiveProjectionMatrix(),i_width,i_height,this._ps_projection,this.projection,this._frustum); |
arPerspectiveMat2Projection(this._ar_param.getPerspectiveProjectionMatrix(),i_width,i_height,this._ps_projection,this._frustum); |
| 94 |
}catch(NyARException e){ |
}catch(NyARException e){ |
| 95 |
this._ref_papplet.die("Error while setting up NyARToolkit for java", e); |
this._ref_papplet.die("Error while setting up NyARToolkit for java", e); |
| 96 |
} |
} |
| 97 |
return; |
return; |
| 98 |
} |
} |
| 99 |
private final static double view_distance_min = 100; |
private final static double view_distance_min = 100; |
| 100 |
private final static double view_distance_max = 100000; |
private final static double view_distance_max = 100000; |
| 108 |
i_pa.die("Please set constant CS_LEFT_HAND or CS_RIGHT_HAND."); |
i_pa.die("Please set constant CS_LEFT_HAND or CS_RIGHT_HAND."); |
| 109 |
} |
} |
| 110 |
} |
} |
| 111 |
private static void arPerspectiveMat2Projection(NyARPerspectiveProjectionMatrix i_prjmat,int i_w,int i_h,PMatrix3D o_projection,double[] o_gl_projection,NyARFrustum o_frustum) |
|
| 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 PGraphicsOpenGL)){ |
| 136 |
|
this._ref_papplet.die("NyAR4Psg require PGraphicsOpenGL 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 |
|
if(this._ref_papplet.g instanceof PGraphicsOpenGL) |
| 145 |
|
{ |
| 146 |
|
GL gl=((PGraphicsOpenGL)g).gl; |
| 147 |
|
gl.glMatrixMode(GL.GL_PROJECTION); |
| 148 |
|
PMatrix2GLProjection(i_projection,_tmpf); |
| 149 |
|
gl.glLoadMatrixf(_tmpf,0); |
| 150 |
|
} |
| 151 |
|
return ret; |
| 152 |
|
} |
| 153 |
|
|
| 154 |
|
protected static void PMatrix2GLProjection(PMatrix3D i_in,float[] o_out) |
| 155 |
|
{ |
| 156 |
|
o_out[ 0]=i_in.m00; |
| 157 |
|
o_out[ 1]=i_in.m10; |
| 158 |
|
o_out[ 2]=i_in.m20; |
| 159 |
|
o_out[ 3]=i_in.m30; |
| 160 |
|
o_out[ 4]=i_in.m01; |
| 161 |
|
o_out[ 5]=i_in.m11; |
| 162 |
|
o_out[ 6]=i_in.m21; |
| 163 |
|
o_out[ 7]=i_in.m31; |
| 164 |
|
o_out[ 8]=i_in.m02; |
| 165 |
|
o_out[ 9]=i_in.m12; |
| 166 |
|
o_out[10]=i_in.m22; |
| 167 |
|
o_out[11]=i_in.m32; |
| 168 |
|
o_out[12]=i_in.m03; |
| 169 |
|
o_out[13]=i_in.m13; |
| 170 |
|
o_out[14]=i_in.m23; |
| 171 |
|
o_out[15]=i_in.m33; |
| 172 |
|
} |
| 173 |
|
protected static void PMatrix2GLProjection(PMatrix3D i_in,double[] o_out) |
| 174 |
|
{ |
| 175 |
|
o_out[ 0]=i_in.m00; |
| 176 |
|
o_out[ 1]=i_in.m10; |
| 177 |
|
o_out[ 2]=i_in.m20; |
| 178 |
|
o_out[ 3]=i_in.m30; |
| 179 |
|
o_out[ 4]=i_in.m01; |
| 180 |
|
o_out[ 5]=i_in.m11; |
| 181 |
|
o_out[ 6]=i_in.m21; |
| 182 |
|
o_out[ 7]=i_in.m31; |
| 183 |
|
o_out[ 8]=i_in.m02; |
| 184 |
|
o_out[ 9]=i_in.m12; |
| 185 |
|
o_out[10]=i_in.m22; |
| 186 |
|
o_out[11]=i_in.m32; |
| 187 |
|
o_out[12]=i_in.m03; |
| 188 |
|
o_out[13]=i_in.m13; |
| 189 |
|
o_out[14]=i_in.m23; |
| 190 |
|
o_out[15]=i_in.m33; |
| 191 |
|
} |
| 192 |
|
|
| 193 |
|
private static void arPerspectiveMat2Projection(NyARPerspectiveProjectionMatrix i_prjmat,int i_w,int i_h,PMatrix3D o_projection,NyARFrustum o_frustum) |
| 194 |
{ |
{ |
| 195 |
NyARDoubleMatrix44 tmp=new NyARDoubleMatrix44(); |
NyARDoubleMatrix44 tmp=new NyARDoubleMatrix44(); |
| 196 |
i_prjmat.makeCameraFrustumRH(i_w, i_h, view_distance_min, view_distance_max,tmp); |
i_prjmat.makeCameraFrustumRH(i_w, i_h, view_distance_min, view_distance_max,tmp); |
| 197 |
o_projection.m00=(float)(o_gl_projection[ 0]=tmp.m00); |
o_projection.m00=(float)(tmp.m00); |
| 198 |
o_projection.m01=(float)(o_gl_projection[ 1]=tmp.m10); |
o_projection.m01=(float)(tmp.m01); |
| 199 |
o_projection.m02=(float)(o_gl_projection[ 2]=tmp.m20); |
o_projection.m02=(float)(tmp.m02); |
| 200 |
o_projection.m03=(float)(o_gl_projection[ 3]=tmp.m30); |
o_projection.m03=(float)(tmp.m03); |
| 201 |
o_projection.m10=(float)(o_gl_projection[ 4]=tmp.m01); |
o_projection.m10=(float)(tmp.m10); |
| 202 |
o_projection.m11=(float)(o_gl_projection[ 5]=tmp.m11); |
o_projection.m11=(float)(tmp.m11); |
| 203 |
o_projection.m12=(float)(o_gl_projection[ 6]=tmp.m21); |
o_projection.m12=(float)(tmp.m12); |
| 204 |
o_projection.m13=(float)(o_gl_projection[ 7]=tmp.m31); |
o_projection.m13=(float)(tmp.m13); |
| 205 |
o_projection.m20=(float)(o_gl_projection[ 8]=tmp.m02); |
o_projection.m20=(float)(tmp.m20); |
| 206 |
o_projection.m21=(float)(o_gl_projection[ 9]=tmp.m12); |
o_projection.m21=(float)(tmp.m21); |
| 207 |
o_projection.m22=(float)(o_gl_projection[10]=tmp.m22); |
o_projection.m22=(float)(tmp.m22); |
| 208 |
o_projection.m23=(float)(o_gl_projection[11]=tmp.m32); |
o_projection.m23=(float)(tmp.m23); |
| 209 |
o_projection.m30=(float)(o_gl_projection[12]=tmp.m03); |
o_projection.m30=(float)(tmp.m30); |
| 210 |
o_projection.m31=(float)(o_gl_projection[13]=tmp.m13); |
o_projection.m31=(float)(tmp.m31); |
| 211 |
o_projection.m32=(float)(o_gl_projection[14]=tmp.m23); |
o_projection.m32=(float)(tmp.m32); |
| 212 |
o_projection.m33=(float)(o_gl_projection[15]=tmp.m33); |
o_projection.m33=(float)(tmp.m33); |
| 213 |
o_frustum.setValue(tmp, i_w, i_h); |
o_frustum.setValue(tmp, i_w, i_h); |
| 214 |
} |
} |
| 215 |
protected static void matResult2GLArray(NyARTransMatResult i_src,double[] o_gl_array) |
protected static void matResult2GLArray(NyARTransMatResult i_src,double[] o_gl_array) |