0

私は openGL でロボットを構築しています。ロボットは移動および回転する必要があります。押すとロボットは前方に移動し、t を押すとローカル軸を中心に 15* 回転し、f を押すと再び歩きます。私はやった、ロボットは歩いて回転するが、問題は彼がローカル軸に対して回転していないことであり、彼は(0,0,0)に従っている。希望する効果を得るために、移動と回転の構成をどのように作成する必要があるかを理解していないと思います。

私は今、スケーリングされた球だけで試しています。ここに表示関数を追加して、皆さんにとってより明確になるようにします。

void display() 
{ 
    glEnable(GL_DEPTH_TEST);    // need depth test to correctly draw 3D objects 
    glClearColor(0,0,0,1); 
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); 
    glShadeModel(GL_SMOOTH);

    //All color and material stuffs go here
    glEnable(GL_LIGHTING); 
    glEnable(GL_LIGHT0); 
    glEnable(GL_NORMALIZE);   // normalize normals 

    glEnable(GL_COLOR_MATERIAL); 
    glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE); 

    // set up the parameters for lighting 
    GLfloat light_ambient[] = {0,0,0,1}; 
    GLfloat light_diffuse[] = {.6,.6,.6,1};
    GLfloat light_specular[] = {1,1,1,1}; 
    GLfloat light_pos[] = {10,10,10,1}; 

    glLightfv(GL_LIGHT0,GL_AMBIENT, light_ambient); 
    glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse); 
    glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular); 


    GLfloat mat_specular[] = {.9, .9, .9,1}; 
    GLfloat mat_shine[] = {10}; 
    glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular); 
    glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mat_shine); 
    //color specs ends ////////////////////////////////////////

    //glPolygonMode(GL_FRONT_AND_BACK,GL_LINE); // comment this line to enable polygon shades

    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 
    gluPerspective(90, 1, 1, 100); 

    glMatrixMode(GL_MODELVIEW); 
    glLoadIdentity(); 

    glLightfv(GL_LIGHT0, GL_POSITION, light_pos); 

    gluLookAt(0,0,30,0,0,0,0,1,0); 

    glRotatef(x_angle, 0, 1,0); // this is just for mouse handling
    glRotatef(y_angle, 1,0,0); // this is just for mouse handling
    glScalef(scale_size, scale_size, scale_size); // for zooming effect

    draw_coordinate();

    //Drawing using VBO starts here
    glTranslatef(walk*sin(M_PI*turn/180),0,walk*cos(M_PI*turn/180));
    glRotatef(turn,0,1,0);
    draw_sphere(3,1,1);

    glDisableClientState(GL_VERTEX_ARRAY); // enable the vertex array on the client side
    glDisableClientState(GL_NORMAL_ARRAY); // enable the normal array on the client side    


    glutSwapBuffers(); 
} 
4

2 に答える 2

0

ロボットがそれ自体の軸で回転しない場合は、ロボットを中心に移動し、回転させてから、元の位置に戻します。平行移動、回転、拡大縮小、描画を内部に保持します

glPushMatrix();
........your rotation,translation,scalling,drawing goes here..........
glPopMatrix();

これらはシーンを同じに保ちます。これらの機能がわからない場合は、こちらをご覧ください。

于 2012-10-01T01:41:25.467 に答える
0

openglのrotate関数は、(0,0,0)を中心に回転する関数です。回転ポイントを中心に移動してから回転する必要があります。

...
glTranslatef(walk*sin(M_PI*turn/180),0,walk*cos(M_PI*turn/180));
glTranslatef(-x_rot,-y_rot,-z_rot);
glRotatef(turn,0,1,0);
glTranslatef(x_rot,y_rot,z_rot);
...

だからあなたの場合x_rot=walk*sin(M_PI*turn/180)y_rot=0そしてz_rot=walk*cos(M_PI*turn/180)。上記は次のようになります。

...
glRotatef(turn,0,1,0);
glTranslatef(x_rot=walk*sin(M_PI*turn/180),0,walk*cos(M_PI*turn/180));
...
于 2012-09-30T20:23:52.267 に答える