Why is my animation flickering?

  • Follow


Hi,

I have a simple animation of a torus which fades in and out in a
periodic fashion. The problem is that during the fading in and out, the
torus appears to flicker. I'm using double buffering.

Can anyone explain why?

Thanks

Barry.

#include <GL/glut.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>.
#include <sys/timeb.h>
#include <time.h>
#include <stdio.h>

#define FRAMES_PER_SEC     	20.0f

int winWidth = 768;
int winHeight = 768;

int count = 0;
double amp = 0;

void myTimer(int value)
{
  if (count==0||count==40)
	amp = 0;
  if (count==1||count==39)
	amp = 0.1;
  if (count==2||count==38)
	amp = 0.2;
  if (count==3||count==37)
	amp = 0.3;
  if (count==4||count==36)
	amp = 0.4;
  if (count==5||count==35)
	amp = 0.5;
  if (count==6||count==34)
	amp = 0.6;
  if (count==7||count==33)
	amp = 0.7;
  if (count==8||count==32)
	amp = 0.8;
  if (count==9||count==31)
	amp = 0.9;
  if (count==10)
	amp = 1.0;

  count++;
  if(count==40)
	count = 0;

  glutPostRedisplay();
  glutTimerFunc(1000/FRAMES_PER_SEC, myTimer, 1);
}

/* used to get current width and height of viewport */
void reshape(int wid, int ht)
{
  glViewport(0, 0, wid, ht);
  winWidth = wid;
  winHeight = ht;
}

GLfloat viewangle;

void redraw()
{
  glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

  glColor3f(.0f, amp, amp);
  glutSolidTorus(6., 30, 64, 128);

  glutSwapBuffers();
}

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

  static GLfloat matAmbient[]= { 0.1f, 0.1f, 1.0f, 1.0f };
  static GLfloat matSpecular[] = {0.0f, 0.0f, 1.0f , 1.0f};
  static GLfloat matDiffuse[] = { 0.0f, 0.0f, 1.0f, 1.0f };
  static GLfloat matShininess[] = { 128.0f };

  static GLfloat lightPos[] = {50.f, 30.f, 50.f, 1.0f};

  static GLfloat lightAmbient[]= { 0.25f, 0.25f, 0.25f, 1.0f };

  static GLfloat lightSpecular[] = {1.0f, 1.0f, 1.0f , 1.0f};
  static GLfloat lightDiffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
  static GLfloat localAmbient[] = { 0.0f, 0.0f, 0.0, 1.0f };

  glutInit(&argc, argv);
  glutInitWindowSize(712, 712);
  glutInitDisplayMode(GLUT_DEPTH|GLUT_DOUBLE);
  (void)glutCreateWindow("Window");
  glutDisplayFunc(redraw);

  glClearColor(.0f, .0f, .0f, .0f);
  glEnable(GL_DEPTH_TEST);

  glEnable(GL_LIGHTING);
  glEnable(GL_LIGHT0);

  glLightModelfv(GL_LIGHT_MODEL_AMBIENT, localAmbient);

  glLightfv(GL_LIGHT0, GL_DIFFUSE, lightDiffuse);

  glLightfv(GL_LIGHT0, GL_AMBIENT, lightAmbient);
  glLightfv(GL_LIGHT0, GL_SPECULAR, lightSpecular);
  glLightfv(GL_LIGHT0, GL_POSITION, lightPos);

  glEnable ( GL_COLOR_MATERIAL ) ;

  glLightfv(GL_FRONT, GL_DIFFUSE, matDiffuse);
  glLightfv(GL_FRONT, GL_AMBIENT, matAmbient);
  glLightfv(GL_FRONT, GL_SPECULAR, matSpecular);
  glLightfv(GL_FRONT, GL_SHININESS, matShininess);

  glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);


  glEnable(GL_BLEND);
  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

  glHint(GL_POLYGON_SMOOTH, GL_NICEST);
  glEnable(GL_POLYGON_SMOOTH);

  glMatrixMode(GL_PROJECTION);
  glOrtho(-50., 50., -50., 50., -50., 50.);
  glMatrixMode(GL_MODELVIEW);

  glutSetCursor(GLUT_CURSOR_NONE);

  glutTimerFunc(1000/FRAMES_PER_SEC,myTimer,0);
  glutMainLoop();

  return 0;             /* ANSI C requires main to return int. */
}

