My animation flickering.I have two texture: a backgroung texture and
move a pacman texture on it.
How I can resolve my problem?
I use double buffering and glut.
Post my code:
#include <windows.h>
#include <stdio.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include “Texture.h”
GLuint texName[3];
GLfloat dx=-10;
GLfloat dy=10;
GLfloat dz=0;
bool KeyUP=false;
bool KeyDOWN=false;
bool KeyRIGHT=false;
bool KeyLEFT=false;
bool open=false;
void init(void)
{
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
//glClearDepth(1.0);
glEnable (GL_BLEND);
glEnable(GL_TEXTURE_2D);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glShadeModel (GL_SMOOTH);
glGenTextures(3, texName);
Texture(“level.png”,texName[0]); // texture
background
Texture(“palla2.png”,texName[1]); // texture
pacman1
Texture(“palla2a.png”,texName[2]); // texture
pacman2
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
static int sumtime;
static int lasttime;
int dt;
dt=glutGet(GLUT_ELAPSED_TIME)-lasttime;
sumtime+=dt;
glBlendFunc(GL_ONE,GL_ZERO);
glMatrixMode(GL_MODELVIEW);
glBindTexture(GL_TEXTURE_2D,texName[0]);
glLoadIdentity();
gluLookAt(0,0,5,0,0,0,0,1,0);
glBegin(GL_QUADS); // draw the
background
glTexCoord2f(0, 0);
glVertex3f(-10,10,0);
glTexCoord2f(1, 0);
glVertex3f(10,10,0);
glTexCoord2f(1, 1);
glVertex3f(10,-10,0);
glTexCoord2f(0, 1);
glVertex3f(-10,-10,0);
glEnd();
// filter black pixel
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
if (KeyDOWN) dy-=0.1;
if (KeyUP) dy+=0.1;
if (KeyLEFT) dx-=0.1;
if (KeyRIGHT) dx+=0.1;
if (sumtime>=500){
open=!open;
sumtime=0;
}
if (open) glBindTexture(GL_TEXTURE_2D,texName[2]);
else glBindTexture(GL_TEXTURE_2D,texName[1]);
glTranslatef(dx,dy,dz); // draw pacman texture
glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glVertex3f(-1.5,1.5,1);
glTexCoord2f(1, 0);
glVertex3f(1.5,1.5,1);
glTexCoord2f(1, 1);
glVertex3f(1.5,-1.5,1);
glTexCoord2f(0, 1);
glVertex3f(-1.5,-1.5,1);
glEnd();
glutSwapBuffers();
}
void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
glOrtho(-10,10,-10,10,0,10);
}
/
void KeyDown(int key,int x,int y){
switch(key){
case GLUT_KEY_UP:
KeyUP=true;
break;
case GLUT_KEY_DOWN:
KeyDOWN=true;
break;
case GLUT_KEY_LEFT:
KeyLEFT=true;
break;
case GLUT_KEY_RIGHT:
KeyRIGHT=true;
break;
}
}
void KeyUp(int key,int x,int y){
switch(key){
case GLUT_KEY_UP:
KeyUP=false;
break;
case GLUT_KEY_DOWN:
KeyDOWN=false;
break;
case GLUT_KEY_LEFT:
KeyLEFT=false;
break;
case GLUT_KEY_RIGHT:
KeyRIGHT=false;
break;
}
}
void Idle(void)
{
glutPostRedisplay();
}
void Visible(int vis)
{
if (vis == GLUT_VISIBLE)
glutIdleFunc(Idle);
else
glutIdleFunc(NULL);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH );
glutInitWindowSize (800, 800);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
init ();
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutIdleFunc(Idle);
glutVisibilityFunc(Visible);
glutSpecialFunc(KeyDown);
glutSpecialUpFunc(KeyUp);
glutMainLoop();
return 0;
}