【问题标题】:OpenGl GluLookAt. Cannot orient objectOpenGL GluLookAt。无法定位对象
【发布时间】:2014-06-28 00:16:46
【问题描述】:

我在不同的坐标系中有 2 个对象:行星和圆柱体。我有两个物体的眼睛坐标。我想沿着连接它和行星的向量来定位圆柱体。

  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();

绘制圆柱代码:

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

我也尝试将 normal 指定为:

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

所以最后我希望看到 2 个圆柱体 - 1 个绿色,1 个 - 红色。红色应沿红色段定向。但我没有看到红色的。你能帮我找出这里有什么问题吗?红色圆柱没有出现在绿色圆柱坐标系的中心..

【问题讨论】:

  • 请给我你的point1和point2。并告诉我们你是如何绘制圆柱体的。
  • 根据您的评论编辑问题。谢谢

标签: opengl glulookat


【解决方案1】:

gluCylinder(/**/)

  1. 沿局部 z 轴构建,因此沿红线指向外部
  2. 局部 y 轴是全局 z 轴(指向观察者),投影到行星的观察平面上
  3. 因此,局部 x 轴沿着行星的观察平面大致向上
  4. 因此在 glRotatef(90, 1, 0, 0);圆柱体远离用户指向图片的深处,并隐藏在行星后面
  5. 因此如果改为 glRotatef(180, 1, 0 ,0 );使用,一切都应该没问题。

我身后有一个夜班,所以我的视力可能有缺陷,但试一试很便宜,不是吗。

如果我错了,请尝试对行星使用 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE),然后查看圆柱体的位置。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多