【问题标题】:Moving the camera to the left and right based on mouse movement根据鼠标移动左右移动相机
【发布时间】:2020-11-27 04:44:55
【问题描述】:

我知道以前有人问过这个问题,但我还没有找到适合我的答案。 基本上,我希望相机根据鼠标光标位置左右移动。鼠标越向左,相机越向左转动。所以应该可以转身并朝相反的方向移动。我该怎么做?

这是我的相机位置:

GLfloat cameraPosition[] = { 0.0, 0.0, 3.5 };
GLfloat lx = 0.0; GLfloat ly = 0.0;

这是我的投影矩阵:

    // set to projection mode 
    glMatrixMode(GL_PROJECTION);

    // clear any previous transformations
    glLoadIdentity();

    // set the perspective 
    gluPerspective(45, (float)windowWidth / (float)windowHeight, 0.1, 20);

在 myDisplay 函数中,这是我设置相机位置的方式:

    glMatrixMode(GL_MODELVIEW);

    glLoadIdentity();

    // set the camera position
    gluLookAt(cameraPosition[0], cameraPosition[1], cameraPosition[2],
        lx, ly, cameraPosition[2] - 100,
        0, 1, 0);

glutPassiveMotionFunc 函数应该做什么?

【问题讨论】:

    标签: c opengl glut


    【解决方案1】:

    很可能你需要做这样的事情

    glRotatef(-yAngle, 0.0f, 1.0, 0.0f);
    glRotatef(-xAngle, 1.0f, 0.0f, 0.0f);
    glTranslatef(cameraPosition[0], cameraPosition[1], cameraPosition[2])
    

    而不是 gluLookAt()。试试看,也许它会解决你的问题。

    【讨论】:

      猜你喜欢
      • 2023-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-11
      • 1970-01-01
      • 1970-01-01
      • 2017-07-02
      • 1970-01-01
      相关资源
      最近更新 更多