Results 1 to 7 of 7

Thread: LookAt matrix probem

  1. #1
    Junior Member Newbie
    Join Date
    May 2006
    Posts
    17

    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[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!

  2. #2
    Member Contributor
    Join Date
    Feb 2005
    Posts
    90

    Re: LookAt matrix probem

    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

  3. #3
    Junior Member Newbie
    Join Date
    May 2006
    Posts
    17

    Re: LookAt matrix probem

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

  4. #4
    Senior Member Regular Contributor
    Join Date
    Feb 2005
    Location
    South Tyrol, Italy
    Posts
    107

    Re: LookAt matrix probem

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

    [ www.trenki.net | vector_math (3d math library) | software renderer ]

  5. #5
    Junior Member Newbie
    Join Date
    May 2006
    Posts
    17

    Re: LookAt matrix probem

    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 !

  6. #6
    Junior Member Newbie
    Join Date
    May 2006
    Posts
    17

    Re: LookAt matrix probem

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

  7. #7
    Senior Member Regular Contributor
    Join Date
    Feb 2002
    Posts
    374

    Re: LookAt matrix probem

    Code :
     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(&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(&m[0][0]);
     
     153     glTranslated(-eyex, -eyey, -eyez);
     
     154 }
    I hope it helps. If you need more glu source look here .

Similar Threads

  1. Need help with my lookAt function
    By Rockinredross867 in forum OpenGL: Basic Coding
    Replies: 6
    Last Post: 12-18-2015, 09:09 AM
  2. Is my lookAt function right?
    By Rockinredross867 in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 12-10-2015, 06:16 PM
  3. lookat
    By yuzi in forum COLLADA
    Replies: 1
    Last Post: 09-27-2010, 03:07 PM
  4. Replies: 3
    Last Post: 06-05-2007, 03:49 AM
  5. Spherical Lookat()
    By JoeyBlunt in forum OpenGL: Advanced Coding
    Replies: 3
    Last Post: 10-31-2000, 09:32 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean