Given a quaternionic camera such that:

typedef struct
    vector3 upReference;
    vector3 rightReference;
    vector3 forwardReference;

    vector3 position;
    quaternion orientation;

    float pov;
} camera_t;


  • upReference, rightReference, forwardReference are always (0,1,0, 1,0,0, 0,0,-1) respectively
  • pov is the viewing angle

and quaternion is:

typedef struct 
    float x, y, z, w;
} quaternion;

Is there some way to create a view matrix directly from all of this and skip gluLookAt?

  • \$\begingroup\$ Since I would first have to mentally parse it in order to write a complete answer, I'll just note that Wikpedia has section for this entitled Quaternion-derived Rotation Matrix, which you could use to create the rotation component of a view matrix. The translation would have to applied separately, because the quaternions rotate vectors around an assumed origin. \$\endgroup\$ – Seth Battin Feb 1 '15 at 6:10

Try this below:

XMMATRIX is transpose of the transform matrix, so you read it as column,row

XMMATRIX viewMatrix = XMMatrixRotationQuaternion(v_quaterion);
XMFLOAT4X4 f_view;
XMStoreFloat4x4(&f_view, viewMatrix);
f_view._41 = mPosition.x * f_view._11 + mPosition.y * f_view._21 + mPosition.x * f_view._31;
f_view._42 = mPosition.x * f_view._12 + mPosition.y * f_view._22 + mPosition.x * f_view._32;
f_view._43 = mPosition.x * f_view._13 + mPosition.y * f_view._23 + mPosition.x * f_view._33;
viewMatrix = XMLoadFloat4x4(&f_view);

You need to multiply the translation vector by the rotation first


Another way to do it, if you already have the quaternion-rotation of a vector:

x = q.rotate (1,0,0)
y = q.rotate (0,1,0)
z = crossprod(x,y)
matrix = { x,y,z,p };

It's definitely not the fastest way to do it. But it's one that is easy to understand.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.