2

I just got a new PC and now have to get my demo program running with SDL2/OpenGL. I used the program to try out various techniques and used gluLookAt (which, obviously, shouldn't be used any more or even never should have been used).

Now I'm looking for a way to replace the gluLookAt method by building a transformation matrix doing the same as gluLookAt did. I came across this claiming to be usable replacement for gluLookAt (the answer - not the question).

My implementation of it looks like this (I assume j ^ k means the cross product of j and k - correct me if I'm wrong):

//Compat method: gluLookAt deprecated
void util_compat_gluLookAt(GLfloat eyeX, GLfloat eyeY, GLfloat eyeZ, GLfloat lookAtX, GLfloat lookAtY, GLfloat lookAtZ, GLfloat upX, GLfloat upY, GLfloat upZ) {
   Vector3f up (upX, upY, upZ);
   Vector3f lookAt (-lookAtX, -lookAtY, -lookAtZ);
   Vector3f eye (eyeX, eyeY, eyeZ);
   Vector3f i = up ^ lookAt;
   Matrix4x4 mat (new GLfloat[16]
                     {i.getX(), upX,    lookAt.getX(),  0,
                     i.getY(),  upY,    lookAt.getY(),  0,
                     i.getZ(),  upZ,    lookAt.getZ(),  0,
                     0,     0,      0,          1});
   Vector3f translate = mat * (Vector3f()-eye); // Not yet correctly implemented: Negative of vector eye ([0,0,0]-[eyeX,eyeY,eyeZ])
   mat.setItem(3,0,translate.getX());
   mat.setItem(3,1,translate.getX());
   mat.setItem(3,2,translate.getX());
   glMultMatrixf(mat.transpose().getComponents());
}

Who did the wrong stuff - my source of information or me? And how can I fix this? NOTE: I won't use this in my actual project, but I still need it for my demo program which will also be reviewed for grading.

s3lph
  • 4,575
  • 4
  • 21
  • 38
  • "or even never should have been used"... why not? glu had a ton of useful functions. –  Jun 30 '14 at 21:26
  • I especially meant gluLookAt. It's useful - yes, but it conveys the idea of a "camera" which does not exist in OpenGL. Because of this it's harder now to understand how to use transformation matrics properly. (At least for me) – s3lph Jul 01 '14 at 06:03
  • of course the camera exists, it is at (0,0,0) by default and looks down the positive z axis. try drawing without setting up any other view-projection matrices (or set them all to identity) and you will see what i mean (this is called homogenous clipspace btw...). –  Jul 02 '14 at 16:21

1 Answers1

3

I just had the idea to implement the gluLookAt compatibility method like gluLookAt is implemented in an older version of Mesa3D: (glu.c of Mesa3D 3.2)

Now it looks as below and works:

//Compat method: gluLookAt deprecated
void util_compat_gluLookAt(GLfloat eyeX, GLfloat eyeY, GLfloat eyeZ, GLfloat lookAtX, GLfloat lookAtY, GLfloat lookAtZ, GLfloat upX, GLfloat upY, GLfloat upZ) {
    Vector3f x, y, z;
    z = Vector3f(eyeX-lookAtX, eyeY-lookAtY, eyeZ-lookAtZ).normalize();
    y = Vector3f(upX, upY, upZ);
    x = y ^ z;
    y = z ^ x;
    x = x.normalize();
    y = y.normalize();
    // mat is given transposed so OpenGL can handle it.
    Matrix4x4 mat (new GLfloat[16]
                     {x.getX(), y.getX(),   z.getX(),   0,
                     x.getY(),  y.getY(),   z.getY(),   0,
                     x.getZ(),  y.getZ(),   z.getZ(),   0,
                     -eyeX,     -eyeY,      -eyeZ,      1});
    glMultMatrixf(mat.getComponents());
}

I know this is a way OpenGL is not used today, but somehow all tutorials seem to use gluLookAt and therefore convey the existence of a camera in OpenGL. I still have to get rid of this point of view about OpenGL myself...

s3lph
  • 4,575
  • 4
  • 21
  • 38
  • By "all the tutorials" I assume you mean NeHe? That site is very outdated and pretty much useless, you should avoid it. I've found http://www.arcsynthesis.org/gltut/ to be much more helpful. – Dan Jun 30 '14 at 20:48
  • You can get rid of the glTranslate by including the -eyeX, -eyeY, -eyeZ in the bottom row of your matrix. – Aumnayan Jul 01 '14 at 20:15
  • Makes sense... Thanks for this side note... I'll add this. – s3lph Jul 02 '14 at 20:20