0
Reply bg_ie (117) 8/16/2006 3:31:02 PM

bg_ie@yahoo.com wrote:
> Hi,
>
> I have a simple animation of a torus which fades in and out in a
> periodic fashion. The problem is that during the fading in and out, the
> torus appears to flicker. I'm using double buffering.
>
> Can anyone explain why?
>
> Thanks
>
> Barry.
>
> #include <GL/glut.h>
> #include <math.h>
> #include <stdlib.h>
> #include <string.h>
> #include <sys/types.h>.
> #include <sys/timeb.h>
> #include <time.h>
> #include <stdio.h>
>
> #define FRAMES_PER_SEC     	20.0f
>
> int winWidth = 768;
> int winHeight = 768;
>
> int count = 0;
> double amp = 0;
>
> void myTimer(int value)
> {
>   if (count==0||count==40)
> 	amp = 0;
>   if (count==1||count==39)
> 	amp = 0.1;
>   if (count==2||count==38)
> 	amp = 0.2;
>   if (count==3||count==37)
> 	amp = 0.3;
>   if (count==4||count==36)
> 	amp = 0.4;
>   if (count==5||count==35)
> 	amp = 0.5;
>   if (count==6||count==34)
> 	amp = 0.6;
>   if (count==7||count==33)
> 	amp = 0.7;
>   if (count==8||count==32)
> 	amp = 0.8;
>   if (count==9||count==31)
> 	amp = 0.9;
>   if (count==10)
> 	amp = 1.0;
>
>   count++;
>   if(count==40)
> 	count = 0;
>
>   glutPostRedisplay();
>   glutTimerFunc(1000/FRAMES_PER_SEC, myTimer, 1);
> }
>
> /* used to get current width and height of viewport */
> void reshape(int wid, int ht)
> {
>   glViewport(0, 0, wid, ht);
>   winWidth = wid;
>   winHeight = ht;
> }
>
> GLfloat viewangle;
>
> void redraw()
> {
>   glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
>
>   glColor3f(.0f, amp, amp);
>   glutSolidTorus(6., 30, 64, 128);
>
>   glutSwapBuffers();
> }
>
> int
> main(int argc, char **argv)
> {
>
>   static GLfloat matAmbient[]= { 0.1f, 0.1f, 1.0f, 1.0f };
>   static GLfloat matSpecular[] = {0.0f, 0.0f, 1.0f , 1.0f};
>   static GLfloat matDiffuse[] = { 0.0f, 0.0f, 1.0f, 1.0f };
>   static GLfloat matShininess[] = { 128.0f };
>
>   static GLfloat lightPos[] = {50.f, 30.f, 50.f, 1.0f};
>
>   static GLfloat lightAmbient[]= { 0.25f, 0.25f, 0.25f, 1.0f };
>
>   static GLfloat lightSpecular[] = {1.0f, 1.0f, 1.0f , 1.0f};
>   static GLfloat lightDiffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
>   static GLfloat localAmbient[] = { 0.0f, 0.0f, 0.0, 1.0f };
>
>   glutInit(&argc, argv);
>   glutInitWindowSize(712, 712);
>   glutInitDisplayMode(GLUT_DEPTH|GLUT_DOUBLE);
>   (void)glutCreateWindow("Window");
>   glutDisplayFunc(redraw);
>
>   glClearColor(.0f, .0f, .0f, .0f);
>   glEnable(GL_DEPTH_TEST);
>
>   glEnable(GL_LIGHTING);
>   glEnable(GL_LIGHT0);
>
>   glLightModelfv(GL_LIGHT_MODEL_AMBIENT, localAmbient);
>
>   glLightfv(GL_LIGHT0, GL_DIFFUSE, lightDiffuse);
>
>   glLightfv(GL_LIGHT0, GL_AMBIENT, lightAmbient);
>   glLightfv(GL_LIGHT0, GL_SPECULAR, lightSpecular);
>   glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
>
>   glEnable ( GL_COLOR_MATERIAL ) ;
>
>   glLightfv(GL_FRONT, GL_DIFFUSE, matDiffuse);
>   glLightfv(GL_FRONT, GL_AMBIENT, matAmbient);
>   glLightfv(GL_FRONT, GL_SPECULAR, matSpecular);
>   glLightfv(GL_FRONT, GL_SHININESS, matShininess);
>
>   glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
>
>
>   glEnable(GL_BLEND);
>   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
>
>   glHint(GL_POLYGON_SMOOTH, GL_NICEST);
>   glEnable(GL_POLYGON_SMOOTH);
>
>   glMatrixMode(GL_PROJECTION);
>   glOrtho(-50., 50., -50., 50., -50., 50.);
>   glMatrixMode(GL_MODELVIEW);
>
>   glutSetCursor(GLUT_CURSOR_NONE);
>
>   glutTimerFunc(1000/FRAMES_PER_SEC,myTimer,0);
>   glutMainLoop();
>
>   return 0;             /* ANSI C requires main to return int. */
> }

