PDA

View Full Version : LookAt matrix probem

cignox1
09-15-2007, 03:35 AM
Hi all, I don't know if this is the right forum where to post my question. I'm not even using openGL, but I'm writing a matrix class that follows the same layout and conventions than openGL, so I hope to receive halp here.

I'm writing a LookAt method with the following code:

void matrix::LookAt(const vector4 &eye, const vector4 &target, const vector4 &up)
{
if(eye == target)
{
Identity();
return; //eye and target are the same
}

vector4 z(eye - target);
z.Normalize();

vector4 u(up);
if(IsZero(z.x) && !IsZero(z.y) && IsZero(z.z)) u = vector4(0, 0, 1.0);

vector4 x(u^z);
x.Normalize();

vector4 y(z^x);

mat[0][0] = x.x;
mat[0][1] = x.y;
mat[0][2] = x.z;
mat[0][3] = 0;
mat[1][0] = y.x;
mat[1][1] = y.y;
mat[1][2] = y.z;
mat[1][3] = 0;
mat[2][0] = z.x;
mat[2][1] = z.y;
mat[2][2] = z.z;
mat[2][3] = 0;
mat[3][0] = -(x*eye);
mat[3][1] = -(y*eye);
mat[3][2] = -(z*eye);
mat[3][3] = 1;
}

I searched all over the web for the formula. I used the one specified in the DX reference for right handed matrix, as well as many other places. This one just flips my y axis.
With the following parameters I expect to ge the identity matrix:
eye = (0,0,0)
target = (0,0,-1)
up = (0,1,0).

But the element in (1,1) is -1 instead than 1.
Where am I wrong?
Thank you!

Ido_Ilan
09-15-2007, 10:21 AM
Hi,

I think that (changes):

vector4 z(target - eye);

mat[3][0] = -eye;
mat[3][1] = -eye;
mat[3][2] = -eye;
mat[3][3] = 1;

Ido

cignox1
09-15-2007, 10:33 AM
thank you, but with these changes I get all the diagonal elements = -1 (except the [4][4] one, of course)

Trenki
09-15-2007, 12:13 PM
Hi, I have implemented a vector math library already. You may want to take a look at it.

[ www.trenki.net (http://www.trenki.net) | vector_math (3d math library) (http://www.trenki.net/content/view/16/36/) | software renderer (http://www.trenki.net/content/view/18/38/) ]

cignox1
09-15-2007, 03:39 PM
Thank you, tomorrow I will give it a look. By the way, I'm from South Tyrol too, Bolzano, to be more precise.
Grazie ancora (oder danke schön :-)!

cignox1
09-16-2007, 02:49 AM
I gave a look to the library written by Trenki. It will be of inspiration for me (expecially for some more operations over vectors and matrices that I may add to my own library) but it did not help me with my specific problem: as I said, I've tried many different versions, but even while I can set it up to retutn the identity matrix in the previous example, I still don't know if this would be a correct matrix or not.
Is there someone willing to check my code? Perhaps I made something wrong in other parts (cross mult i.e.)?

satan
09-17-2007, 05:08 PM
gluLookAt(GLdouble eyex, GLdouble eyey, GLdouble eyez, GLdouble centerx,

116 GLdouble centery, GLdouble centerz, GLdouble upx, GLdouble upy,

117 GLdouble upz)

118 {

119 float forward[3], side[3], up[3];

120 GLfloat m[4][4];

121

122 forward[0] = centerx - eyex;

123 forward[1] = centery - eyey;

124 forward[2] = centerz - eyez;

125

126 up[0] = upx;

127 up[1] = upy;

128 up[2] = upz;

129

130 normalize(forward);

131

132 /* Side = forward x up */

133 cross(forward, up, side);

134 normalize(side);

135

136 /* Recompute up as: up = side x forward */

137 cross(side, forward, up);

138

139 __gluMakeIdentityf(&amp;m[0][0]);

140 m[0][0] = side[0];

141 m[1][0] = side[1];

142 m[2][0] = side[2];

143

144 m[0][1] = up[0];

145 m[1][1] = up[1];

146 m[2][1] = up[2];

147

148 m[0][2] = -forward[0];

149 m[1][2] = -forward[1];

150 m[2][2] = -forward[2];

151

152 glMultMatrixf(&amp;m[0][0]);

153 glTranslated(-eyex, -eyey, -eyez);

154 } I hope it helps. If you need more glu source look here (http://gitweb.freedesktop.org/?p=mesa/mesa.git;a=tree;h=be7c94bafda75e491877e19f8ce783bb d7ad3e37;hb=c29886718ac8389b526b3eb5947c286b7fe00a 88;f=src/glu/sgi) .