OpenGL的 - 移动摄像头,鼠标 [英] OpenGL - moving camera with mouse

查看:410
本文介绍了OpenGL的 - 移动摄像头,鼠标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道究竟该如何解释这一点,但希望你能明白我的意思。

基本上,我画一个立方体,并希望拖动鼠标以查看所有边。 旋转只有水平或垂直方向是好的,但如果我尝试将这些再结合事情一点都不奇怪。

例如。如果我旋转180度,垂直(获得倒挂),然后拖动鼠标水平则立方体旋转的相反方向的鼠标移动。

这是有关code:

 双camera_angle_h = 0;
双camera_angle_v = 0;

INT drag_x_origin;
INT drag_y_origin;
INT拖动= 0;

无效显示器(){
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    gluLookAt(0.0,0.0,25.0,0.0,0.0,0.0,0.0,1.0,0.0);
    glRotated(camera_angle_v,1.0,0.0,0.0);
    glRotated(camera_angle_h,0.0,1.0,0.0);

    draw_cube();

    glFlush();
    glutSwapBuffers();
}

无效mouse_click(INT按钮,INT状态,诠释的x,int y)对{
    如果(键== GLUT_LEFT_BUTTON){
        如果(国家== GLUT_DOWN){
            拖= 1;
            drag_x_origin = X;
            drag_y_origin = Y;
        }
        其他
            拖动= 0;
    }
}

无效MOUSE_MOVE(INT X,int y)对{
    如果(拖){
        camera_angle_v + =(Y  -  drag_y_origin)* 0.3;
        camera_angle_h + =(X  -  drag_x_origin)* 0.3;
        drag_x_origin = X;
        drag_y_origin = Y;
    }
}
 

解决方案

您正在使用欧拉角等有万向节锁定的问题。

有一个简单的解决方案是将旋转模型矩阵内,该旋转矩阵,而不是只存储2角(通过使用矩阵乘法)的。

。看看这个答案的详细资料: <一href="http://stackoverflow.com/questions/1225377/will-this-cause-gimbal-lock">http://stackoverflow.com/questions/1225377/will-this-cause-gimbal-lock

一个更复杂的解决方案是将四元数

I'm not sure exactly how to explain this but hopefully you'll understand what I mean.

Basically I've drawn a cube and want to drag the mouse around to view all the sides. Rotating only horizontally or vertically is fine, but if I try to combine these then things go a little weird.

e.g. if I rotate 180 degrees vertically (to get 'upside down') and then drag the mouse horizontally then the cube spins in the opposite direction to the mouse motion.

This is the relevant code:

double camera_angle_h = 0;
double camera_angle_v = 0;

int drag_x_origin;
int drag_y_origin;
int dragging = 0;

void display() {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    gluLookAt(0.0, 0.0, 25.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
    glRotated(camera_angle_v, 1.0, 0.0, 0.0);
    glRotated(camera_angle_h, 0.0, 1.0, 0.0);

    draw_cube();

    glFlush();
    glutSwapBuffers();
}

void mouse_click(int button, int state, int x, int y) {
    if(button == GLUT_LEFT_BUTTON) {
        if(state == GLUT_DOWN) {
            dragging = 1;
            drag_x_origin = x;
            drag_y_origin = y;
        }
        else
            dragging = 0;
    }
}

void mouse_move(int x, int y) {
    if(dragging) {
        camera_angle_v += (y - drag_y_origin)*0.3;
        camera_angle_h += (x - drag_x_origin)*0.3;
        drag_x_origin = x;
        drag_y_origin = y;
    }
}

解决方案

You are using euler angles and so have the problem of gimbal lock.

A simple solution is to store rotation inside your model-matrix and rotate this matrix instead of storing only 2 angles (by using matrix multiplication).

Look into this answer for more information: http://stackoverflow.com/questions/1225377/will-this-cause-gimbal-lock

a more complex solution is to store quaternions

这篇关于OpenGL的 - 移动摄像头,鼠标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