GLUT: Drawing keeps on blinking. Need Urgent help

Dec 25, 2011 at 3:34am
Hi, I have been drawing circles using glut. But they keep on blinking. Please help!
functions are below:

main:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main ( int argc, char **argv)
{
 glutInit (&argc, argv);
 glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGB);
 glutInitWindowPosition (1,1);
 glutInitWindowSize (1000,700); 
 glutCreateWindow ("Mastermind");
 glutFullScreen(); 
 glutReshapeFunc(changeSize); 
 glutDisplayFunc(background); 
 glutIdleFunc(background);
 glEnable(GL_DEPTH_TEST);
 glutMainLoop();
 return 1;  
}

drawcircle:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
void drawcircle()
{
         float y = 3;                 
         float angle = 0;
         float r = 0.2;
         float x = -6.5; 
     for ( x ; x <= -3.5; x++)
     {               
         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
         glLoadIdentity(); // reset transformations
         glEnable(GL_COLOR_MATERIAL); //Enable color
         gluLookAt(0.0f, 0.0f, 10.0f,0.0f, 0.0f,  0.0f,  0.0f, 1.0f,  0.0f);        
         float delta_theta = 0.01;
      
             glTranslatef(x,y,0);                 
             glBegin( GL_POLYGON ); // OR GL_LINE_LOOP     
             for (angle = 0; angle < 2*PI; angle += delta_theta )
             {
                     glColor3f (1, 1, 1);         
                     glVertex3f( r*cos(angle), r*sin(angle), 0 );
             } 
             glEnd();
             glutSwapBuffers();                          
     }              
}

rendering:
1
2
3
4
5
6
void background()
{       
     glClearColor(0.0f, 0.0f, 0.0f, 0.0f);                     
     drawcircle ();             
     glutSwapBuffers();       
}
Dec 25, 2011 at 5:38am
You might consider using Qt, they have a widget called QGLWidget for opengl. I seem to remember a similar problem when using glut awhile back, and have not had the same issue when using qglwidget.
Dec 25, 2011 at 7:19am
1
2
3
glClear( GL_COLOR_BUFFER_BIT ); //clear the back buffer
draw_circle(); //draws in the back buffer
glutSwapBuffers(); //now back is front (screen) 
But you are doing that inside 'draw_circle'. So you are swapping twice, hence the blinking.

glutIdleFunc(background); ¿why? I think that you will be eating your processor.
Dec 25, 2011 at 3:05pm
Ok if i remove glutIdleFunc(background); it will not matter. I will do it. I did not know it would eat my computer. thanks

I removed the glutSwapbuffer(); . Now I can only see the last circle.
what do you think I need to do to solve this problem?

I also tried making the loop in my background function, but it still does not work.
Last edited on Dec 25, 2011 at 3:09pm
Dec 25, 2011 at 4:21pm
You've got glClear inside the loop. You are erasing what you just drew.
Dec 25, 2011 at 5:14pm
Still not sure. Could you say what exactly to change? Ok, so I it this way:

way 1:
output: draws just the first circle
function background:
void background()
1
2
3
4
5
{       
     glClearColor(0.0f, 0.0f, 0.0f, 0.0f);                     
     drawcircle ();             
     glutSwapBuffers();       
}

function drawcircle:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void drawcircle()
{
         float y = 3;                 
         float angle = 0;
         float r = 0.2;
         float x = -6.5; 
         for ( x ; x <= -3.5; x++)
         {               
             glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
             glLoadIdentity(); // reset transformations
            // glEnable(GL_COLOR_MATERIAL); //Enable color
             gluLookAt(0.0f, 0.0f, 10.0f,0.0f, 0.0f,  0.0f,  0.0f, 1.0f,  0.0f);        
             float delta_theta = 0.01;          
             glTranslatef(x,y,0);                 
             glBegin( GL_POLYGON ); // OR GL_LINE_LOOP     
             for (angle = 0; angle < 2*PI; angle += delta_theta )
             {
               glColor3f (1, 1, 1);         
               glVertex2f( r*cos(angle), r*sin(angle) );
             }    
                                       
          }
          glEnd();              
}


way 2:
output: draws all the circles but it is still blinking.
function background:
1
2
3
4
5
6
7
8
9
10
11
void background()
{
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);  // creating colours and depth buffers  
     glClearColor(0.0f, 0.0f, 0.0f, 0.0f);    
     for ( float x = -6.5; x <= -3.5; x++)
     {
         float y = 3;                            
         drawcircle (x, y); 
         glutSwapBuffers();
     }
}

function drawcircle:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void drawcircle(float x, float y)
{ 
            
     float angle = 0;
     float r = 0.2; 
                 
         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
         glLoadIdentity(); // reset transformations
         glEnable(GL_COLOR_MATERIAL); //Enable color
         gluLookAt(0.0f, 0.0f, 10.0f,0.0f, 0.0f,  0.0f,  0.0f, 1.0f,  0.0f);  
         glColor3f (1.0f, 1.0f, 1.0f);
         float delta_theta = 0.01;
         glTranslatef(x,y,0);        
         glBegin( GL_POLYGON ); // OR GL_LINE_LOOP     
         for (angle = 0; angle < 2*PI; angle += delta_theta )
         {
                 glColor3f (1.0f, 1.0f, 1.0f);         
                 glVertex3f( r*cos(angle), r*sin(angle), 0 );
         } 
         glEnd();
         glutSwapBuffers();
}
Dec 25, 2011 at 6:12pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void background(){
  glClear( GL_COLOR_BUFFER_BIT );
  draw_background();
  glutSwapBuffers();
}

void draw_background(){
  for ( float x = -6.5; x <= -3.5; x++)
    draw_circle (x, 3);
}

void draw_circle(float x, float y){
  glLoadIdentity();
  glTranslatef(x,y,0);
  glBegin( GL_POLYGON );{
    //set the vertex. Don't put glClear or glutSwapBuffers here
  }glEnd();
}


1_ Start with a new piece of paper.
2_ Draw what you want.
3_ Show it.
4_ Go back to 1
Dec 25, 2011 at 7:46pm
Umm. Still does not work: I only see the last circle
functions:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
float drawcircle(float x ,float y)
{ 
           
         float angle = 0;
         float r = 0.2;                
         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
         glLoadIdentity(); // reset transformations
         glTranslatef(x,y,0);
         glEnable(GL_COLOR_MATERIAL); //Enable color
         gluLookAt(0.0f, 0.0f, 10.0f,0.0f, 0.0f,  0.0f,  0.0f, 1.0f,  0.0f);  
         glColor3f (1.0f, 1.0f, 1.0f);
         float delta_theta = 0.01;        
         glBegin( GL_POLYGON ); // OR GL_LINE_LOOP     
         for (angle = 0; angle < 2*PI; angle += delta_theta )
         {
                 glColor3f (1.0f, 1.0f, 1.0f);         
                 glVertex3f( r*cos(angle), r*sin(angle), 0 );
         } 
         glEnd();       
}


1
2
3
4
5
6
void background()
{
  glClear( GL_COLOR_BUFFER_BIT );
  draw_background();
  glutSwapBuffers();
}


1
2
3
4
5
6
7
8
9
10
11
void draw_background()
{
    // glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);  // creating colours and depth buffers  
     glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
     for ( float x = -6.5; x <= -3.5; x++)
     {
         float y = 3;          
         drawcircle (x,y);                  
         
     }     
}
Last edited on Dec 25, 2011 at 7:46pm
Dec 25, 2011 at 8:00pm
ok removing the glclear works!! :D thanks so much
Topic archived. No new replies allowed.