Anyone have any suggestions about this? Is the frame rate too low?

0
Reply bg_ie 8/18/2006 10:24:13 AM


bg_ie@yahoo.com wrote:

> Anyone have any suggestions about this? Is the frame rate too
> low?

That wouldn't explain the flickering. The best guess is, that
doublebuffering isn't enabled, OTOH you requested a double
buffer but you didn't specify if you want GLUT_RGBA or GLUT_RGB
or GLUT_INDEXED. My guess is that you didn't got the pixelformat
you wanted.

A few other hints: Don't call glutPostRedisplay from the
TimerFunc. Instead place it in the (net yet existing in your
code) idle function (call glutPostRedisplay as often as
possible). All the glLightfv calls should be placed in the
redraw function (along with all matrix intitialization), that is
to ensure, that upong drawing something everything is in the
state you expect.

Wolfgang Draxinger
-- 
E-Mail address works, Jabber: hexarith@jabber.org, ICQ: 134682867
GPG key FP: 2FC8 319E C7D7 1ADC 0408 65C6 05F5 A645 1FD3 BD3E
0
Reply Wolfgang 8/18/2006 11:49:54 AM

Wolfgang Draxinger wrote:
> bg_ie@yahoo.com wrote:
>
> > Anyone have any suggestions about this? Is the frame rate too
> > low?
>
> That wouldn't explain the flickering. The best guess is, that
> doublebuffering isn't enabled, OTOH you requested a double
> buffer but you didn't specify if you want GLUT_RGBA or GLUT_RGB
> or GLUT_INDEXED. My guess is that you didn't got the pixelformat
> you wanted.
>
> A few other hints: Don't call glutPostRedisplay from the
> TimerFunc. Instead place it in the (net yet existing in your
> code) idle function (call glutPostRedisplay as often as
> possible). All the glLightfv calls should be placed in the
> redraw function (along with all matrix intitialization), that is
> to ensure, that upong drawing something everything is in the
> state you expect.
>
> Wolfgang Draxinger
> --
> E-Mail address works, Jabber: hexarith@jabber.org, ICQ: 134682867
> GPG key FP: 2FC8 319E C7D7 1ADC 0408 65C6 05F5 A645 1FD3 BD3E


Thanks for your help. I made the changes you suggested but I don't see
a difference. Also, perhaps flickering is the wrong word. What I see is
a line across my torus as it fades in and out. This line moves upwards
and is similar to the effect you see when a computer screen is shown on
tv. Anti-aliasing I think you call it. In my case, its a subtle effect,
but noticeable all the same. I'm completely new to graphics so perhaps
I'm missing something. Would you be kind enough to run my program to
see if you get the same effect?

Thanks,

Barry.

0
Reply bg_ie 8/18/2006 4:01:00 PM

