Setting up OpenGL camera with off-center perspective

Posted by user5484 on Game Development See other posts from Game Development or by user5484
Published on 2011-02-16T02:53:41Z Indexed on 2011/02/16 7:34 UTC
Read the original article Hit count: 560

Filed under:
|

Hi,

I'm using OpenGL ES (in iOS) and am struggling with setting up a viewport with an off-center distance point.

Consider a game where you have a character in the left hand side of the screen, and some controls alpha'd over the left-hand side. The "main" part of the screen is on the right, but you still want to show whats in the view on the left. However when the character moves "forward" you want the character to appear to be going "straight", or "up" on the device, and not heading on an angle to the point that is geographically at the mid-x position in the screen.

Here's the jist of how i set my viewport up where it is centered in the middle:

// setup the camera
    //

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    const GLfloat zNear = 0.1;
    const GLfloat zFar = 1000.0;
    const GLfloat fieldOfView = 90.0; // can definitely adjust this to see more/less of the scene

    GLfloat size = zNear * tanf(DEGREES_TO_RADIANS(fieldOfView) / 2.0);
    CGRect rect;
    rect.origin = CGPointMake(0.0, 0.0);
    rect.size = CGSizeMake(backingWidth, backingHeight);
    glFrustumf(-size, size, -size / (rect.size.width / rect.size.height), size / (rect.size.width / rect.size.height), zNear, zFar);

    glMatrixMode(GL_MODELVIEW);


    // rotate the whole scene by the tilt to face down on the dude
    const float tilt = 0.3f;
    const float yscale = 0.8f;
    const float zscale = -4.0f;
    glTranslatef(0.0, yscale, zscale);
    const int rotationMinDegree = 0;
    const int rotationMaxDegree = 180;

        glRotatef(tilt * (rotationMaxDegree - rotationMinDegree) / 2, 1.0f, 0.0f, 0.0f);

    glTranslatef(0, -yscale, -zscale);

    static float b = -25; //0;
    static float c = 0;


    // rotate by to face in the direction of the dude
    float a = RADIANS_TO_DEGREES(-atan2f(-gCamera.orientation.x, -gCamera.orientation.z));
    glRotatef(a, 0.0, 1.0, 0.0);


    // and move to where it is
    glTranslatef(-gCamera.pos.x, -gCamera.pos.y, -gCamera.pos.z);

    // draw the rest of the scene
    ...

I've tried a variety of things to make it appear as though "the dude" is off to the right: - do a translate after the frustrum to the x direction - do a rotation after the frustrum about the up/y-axis - move the camera with a biased lean to the left of the dude

Nothing i do seems to produce good results, the dude will either look like he's stuck on an angle, or the whole scene will appear tilted. I'm no OpenGL expert, so i'm hoping someone can suggest some ideas or tricks on how to "off-center" these model views in OpenGL. Thanks!

© Game Development or respective owner

Related posts about opengl

Related posts about opengl-es