Note that there are some explanatory texts on larger screens.

plurals
  1. POOpenGL 3 (LWJGL) LookAt Matrix Confusion
    primarykey
    data
    text
    <p>I'm learning OpenGL 3 using LWJGL. I have tried to implement an equivalent to <code>gluLookAt()</code>, and although it works am I somewhat confused as to why.</p> <p>I confess to just copying this code from various sources on the web, but after much study I think understand the maths behind it, and that I understand what LWJGL is doing.</p> <p>However the 'correct' <code>gluLookAt</code> code behaved incorrectly in my application, as the camera seemed to be turning the wrong way. I only managed to get my code working by transposing the orthonormal vectors <code>forward</code>, <code>side</code>, and <code>up</code> (hope I'm using the correct terminology!), which I'm pretty sure is wrong...</p> <pre><code>private static final Vector3f forward = new Vector3f(); private static final Vector3f side = new Vector3f(); private static final Vector3f up = new Vector3f(); private static final Vector3f eye = new Vector3f(); public static Matrix4f lookAt(float eyeX, float eyeY, float eyeZ, float centerX, float centerY, float centerZ, float upX, float upY, float upZ) { forward.set(centerX - eyeX, centerY - eyeY, centerZ - eyeZ); forward.normalise(); up.set(upX, upY, upZ); Vector3f.cross(forward, up, side); side.normalise(); Vector3f.cross(side, forward, up); up.normalise(); Matrix4f matrix = new Matrix4f(); matrix.m00 = side.x; matrix.m01 = side.y; matrix.m02 = side.z; matrix.m10 = up.x; matrix.m11 = up.y; matrix.m12 = up.z; matrix.m20 = -forward.x; matrix.m21 = -forward.y; matrix.m22 = -forward.z; matrix.transpose(); // &lt;------ My dumb hack eye.set(-eyeX, -eyeY, -eyeZ); matrix.translate(eye); return matrix; } </code></pre> <p>I dont think I should be doing the transpose, but it doesn't work without it. I put <code>transpose()</code> because I couldn't be bothered to retype all the matrix cell positions btw!</p> <p>My understanding is that the form of the lookAt matrix should be as follows</p> <pre><code>[ side.x up.x fwd.x 0 ] [ 1 0 0 -eye.x ] [ side.y up.y fwd.y 0 ] [ 0 1 0 -eye.y ] [ side.z up.z fwd.z 0 ] [ 0 0 1 -eye.z ] [ 0 0 0 1 ] [ 0 0 0 1 ] </code></pre> <p>And I think that the LWJGL <code>Matrix4f</code> class represents matrix cells as <code>m&lt;col&gt;&lt;row&gt;</code>. The <code>translate(Vector3f)</code> method does the following</p> <pre><code>public static Matrix4f translate(Vector3f vec, Matrix4f src, Matrix4f dest) { ... dest.m30 += src.m00 * vec.x + src.m10 * vec.y + src.m20 * vec.z; dest.m31 += src.m01 * vec.x + src.m11 * vec.y + src.m21 * vec.z; dest.m32 += src.m02 * vec.x + src.m12 * vec.y + src.m22 * vec.z; dest.m33 += src.m03 * vec.x + src.m13 * vec.y + src.m23 * vec.z; ... } </code></pre> <p>So I am left hugely confused as to what part of this I have screwed up. Is it my understanding of the lookAt matrix, the column/row major-ness (is that a word?!) of <code>Matrix4f</code>, or something else? Is the rest of my code just broken? Is it actually correct and I'm just worrying too much? Am I just an idiot?</p> <p>Thanks.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload