First I have to apologize that I've made a mistake. The correct computation for column-major indices is
index := col * 4 + row
so that e.g. col == 1 skips the first 4 elements (i.e. column vector 0) and addresses m1[4] up to m1[7] (i.e. the 2nd column) in dependence on row.
The second problem is with the order of inverseMat4 and setting the position vector. The position is an inherent part of the transformation. As such the camera matrix is a composition of
M := T * R
and its inverse is
M-1 = ( T * R )-1 = R-1 * T-1
Your current computation gives
T * R-1
what is obviously different in 2 ways: The translation is not inverted, and the translation is applies on the wrong side.
So please invoke inverseMat4 as last step after setting all 4 columns and just before returning the view matrix result.
Ah ok, makes sense.
I've made those changes and I can see my object again. It's rotating weirdly though as I look around the screen and if I translate the view changes and then after a second or so it disappears. Also my object is a terrain, but it looks like a flat plane currently which is oriented about the up axis, not on the XZ plane like before.
I'll post my changes but I'm sure they adhere to your critiques.
Matrix4x4 createView( Vector3* pos, Vector3* target, Vector3* up )
{
Matrix4x4 cameraMat;
Matrix4x4 view;
Vector3 rowZ;
Vector3 row4 = { pos->x, pos->y, pos->z };
Vector3 rowX;
Vector3 rowY;
setEqual(&rowY, up);
rowZ = sub(target, pos);
normalise(&rowZ);
rowX = crossProd(&rowY, &rowZ);
normalise(&rowX);
rowY = crossProd(&rowX, &rowZ); // new rowY
normalise(&rowY);
setColv(&cameraMat, 0, &rowX, 0.0f);
setColv(&cameraMat, 1, &rowY, 0.0f);
setColv(&cameraMat, 2, &rowZ, 0.0f);
setColv(&cameraMat, 3, &row4, 1.0f);
view = inverseMat4(&cameraMat); // has to be inverse for camera transformations
return view;
}I'm doing translations in my update loop before I call updateCam(&cam);
They are just simple changes to the camera position to moving in 6 directions, here's an example:
if (glfwGetKey('A'))
{
addVec3f(&cam.pos, 0.5f, 0.0f, 0.0f); cam.needUpdate = 1;
}
if (glfwGetKey('W'))
{
addVec3f(&cam.pos, 0.0f, 0.0f, 0.5f); cam.needUpdate = 1;
}
void addVec3f( Vector3* vec, float x, float y, float z )
{
vec->x += x;
vec->y += y;
vec->z += z;
}