bg_ie@yahoo.com wrote:
> Wolfgang Draxinger wrote:
> > bg_ie@yahoo.com wrote:
> >
> > > Anyone have any suggestions about this? Is the frame rate too
> > > low?
> >
> > That wouldn't explain the flickering. The best guess is, that
> > doublebuffering isn't enabled, OTOH you requested a double
> > buffer but you didn't specify if you want GLUT_RGBA or GLUT_RGB
> > or GLUT_INDEXED. My guess is that you didn't got the pixelformat
> > you wanted.
> >
> > A few other hints: Don't call glutPostRedisplay from the
> > TimerFunc. Instead place it in the (net yet existing in your
> > code) idle function (call glutPostRedisplay as often as
> > possible). All the glLightfv calls should be placed in the
> > redraw function (along with all matrix intitialization), that is
> > to ensure, that upong drawing something everything is in the
> > state you expect.
> >
> > Wolfgang Draxinger
> > --
> > E-Mail address works, Jabber: hexarith@jabber.org, ICQ: 134682867
> > GPG key FP: 2FC8 319E C7D7 1ADC 0408 65C6 05F5 A645 1FD3 BD3E
>
>
> Thanks for your help. I made the changes you suggested but I don't see
> a difference. Also, perhaps flickering is the wrong word. What I see is
> a line across my torus as it fades in and out. This line moves upwards
> and is similar to the effect you see when a computer screen is shown on
> tv. Anti-aliasing I think you call it. In my case, its a subtle effect,
> but noticeable all the same. I'm completely new to graphics so perhaps
> I'm missing something. Would you be kind enough to run my program to
> see if you get the same effect?
>
> Thanks,
>
> Barry.


It sounds like you may have tearing.  Most people know this problem
from when you play a First Person Shooter type of game, and rotate your
view very quickly, sometimes you will notice a horizontal "break"
partway through the screen.  The top half of the screen seems to lag
momentarily behind the bottom half.  Does this sound like what you
have?

Also, you may want to check your frame rate.  It sounds like you have a
pretty simple scene, so your graphics hardware can probably draw it
very quickly.  Do you get a frame rate of something like 60 or 70 FPS?
Or do you get something much much faster than your monitor could
actually display, like 237 FPS?  If this is the case, you are likely to
get tearing.

So, if this is the problem, caused by the card swapping buffers in the
middle of updating the screen, you will need to poke around your
display settings, and enable an option for "sync swap" or disable an
option for "unlimited frame rate" or something similar.  The exact name
for the option will vary depending on your card and drivers and OS,
etc.  But, if you know generally what you are looking for, it shouldn't
be too hard to find in there.

0
Reply forkazoo 8/18/2006 5:23:46 PM

