首页 > 解决方案 > glFlush() 不显示任何内容

问题描述

当我在 Windows 7 上的 Codeblocks 中运行 glut 项目时,我的 OpenGL glFlush() 没有显示任何内容。这是我的主要功能。

#include <windows.h>
#include <GL/glut.h>
#include <stdlib.h>
#include <stdio.h>

float Color1=0.0, Color2=0.0, Color3=0.0;
int r,p,q;


void keyboard(unsigned char key, int x, int y)
{
  switch (key)
  {
  case 27:             // ESCAPE key
      exit (0);
      break;

  case 'r':
     Color1=1.0, Color2=0.0, Color3=0.0;
     break;
  case 'g':
     Color1=0.0, Color2=1.0, Color3=0.0;
     break;
  case 'b':
      Color1=0.0, Color2=0.0, Color3=1.0;
      break;
  }
  glutPostRedisplay();

}

void Init(int w, int h)
{
    glClearColor(1.0, 1.0, 1.0, 1.0);
    glViewport(0,0, (GLsizei)w,(GLsizei)h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D( (GLdouble)w/-2,(GLdouble)w/2, (GLdouble)h/-2, (GLdouble)h/2);

}

static void display(void)
{

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    int i=0;
    glColor4f(0,0,0,1);
    glPointSize(1);
    glBegin(GL_POINTS);
    for( i=-320;i<=320;i++)
        glVertex2f(i,0);
    for( i=-240;i<=240;i++)
        glVertex2f(0,i);
    glEnd();

    glColor4f(Color1,Color2, Color3,1);
    glPointSize(1);
    glBegin(GL_POINTS);
    int x=0, y = r;
    int d= 1-r;
    while(y>=x)
    {
        glVertex2f(x+p, y+q);
        glVertex2f(y+p, x+q);
        glVertex2f(-1*y+p, x+q);
        glVertex2f(-1*x+p, y+q);

        glVertex2f(-1*x+p, -1*y+q);
        glVertex2f(-1*y+p, -1*x+q);
        glVertex2f(y+p, -1*x+q);
        glVertex2f(x+p, -1*y+q);

        if(d<0)
            d += 2*x + 3;
        else
        {
            d += 2*(x-y) + 5;
            y--;
        }
        x++;
    }

    glEnd();
    glFlush();
    //glutSwapBuffers();
}


int main(int argc, char *argv[])
{

    printf("Enter the center point and radius: ");
    scanf("%d %d %d",&p,&q,&r);
    glutInit(&argc, argv);
    glutInitWindowSize(640,480);
    glutInitWindowPosition(10,10);
    glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);

    glutCreateWindow("Circle drawing");

    Init(640, 480);
    glutKeyboardFunc(keyboard);
    glutDisplayFunc(display);


    glutMainLoop();

    return 0;
}

但是当我改变这两行时,它就可以正常工作了。

glFlush(); 到 glutSwapBuffers(); 和 glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE); 到 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);

谁能告诉我我的代码有什么问题,为什么 glFlush() 不起作用?

标签: copenglcodeblocksglut

解决方案


现代图形系统(Windows DWM/Aero、MacOS Quartz Extreme、X11 Composite)是围绕组合概念构建的。组合总是意味着双缓冲,因此依赖于缓冲区交换来启动组合刷新。

您可以在 Windows 上禁用 DWM/Aero 并限制在 X11 上使用合成窗口管理器,然后单缓冲 OpenGL 应该可以按预期工作。

但是为什么你想要单缓冲绘图呢?现代 GPU 实际上假定使用双缓冲来有效地泵送其演示管道。单缓冲的好处为零。


推荐阅读