| 36 |
import jp.nyatla.nyartoolkit.core.transmat.*; |
import jp.nyatla.nyartoolkit.core.transmat.*; |
| 37 |
import jp.nyatla.nyartoolkit.core.types.NyARDoublePoint2d; |
import jp.nyatla.nyartoolkit.core.types.NyARDoublePoint2d; |
| 38 |
import jp.nyatla.nyartoolkit.core.types.NyARDoublePoint3d; |
import jp.nyatla.nyartoolkit.core.types.NyARDoublePoint3d; |
| 39 |
|
import jp.nyatla.nyartoolkit.core.types.NyARIntSize; |
| 40 |
import jp.nyatla.nyartoolkit.core.types.matrix.NyARDoubleMatrix44; |
import jp.nyatla.nyartoolkit.core.types.matrix.NyARDoubleMatrix44; |
| 41 |
|
|
| 42 |
|
|
| 47 |
*/ |
*/ |
| 48 |
class NyARPsgBaseClass |
class NyARPsgBaseClass |
| 49 |
{ |
{ |
| 50 |
|
/** |
| 51 |
|
* nearクリップ面のデフォルト値です。 |
| 52 |
|
*/ |
| 53 |
|
public final float FRUSTUM_DEFAULT_NEAR_CLIP=100; |
| 54 |
|
/** |
| 55 |
|
* farクリップ面のデフォルト値です。 |
| 56 |
|
*/ |
| 57 |
|
public final float FRUSTUM_DEFAULT_FAR_CLIP=100000; |
| 58 |
|
|
| 59 |
/** |
/** |
| 60 |
* バージョン文字列です。 |
* バージョン文字列です。 |
| 61 |
* NyAR4psgのバージョン情報を示します。 |
* NyAR4psgのバージョン情報を示します。 |
| 62 |
*/ |
*/ |
| 63 |
public final static String VERSION = "NyAR4psg/1.0.3;NyARToolkit for java/3.0.0+;ARToolKit/2.72.1"; |
public final static String VERSION = "NyAR4psg/1.1.0;NyARToolkit for java/3.0.0+;ARToolKit/2.72.1"; |
| 64 |
/** 参照するAppletのインスタンスです。*/ |
/** 参照するAppletのインスタンスです。*/ |
| 65 |
protected PApplet _ref_papplet; |
protected PApplet _ref_papplet; |
| 66 |
/** ProcessingスタイルのProjectionMatrixです。*/ |
/** ProcessingスタイルのProjectionMatrixです。*/ |
| 74 |
protected PImageRaster _src_raster; |
protected PImageRaster _src_raster; |
| 75 |
/** 画像抽出用のオブジェクトです。{@link #_src_raster}を参照します。*/ |
/** 画像抽出用のオブジェクトです。{@link #_src_raster}を参照します。*/ |
| 76 |
protected NyARPerspectiveRasterReader _preader; |
protected NyARPerspectiveRasterReader _preader; |
| 77 |
|
|
| 78 |
|
private float _clip_far; |
| 79 |
|
private float _clip_near; |
| 80 |
|
|
| 81 |
/** |
/** |
| 82 |
* コンストラクタです。 |
* コンストラクタです。 |
| 83 |
*/ |
*/ |
| 92 |
this._preader=new NyARPerspectiveRasterReader(this._src_raster.getBufferType()); |
this._preader=new NyARPerspectiveRasterReader(this._src_raster.getBufferType()); |
| 93 |
try{ |
try{ |
| 94 |
this._ar_param.loadARParam(this._ref_papplet.createInput(i_cparam_file)); |
this._ar_param.loadARParam(this._ref_papplet.createInput(i_cparam_file)); |
| 95 |
this._ar_param.changeScreenSize(i_width, i_height); |
this._ar_param.changeScreenSize(i_width, i_height);//w,hはここでコピー |
| 96 |
|
|
| 97 |
//ProcessingのprojectionMatrixの計算と、Frustumの計算 |
//ProcessingのprojectionMatrixの計算と、Frustumの計算 |
| 98 |
arPerspectiveMat2Projection(this._ar_param.getPerspectiveProjectionMatrix(),i_width,i_height,this._ps_projection,this._frustum); |
this.setARClipping(FRUSTUM_DEFAULT_NEAR_CLIP,FRUSTUM_DEFAULT_FAR_CLIP); |
| 99 |
}catch(NyARException e){ |
}catch(NyARException e){ |
| 100 |
this._ref_papplet.die("Error while setting up NyARToolkit for java", e); |
this._ref_papplet.die("Error while setting up NyARToolkit for java", e); |
| 101 |
} |
} |
| 102 |
return; |
return; |
| 103 |
|
} |
| 104 |
|
/** |
| 105 |
|
* この関数は、PImageをバックグラウンドへ描画します。PImageはfarclip面に描画されます。 |
| 106 |
|
* <div>この関数は、次のコードと等価です。</div> |
| 107 |
|
* <hr/> |
| 108 |
|
* :<br/> |
| 109 |
|
* PMatrix3D om=new PMatrix3D(((PGraphics3D)g).projection);<br/> |
| 110 |
|
* ortho(-width/2, width/2,-height/2,height/2,near,far);<br/> |
| 111 |
|
* pushMatrix();<br/> |
| 112 |
|
* translate(0,0,far);<br/> |
| 113 |
|
* image(img,-width/2,-height/2);<br/> |
| 114 |
|
* popMatrix();<br/> |
| 115 |
|
* setPerspective(om);<br/> |
| 116 |
|
* :<br/> |
| 117 |
|
* <hr/> |
| 118 |
|
* この関数は、PrjectionMatrixとModelViewMatrixを復帰するため、若干のオーバヘッドがあります。 |
| 119 |
|
* 高速な処理が必要な場合には、展開してください。 |
| 120 |
|
* @param i_img |
| 121 |
|
* 背景画像を指定します。 |
| 122 |
|
*/ |
| 123 |
|
public void drawBackground(PImage i_img) |
| 124 |
|
{ |
| 125 |
|
PApplet pa=this._ref_papplet; |
| 126 |
|
PMatrix3D om=new PMatrix3D(((PGraphics3D)pa.g).projection); |
| 127 |
|
this.setAROrtho(i_img.width,i_img.height); |
| 128 |
|
pa.pushMatrix(); |
| 129 |
|
pa.resetMatrix(); |
| 130 |
|
pa.translate(0,0,this._clip_far); |
| 131 |
|
pa.image(i_img,-i_img.width/2,-i_img.height/2); |
| 132 |
|
pa.popMatrix(); |
| 133 |
|
//行列の復帰 |
| 134 |
|
this.setPerspective(om); |
| 135 |
|
} |
| 136 |
|
|
| 137 |
|
/** |
| 138 |
|
* この関数は、視錐台のクリップ面を設定します。この値のデフォルト値は、{@link #FRUSTUM_DEFAULT_NEAR_CLIP}と{@link #FRUSTUM_DEFAULT_FAR_CLIP}です。 |
| 139 |
|
* 設定値は、次回の{@link #setARPerspective()}から影響を及ぼします。現在の設定値にただちに影響を及ぼすものではありません。 |
| 140 |
|
* @param i_near |
| 141 |
|
* NearPlaneの値を設定します。単位は[mm]です。 |
| 142 |
|
* @param i_far |
| 143 |
|
* FarPlaneの値を設定します。単位は[mm]です。 |
| 144 |
|
*/ |
| 145 |
|
public void setARClipping(float i_near,float i_far) |
| 146 |
|
{ |
| 147 |
|
this._clip_far=i_far; |
| 148 |
|
this._clip_near=i_near; |
| 149 |
|
arPerspectiveMat2Projection(this._ar_param,i_near,i_far,this._ps_projection,this._frustum); |
| 150 |
|
} |
| 151 |
|
/** |
| 152 |
|
* この関数は、正射影行列をProcessingへセットします。 |
| 153 |
|
* 画面の中心が0,0にセットされます。near,farクリップは、{@link #setARClipping}でセットしたクリップ面をそのまま指定します。 |
| 154 |
|
* <div>この関数は、次のコードと等価です。</div> |
| 155 |
|
* <hr/> |
| 156 |
|
* :<br/> |
| 157 |
|
* ortho(-i_width/2, i_width/2,-i_height/2,i_height/2,near,far);<br/> |
| 158 |
|
* :<br/> |
| 159 |
|
* <hr/> |
| 160 |
|
* @param i_width |
| 161 |
|
* 幅を指定します。 |
| 162 |
|
* @param i_height |
| 163 |
|
* 高さを指定します。 |
| 164 |
|
*/ |
| 165 |
|
public void setAROrtho(int i_width,int i_height) |
| 166 |
|
{ |
| 167 |
|
float half_w=i_width/2; |
| 168 |
|
float half_h=i_height/2; |
| 169 |
|
this._ref_papplet.ortho(-half_w, half_w,-half_h,half_h,this._clip_near,this._clip_far); |
| 170 |
} |
} |
|
private final static float view_distance_min = 100; |
|
|
private final static float view_distance_max = 100000; |
|
|
|
|
|
|
|
| 171 |
/** |
/** |
| 172 |
* この関数は、ARToolKit準拠のProjectionMatrixをProcessingにセットします。 |
* この関数は、ARToolKit準拠のProjectionMatrixをProcessingにセットします。 |
| 173 |
* 関数を実行すると、ProcessingのProjectionMatrixがARToolKitのカメラパラメータのものに変わり、映像にマッチした描画ができるようになります。 |
* 関数を実行すると、ProcessingのProjectionMatrixがARToolKitのカメラパラメータのものに変わり、映像にマッチした描画ができるようになります。 |
| 174 |
* ProcessingのデフォルトFrustumに戻すときは、{@link PGraphics3D#perspective()}を使います。 |
* ProcessingのデフォルトFrustumに戻すときは、{@link PGraphics3D#perspective()}を使います。 |
| 175 |
* Frustumの有効期間は、次に{@link PGraphics3D#perspective()}か{@link PGraphics3D#perspective()}をコールするまでです。 |
* Frustumの有効期間は、次に{@link PGraphics3D#perspective()}か{@link PGraphics3D#perspective()}をコールするまでです。 |
|
* @return |
|
|
* 置き換えら得る前のprojectionMatrixを返します。 |
|
| 176 |
*/ |
*/ |
| 177 |
public PMatrix3D setARPerspective() |
public void setARPerspective() |
| 178 |
{ |
{ |
| 179 |
return this.setPerspective(this._ps_projection); |
this.setPerspective(this._ps_projection); |
| 180 |
} |
} |
| 181 |
/** |
/** |
| 182 |
* この関数は、ProjectionMatrixをProcessingにセットします。 |
* この関数は、ProjectionMatrixをProcessingにセットします。 |
| 183 |
* @param i_projection |
* @param i_projection |
| 184 |
* 設定するProjectionMatrixを指定します。 |
* 設定するProjectionMatrixを指定します。 |
|
* @return |
|
|
* 置き換えら得る前のprojectionMatrixを返します。 |
|
| 185 |
* |
* |
| 186 |
* <p> |
* <p> |
| 187 |
* Processing/1.3になったら、{@link PApplet#matrixMode}使ってきちんと使えるようになると思う。 |
* Processing/1.3になったら、{@link PApplet#matrixMode}使ってきちんと使えるようになると思う。 |
| 188 |
* 今は無理なので、frustum経由 |
* 今は無理なので、frustum経由 |
| 189 |
* </p> |
* </p> |
| 190 |
*/ |
*/ |
| 191 |
public PMatrix3D setPerspective(PMatrix3D i_projection) |
public void setPerspective(PMatrix3D i_projection) |
| 192 |
{ |
{ |
|
if(!(this._ref_papplet.g instanceof PGraphics3D)){ |
|
|
this._ref_papplet.die("NyAR4Psg require PGraphics3D instance."); |
|
|
} |
|
|
PGraphics3D g=(PGraphics3D)this._ref_papplet.g; |
|
|
//現在のProjectionMatrixを保存する。 |
|
|
PMatrix3D ret=new PMatrix3D(); |
|
|
ret.set(g.projection); |
|
| 193 |
//Projectionをfrustum経由で設定。 |
//Projectionをfrustum経由で設定。 |
| 194 |
float far=i_projection.m23/(i_projection.m22+1); |
float far=i_projection.m23/(i_projection.m22+1); |
| 195 |
float near=i_projection.m23/(i_projection.m22-1); |
float near=i_projection.m23/(i_projection.m22-1); |
| 199 |
(i_projection.m12-1)*near/i_projection.m11, |
(i_projection.m12-1)*near/i_projection.m11, |
| 200 |
(i_projection.m12+1)*near/i_projection.m11, |
(i_projection.m12+1)*near/i_projection.m11, |
| 201 |
near,far); |
near,far); |
| 202 |
return ret; |
return; |
| 203 |
} |
} |
| 204 |
|
|
| 205 |
protected static void PMatrix2GLProjection(PMatrix3D i_in,float[] o_out) |
protected static void PMatrix2GLProjection(PMatrix3D i_in,float[] o_out) |
| 241 |
o_out[15]=i_in.m33; |
o_out[15]=i_in.m33; |
| 242 |
} |
} |
| 243 |
|
|
| 244 |
private static void arPerspectiveMat2Projection(NyARPerspectiveProjectionMatrix i_prjmat,int i_w,int i_h,PMatrix3D o_projection,NyARFrustum o_frustum) |
private static void arPerspectiveMat2Projection(NyARParam i_param,float i_near,float i_far,PMatrix3D o_projection,NyARFrustum o_frustum) |
| 245 |
{ |
{ |
| 246 |
NyARDoubleMatrix44 tmp=new NyARDoubleMatrix44(); |
NyARDoubleMatrix44 tmp=new NyARDoubleMatrix44(); |
| 247 |
i_prjmat.makeCameraFrustumRH(i_w, i_h, view_distance_min, view_distance_max,tmp); |
NyARIntSize s=i_param.getScreenSize(); |
| 248 |
|
i_param.getPerspectiveProjectionMatrix().makeCameraFrustumRH(s.w,s.h,i_near,i_far,tmp); |
| 249 |
o_projection.m00=(float)(tmp.m00); |
o_projection.m00=(float)(tmp.m00); |
| 250 |
o_projection.m01=(float)(tmp.m01); |
o_projection.m01=(float)(tmp.m01); |
| 251 |
o_projection.m02=(float)(tmp.m02); |
o_projection.m02=(float)(tmp.m02); |
| 262 |
o_projection.m31=(float)(tmp.m31); |
o_projection.m31=(float)(tmp.m31); |
| 263 |
o_projection.m32=(float)(tmp.m32); |
o_projection.m32=(float)(tmp.m32); |
| 264 |
o_projection.m33=(float)(tmp.m33); |
o_projection.m33=(float)(tmp.m33); |
| 265 |
o_frustum.setValue(tmp, i_w, i_h); |
o_frustum.setValue(tmp, s.w, s.h); |
| 266 |
} |
} |
| 267 |
protected static void matResult2GLArray(NyARTransMatResult i_src,double[] o_gl_array) |
protected static void matResult2GLArray(NyARTransMatResult i_src,double[] o_gl_array) |
| 268 |
{ |
{ |
| 286 |
/** |
/** |
| 287 |
* 左手系変換用の行列 |
* 左手系変換用の行列 |
| 288 |
*/ |
*/ |
| 289 |
private final static PMatrix3D _lh_mat=new PMatrix3D(-1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1); |
private final static PMatrix3D _lh_mat=new PMatrix3D( |
| 290 |
|
-1,0,0,0, |
| 291 |
|
0,1,0,0, |
| 292 |
|
0,0,1,0, |
| 293 |
|
0,0,0,1); |
| 294 |
|
|
| 295 |
/** |
/** |
| 296 |
* 変換行列をProcessingのMatrixへ変換します。 |
* 変換行列をProcessingのMatrixへ変換します。 |