forkazoo wrote:
> bg_ie@yahoo.com wrote:
> > Wolfgang Draxinger wrote:
> > > bg_ie@yahoo.com wrote:
> > >
> > > > Anyone have any suggestions about this? Is the frame rate too
> > > > low?
> > >
> > > That wouldn't explain the flickering. The best guess is, that
> > > doublebuffering isn't enabled, OTOH you requested a double
> > > buffer but you didn't specify if you want GLUT_RGBA or GLUT_RGB
> > > or GLUT_INDEXED. My guess is that you didn't got the pixelformat
> > > you wanted.
> > >
> > > A few other hints: Don't call glutPostRedisplay from the
> > > TimerFunc. Instead place it in the (net yet existing in your
> > > code) idle function (call glutPostRedisplay as often as
> > > possible). All the glLightfv calls should be placed in the
> > > redraw function (along with all matrix intitialization), that is
> > > to ensure, that upong drawing something everything is in the
> > > state you expect.
> > >
> > > Wolfgang Draxinger
> > > --
> > > E-Mail address works, Jabber: hexarith@jabber.org, ICQ: 134682867
> > > GPG key FP: 2FC8 319E C7D7 1ADC 0408 65C6 05F5 A645 1FD3 BD3E
> >
> >
> > Thanks for your help. I made the changes you suggested but I don't see
> > a difference. Also, perhaps flickering is the wrong word. What I see is
> > a line across my torus as it fades in and out. This line moves upwards
> > and is similar to the effect you see when a computer screen is shown on
> > tv. Anti-aliasing I think you call it. In my case, its a subtle effect,
> > but noticeable all the same. I'm completely new to graphics so perhaps
> > I'm missing something. Would you be kind enough to run my program to
> > see if you get the same effect?
> >
> > Thanks,
> >
> > Barry.
>
>
> It sounds like you may have tearing.  Most people know this problem
> from when you play a First Person Shooter type of game, and rotate your
> view very quickly, sometimes you will notice a horizontal "break"
> partway through the screen.  The top half of the screen seems to lag
> momentarily behind the bottom half.  Does this sound like what you
> have?
>
> Also, you may want to check your frame rate.  It sounds like you have a
> pretty simple scene, so your graphics hardware can probably draw it
> very quickly.  Do you get a frame rate of something like 60 or 70 FPS?
> Or do you get something much much faster than your monitor could
> actually display, like 237 FPS?  If this is the case, you are likely to
> get tearing.
>
> So, if this is the problem, caused by the card swapping buffers in the
> middle of updating the screen, you will need to poke around your
> display settings, and enable an option for "sync swap" or disable an
> option for "unlimited frame rate" or something similar.  The exact name
> for the option will vary depending on your card and drivers and OS,
> etc.  But, if you know generally what you are looking for, it shouldn't
> be too hard to find in there.

It sounds like you may have tearing.  Most people know this problem
from when you play a First Person Shooter type of game, and rotate your

view very quickly, sometimes you will notice a horizontal "break"
partway through the screen.  The top half of the screen seems to lag
momentarily behind the bottom half.  Does this sound like what you
have?

Yeah that sounds like my problem. My frame rate is 20 fps -

#define FRAMES_PER_SEC          20.0f
glutTimerFunc(1000/FRAMES_PER_SEC,myTimer,0);

I have a Radeon x300. I looked in the settings but souldn't find what
you referred to. There is actually an openGL section in the settings
section for this Card/Plug and Play Monitor. There was a setting called
"Wait for Verticle Sync". Also, the screen is set to refresh at 75Hz,
but can also be set to 60Hz or 85Hz.

0
Reply bg_ie 8/18/2006 7:19:41 PM

