Edward Angel示例OpenGL代码:在联想X60上运行正常,但不能在我的桌面上渲染窗口(英特尔高清显卡)

时间:2013-08-20 08:45:04

标签: opengl rendering

我是OpenGL的新手,并尝试运行Edward Angel着名的书籍Interactive Computer Graphics的一些经典示例代码。但是,我遇到了一个让我感到很困惑的问题,但对很多人来说可能非常明显。示例代码为“Gasket.c”,可以从http://www.cs.unm.edu/~angel/BOOK/INTERACTIVE_COMPUTER_GRAPHICS/FIFTH_EDITION/PROGRAMS/CHAPTER02/

下载

我在联想X60(Windows 7)上使用VS2010 Express运行此代码,并成功绘制了Sierspinski Gasket。但是,当我尝试在具有相同软件系统的桌面(英特尔高清显卡)上运行相同的代码时,它没有绘制任何内容,只显示了一个空白(白色)窗口。文件编译/构建没有问题,没有错误/警告。

Gasket.c如下

/* Two-Dimensional Sierpinski Gasket          */
/* Generated Using Randomly Selected Vertices */
/* And Bisection                              */

#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif

void myinit()
{

/* attributes */

     glClearColor(1.0, 1.0, 1.0, 1.0); /* white background */
     glColor3f(1.0, 0.0, 0.0); /* draw in red */

/* set up viewing */
/* 500 x 500 window with origin lower left */

     glMatrixMode(GL_PROJECTION);
     glLoadIdentity();
     gluOrtho2D(0.0, 50.0, 0.0, 50.0);
     glMatrixMode(GL_MODELVIEW);
}

void display( void )
{
    GLfloat vertices[3][2]={{0.0,0.0},{25.0,50.0},{50.0,0.0}}; /* A triangle */

    int j, k;
    int rand();       /* standard random number generator */
    GLfloat p[2] ={7.5,5.0};  /* An arbitrary initial point inside traingle */

    glClear(GL_COLOR_BUFFER_BIT);  /*clear the window */


/* compute and plots 5000 new points */

        glBegin(GL_POINTS);

    for( k=0; k<5000; k++)
    {
         j=rand()%3; /* pick a vertex at random */


     /* Compute point halfway between selected vertex and old point */

         p[0] = (p[0]+vertices[j][0])/2.0; 
         p[1] = (p[1]+vertices[j][1])/2.0;

     /* plot new point */

        glVertex2fv(p); 

     }
     glEnd();
     glFlush(); /* clear buffers */
   }

void main(int argc, char** argv)
{

/* Standard GLUT initialization */

    glutInit(&argc,argv);
    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); /* default, not needed */
    glutInitWindowSize(500,500); /* 500 x 500 pixel window */
    glutInitWindowPosition(0,0); /* place window top left on display */
    glutCreateWindow("Sierpinski Gasket"); /* window title */
    glutDisplayFunc(display); /* display callback invoked when window opened */

    myinit(); /* set attributes */

    glutMainLoop(); /* enter event loop */
}

1 个答案:

答案 0 :(得分:1)

尝试从单缓冲(GLUT_SINGLE)切换到双缓冲(GLUT_DOUBLE):

#include <GL/glut.h>

void display( void )
{
    GLfloat vertices[3][2]={{0.0,0.0},{25.0,50.0},{50.0,0.0}}; /* A triangle */

    int j, k;
    int rand();       /* standard random number generator */
    GLfloat p[2] ={7.5,5.0};  /* An arbitrary initial point inside traingle */

    glClearColor(1.0, 1.0, 1.0, 1.0); /* white background */
    glClear(GL_COLOR_BUFFER_BIT);  /*clear the window */

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    gluOrtho2D(0.0, 50.0, 0.0, 50.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    /* compute and plots 5000 new points */
    glColor3f(1.0, 0.0, 0.0); /* draw in red */
    glBegin(GL_POINTS);
    for( k=0; k<5000; k++)
    {
        j=rand()%3; /* pick a vertex at random */

        /* Compute point halfway between selected vertex and old point */
        p[0] = (p[0]+vertices[j][0])/2.0; 
        p[1] = (p[1]+vertices[j][1])/2.0;

        /* plot new point */
        glVertex2fv(p); 

    }
    glEnd();

    glutSwapBuffers();
}

void main(int argc, char** argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB); /* default, not needed */
    glutInitWindowSize(500,500); /* 500 x 500 pixel window */
    glutInitWindowPosition(0,0); /* place window top left on display */
    glutCreateWindow("Sierpinski Gasket"); /* window title */
    glutDisplayFunc(display); /* display callback invoked when window opened */
    glutMainLoop(); /* enter event loop */
}