基于四元数的旋转和枢轴位置
我无法弄清楚如何在使用Quaternion的同时考虑到OpenGL中的枢轴位置来执行矩阵旋转。目前我正在获取的是围绕某个点在空间中旋转对象,而不是我想要的局部枢轴。 这里是代码[使用Java]
四元数旋转方法:
  public void rotateTo3(float xr, float yr, float zr) {
    _rotation.x = xr;
    _rotation.y = yr;
    _rotation.z = zr;
    Quaternion xrotQ = Glm.angleAxis((xr), Vec3.X_AXIS);
    Quaternion yrotQ = Glm.angleAxis((yr), Vec3.Y_AXIS);
    Quaternion zrotQ = Glm.angleAxis((zr), Vec3.Z_AXIS);
    xrotQ = Glm.normalize(xrotQ);
    yrotQ = Glm.normalize(yrotQ);
    zrotQ = Glm.normalize(zrotQ);
    Quaternion acumQuat;
    acumQuat = Quaternion.mul(xrotQ, yrotQ);
    acumQuat = Quaternion.mul(acumQuat, zrotQ);
    Mat4 rotMat = Glm.matCast(acumQuat);
    _model = new Mat4(1);
   scaleTo(_scaleX, _scaleY, _scaleZ);
    _model = Glm.translate(_model, new Vec3(_pivot.x, _pivot.y, 0));
    _model =rotMat.mul(_model);//_model.mul(rotMat); //rotMat.mul(_model);
    _model = Glm.translate(_model, new Vec3(-_pivot.x, -_pivot.y, 0));
   translateTo(_x, _y, _z);
    notifyTranformChange();
   }
模型矩阵缩放方法:public void scaleTo(float x,float y,float z){
    _model.set(0, x);
    _model.set(5, y);
    _model.set(10, z);
    _scaleX = x;
    _scaleY = y;
    _scaleZ = z;
    notifyTranformChange();
  }
翻译方法:public void translateTo(float x,float y,float z){
    _x = x - _pivot.x;
    _y = y - _pivot.y;
    _z = z;
    _position.x = _x;
    _position.y = _y;
    _position.z = _z;
    _model.set(12, _x);
    _model.set(13, _y);
    _model.set(14, _z);
    notifyTranformChange();
   }
但是这种我不使用Quaternion的方法工作正常:
  public void rotate(Vec3 axis, float angleDegr) {
    _rotation.add(axis.scale(angleDegr));
    //  change to GLM:
    Mat4 backTr = new Mat4(1.0f);
    backTr = Glm.translate(backTr, new Vec3(_pivot.x, _pivot.y, 0));
    backTr = Glm.rotate(backTr, angleDegr, axis);
    backTr = Glm.translate(backTr, new Vec3(-_pivot.x, -_pivot.y, 0));
    _model =_model.mul(backTr);///backTr.mul(_model);
    notifyTranformChange();
   }
在我看来,你已经考虑到轮换之前和之后的来回翻译。 为什么translateTo的最终调用?
此外,当你旋转时,一个纯粹的旋转总是围绕着原点。 所以如果你想围绕你的枢轴点旋转。 我会将您的枢轴点转换为原点,然后旋转,然后转回到枢轴点是正确的。 因此,我希望你的代码看起来像这样:
_model = Glm.translate(_model, new Vec3(-_pivot.x, -_pivot.y, 0));
_model =rotMat.mul(_model);//_model.mul(rotMat); //rotMat.mul(_model);
_model = Glm.translate(_model, new Vec3(_pivot.x, _pivot.y, 0));
  并且没有调用translateTo(_x, _y, _z);  。  另外,你能否确认旋转部分已经做到了它应该做的?  你可以通过比较rotMat和Glm.rotate(new Mat4(1.0f), angleDegr, axis) 。  他们应该是相同的轮换相同。 
四元数只描述一个旋转。 因此,你想如何绕一个枢轴点旋转一些东西只有一个四元数?
你需要的最小值是一个R3矢量和一个四元数。 只有一个转换级别,你首先将物体转过来然后移动到那里。
如果你想创建一个矩阵,你首先创建日粮矩阵,然后添加未经修改的翻译。
如果您只想调用glTranslate和glRotate(或glMultMatrix),则首先调用glTranslate然后glRoatate。
编辑:
如果你没有渲染,只想知道每个顶点在哪里:
Vector3 newVertex = quat.transform(oldVertex) + translation;
