4
\$\begingroup\$

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?

\$\endgroup\$
1

2 Answers 2

9
\$\begingroup\$

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

\$\endgroup\$
11
  • \$\begingroup\$ 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. \$\endgroup\$
    – user5584
    Feb 20, 2011 at 23:52
  • \$\begingroup\$ @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? \$\endgroup\$
    – Notabene
    Feb 20, 2011 at 23:58
  • \$\begingroup\$ 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. \$\endgroup\$
    – zacharmarz
    Feb 21, 2011 at 8:27
  • \$\begingroup\$ Tried, but does not seem to work. \$\endgroup\$
    – user5584
    Feb 21, 2011 at 8:51
  • \$\begingroup\$ @user5584 please specify what do you mean by rotation. \$\endgroup\$
    – Notabene
    Feb 21, 2011 at 9:04
2
\$\begingroup\$

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.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .