如何在OpenGL中正确地移动(移入和移出对象)

时间:2014-11-29 02:59:14

标签: c++ opengl

我一直在搜索许多论坛(此处和其他地方),以了解如何在OpenGL中正确放大对象。在阅读了很多内容之后,我终于想出了如何使用GluLookAt()。我可以让相机像我想的那样移动而没有任何问题。我知道它的工作原理是因为物体在相机移动时会消失。但是,我想在平移相机时放大物体。我认为相机位置会照顾它,但更新相机位置似乎没有帮助。我也尝试了gScalef(),但是当我按下' w'键。

void keyboard(unsigned char c, int x,int y)
{
  if(c==27)
    {exit(0);}
  if (c=='w')
    {
      glMatrixMode(GL_MODELVIEW);
      glLoadIdentity();

      float forward_x, forward_y,forward_z;
      gluLookAt(current[0], current[1], current[2],
        current[0]+center_x, current[1]+center_y, current[2]+center_z,
        0.0, 1.0, 0.0);  
      // glScalef(1.2,1.2,1.2);
      current[0]= (current[0]- 0.1*center_x);
      current[1]= (current[1]-0.1*center_y);
      current[2]= (current[2]- 0.1*center_z);

    }

}

void Camera_rotation(int x, int y)
{
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  center_x = cos(-0.05*x)*sin(-.05*y);
  center_y = cos(-.05*y);
  center_z = sin(0.05*x)*sin(.05*y);
  eye_x = x; eye_y = 1.0,
  float new_x = x; 
  gluLookAt(current[0], current[1], current[2],
            current[0]+center_x,
            current[1]+center_y,
            current[2]+center_z,
            0.0, 1.0, 0.0);
  render();
}

'电流'和' center_x [yz]'是全局变量。有人可以帮忙吗?

2 个答案:

答案 0 :(得分:3)

只要相机瞄准对象,您就可以在FOV上设置Perspective Projection Matrix(视野)以获得缩放效果。 FOV越小,观看区域越小,这就产生了变焦的幻觉。

可以找到有关该主题的精彩教程here。它最后讨论了缩放。

答案 1 :(得分:0)

讨论后的可能解决方案:

在render()函数(或显示函数)中,提及投影矩阵。这将设置相机的行为方式。代码:

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(30.0,1.0,-1.0,1.0);// FOV = 30 degrees, aspect ratio is one and I can see throughout the plane