2017-07-01 36 views
1

我正在尝试创建一个小型应用程序,它可以随着鼠标移动在一个立方体周围旋转相机。它可以完美地绕Y轴旋转,但是我在绕X轴旋转时遇到了问题。当我不断向上移动鼠标时,立方体开始在正X方向旋转。经过一段时间后,它反转它的旋转方向并开始在负X方向上旋转,然后一段时间后再次沿正X方向旋转。只要我向上移动鼠标,我想使它围绕正X旋转。这里有什么问题?相机旋转方向逆转一段时间后

立方体位于坐标系的中心。投影角度:

编辑:这是涉及到的问题视频:https://youtu.be/997ZdUM8fcQ

vec4 eye; 
vec4 at; 
vec4 up; 
GLfloat mouseX = -1; 
GLfloat mouseY = -1; 

// Bound to glutPassiveMotionFunc 
void mouse(int x, int y) { 
    // This rotation works perfect, cube rotates on same direction continuously as far as I move the mouse left or right 
    GLfloat acceleration_x = mouseX - x; 
    eye = RotateY(acceleration_x/10.f) * eye; 
    mouseX = x; 


    // This rotation doesn't work properly, after some time, cube's rotation direction inverses 
    GLfloat acceleration_y = mouseY - y; 
    eye = RotateX(acceleration_y/10.f) * eye; 
    mouseY = y; 


    // This part teleports pointer to opposite side of window after reaching window bounds 
    if (x >= 1364) { 
     glutWarpPointer(1, y); 
     mouseX = 1; 
    } 
    else if (x <= 0) { 
     glutWarpPointer(1363, y); 
     mouseX = 1363; 
    } 

    if (y >= 751) { 
     glutWarpPointer(x, 3); 
     mouseY = 3; 
    } 
    else if (y <= 2) { 
     glutWarpPointer(x, 750); 
     mouseY = 750; 
    } 
} 


void display(void) 
{ 
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 

    vec4 up(0.0, 1.0, 0.0, 0.0); 

    mat4 model_view = LookAt(eye, at, up); 
    glUniformMatrix4fv(ModelView, 1, GL_TRUE, model_view); 

    glDrawArrays(GL_TRIANGLES, 0, NumVertices); 
    glutSwapBuffers(); 
} 

// Bound to glutTimerFunc 
void timer(int id) 
{  
    glutPostRedisplay(); 
    glutTimerFunc(1000.f/60.f, timer, 0); 
} 

int main(int argc, char **argv) 
{ 
    ...... 
    ...... 

    eye = vec4(0, 0, 2, 0); 
    at = vec4(0, 0, 0, 0); 
    up = vec4(0.0, 1.0, 0.0, 0.0); 

    ..... 
    ..... 
} 

回答

1

这是因为相机被翻转。像这样使用你的手:让食指是dir,你的拇指是up(thumb =(0,1,0))。将您的索引器向前拨,然后向上拨拇指。现在,开始绕X旋转你的手:你的食指开始指向下,而你的拇指向前(在此期间,thumb.y为正)。当您将手旋转90度时,食指指向下方,拇指向前(thumb.y为0)。当你继续旋转时,你的拇指开始向前指向+(拇指变成负向)。

此时,LookAt函数将不会计算您想要的矩阵,因为您希望up矢量指向下(如我们所说,thumb.y为负数),但在代码中,up矢量是一个常数(0,1,0),所以LookAt将计算一个具有正数的矩阵。up.y相机翻转

一个解决方案可能是您注册所需的旋转角度,并旋转相机矩阵(而不是dir)与这些角度