alexpov
alexpov

Reputation: 1188

Iphone OpenGl ES 1.1: How to modify the camera center from (0,0,0) to (x,y,z)?

For example, center of my object is (1,2,-1). How can i modify the center of opengl es "camera" (center of the coordinates system) from (0,0,0) to (1,2,-1)

If iam not mistaken, gluLookAt is the one to do this but it is not included in openGl es 1.1. Is there any other function that does the same

Upvotes: 3

Views: 347

Answers (1)

Tommy
Tommy

Reputation: 100652

gluLookAt simply multiplies the current matrix with a well-defined matrix created from the inputs. Its man page contains the formulas you need.

So, for example:

void mygluLookAt(GLfloat eyeX, GLfloat eyeY, GLfloat eyeZ, GLfloat centreX, GLfloat centreY, GLfloat centreZ, GLfloat upX, GLfloat upY, GLfloat upZ)
{
    GLfloat f[3] =
    {
        centreX - eyeX,
        centreY - eyeY,
        centreZ - eyeZ,
    };

    // make f and the up vector have unit length
    GLfloat lengthOfF = sqrtf(f[0]*f[0] + f[1]*f[1] + f[2]*f[2]);
    f[0] /= lengthOfF;
    f[1] /= lengthOfF;
    f[2] /= lengthOfF;

    GLfloat lengthOfUp = sqrtf(upX*upX + upY*upY + upZ*upZ);
    upX /= lengthOfUp;
    upY /= lengthOfUp;
    upZ /= lengthOfUp;

    // use the cross product of f and Up to get s,
    // and the cross product of s and f to get u
    GLfloat s[3] =
    {
        f[1]*upZ - upY*f[2],
        f[2]*upX - upZ*f[0],
        f[0]*upY - upX*f[1]
    };

    GLfloat u[3] =
    {
        s[1]*f[2] - f[1]*s[2],
        s[2]*f[0] - f[2]*s[0],
        s[0]*f[1] - f[0]*s[1]
    };

    // Fill the matrix as prescribed.
    // Note: OpenGL is "column major"
    GLfloat M[16] =
    {
          s[0], u[0], -f[0], 0.0f,
          s[1], u[1], -f[1], 0.0f,
          s[2], u[2], -f[2], 0.0f,
          0.0f, 0.0f, 0.0f,  1.0f
    };

    glMultMatrixf(M);
    glTranslatef(-eyeX, -eyeY, -eyeZ);
}

That's typed directly in here, from the man page. So it's untested but it should be easy to spot if I've made any errors.

Upvotes: 2

Related Questions