PDA

View Full Version : why is it flickering?


arramor
02-12-2005, 09:42 PM
I hope this doesn't sound too stupid a question:

I'm working with glut in visual c++, but all motion programs i try to run keeps on flickering. I was hoping someone could maybe tell me why. Am I doing something wrong?

here is parts of an example that flickers: (planet orbiting around sun, found in redbook)

int main(int argc, char* argv[]) {
glutInit(&argc, argv);
/* creating window ... */
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_FLAT);

glutSpecialFunc(processSpecialKeys);
glutDisplayFunc(Display);
glutReshapeFunc(Reshape);
glutMainLoop();
return 0;
}

void Display (void) {
glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0, 1.0, 1.0);
glPushMatrix();
glutWireSphere(1.0, 20.0, 20.0); /* draw sun */
glRotatef( (GLfloat) year, 0.0, 1.0, 0.0); /* planet rotate around sun */
glTranslatef(2.0, 0.0, 0.0); /* orbit of 2 units from sun */
glRotatef( (GLfloat) day, 0.0, 1.0, 0.0); /* planet rotate around himself */
glutWireSphere(0.2, 10.0, 10.0); /* draw smaller planet */
glPopMatrix();
// glFlush();
glutSwapBuffers();
glutPostRedisplay();
}


all programs that i try to rotate or move something keeps on flickering. Do i do something out of order? (example tutorials i download never flicker and i can't seem to see the difference between mine and theirs)

arramor
02-13-2005, 09:59 PM
please, it just won't stop flickering. Isn't the glutswapbuffers() suppose to cure this?

mummey
02-14-2005, 03:04 AM
please, it just won't stop flickering. Isn't the glutswapbuffers() suppose to cure this?

Putting the two

glutSwapBuffers();
glutPostRedisplay();

next to each other may be the problem.

shadowMaster
02-14-2005, 06:41 AM
arramor, it looks like you're not clearing the depth buffer- perhaps that's the problem?
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


Putting the two
glutSwapBuffers();
glutPostRedisplay();
next to each other may be the problem.

No, that is absolutely fine to do.

Robert Bateman
02-14-2005, 09:50 AM
at a guess i'd say you have v-sync disabled in your windows display settings. Your screen may be re-drawing faster than the actual monitor can display which is leading to artifacts... try enabling v-sync (if you like fixed 60fps refreshes), or simply draw a *lot* more polygons......

arramor
02-14-2005, 10:09 PM
how do i enable v-sync, i couldn't find anything like that in my display settings?
My refresh rate is 60 fps.

thanks for replies so far

arramor
02-14-2005, 10:55 PM
On second thought, my monitor is fine. Like I said in first posting at top, i ran some tutorial programs that worked without flickering. So it must be something i do wrong in my code.

I link and include everything i should, like tutorials that work without flickering. Something must be ill in my code. Grrr, what could it be? Anyone? Should I include my whole code maybe if somebody would please take a look at it?

shadowMaster
02-15-2005, 01:46 AM
did you try clearing the depth buffer like I mentioned? you're init call is asking for a depth buffer and you're using functions that write to the depth buffer, but you are not clearing it.

gga
02-15-2005, 03:55 AM
You are not creating a double buffered window. Ergo, glutSwapBuffer will do nothing.
Add GLUT_DOUBLE to your glutInitDisplayMode call.

Robert Bateman
02-15-2005, 08:46 AM
You are not creating a double buffered window. Ergo, glutSwapBuffer will do nothing.
Add GLUT_DOUBLE to your glutInitDisplayMode call.

err, he already has that.....

i still reackon it's the v-sync on his graphics card. You'll need to go into the advanced openGL settings for the card to change it.....

Robert Bateman
02-15-2005, 08:52 AM
did you try clearing the depth buffer like I mentioned? you're init call is asking for a depth buffer and you're using functions that write to the depth buffer, but you are not clearing it.

I can't see where he is "writing" to the depth buffer as you put it, since he does not enable the depth test. yes, he does ask for one and doesn't use it, but it certainly isn't a crime; so I think we can rule out the depth test being the cause of the problem.....

arramor
02-15-2005, 09:14 AM
thanks for all replies

i'm sorry about the clearing of depth buffer, i almost always include that, but for this example i forgot. in other examples where i clear depth buffer it still flickers. and i do add GL_DOUBLE to my initDisplayMode.

I don't think it's the depth buffer?

The problem is I keep on running code examples from downloads and they seem fine with no flickering. So then is it possible for my graphics card to correctly run these other opengl programs, but still flicker mine? I mean I still compile and run these other programs on my system too, and they don't flicker.

I must be doing something weird wrong. All my examples come from the red book, but the red book i have don't use glut, they import aux.h for the windowing. So it must be something with glut I'm doing wrong?

Oh, and i do enable depth test also in ,most cases, just forgot here.

shadowMaster
02-16-2005, 06:19 AM
I can't see where he is "writing" to the depth buffer as you put it, since he does not enable the depth test.

Righto. Hard to tell since we don't have all the code, the request for the depth buffer led me to assume.


arramor, I suspect you're right that something is wrong with the code since some examples work correctly. I certainly get a kind of flickering if I remove the request for a double buffer... but it looks like you're doing that part correctly. You're missing a call to glutCreateWindow()... on my machine that causes a seg fault but perhaps for you it succeeds but doesn't initialize properly?

I tried recreating your program... with a couple of minor changes just to get it compiling, it doesn't flicker. So if you run this and it flickers... it's probably a hardware thing.

-------------------------


#include <gl/glut.h>
GLfloat year = 0;
GLfloat day = 0;
void Reshape(int width, int height) {
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
}
void Display (void) {
day += 0.2; year += 1.0;
glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0, 1.0, 1.0);
glPushMatrix();
glutWireSphere(1.0, 20.0, 20.0); /* draw sun */
glRotatef( (GLfloat) year, 0.0, 1.0, 0.0); /* planet rotate around sun */
glTranslatef(1.0, 0.0, 0.0); /* orbit of 2 units from sun */
glRotatef( (GLfloat) day, 0.0, 1.0, 0.0); /* planet rotate around himself */
glutWireSphere(0.2, 10.0, 10.0); /* draw smaller planet */
glPopMatrix();
glutSwapBuffers();
glutPostRedisplay();
}
int main(int argc, char* argv[]) {
glutInit(&argc, argv);
/* creating window ... */
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutCreateWindow("jack");
glClearColor(0.0, 0.0, 0.0, 0.0);
glutDisplayFunc(Display);
glutReshapeFunc(Reshape);
glutMainLoop();
return 0;
}

