Search code examples
openglglulookat

OpenGl GluLookAt. Cannot orient object


I have 2 objects in different coordinate systems: planet and cylinder. I have eye coordinates of both objects. I want to orient cylinder along vector that connects it and the planet.

orient cylinder along red vector

  glLoadMatrixf( PlanetTransform);
  // .. draw planet
  glLoadMatrixf( CylinderTransform);
  glColor3f(0, 1, 0);
  DrawCylinder();

  // draw vector that connects cylinder and planet
  glColor3f(1, 0, 0);
  glLoadIdentity();
  glBegin(GL_LINES);
     // planet eye pos e.g. (0.045; -0.049; -0.186)
     glVertex3f(point1.x, point1.y, point1.z);
     // cylinder eye pos e.g. (-0.109; -0.064; -0.203)
     glVertex3f(point2.x, point2.y, point2.z);
  glEnd();

  // orient cylinder along red vector
  // TVector normal(0, 0, 1);
  glLoadIdentity();
  gluLookAt(point2.x, point2.y, point2.z,
     point1.x, point1.y, point1.z,
     normal.x, normal.y, normal.z);

  DrawCylinder();

Draw cylinder code:

  void DrawCylinder()
  {
  glPushMatrix();
  glRotatef(90, 1, 0, 0);
  gluCylinder(gp_quadratic, 0.01, 0, 0.03, 15, 15);
  glPopMatrix();
  }

I tried also to specify normal as:

  TVector normal;
  TVector::cross(point2 - point1, TVector(0, 0, 1), normal);

So at the end I expect to see 2 cylinders - 1 green, 1 - red. Red should be oriented along red segment. But I do not see red one. Could you please help me to find what is wrong here? Red cylinder does not appear in center of green cylinder coordinate system..


Solution

  • the gluCylinder(/**/)

    1. is built along the local z-axis, thus pointing outwards along the red line
    2. the local y-axis is the global z_axis (pointing to the viewer), projected onto the viewing plane of the planet
    3. thus the local x-axis is poiting roughly up along the viewing plane of the planet
    4. thus after glRotatef(90, 1, 0, 0); the cylinder points away from the user into depth of the picture and is hidden behind the planet
    5. thus if instead glRotatef(180, 1, 0 ,0 ); is used, everything should be fine afais.

    I've quite a nightshift behind me, so my vision may be flawed, but it's cheap to try that, isn't it.

    If I'm in error, try using glPolygonMode(GL_FRONT_AND_BACK, GL_LINE) for the planet, and just look where the cylinder is.