Results 1 to 4 of 4

Thread: Camera placement and movement

  1. #1

    Camera placement and movement

    What would be the easiest way to create a camera at a certain location and be able to move it around with the keyboard? To get an idea of what I'm trying to do you can imagine a canyon and I want a camera to be the viewer's eyes when your at the base bottom. Pressing forward and back keys will move you forward and left/right will turn rotate the camera.

  2. #2
    Senior Member Regular Contributor
    Join Date
    Nov 2002
    Location
    USA
    Posts
    254

    Re: Camera placement and movement

    Probably:

    static int xcam, ycam, zcam = 0.0;

    glRotated ( ( GLdouble ) xcam, 1.0, 0.0, 0.0 ); // For Rotating Camera on x-Axis
    glRotated ( ( GLdouble ) ycam, 0.0, 1.0, 0.0 ); // For Rotating Camera on y-Axis
    glRotated ( ( GLdouble ) zcam, 0.0, 0.0, 1.0 ); // For Rotating Camera on z-Axis

    then on keyboard function:

    Left-Key:
    ycam = ( ycam - 1 ) % 360;

    Right-Key:
    ycam = ( ycam + 1 ) % 360;

    Up-Key:
    xcam = ( xcam - 1 ) % 360;

    Down-Key:
    xcam = ( xcam + 1 ) % 360;

    I hope that helps.

  3. #3

    Re: Camera placement and movement

    whats problematic to me is, I'm using gluLookAt( ) because I can't get my camera to view without it. This is my current setup:

    void display ( void )
    {
    float x1, y1, z1, x2, y2, z2, x3, y3, z3,
    dx1, dy1, dz1, dx2, dy2, dz2;
    /* origin of camera */
    x1 = 5.0f; // 5.0; -5.0; -5.0;
    y1 = 5.0f; // 3.5; 3.5; 3.5;
    z1 = 4.0f; // -4.0; -4.0; 4.0;

    /* point looking at */
    x2 = 0.0f;
    y2 = 0.2f;
    z2 = 0.0f;

    /* camera rotation */
    x3 = 0.0f;
    y3 = 1.0f;
    z3 = 0.0f;

    dx1 = -1.5;
    dy1 = 0.5;
    dz1 = 3.8;

    dx2 = dx1;
    dy2 = dy1;
    dz2 = dz1 - 0.1;

    if( overview == ON ) {
    glMatrixMode ( GL_PROJECTION );
    glLoadIdentity ( );
    glFrustum( -0.33, 0.33, -0.33, 0.33, 0.5, 20.0 );
    //gluPerspective( 45.0f, (800/600), 0.1f, 100.0f );
    gluLookAt( x1, y1, z1, x2, y2, z2, x3, y3, z3);
    glRotatef( yAngle, 0.0, 1.0, 0.0 );
    glMatrixMode ( GL_MODELVIEW );
    glLoadIdentity( );
    }
    else {
    glMatrixMode ( GL_PROJECTION );
    glLoadIdentity( );
    //glOrtho( -0.33, 0.33, -0.33, 0.33, 0.1, 10.0 );
    gluPerspective( 45.0f, (800/600), 0.1f, 10.0f );
    gluLookAt( dx1, dy1, dz1, dx2, dy2, dz2, x3, y3, z3 );
    glRotatef( newY, 0.0, 1.0, 0.0 );
    glTranslatef( 0.0, 0.0, newZ);

    glMatrixMode ( GL_MODELVIEW );
    glLoadIdentity( );
    }
    .......
    }

    void SpecialKeys(int key, int x, int y)
    {
    switch (key) {
    case GLUT_KEY_LEFT:
    if( overview == ON ) {
    yAngle -= 1.0;
    glutPostRedisplay( );
    }
    else {
    newY -= 1;
    glutPostRedisplay( );
    }
    break;
    case GLUT_KEY_RIGHT:
    if( overview == ON ) {
    yAngle += 1.0;
    glutPostRedisplay();
    }
    else {
    newY += 1;
    glutPostRedisplay( );
    }
    break;
    case GLUT_KEY_UP:
    if( overview == OFF ) {
    newZ += 0.05;
    glutPostRedisplay( );
    }
    break;
    case GLUT_KEY_DOWN:
    if( overview == OFF ) {
    newZ -= 0.05;
    glutPostRedisplay( );
    }
    break;
    default:
    break;
    }
    }

  4. #4
    Senior Member Regular Contributor
    Join Date
    Nov 2002
    Location
    USA
    Posts
    254

    Re: Camera placement and movement

    You can goto Steve's website at: http://www.gldomain.com/programs/FirstPersonShooter.htm

    It comes with the source code.

Similar Threads

  1. Dynamic camera placement
    By SamMillner in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 03-05-2012, 03:43 PM
  2. Camera placement
    By Irena in forum OpenGL: General
    Replies: 4
    Last Post: 03-15-2010, 09:23 AM
  3. Free camera placement and coordinate transformatio
    By DrittesBinom in forum OpenGL: Basic Coding
    Replies: 15
    Last Post: 10-27-2009, 08:55 AM
  4. Camera movement?
    By McZ in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 04-04-2002, 02:09 AM
  5. Pixel placement and Bitmap placement with OpenGL
    By D_Lux in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 06-11-2001, 07:58 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean