PDA

View Full Version : How to avoid flash in an animation?



Debugger
12-25-2006, 04:06 PM
I am a beginner of OPenGL and now nearly learn it about two months.
This is a simple program that drawing a wired sphere with briefly rotation and scale
what i want to know is why the sphere keeping FLASH during its moving?
How to avoid flash and make it became more smooth?
any idea is welcome!


here is all, click left button on the window's client area will start the animation

#include "windows.h"
#include "GL/glut.h"

//sphere radius
GLfloat radius = 1.0 ;

void init(void)
{
//background color
glClearColor(0.6, 0.0, 0.0, 0.0) ;
}

void display(void)
{
//clear buffers
glClear(GL_COLOR_BUFFER_BIT) ;
//execute
glFlush() ;
}

void spinDisplay(void)
{
//draw a wired sphere
glutWireSphere(radius, 12, 8) ;
//rotation
glRotated(1.0, 1.0, 1.0, 1.0) ;
//zoom
glScalef(1.001, 1.001, 1.001) ;
//deferment
Sleep(10) ;
//redraw
glutPostRedisplay() ;
}


//handle mouse event, start when left button down, do not respond other clicks

void mouse(int button, int state, int x, int y)
{
switch(button)
{
case GLUT_LEFT_BUTTON:
if(state==GLUT_DOWN)
glutIdleFunc(spinDisplay) ;
break ;
default:
if(state==GLUT_DOWN)
glutIdleFunc(NULL) ;
break ;
}
}

//reshape window function
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei)w, (GLsizei)h) ;
glMatrixMode(GL_PROJECTION) ;
glLoadIdentity() ;
if(w<=h)
glOrtho(-1.5, 1.5, -1.5*(GLfloat)h/(GLfloat)w,
1.5*(GLfloat)h/(GLfloat)w, -10.0, 10.0) ;
else
glOrtho(-1.5*(GLfloat)w/(GLfloat)h,
1.5*(GLfloat)w/(GLfloat)h, -1.5, 1.5, -10.0, 10.0) ;
glMatrixMode(GL_MODELVIEW) ;
glLoadIdentity() ;
}

int main(int argc, char **argv)
{
//Call this function at first
glutInit(&amp;argc, argv) ;
//window pattern
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPT H) ;
//window size
glutInitWindowSize(800, 500) ;
//window position
glutInitWindowPosition(0, 0) ;
//create window
glutCreateWindow("sphere") ;
//initialization
init() ;
//redraw funtion
glutDisplayFunc(display) ;
//call this function when window size changed or window moved
glutReshapeFunc(reshape) ;
//mouse event function
glutMouseFunc(mouse) ;
//loop function
glutMainLoop() ;

return 0 ;
}

sqrt[-1]
12-25-2006, 08:51 PM
This is due to the screen re-drawing. you need to use double buffering.

Replace
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPT H); with

glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPT H);

RigidBody
12-25-2006, 11:11 PM
not to forget glutSwapBuffers at the end of the drawing routine...

btw, since you have a depth buffer: use glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) instead of glClear(GL_COLOR_BUFFER_BIT).

Debugger
12-26-2006, 02:50 AM
ok, it works well now, thank you all !