arramor
02-17-2005, 08:53 PM
Cool, yours doesn't flicker. But mine still does :), can you explain the difference between our programs? Does mine flicker on your system?

#include <GL/glut.h>
#define WINDOW_WIDTH 500
#define WINDOW_HEIGHT 500
#define WINDOW_X 100
#define WINDOW_Y 100
#define WINDOW_TITLE "Planet orbiting around Sun"
static int year = 10, day = 10;
void Display(void);
void Reshape(GLsizei w, GLsizei h);
void dayAdd (void)
{
day = (day + 10) % 360;
}
void daySubtract (void)
{
day = (day - 10) % 360;
}
void yearAdd (void)
{
year = (year + 5) % 360;
}
void yearSubtract (void)
{
year = (year - 5) % 360;
}
void processSpecialKeys(int key, int x, int y) {
switch (key) {

case GLUT_KEY_LEFT :
yearSubtract(); break;
case GLUT_KEY_RIGHT :
yearAdd(); break;
case GLUT_KEY_UP :
dayAdd(); break;
case GLUT_KEY_DOWN :
daySubtract(); break;
}
}
void myinit() {
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_FLAT);
}
int main(int argc, char* argv[]) {
glutInit(&argc, argv);

glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT);
glutInitWindowPosition(WINDOW_X, WINDOW_Y);
glutCreateWindow(WINDOW_TITLE);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
myinit();
glutSpecialFunc(processSpecialKeys);
glutDisplayFunc(Display);
glutReshapeFunc(Reshape);

glutMainLoop();
return 0;
}
void Display (void) {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
glPushMatrix();
glutWireSphere(1.0, 20.0, 20.0); /* draw sun */
glRotatef( (GLfloat) year, 0.0, 1.0, 0.0); /* planet rotate around sun */
glTranslatef(2.0, 0.0, 0.0); /* orbit of 2 units from sun */
glRotatef( (GLfloat) day, 0.0, 1.0, 0.0); /* planet rotate around himself */
glutWireSphere(0.2, 10.0, 10.0); /* draw smaller planet */
glPopMatrix();
glutSwapBuffers();
glutPostRedisplay();

}
void Reshape(GLsizei w, GLsizei h) {
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0, 0.0, -5.0);
}

shadowMaster
02-18-2005, 06:51 AM
Cool, yours doesn't flicker. But mine still does :), can you explain the difference between our programs? Does mine flicker on your system?

glutCreateWindow(WINDOW_TITLE);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);


Aha! there it is, the init() must come before the create(). When create() is first, it defaults the window to single buffered.

arramor
02-18-2005, 12:23 PM
EUREKA! :) Sorry for putting you all through that. Thanks very much shadowMaster!

*bow to sensei shadowmaster*

CGTalk Moderation
02-18-2006, 01:00 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.