Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 4 of 4

Thread: move camera in the direction its looking

  1. #1
    Junior Member Newbie
    Join Date
    Apr 2014
    Posts
    8

    Question move camera in the direction its looking

    I am rotating the camera around itself (when the camera is located at (0,0,0)) using the following:
    Code :
    glRotatef(x_camera_angle, 1.0, 0.0, 0.0);
    glRotatef(y_camera_angle, 0.0, 1.0, 0.0);

    I now want to move the camera in the direction its looking. For example, I want to move the camera 5 units right and 3 units forward. How can this be done?

    I've searched a lot and couldn't find a satisfying answer. Any help would be highly appreciated!

  2. #2
    Senior Member OpenGL Pro Ilian Dinev's Avatar
    Join Date
    Jan 2008
    Location
    Watford, UK
    Posts
    1,290
    Get the modelview matrix, see the numbers. 3 of the numbers (a column or a row) from the 3x3 sub-section (of the 4x4 matrix) are what you're looking for. Right now I don't remember which row/column.

  3. #3
    Junior Member Newbie
    Join Date
    Apr 2014
    Posts
    8
    I tried achieving it using the modelview matrix as shown here: http://stackoverflow.com/questions/1...tion-im-facing
    However, this just doesn't seem to work:
    Code :
    float mview[16];
    float front[4], up[4], left[4];
    glGetFloatv(GL_MODELVIEW_MATRIX, mview);
    left[0] = mview[0]; left[1] = mview[1]; left[2] = mview[2]; left[3] = 1.0;
    up[0] = mview[4]; up[1] = mview[5]; up[2] = mview[6]; up[3] = 1.0;
    front[0] = mview[8]; front[1] = mview[9]; front[2] = mview[10]; front[3] = 1.0;
     
    glTranslatef(front[0] * units_forward, front[1] * units_forward, front[2] * units_forward);
    Last edited by xorserq; 06-01-2014 at 03:15 AM.

  4. #4
    Junior Member Newbie
    Join Date
    Sep 2012
    Posts
    28
    Try with some trigonometry. If you know the angles you can simply use sin and cos to move camera.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •