# View matrix in opengl

Sorry for my clumsy question. But I don't know where I am wrong at creating view matrix.

I have the following code:

 createMatrix(vec4f(xAxis.x, xAxis.y, xAxis.z, dot(xAxis,eye)),
vec4f(  yAxis.x_, yAxis.y_, yAxis.z_, dot(yAxis,eye)),
vec4f(-zAxis.x_, -zAxis.y_, -zAxis.z_, -dot(zAxis,eye)),
vec4f(0, 0, 0, 1)); //column1, column2,...


I have tried to transpose it, but with no success. I have also tried to use gluLookAt(...) with success. At the reference page, I watched the remarks about the to-be-created matrix, and it seems the same as mine. Where I am wrong?

In openGl matrices are transposed in memory. So transpose the matrix is OK. But your code doesn't look correct.

So you are in OpenGl. OpenGl uses right handed coordinate system. And for RH is lookat function defined like this:

zaxis = normal(cameraPosition - cameraTarget)
xaxis = normal(cross(cameraUpVector, zaxis))
yaxis = cross(zaxis, xaxis)

xaxis.x           yaxis.x           zaxis.x          0
xaxis.y           yaxis.y           zaxis.y          0
xaxis.z           yaxis.z           zaxis.z          0
-dot(xaxis, cameraPosition)  -dot(yaxis, cameraPosition)  -dot(zaxis, cameraPosition)  1


Especialy take care about zaxis = normal(cameraPosition - cameraTarget) because it is the only difference between RH and LH system.

Save it transposed and that's it

• Still does not work. I can move the camera pos, but the rotation does not work correctly.(it looks like the world object rotates around of the orig instead of the camera. – user5584 Feb 20 '11 at 23:52
• @user5584 Interesting. And with the same inputs gluLookAt works? And if you say rotate you mean rotate camera target or multiply view matrix with rotation matrix? Does your rotation work for gluLookAt? – Notabene Feb 20 '11 at 23:58
• Try to set cameraPosition.x, cameraPosition.y and cameraPosition.z instead of those tree 0 (right column. Or -cameraPosition. But it's only suggestion, it might not work. – zacharmarz Feb 21 '11 at 8:27
• Tried, but does not seem to work. – user5584 Feb 21 '11 at 8:51
• @user5584 please specify what do you mean by rotation. – Notabene Feb 21 '11 at 9:04

I don't know what is wrong with your code, but it looks different to mine. Here's what I've been using in my projects for the past few years.

    return new float4x4(
Xax.X, Yax.X, Zax.X, 0,
Xax.Y, Yax.Y, Zax.Y, 0,
Xax.Z, Yax.Z, Zax.Z, 0,
-float3.Dot(pos, Xax), -float3.Dot(pos, Yax), -float3.Dot(pos, Zax), 1
);


I hope this helps somewhat.