Compatibility function for gluLookAt

跟風遠走 提交于 2021-02-11 07:53:08

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/24492274/compatibility-function-for-glulookat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!