The gluLookAt alternative does not work

I am trying to calculate the lookat matrix myself, instead of using gluLookAt (). My problem is that my matrix is ​​not working. using the same parameters on gluLookAt really works.

my way to create a lookat matrix:

Vector3 Eye, At, Up; //these should be parameters =) Vector3 zaxis = At - Eye; zaxis.Normalize(); Vector3 xaxis = Vector3::Cross(Up, zaxis); xaxis.Normalize(); Vector3 yaxis = Vector3::Cross(zaxis, xaxis); yaxis.Normalize(); float r[16] = { xaxis.x, yaxis.x, zaxis.x, 0, xaxis.y, yaxis.y, zaxis.y, 0, xaxis.z, yaxis.z, zaxis.z, 0, 0, 0, 0, 1, }; Matrix Rotation; memcpy(Rotation.values, r, sizeof(r)); float t[16] = { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, -Eye.x, -Eye.y, -Eye.z, 1, }; Matrix Translation; memcpy(Translation.values, t, sizeof(t)); View = Rotation * Translation; // i tried reversing this as well (translation*rotation) 

Now when I try to use this matrix by calling glMultMatrixf, nothing is displayed in my engine, when using the same image, eye, lookat and up on gluLookAt works fine, as I said earlier.

 glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glMultMatrixf(View); 

the problem should be somewhere in the code indicated here, I know that the problem is not in my Vector3 / Matrix classes, because they work fine when creating the projection matrix.

+4
source share
1 answer

I assume that you have the right coordinate system (the default is OpenGL). Try the following code. I think you forgot to normalize, and you should put "-zaxis" in the matrix.

 Vector3 Eye, At, Up; //these should be parameters =) Vector3 zaxis = At - Eye; zaxis.Normalize(); Up.Normalize(); Vector3 xaxis = Vector3::Cross(Up, zaxis); xaxis.Normalize(); Vector3 yaxis = Vector3::Cross(zaxis, xaxis); yaxis.Normalize(); float r[16] = { xaxis.x, yaxis.x, -zaxis.x, 0, xaxis.y, yaxis.y, -zaxis.y, 0, xaxis.z, yaxis.z, -zaxis.z, 0, 0, 0, 0, 1, }; 
+3
source

Source: https://habr.com/ru/post/1310403/


All Articles