C OpenGL Quaternion for Camera翻转它

C OpenGL Quaternion for Camera翻转它,第1张

概述当我向右移动目标并向上看它直到它走到-zaxis的180度并决定走向另一个方向时,它会看到目标. Matrix4x4 camera::GetViewMat(){ Matrix4x4 oRotate, oView; oView.SetIdentity(); Vector3 lookAtDir = m_targetPosition - m_camPosition; 当我向右移动目标并向上看它直到它走到-zaxis的180度并决定走向另一个方向时,它会看到目标.

Matrix4x4 camera::GetVIEwMat(){    Matrix4x4 oRotate,oVIEw;    oVIEw.SetIDentity();    Vector3 lookAtDir = m_targetposition - m_camposition;    Vector3 lookAtHorizontal = Vector3(lookAtDir.GetX(),0.0f,lookAtDir.GetZ());    lookAtHorizontal.normalize();    float angle = acosf(Vector3(0.0f,-1.0f).Dot(lookAtHorizontal));    Quaternions horizontalOrIEnt(angle,Vector3(0.0f,1.0f,0.0f));    ori = horizontalOrIEnt;    ori.Conjugate();    oRotate = ori.ToMatrix();    Vector3 inverseTranslate = Vector3(-m_camposition.GetX(),-m_camposition.GetY(),-m_camposition.GetZ());    oRotate.transform(inverseTranslate);    oRotate.Set(0,3,inverseTranslate.GetX());    oRotate.Set(1,inverseTranslate.GetY());    oRotate.Set(2,inverseTranslate.GetZ());    oVIEw = oRotate;    return oVIEw;}
解决方法 正如所承诺的那样,一些代码显示了我让摄像机始终在空间中的特定点观察的方式.

首先,我们需要一种从角度和轴构造四元数的方法,我碰巧在pastebin上有这个,角度输入是弧度:

http://pastebin.com/vLcx4Qqh

确保你没有输入轴(0,0),这没有任何意义.

现在是实际的更新方法,我们需要让四元数从默认方向旋转相机指向目标点.请注意我刚刚写了这篇文章,它可能需要一些调试,可能需要一些优化,但这至少应该给你一个正确的方向.

voID camera::update(){    // First get the direction from the camera's position to the target point    vec3 lookAtDir = m_targetPoint - m_position;    // I'm going to divIDe the vector into two 'components',the Y axis rotation    // and the Up/Down rotation,like a regular camera would work.    // First to calculate the rotation around the Y axis,so we zero out the y    // component:    vec3 lookAtHorizontal = vec3(lookAtDir.x,lookAtDir.z).normalize();    // Get the quaternion from 'default' direction to the horizontal direction    // In this case,'default' direction is along the -z axis,like most OpenGL    // programs. Make sure the projection matrix works according to this.    float angle = acos(vec3(0.0f,-1.0f).dot(lookAtHorizontal));    quaternion horizontalOrIEnt(angle,vec3(0.0f,0.0f));    // Since we already stripped the Y component,we can simply get the up/down    // rotation from it as well.    angle = acos(lookAtDir.normalize().dot(lookAtHorizontal));    if(angle) horizontalOrIEnt *= quaternion(angle,lookAtDir.cross(lookAtHorizontal));    // ...    m_orIEntation = horizontalOrIEnt;}

现在实际上采用m_orIEntation和m_position来获取世界 – >相机矩阵

// First inverse each element (-position and inverse the quaternion),// the position is rotated since the position within a matrix is 'added' last// to the output vector,so it needs to account for rotation of the space.mat3 rotationMatrix = m_orIEntation.inverse().toMatrix();vec3 inverseTranslate = rotationMatrix * -m_position; // Note the minusmat4 matrix = mat3; // just means the matrix is expanded,the last entry (bottom right of the matrix) is a 1.0f like an IDentity matrix would be.// This bit is row-major in my case,you just need to set the translation of the matrix.matrix[3] = inverseTranslate.x;matrix[7] = inverseTranslate.y;matrix[11] = inverseTranslate.z;

编辑我认为它应该是显而易见的,但只是为了完整性,.dot()采用向量的点积,.cross()采用叉积,执行方法的对象是向量A,方法的参数是向量B.

总结

以上是内存溢出为你收集整理的C OpenGL Quaternion for Camera翻转它全部内容,希望文章能够帮你解决C OpenGL Quaternion for Camera翻转它所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1230370.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存