# LookAt matrix probem

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 = x.x;
mat = x.y;
mat = x.z;
mat = 0;
mat = y.x;
mat = y.y;
mat = y.z;
mat = 0;
mat = z.x;
mat = z.y;
mat = z.z;
mat = 0;
mat = -(x*eye);
mat = -(y*eye);
mat = -(z*eye);
mat = 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!

Hi,

I think that (changes):

vector4 z(target - eye);

mat = -eye;
mat = -eye;
mat = -eye;
mat = 1;

Ido

thank you, but with these changes I get all the diagonal elements = -1 (except the  one, of course)

Hi, I have implemented a vector math library already. You may want to take a look at it.

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 :-)!

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.)?

`````` 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, side, up;

120     GLfloat m;

121

122     forward = centerx - eyex;

123     forward = centery - eyey;

124     forward = centerz - eyez;

125

126     up = upx;

127     up = upy;

128     up = 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(&m);

140     m = side;

141     m = side;

142     m = side;

143

144     m = up;

145     m = up;

146     m = up;

147

148     m = -forward;

149     m = -forward;

150     m = -forward;

151

152     glMultMatrixf(&m);

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

154 }
``````

I hope it helps. If you need more glu source look here .