b...@yahoo.com wrote:
> forkazoo wrote:
> > bg_ie@yahoo.com wrote:
> > > Wolfgang Draxinger wrote:
> > > > bg_ie@yahoo.com wrote:
> > > >
> > > > > Anyone have any suggestions about this? Is the frame rate too
> > > > > low?
> > > >
> > > > That wouldn't explain the flickering. The best guess is, that
> > > > doublebuffering isn't enabled, OTOH you requested a double
> > > > buffer but you didn't specify if you want GLUT_RGBA or GLUT_RGB
> > > > or GLUT_INDEXED. My guess is that you didn't got the pixelformat
> > > > you wanted.
> > > >
> > > > A few other hints: Don't call glutPostRedisplay from the
> > > > TimerFunc. Instead place it in the (net yet existing in your
> > > > code) idle function (call glutPostRedisplay as often as
> > > > possible). All the glLightfv calls should be placed in the
> > > > redraw function (along with all matrix intitialization), that is
> > > > to ensure, that upong drawing something everything is in the
> > > > state you expect.
> > > >
> > > > Wolfgang Draxinger
> > > > --
> > > > E-Mail address works, Jabber: hexarith@jabber.org, ICQ: 134682867
> > > > GPG key FP: 2FC8 319E C7D7 1ADC 0408 65C6 05F5 A645 1FD3 BD3E
> > >
> > >
> > > Thanks for your help. I made the changes you suggested but I don't see
> > > a difference. Also, perhaps flickering is the wrong word. What I see is
> > > a line across my torus as it fades in and out. This line moves upwards
> > > and is similar to the effect you see when a computer screen is shown on
> > > tv. Anti-aliasing I think you call it. In my case, its a subtle effect,
> > > but noticeable all the same. I'm completely new to graphics so perhaps
> > > I'm missing something. Would you be kind enough to run my program to
> > > see if you get the same effect?
> > >
> > > Thanks,
> > >
> > > Barry.
> >
> >
> > It sounds like you may have tearing.  Most people know this problem
> > from when you play a First Person Shooter type of game, and rotate your
> > view very quickly, sometimes you will notice a horizontal "break"
> > partway through the screen.  The top half of the screen seems to lag
> > momentarily behind the bottom half.  Does this sound like what you
> > have?
> >
> > Also, you may want to check your frame rate.  It sounds like you have a
> > pretty simple scene, so your graphics hardware can probably draw it
> > very quickly.  Do you get a frame rate of something like 60 or 70 FPS?
> > Or do you get something much much faster than your monitor could
> > actually display, like 237 FPS?  If this is the case, you are likely to
> > get tearing.
> >
> > So, if this is the problem, caused by the card swapping buffers in the
> > middle of updating the screen, you will need to poke around your
> > display settings, and enable an option for "sync swap" or disable an
> > option for "unlimited frame rate" or something similar.  The exact name
> > for the option will vary depending on your card and drivers and OS,
> > etc.  But, if you know generally what you are looking for, it shouldn't
> > be too hard to find in there.
>
> It sounds like you may have tearing.  Most people know this problem
> from when you play a First Person Shooter type of game, and rotate your
>
> view very quickly, sometimes you will notice a horizontal "break"
> partway through the screen.  The top half of the screen seems to lag
> momentarily behind the bottom half.  Does this sound like what you
> have?
>
> Yeah that sounds like my problem. My frame rate is 20 fps -
>
> #define FRAMES_PER_SEC          20.0f
> glutTimerFunc(1000/FRAMES_PER_SEC,myTimer,0);
>
> I have a Radeon x300. I looked in the settings but souldn't find what
> you referred to. There is actually an openGL section in the settings
> section for this Card/Plug and Play Monitor. There was a setting called
> "Wait for Verticle Sync". Also, the screen is set to refresh at 75Hz,
> but can also be set to 60Hz or 85Hz.

Set "Wait for Verticle Sync" to Always on. The problem is still there.
Has anyone tried my code? Do you think its my code or my graphics card?

Thanks for your help,

Barry.

0
Reply bg_ie 8/18/2006 7:24:03 PM

bg_ie@yahoo.com wrote:

> Set "Wait for Verticle Sync" to Always on. The problem is still there.
> Has anyone tried my code? Do you think its my code or my graphics card?
> 
> Thanks for your help,
> 
> Barry.

I tried your code, and I got subtle flickering as you described -- similar
to TV artifacts as seen through a video camera.  I have a GeForce 6600.

0
Reply Tony 8/20/2006 10:07:41 PM

bg_ie@yahoo.com wrote:

> Set "Wait for Verticle Sync" to Always on. The problem is still there.
> Has anyone tried my code? Do you think its my code or my graphics card?

My WAG is that the tearing is caused by forcing the redraw in the timer
rather than where drawing is normally supposed to occur (the idle function? 
I'm not experienced with glut).

0
Reply Tony 8/20/2006 10:09:59 PM

bg_ie@yahoo.com wrote:
> 
> Set "Wait for Verticle Sync" to Always on. The problem is still there.
> Has anyone tried my code? Do you think its my code or my graphics card?
>

Until you display your frame rate there's no
way to tell.


-- 
<\___/>
/ O O \
\_____/  FTB.    For email, remove my socks.

In science it often happens that scientists say, 'You know
that's a really good argument; my position is mistaken,'
and then they actually change their minds and you never
hear that old view from them again.  They really do it.
It doesn't happen as often as it should, because scientists
are human and change is sometimes painful.  But it happens
every day.  I cannot recall the last time something like
that happened in politics or religion.

- Carl Sagan, 1987 CSICOP keynote address

0
Reply fungus 8/21/2006 5:34:14 AM

9 Replies
121 Views

(page loaded in 0.114 seconds)

Similiar Articles:













7/16/2012 9:36:57 PM


Reply: