1

I have a cube in open GL. I have the cube rotating by multiplying by the rotation matrix. I have also got the camera rotating around the cube on the x axis using gluLookAt as you can see below.

    case 'j':
        eyeX = 10*cos(angle);
        eyeZ = 10*sin(angle);
        centerX = -cos(angle);
        centerZ = -sin(angle);
        angle -= 0.1;
        break;
    case'l':
        eyeX = 10*cos(angle);
        eyeZ = 10*sin(angle);
        centerX = -cos(angle);
        centerZ = -sin(angle);
        angle += 0.1;
        break;

where

gluLookAt( eyeX, eyeY, eyeZ, centerX, centerY, centerZ, 0.0, 1.0, 0.0);

What I am struggling with is getting the camera to rotate above and below the cube circling it on the y axis.

genpfault
  • 51,148
  • 11
  • 85
  • 139
Josh Horton
  • 77
  • 1
  • 1
  • 7

1 Answers1

0

To make the cam orbiting an object

  • subtract the object's world pos. from the cam world pos.,

    cam_pos_os = cam_pos_ws - object_pos_ws

  • rotate the vector pointing to cam_pos_os like you do it in world space in your code

  • add the object's world pos. again to the cam pos. in object space,

    cam_pos_ws = cam_pos_os + object_pos_ws

Colonel Thirty Two
  • 23,953
  • 8
  • 45
  • 85
decltype_auto
  • 1,706
  • 10
  • 19
  • Thank you. I completely understand what you mean Im just having trouble understanding in terms of openGL and C++. Do you recommend glTranslate glRotate and glTranslate again? How would I get the world position and the camera position? – Josh Horton Nov 19 '15 at 23:44
  • The cam pos in world coords is what you enter as eyeX,Y,Z in gluLookAt. You may rotate and translate by whatever means you want (correctness given); but I can recommend that you skip the deprecated "glTranform/Rotate/Scale" stuff and move to modern Shader/Buffer/Uniform based GL and use [glm](http://glm.g-truc.net/0.9.5/index.html) for the remaining CPU-side algebra. – decltype_auto Nov 20 '15 at 00:11