OpenGL qeustion:为什么glutSwapBuffer()函数不起作用?

时间:2012-08-07 15:24:06

标签: opengl

此代码来自红皮书,例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 buffer\n");
        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 buffer\n");
        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在返回之前完成。调用glutSwapBuffers后,可以立即发出后续的OpenGL命令,但在缓冲区交换完成之前不会执行。 如果使用的图层不是双缓冲的,则glutSwapBuffers无效。

我怎么知道使用的图层是否是双缓冲的?给出一个双缓冲的例子。我写的这个代码是双缓冲的吗?

1 个答案:

答案 0 :(得分:5)

main功能中,您可以致电

glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);

那需要GLUT_DOUBLE。请参阅glutInitDisplayMode()的文档。