0

My OpenGL glFlush() didn't show anything when I run a glut project in Codeblocks on windows 7. Here my main function.

#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;
}

But when I change these two lines, it simply works fine.

glFlush(); to glutSwapBuffers(); and glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE); to glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);

Can anyone tell me what's the problem with my code and why not glFlush() didn't work?

genpfault
  • 51,148
  • 11
  • 85
  • 139
MD. ABU SAYED
  • 241
  • 2
  • 5

2 Answers2

3

Modern graphics systems (Windows DWM/Aero, MacOS Quartz Extreme, X11 Composite) are built around the concept of composition. Composition always implies double buffering and hence relies on the buffer swap to initiate a composition refresh.

You can disable DWM/Aero on Windows and restrain from using a compositing window manager on X11, and then single buffered OpenGL should work as expected.

But why exactly do you want single buffered drawing? Modern GPUs are actually presuming that double buffering is used to pump their presentation pipeline efficiently. There's zero benefit in being single buffered.

genpfault
  • 51,148
  • 11
  • 85
  • 139
datenwolf
  • 159,371
  • 13
  • 185
  • 298
-1

glFlush works as documented:

The glFlush function forces execution of OpenGL functions in finite time.

What this does, is it forces all outstanding OpenGL operations to compleate rendering to the back buffer. This will not magically display the back buffer. To do that you need to swap the font buffer and the back buffer.

So the correct use of glFlush, is in conjunction with glutSwapBuffers. But that is redundant, since glutSwapBuffers will flush all outstanding rendering operations anyway.

It appears that you are using an old OpenGL 1.1 tutorial, where double buffering was an expensive novelty. Currently double buffering is the norm and you need to jump through quite some expensive hoops to get single buffering.

Since OpenGL is currently at version 4.6, I would encourage you to at least start using 4.0.

rioki
  • 5,988
  • 5
  • 32
  • 55