【问题标题】:Bus error when using GLUT使用 GLUT 时出现总线错误
【发布时间】:2011-03-23 16:27:45
【问题描述】:

我正在尝试创建一个 GLUT 应用程序。目前,我有这个代码:

void init ( GLvoid )     // Create Some Everyday Functions
{

    glShadeModel(GL_SMOOTH);                            // Enable Smooth Shading
    glClearColor(0.0f, 0.0f, 0.0f, 0.5f);               // Black Background
    glClearDepth(1.0f);                                 // Depth Buffer Setup
    glEnable(GL_DEPTH_TEST);                            // Enables Depth Testing
    glDepthFunc(GL_LEQUAL);                             // The Type Of Depth Testing To Do
    glEnable ( GL_COLOR_MATERIAL );
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
}

void display ( void )   // Create The Display Function
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
    glLoadIdentity();                                   // Reset The Current Modelview Matrix
    glTranslatef(-1.5f,0.0f,-6.0f);                     // Move Left 1.5 Units And Into The Screen 6.0
    glBegin(GL_TRIANGLES);                              // Drawing Using Triangles
    glVertex3f( 0.0f, 1.0f, 0.0f);                  // Top
    glVertex3f(-1.0f,-1.0f, 0.0f);                  // Bottom Left
    glVertex3f( 1.0f,-1.0f, 0.0f);                  // Bottom Right
    glEnd();                                            // Finished Drawing The Triangle
    glTranslatef(3.0f,0.0f,0.0f);                       // Move Right 3 Units
    glBegin(GL_QUADS);                                  // Draw A Quad
    glVertex3f(-1.0f, 1.0f, 0.0f);                  // Top Left
    glVertex3f( 1.0f, 1.0f, 0.0f);                  // Top Right
    glVertex3f( 1.0f,-1.0f, 0.0f);                  // Bottom Right
    glVertex3f(-1.0f,-1.0f, 0.0f);                  // Bottom Left
    glEnd();


    glutSwapBuffers ( );
    // Swap The Buffers To Not Be Left With A Clear Screen
}

void reshape ( int w, int h )   // Create The Reshape Function (the viewport)
{
    glViewport     ( 0, 0, w, h );
    glMatrixMode   ( GL_PROJECTION );  // Select The Projection Matrix
    glLoadIdentity ( );                // Reset The Projection Matrix
    if ( h==0 )  // Calculate The Aspect Ratio Of The Window
        gluPerspective ( 80, ( float ) w, 1.0, 5000.0 );
    else
        gluPerspective ( 80, ( float ) w / ( float ) h, 1.0, 5000.0 );
    glMatrixMode   ( GL_MODELVIEW );  // Select The Model View Matrix
    glLoadIdentity ( );    // Reset The Model View Matrix
}

void keyboard ( unsigned char key, int x, int y )  // Create Keyboard Function
{
    switch ( key ) {
        case 27:        // When Escape Is Pressed...
            exit ( 0 );   // Exit The Program
            break;        // Ready For Next Case
        default:        // Now Wrap It Up
            break;
    }
}

void arrow_keys ( int a_keys, int x, int y )  // Create Special Function (required for arrow keys)
{
    switch ( a_keys ) {
        case GLUT_KEY_UP:     // When Up Arrow Is Pressed...
            glutFullScreen ( ); // Go Into Full Screen Mode
            break;
        case GLUT_KEY_DOWN:               // When Down Arrow Is Pressed...
            glutReshapeWindow ( 500, 500 ); // Go Into A 500 By 500 Window
            break;
        default:
            break;
    }
}

int main(int argc, char *argv[]) {
    glutInit(&argc, argv);
    init();
    glutInitDisplayMode ( GLUT_RGB | GLUT_DOUBLE ); // Display Mode
    glutInitWindowSize  ( 500, 500 ); // If glutFullScreen wasn't called this is the window size
    glutCreateWindow    ( "NeHe's OpenGL Framework" ); // Window Title (argv[0] for current directory as title)
    glutFullScreen      ( );          // Put Into Full Screen
    glutDisplayFunc     ( display );  // Matching Earlier Functions To Their Counterparts
    glutReshapeFunc     ( reshape );
    glutKeyboardFunc    ( keyboard );
    glutSpecialFunc     ( arrow_keys );
    glutMainLoop        ( );          // Initialize The Main Loop
    return 0;
}

我的终端中唯一的结果是Bus error。我在 Mac OS X 10.5 下使用 Apple 的 GCC 进行编译,链接到 OpenGL 和 GLUT 框架。

谁能帮帮我?

【问题讨论】:

    标签: macos opengl bus-error


    【解决方案1】:

    我认为你初始化得太早了

    int main(int argc, char *argv[]) {
      glutInit(&argc, argv);
      glutInitDisplayMode ( GLUT_RGB | GLUT_DOUBLE ); // Display Mode
      glutInitWindowSize  ( 500, 500 ); // If glutFullScreen wasn't called this is the window size
      glutCreateWindow    ( "NeHe's OpenGL Framework" ); // Window Title (argv[0] for current directory as title)
      init();
      glutFullScreen      ( );          // Put Into Full Screen
      glutDisplayFunc     ( display );  // Matching Earlier Functions To Their Counterparts
      glutReshapeFunc     ( reshape );
      glutKeyboardFunc    ( keyboard );
      glutSpecialFunc     ( arrow_keys );
      glutMainLoop        ( );          // Initialize The Main Loop
      return 0;
    }
    

    为我工作。

    【讨论】:

      猜你喜欢
      • 2011-05-24
      • 2014-05-20
      • 1970-01-01
      • 1970-01-01
      • 2015-10-30
      • 1970-01-01
      • 2021-04-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多