OpenGL函数不输出任何错误但不起作用

时间:2017-11-24 20:57:47

标签: c opengl glut

我创建了这个代码来绘制一个对象,并根据键盘按钮在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;
}

1 个答案:

答案 0 :(得分:1)

问题在于那些

'key' = env('someRandom36CharsString'),

'key' = 'someRandom36CharsString', 和轴case '0': ... case '1': ... (ASCII 48)不一样,切换到

0

按下x,y,z ......