我创建了这段代码来绘制对象,然后根据键盘按钮将其沿x,y或z方向移动。

它工作完美,不会输出任何错误,但是对象只是出现而没有移动。

这是代码:

#include <GL/glut.h>

static GLint rotate = 0;
static GLint axis = 0;
static int xDegrees = 0;
static int yDegrees = 0;
static int zDegrees = 0;
static int direction = 0;
static int stop = 0;

void specialKeyboard( int axis, int x, int y )
{
    switch( axis )
    {
    case GLUT_KEY_RIGHT:
        direction = 1;
        break;

    case GLUT_KEY_LEFT:
        direction = -1;
        break;
    }
}

void change( int* degrees )
{
    *degrees = ( ( *degrees ) + direction ) % 360;
}

void rotate1()
{
    switch( axis )
    {
    case '0':
        change( &xDegrees );
        break;

    case '1':
        change( &yDegrees );
        break;

    case '2':
        change( &zDegrees );
        break;
    }

    glutPostRedisplay();
}

void keyboard( unsigned char key, int x, int y )
{
    switch( key )
    {
    case 'x':
        axis = 0;
        glutIdleFunc( rotate1 );
        stop = 0;
        break;

    case 'y':
        axis = 1;
        glutIdleFunc( rotate1 );
        stop = 0;
        break;

    case 'z':
        axis = 2;
        glutIdleFunc( rotate1 );
        stop = 0;
        break;

    case 's':
        stop = !stop;
        if( stop )
            glutIdleFunc( NULL );
        else
            glutIdleFunc( rotate1 );

        break;
    }
}

void resize( int w, int h )
{
    glViewport( 0, 0, w, h );

    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();
    gluPerspective( 50., w / (double)h, 1., 10. );

    glMatrixMode( GL_MODELVIEW );
}

void render()
{
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
    glLoadIdentity();
    gluLookAt( 0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0 );

    // Rotate the teapot
    glRotatef( xDegrees, 1, 0, 0 );
    glRotatef( yDegrees, 0, 1, 0 );
    glRotatef( zDegrees, 0, 0, 1 );

    glColor3f( 1.0, 1.0, 1.0 );
    glutWireTeapot( 1.5 );
    glutSwapBuffers();
}

int main( int argc, char* argv[] )
{
    glutInit( &argc, argv );
    glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
    glutInitWindowSize( 640, 480 );
    glutCreateWindow( "IVI - Sesion 2" );
    glEnable( GL_DEPTH_TEST );

    glutDisplayFunc( render );
    glutReshapeFunc( resize );
    glutKeyboardFunc( keyboard );
    glutSpecialFunc( specialKeyboard );

    glutMainLoop();

    return 0;
}

最佳答案

问题是因为那些

case '0':
  ...
case '1':
  ...


0和轴'0'(ASCII 48)不同,请切换到

case 0:
  ...
case 1:
  ...


它会按x,y,z ...

09-12 16:30