gluLookAt and CoreMotion

Member
Posts: 65
Joined: 2009.03
Post: #1
Hi guys

I've been interested in having a play with CoreMotion and using it to look around inside a 3D scene. I've used gluPerspective to set up my projection and I'm rendering a number of coloured cubes so that I can see how I can move around the scene using gluLookAt.

What I'm getting stuck on is how to best use the information I'm getting from CoreMotion to change what I'm seeing in my scene. The effect I'm after is that the iPhone screen is like a window into my 3D scene, and as I move the phone around, the scene changes to match i.e. I can look to my left and see the cubes to my left, or above.

My matrix math is rusty and I'm really not sure how to make use of Quaternions, so I'm hoping someone more experienced in this area could give me a nudge in the right direction.

At the moment I'm using the rotationMatrix that CMAttitude provides and multiplying that with the ModelView matrix. The scene is moving, but the movement seems reversed to what I would expect. I started with this approach and once working was planning to move onto using gluLookAt, well, that was the plan anyway.....

What I'd really like to do us use gluLookAt with an eye location of say 0, 0, 0 and for the location I'm looking at to be adjusted by the movement of the device.

My sample project is built on top of the OpenGL ES template in Xcode and my current render method using the matrix approach looks like this:
Code:
- (void)render
{
    if (!referenceAttitude) {
        referenceAttitude = [motionManager.deviceMotion.attitude retain];
    }
    
    glBindFramebufferOES(GL_FRAMEBUFFER_OES, defaultFramebuffer);
    
    glClearColor(0.7, 0.7, 0.7, 1.0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
    glLoadIdentity();

    // Grab the current attitude from core motion
    CMAttitude *attitude = motionManager.deviceMotion.attitude;
    
    // Get the difference between the reference attitude and the current device position
    [attitude multiplyByInverseOfAttitude:referenceAttitude];

    // Create an array to hold the rotation matrix so it can be used in glMultMatrix
    GLfloat m[] = {attitude.rotationMatrix.m11, attitude.rotationMatrix.m12, attitude.rotationMatrix.m13, 0,
                   attitude.rotationMatrix.m21, attitude.rotationMatrix.m22, attitude.rotationMatrix.m23, 0,
                   attitude.rotationMatrix.m31, attitude.rotationMatrix.m32, attitude.rotationMatrix.m33, 0,
                   0, 0, 0, 1};
    
    glMultMatrixf(m);

    // Render the cubes
    int n = 6;
    for(int x=-n; x<=n; x+=2)
    {
        for(int y=-n; y<=n; y+=2)
        {
            for(int z=-n; z<n; z+=2)
            {
                if(x!=0 || y!=0 || z!=0)
                {
                    glTranslatef(x, y, z);
                    [cube render];
                    glTranslatef(-x, -y, -z);
                }
            }
        }
    }

    glBindRenderbufferOES(GL_RENDERBUFFER_OES, colorRenderbuffer);
    [context presentRenderbuffer:GL_RENDERBUFFER_OES];
}

Any help, suggestions or nudges in the right direction would be really appreciated Smile

Mike

iPhone Game Development Blog - 71Squared
Quote this message in a reply
Post Reply