bambooflow Note

カメラの設定

最終更新:

bambooflow

- view
メンバー限定 登録/ログイン

カメラの設定



カメラのデフォルトの状態


デフォルトでは、カメラは原点に置かれ、負のz軸方向を指し、上方向ベクトルは (0, 1, 0) になっている。
gluLookAt (0.0, 0.0, 0.0, 0.0, 0.0, -1.0, 0.0, 1.0, 0.0);


glutにてdrawstuffのようなカメラの動きを。


static float view_xyz[3] = {0,-10,3};
static float view_hpr[3] = {90,-10,0};
static int ms_mode = 0;
static int ms_pre_x = 0;
static int ms_pre_y = 0;
 
void setCamera( float x, float y, float z, float h, float p, float r)
{
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glRotatef(90, 0, 0, 1);
    glRotatef(90, 0, 1, 0);
    glRotatef(r, 1, 0, 0);
    glRotatef(p, 0, 1, 0);
    glRotatef(h, 0, 0, -1);
    glTranslatef( -x, -y, -z );
}
 
void mouse(int button, int state, int x, int y)
{
    switch (button) {
    case GLUT_LEFT_BUTTON:
        if (state == GLUT_DOWN) {
            ms_mode += 1; ms_pre_x = x; ms_pre_y = y;
        }
        else {
            ms_mode -= 1;
        }
        break;
    case GLUT_RIGHT_BUTTON:
        if (state == GLUT_DOWN) {
            ms_mode += 2; ms_pre_x = x; ms_pre_y = y;
        }
        else {
            ms_mode -= 2;
        }
        break;
    case GLUT_MIDDLE_BUTTON:
        if (state == GLUT_DOWN) {
            ms_mode += 4; ms_pre_x = x; ms_pre_y = y;
        }
        else {
            ms_mode -= 4;
        }
        break;
    default:
        break;
    }
}
 
void motion(int x, int y)
{
    int deltax = x - ms_pre_x;
    int deltay = y - ms_pre_y;
 
    float side = 0.01f * float(deltax);
    float fwd = (ms_mode==4) ? (0.01f*float(deltay)) : 0.0f;
    float s = (float) sin(view_hpr[0]*(M_PI/180.0));
    float c = (float) cos(view_hpr[0]*(M_PI/180.0));
 
    if (ms_mode == 1) {
        view_hpr[0] += float(deltax) * 0.5f;
        view_hpr[1] += float(deltay) * 0.5f;
    }   
    else {
        view_xyz[0] += -s*side + c*fwd;
        view_xyz[1] += c*side + s*fwd;
        if (ms_mode==2 || ms_mode==5) view_xyz[2] += 0.01f * float(deltay);
    }   
    for (int i=0; i<3; i++) {
        while (view_hpr[i]>180) view_hpr[i] -= 360;
        while (view_hpr[i]<-180) view_hpr[i] += 360;
    }   
    ms_pre_x = x;
    ms_pre_y = y;
}
 
void display(void)
{
    // ・・・
    setCamera( view_xyz[0], view_xyz[1], view_xyz[2], view_hpr[0], view_hpr[1], view_hpr[2] );
 
    // ・・・
}
 
int main(int argc, char *argv[])
{
    // ・・・
 
    glutDisplayFunc(display);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
 
    // ・・・
}
 

タグ:

OpenGL
記事メニュー
ウィキ募集バナー