3

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

genpfault
  • 51,148
  • 11
  • 85
  • 139
alexpov
  • 1,158
  • 2
  • 16
  • 29
  • Do you want to just shift the origin for drawing one object or permanently for all objects, so that you don'y have to shift again and again for other objects? – tipycalFlow Sep 15 '11 at 07:47
  • @tipycalFlow yes, to shift for all objects (i have only one). The reason i need this is to be able to rotate the object on 3 axes – alexpov Sep 15 '11 at 07:52
  • hmm... see this http://stackoverflow.com/questions/1130520/how-to-change-the-center-of-rotation-in-opengl – tipycalFlow Sep 15 '11 at 08:52

1 Answers1

2

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.

Tommy
  • 99,986
  • 12
  • 185
  • 204