/* 构造旋转矩阵 */ void angleMatrix(Vector v, Matrix *m){ //定义中间变量 float cr,cp,cy,sr,sp,sy; float tempAngle; /* 计算roll,pitch,yaw角的cos,sin值,用来构造旋转矩阵 */ //yaw:表示围绕y轴的旋转,但数值是向量在XOZ面上的投影与z负的夹角 tempAngle = v.z; sy = sin(tempAngle); cy = cos(tempAngle); //pitch:表示围绕x轴的旋转,但数值是向量在YOZ面上的投影与z负的夹角 tempAngle = v.y; sp = sin(tempAngle); cp = cos(tempAngle); //roll:表示围绕y轴的旋转,但数值是向量在XOY面上的投影与y正的夹角 tempAngle = v.x; sr = sin(tempAngle); cr = cos(tempAngle); /* 构造旋转矩阵,tips:要按照列序构造才符合D-H坐标要求 */ /*Vector xBasis(cp*cy,sr*sp*cy+cr*-sy,(cr*sp*cy+-sr*-sy)); Vector yBasis(cp*sy,sr*sp*sy+cr*cy,(cr*sp*sy+-sr*cy)); Vector zBasis(-sp,sr*cp,cr*cp);*/ Vector xBasis(cp*cy,cp*sy,-sp); Vector yBasis(sr*sp*cy+cr*-sy,sr*sp*sy+cr*cy,sr*cp); Vector zBasis((cr*sp*cy+-sr*-sy),(cr*sp*sy+-sr*cy),cr*cp); m->xBasis = xBasis; m->yBasis = yBasis; m->zBasis = zBasis; /*m->origin = origin;*/ }
//------------------------------------------------------------------------------------------------ void ChsModelViewCamera::update(){ if( !this->isNeedUpdate ){ return; } this->isNeedUpdate = false; // Change the radius from the camera to the model based on wheel scrolling if( this->zoomDelta ){ this->radius -= this->zoomDelta * this->radius * 0.001f; } this->radius = this->maxRadius < this->radius ? this->maxRadius : this->radius; this->radius = this->minRadius > this->radius ? this->minRadius : this->radius; this->zoomDelta = 0; // Transform vectors based on camera's rotation matrix // Update the eye point based on a radius away from the lookAt position this->position = this->target - worldAhead * this->radius; // Update the view matrix this->mtxView.lookAt( this->position, this->target, worldUp ); ChsMatrix mInvView = this->mtxView; mInvView.inverse(); mInvView._m41 = mInvView._m42 = mInvView._m43 = 0.0f; ChsMatrix mModelLastRotInv = this->mtxModelLastRotate; mModelLastRotInv.inverse(); ChsMatrix mModelRot = worldArcBall.getRotationMatrix(); ChsMatrix temp = this->mtxView * mModelLastRotInv * mModelRot * mInvView; this->mxtModelRotate *= temp; this->mtxModelLastRotate = mModelRot; // Since we're accumulating delta rotations, we need to orthonormalize // the matrix to prevent eventual matrix skew ChsVector3 xBasis(this->mxtModelRotate._m11, this->mxtModelRotate._m12, this->mxtModelRotate._m13 ); ChsVector3 yBasis(this->mxtModelRotate._m21, this->mxtModelRotate._m22, this->mxtModelRotate._m23 ); ChsVector3 zBasis(this->mxtModelRotate._m31, this->mxtModelRotate._m32, this->mxtModelRotate._m33 ); xBasis.normalize(); yBasis = ChsVector3::cross(zBasis, xBasis); yBasis.normalize(); zBasis = ChsVector3::cross(xBasis, yBasis); this->mxtModelRotate._m11 = xBasis.x; this->mxtModelRotate._m12 = xBasis.y; this->mxtModelRotate._m13 = xBasis.z; this->mxtModelRotate._m21 = yBasis.x; this->mxtModelRotate._m22 = yBasis.y; this->mxtModelRotate._m23 = yBasis.z; this->mxtModelRotate._m31 = zBasis.x; this->mxtModelRotate._m32 = zBasis.y; this->mxtModelRotate._m33 = zBasis.z; // Translate the rotation matrix to the same position as the lookAt position this->mxtModelRotate._m41 = this->target.x; this->mxtModelRotate._m42 = this->target.y; this->mxtModelRotate._m43 = this->target.z; // Translate world matrix so its at the center of the model ChsMatrix mTrans; mTrans.translation( - this->modelCenter ); this->mtxWorld = this->mxtModelRotate * mTrans; this->mtxWVP = this->mtxWorld * this->mtxView * this->mtxProjection; }