gluLookAt - Wrong Matrix

Started by
3 comments, last by Sneftel 15 years, 8 months ago
anyone know why you would get the wrong matrix for gluLookAt ? I really can't figure this one out. If you look at the jpg pasted below, the position is 0.0, 0.0, -10.0 instead of (-10.0, 0.0, 0.0) like it is suppose to be. Thanks http://stringa.runeagin.com/wrong.jpg
Advertisement
Post the definition of your camera_transform structure.
camera_transform is just a float[16] .

I am using NVIDIA's math library and it's just a mat4.

[source:lang=c++]struct DECLSPEC_NV_MATH mat4{    mat4();    mat4(const nv_scalar * array);    mat4(const mat4 & M);    mat4( const nv_scalar& f0,  const nv_scalar& f1,  const nv_scalar& f2,  const nv_scalar& f3,  		  const nv_scalar& f4,  const nv_scalar& f5,  const nv_scalar& f6,  const nv_scalar& f7,  		  const nv_scalar& f8,  const nv_scalar& f9,  const nv_scalar& f10, const nv_scalar& f11,  		  const nv_scalar& f12, const nv_scalar& f13, const nv_scalar& f14, const nv_scalar& f15 )  		  : a00( f0 ), a10( f1 ), a20( f2 ), a30( f3 ),  		    a01( f4 ), a11( f5 ), a21( f6 ), a31( f7 ),  		    a02( f8 ), a12( f9 ), a22( f10), a32( f11),			a03( f12), a13( f13), a23( f14), a33( f15) { }     const vec4 col(const int i) const    {        return vec4(&mat_array);<br>    }<br>    <br>    <span class="cpp-keyword">const</span> vec4 <span class="cpp-keyword">operator</span>[](<span class="cpp-keyword">const</span> <span class="cpp-keyword">int</span>&amp; i) <span class="cpp-keyword">const</span><br>    {<br>        <span class="cpp-keyword">return</span> vec4(mat_array<span style="font-weight:bold;">, mat_array, mat_array, mat_array);<br>    }<br>   <br>    <span class="cpp-keyword">const</span> nv_scalar&amp; <span class="cpp-keyword">operator</span>()(<span class="cpp-keyword">const</span> <span class="cpp-keyword">int</span>&amp; i, <span class="cpp-keyword">const</span> <span class="cpp-keyword">int</span>&amp; j) <span class="cpp-keyword">const</span><br>    {<br>        <span class="cpp-keyword">return</span> mat_array[ j * <span class="cpp-number">4</span> + i ];<br>    }<br><br>    nv_scalar&amp; <span class="cpp-keyword">operator</span>()(<span class="cpp-keyword">const</span> <span class="cpp-keyword">int</span>&amp; i, <span class="cpp-keyword">const</span> <span class="cpp-keyword">int</span>&amp; j)<br>    {<br>        <span class="cpp-keyword">return</span>  mat_array[ j * <span class="cpp-number">4</span> + i ];<br>    }<br><br>    <span class="cpp-keyword">void</span> set_col(<span class="cpp-keyword">int</span> i, <span class="cpp-keyword">const</span> vec4 &amp; v)<br>    {<br>        mat_array = v.x;<br>        mat_array = v.y;<br>        mat_array = v.z;<br>        mat_array = v.w;<br>    }<br><br>    <span class="cpp-keyword">void</span> set_row(<span class="cpp-keyword">int</span> i, <span class="cpp-keyword">const</span> vec4 &amp; v)<br>    {<br>        mat_array<span style="font-weight:bold;"> = v.x;<br>        mat_array = v.y;<br>        mat_array = v.z;<br>        mat_array = v.w;<br>    }<br><br>    mat3 &amp; get_rot(mat3 &amp; M) <span class="cpp-keyword">const</span>;<br>    quat &amp; get_rot(quat &amp; q) <span class="cpp-keyword">const</span>;<br>    <span class="cpp-keyword">void</span> set_rot(<span class="cpp-keyword">const</span> quat &amp; q);<br>    <span class="cpp-keyword">void</span> set_rot(<span class="cpp-keyword">const</span> mat3 &amp; M);<br>    <span class="cpp-keyword">void</span> set_rot(<span class="cpp-keyword">const</span> nv_scalar &amp; theta, <span class="cpp-keyword">const</span> vec3 &amp; v);<br>    <span class="cpp-keyword">void</span> set_rot(<span class="cpp-keyword">const</span> vec3 &amp; u, <span class="cpp-keyword">const</span> vec3 &amp; v);<br><br>	<span class="cpp-keyword">void</span> set_scale(<span class="cpp-keyword">const</span> vec3&amp; s);<br>	vec3&amp; get_scale(vec3&amp; s) <span class="cpp-keyword">const</span>;<br>    <span class="cpp-keyword">void</span> set_translation(<span class="cpp-keyword">const</span> vec3 &amp; t);<br>    vec3 &amp; get_translation(vec3 &amp; t) <span class="cpp-keyword">const</span>;<br><br>	mat4 <span class="cpp-keyword">operator</span>*(<span class="cpp-keyword">const</span> mat4&amp;) <span class="cpp-keyword">const</span>;<br><br>    <span class="cpp-keyword">union</span> {<br>        <span class="cpp-keyword">struct</span> {<br>            nv_scalar a00, a10, a20, a30;   <span class="cpp-comment">// standard names for components</span><br>            nv_scalar a01, a11, a21, a31;   <span class="cpp-comment">// standard names for components</span><br>            nv_scalar a02, a12, a22, a32;   <span class="cpp-comment">// standard names for components</span><br>            nv_scalar a03, a13, a23, a33;   <span class="cpp-comment">// standard names for components</span><br>        };<br>        <span class="cpp-keyword">struct</span> {<br>            nv_scalar _11, _12, _13, _14;   <span class="cpp-comment">// standard names for components</span><br>            nv_scalar _21, _22, _23, _24;   <span class="cpp-comment">// standard names for components</span><br>            nv_scalar _31, _32, _33, _34;   <span class="cpp-comment">// standard names for components</span><br>            nv_scalar _41, _42, _43, _44;   <span class="cpp-comment">// standard names for components</span><br>        };<br>        <span class="cpp-keyword">union</span> {<br>            <span class="cpp-keyword">struct</span> {<br>                nv_scalar b00, b10, b20, p; <span class="cpp-comment">// standard names for components</span><br>                nv_scalar b01, b11, b21, q; <span class="cpp-comment">// standard names for components</span><br>                nv_scalar b02, b12, b22, r; <span class="cpp-comment">// standard names for components</span><br>                nv_scalar x, y, z, w;       <span class="cpp-comment">// standard names for components</span><br>            };<br>        };<br>        nv_scalar mat_array[<span class="cpp-number">16</span>];     <span class="cpp-comment">// array access</span><br>    };<br>};<br><br><br></pre></div><!–ENDSCRIPT–> 
With the look eye position, target position, and up vector you define a co-ordinate frame that is rotate by -90° around y, so that a rotation matrix R
[ 0  0 -1  0 ][ 0  1  0  0 ][ 1  0  0  0 ][ 0  0  0  1 ]
is the result. You furthur define the eye point as origin, so a translation T
[ 1  0  0 -10 ][ 0  1  0  0  ][ 0  0  1  0  ][ 0  0  0  1  ]
comes to life. The product of both is multiplied with the identity matrix
I * R * T
so that the matrix
[ 0  0 -1  0  ][ 0  1  0  0  ][ 1  0  0 -10 ][ 0  0  0  1  ]
is the end result.

So the result seems me absolutely correct.
It's not the wrong matrix. Thought of in post-multiplicative terms, it consists of a ninety degree rotation about the Y axis, followed by a ten unit translation along the negative Z axis. Thought of in pre-multiplicative terms, it consists of a ten unit translation along the negative X axis, followed by a ninety degree rotation about the Y axis, which is probably how you're thinking about it. Remember, view transformations like this invert the normal order of transformation. Think in terms of transforming the world, instead of the camera, and you'll see what I mean.

This topic is closed to new replies.

Advertisement