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

标签 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;
}

最佳答案

问题是因为这些

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

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

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

按 x、y、z ... 即可工作

关于c - OpenGL函数不输出任何错误但不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47479921/

相关文章:

c++ - 是否有一个 C 预处理器可以根据已定义/未定义的值消除#ifdef block ?

c - 具有给定长度和宽度的给定角度的椭圆的半径

c++ - 如何让 Cmake 包含 gl/GL.h?

c++ - 围绕不断旋转的物体旋转物体

c++ - 具有虚拟文件系统的 c 预处理器

c++ - pthread_mutex_lock 当它是同一个线程时如何不锁定

c++ - OpenGL - 旋转问题 - Qt

c++ - 使用 SOIL 在 openGL 中显示图像

改变茶壶的颜色有时不起作用

c++ - 立方体的一半的 OpenGL 颜色