適当に書いてみたのですが、
・upベクトルを上向きの(0,-1,0)ではなく下向きの(0,1,0)にしないと今までの画面の向きにならないのですが、正常ですか?
・見た目では正しそうに見えますが、本当に正しいですか?
コード:
static void vectorGaiseki(double out[4],double a[4],double b[4]) {
double temp[4];
temp[0]=a[1]*b[2]-b[1]*a[2];
temp[1]=a[2]*b[0]-b[2]*a[0];
temp[2]=a[0]*b[1]-b[0]*a[1];
temp[3]=1;
out[0]=temp[0];
out[1]=temp[1];
out[2]=temp[2];
out[3]=temp[3];
}
static void vectorNormalize(double vec[4]) {
double zettaiti;
zettaiti=sqrt(vec[0]*vec[0]+vec[1]*vec[1]+vec[2]*vec[2]);
vec[0]/=zettaiti;
vec[1]/=zettaiti;
vec[2]/=zettaiti;
}
void updateCameraLookAt(camera_t* camera,
double x,double y,double z,
double upx,double upy,double upz) {
double Vmatrix[4][4]; // ビュー行列
double MVmatrix[4][4]; // 平行移動行列
double RZmatrix[4][4]; // z回転行列
double Pmatrix[4][4]; //射影変換行列
double rot;
double vz[4];
double vy[4];
double vx[4];
vz[0]=x-camera->x;
vz[1]=y-camera->y;
vz[2]=z-camera->z;
vz[3]=1;
vy[0]=upx;
vy[1]=upy;
vy[2]=upz;
vy[3]=1;
vectorGaiseki(vx,vy,vz);
vectorGaiseki(vy,vz,vx);
vectorNormalize(vx);
vectorNormalize(vy);
vectorNormalize(vz);
// ビュー行列
setMatrix( Vmatrix,
vx[0], vy[0], vz[0], 0,
vx[1], vy[1], vz[1], 0,
vx[2], vy[2], vz[2], 0,
0, 0, 0, 1 );
Vmatrix[3][0]=-(camera->x*vx[0]+camera->y*vx[1]+camera->z*vx[2]);
Vmatrix[3][1]=-(camera->x*vy[0]+camera->y*vy[1]+camera->z*vy[2]);
Vmatrix[3][2]=-(camera->x*vz[0]+camera->y*vz[1]+camera->z*vz[2]);
// Z回転マトリックス
rot = -camera->cz;
setMatrix( RZmatrix,
cos(rot), sin(rot), 0, 0,
-sin(rot), cos(rot), 0, 0,
0, 0, 1, 0,
0, 0, 0, 1 );
// マトリックスの合成
calcMatrix(Vmatrix,RZmatrix);
// 射影変換行列を設定する。
{
double depth,ctan;
double cnear,cfar,width,height;
cnear = 0.1;
cfar = 1000.0;
ctan = tan(camera->genkaiangle*0.5);
width = 1.0 / ctan;
height = 1.0 / ctan;
depth = cfar / (cfar - cnear);
setMatrix( Pmatrix,
width, 0, 0, 0,
0, height, 0, 0,
0, 0, depth, 1,
0, 0, -depth * cnear, 0 );
}
// 行列の演算。
calcMatrix(Vmatrix,Pmatrix);
copyMatrix(camera->matrix,Vmatrix);
}
よろしくお願いします。
UIも少し変更したので、プロジェクトを上げ直します。
オフトピック
このライブラリ、DXライブラリと比べるとy軸の方向が180度逆なのですね。
仕様ということで、悪くはないですけど。