一个 OpenGL 问题:为什么 glutSwapBuffer() 函数不起作用?



这段代码来自红皮书,示例2-15(嗯,代码不完全是书中的代码)。注意我注意到的笔记。

#include <fstream>
#include <stdlib.h>
#include <GL/glew.h>
#include <GL/glut.h>
#pragma comment(lib,"glew32.lib")
using namespace std;
#define BUFFER_OFFSET(offset) ((GLubyte *)NULL+offset)
#define XStart              -0.8
#define XEnd                0.8
#define YStart              -0.8
#define YEnd                0.8
#define NumXPoints          11
#define NumYPoints          11
#define NumPoints           (NumXPoints * NumYPoints)
#define NumPointsPerStrip   (2*NumXPoints)
#define NumStrips           (NumYPoints-1)
#define RestartIndex        0xffff
void display(void)
{
    int i,start;
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glColor3f(1,1,1);
    glDrawElements(GL_TRIANGLE_STRIP,NumStrips*(NumPointsPerStrip+1),GL_UNSIGNED_SHORT,BUFFER_OFFSET(0));
    //glFlush();//it works,show a white square with black backgroud
    glutSwapBuffers();///it doesn't work,show what tha area looked like before
}
void init (void) 
{
    GLuint vbo,ebo;
    GLfloat *vertices;
    GLushort *indices;
    glewInit();
    glGenBuffers(1,&vbo);
    glBindBuffer(GL_ARRAY_BUFFER,vbo);
    glBufferData(GL_ARRAY_BUFFER,2*NumPoints*sizeof(GLfloat),NULL,GL_STATIC_DRAW);
    vertices=(GLfloat *)glMapBuffer(GL_ARRAY_BUFFER,GL_WRITE_ONLY);
    if(vertices==NULL)
    {
        fprintf(stderr,"Unable to map vertex buffern");
        exit(EXIT_FAILURE);
    }
    else
    {
        int i,j;
        GLfloat dx=(XEnd-XStart)/(NumXPoints-1);
        GLfloat dy=(YEnd-YStart)/(NumYPoints-1);
        GLfloat *tmp=vertices;
        int n=0;
        for(j=0;j<NumYPoints;++j)
        {
            GLfloat y=YStart+j*dy;
            for(i=0;i<NumXPoints;++i)
            {
                GLfloat x=XStart + i*dx;
                *tmp++=x;
                *tmp++=y;
            }
        }
        glUnmapBuffer(GL_ARRAY_BUFFER);
        glVertexPointer(2,GL_FLOAT,0,BUFFER_OFFSET(0));
        glEnableClientState(GL_VERTEX_ARRAY);
    }
    glGenBuffers(1,&ebo);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,ebo);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER,NumStrips*(NumPointsPerStrip+1)*sizeof(GLushort),NULL,GL_STATIC_DRAW);
    indices=(GLushort *)glMapBuffer(GL_ELEMENT_ARRAY_BUFFER,GL_WRITE_ONLY);
    if(indices==NULL)
    {
        fprintf(stderr,"Unable to map index buffern");
        exit(EXIT_FAILURE);
    }
    else
    {
        int i,j;
        GLushort *index=indices;
        for(j=0;j<NumStrips;++j)
        {
            GLushort bottomRow=j*NumYPoints;
            GLushort topRow=bottomRow+NumYPoints;
            for(i=0;i<NumXPoints;++i)
            {
                *index++=topRow+i;
                *index++=bottomRow+i;
            }
            *index++=RestartIndex;
        }
        glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER);
    }
    glPrimitiveRestartIndex(RestartIndex);
    glEnable(GL_PRIMITIVE_RESTART);
}

void reshape (int w, int h)
{
    glMatrixMode (GL_PROJECTION);
    glLoadIdentity ();
    gluOrtho2D (-1,1,-1,1);
    glViewport (0,0,w,h);
}
void keyboard(unsigned char key, int x, int y)
{
    switch (key) {
      case 27:
          exit(0);
          break;
    }
}
int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize (200, 200);
    glutInitWindowPosition (100, 100);
    glutCreateWindow (argv[0]);
    init ();
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutKeyboardFunc (keyboard);
    glutMainLoop();
    return 0;  
}

正如 http://www.opengl.org/resources/libraries/glut/spec3/node21.html 所说:
隐式 glFlush 由 glutSwapBuffers 在返回之前完成。后续的 OpenGL 命令可以在调用 glutSwapBuffer 后立即发出,但在缓冲区交换完成之前不会执行。
如果使用的层不是双缓冲的,则 glutSwapBuffers 不起作用。

我如何知道正在使用的层是否为双缓冲层?给出双缓冲 example.is 我编写的此代码是双缓冲的?

main函数中,你调用

glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);

这需要GLUT_DOUBLE. 请参阅文档以了解glutInitDisplayMode()

最新更新