与向上矢量对齐时,相机矩阵无效

时间:2015-02-04 15:29:36

标签: c++ camera

我目前正在我的引擎上实现摄像头,当摄像头从顶部到地板时,我遇到了问题(例如,眼睛位置0,0。,50。目标是0,0。 ,0。)我的向上矢量是0.,0.,1 ..

然后当我做数学运算时,位置和向上的交叉积给出0,0,0。然后视图被拧紧,没有任何渲染。如果我移动相机,一切都按预期工作。

我该如何解决这个问题?

if (node==NULL || target==NULL) return;

node->Update();
eye=node->GetWorldMatrix()*GRPVECTOR(0.0,0.0,0.0); //converts from matrix to vector my vector transformation

target->Update();
obj=target->GetWorldMatrix()*GRPVECTOR(0.0,0.0,0.0);


GRPVECTOR ev;
GRPVECTOR z;
GRPVECTOR x_tmp;
GRPVECTOR x;
GRPVECTOR y;

ev = eye - obj;

ev.Normalize();
z=ev;

x_tmp.CrossProduct(&up,&z);             
if (x_tmp.GetLengthf()==0.0f)
    return;  //my view is screwed, I return 

x_tmp.Normalize();
x=x_tmp;

y.CrossProduct(&z,&x);

this->viewmatrix.matrix[0][0] = x.vector[0];
this->viewmatrix.matrix[0][1] = y.vector[0];
this->viewmatrix.matrix[0][2] = z.vector[0];
this->viewmatrix.matrix[0][3] = 0.0f;

this->viewmatrix.matrix[1][0] = x.vector[1];
this->viewmatrix.matrix[1][1] = y.vector[1];
this->viewmatrix.matrix[1][2] = z.vector[1];
this->viewmatrix.matrix[1][3] = 0.0f;

this->viewmatrix.matrix[2][0] = x.vector[2];
this->viewmatrix.matrix[2][1] = y.vector[2];
this->viewmatrix.matrix[2][2] = z.vector[2];
this->viewmatrix.matrix[2][3] = 0.0f;

this->viewmatrix.matrix[3][0] = -x.vector[0] * eye.vector[0] + -x.vector[1] * eye.vector[1] + -x.vector[2] * eye.vector[2];
this->viewmatrix.matrix[3][1] = -y.vector[0] * eye.vector[0] + -y.vector[1] * eye.vector[1] + -y.vector[2] * eye.vector[2];
this->viewmatrix.matrix[3][2] = -z.vector[0] * eye.vector[0] + -z.vector[1] * eye.vector[1] + -z.vector[2] * eye.vector[2];
this->viewmatrix.matrix[3][3] = 1.0f;

GRPMATRIX Translate;
Translate.BuildTranslationMatrix(-obj.vector[0],-obj.vector[1],-obj.vector[2]); 
this->viewmatrix.GetMulplicationMatrix(&this->viewmatrix,&Translate);

0 个答案:

没有答案