# How do I create a view matrix directly from a quaternion and a position vector?

Given a quaternionic camera such that:

typedef struct
{
vector3 upReference;
vector3 rightReference;
vector3 forwardReference;

vector3 position;
quaternion orientation;

float pov;
} camera_t;


Where:

• 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?

• 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. Feb 1, 2015 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;

x = q.rotate (1,0,0)