我想为一个带有纹理的方形框(即 .raw 图像文件)制作动画。我的背景还包括一个更大的盒子,上面还有一个图像。
我正在使用 timer() 函数来为盒子设置动画。问题是当我不断增加背景图像时,我的动画会变慢。我怎样才能提高它的速度..?
raw_texture_load
GLuint raw_texture_load1( const char * filename, int width , int height)
{
GLuint texture;
unsigned char *data;
FILE * file;
// open texture data
file = fopen( filename, "rb" );
if ( file == NULL ) return 0;
// allocate buffer
data= (unsigned char*) malloc(width * height*3);
// read texture data
fread( data, width * height*3 , 1, file );
fclose( file );
// allocate a texture name
glGenTextures( 1, &texture );
// select our current texture
glBindTexture( GL_TEXTURE_2D, texture );
// select modulate to mix texture with color for shading
glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
// when texture area is small, bilinear filter the closest MIP map
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_LINEAR_MIPMAP_NEAREST );
// when texture area is large, bilinear filter the first MIP map
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
// if wrap is true, the texture wraps over at the edges (repeat)
// ... false, the texture ends at the edges (clamp)
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
GL_REPEAT );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
GL_REPEAT );
// build our texture MIP maps
gluBuild2DMipmaps( GL_TEXTURE_2D, 3, width,
height, GL_RGB, GL_UNSIGNED_BYTE, data );
// free buffer
free( data );
return texture;
}
主要代码
.
.
GLuint texture1 ;
GLuint texture2 ;
GLuint raw_texture_load1( const char * filename, int width , int height );
GLuint raw_texture_load2( const char * filename, int width , int height );
int refreshMills = 10 ;
.
.
.
void initGL() {
glClearColor(0.0f, 0.0f, 1.0f, 0.0f);
texture1 = raw_texture_load1("level.raw", 700, 700);
texture2 = raw_texture_load2("mario2.raw", 100, 100);
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT); // Clear the color buffer
glMatrixMode(GL_MODELVIEW); // To operate on Model-View matrix
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D , texture1);
glBegin(GL_QUADS) ;
glTexCoord2d(0,1);glVertex3f(-1.0, -1.0, 0.0);
glTexCoord2d(1,1);glVertex3f(1.0, -1.0, 0.0);
glTexCoord2d(1,0);glVertex3f(1.0, 1.0, 0.0);
glTexCoord2d(0,0);glVertex3f(-1.0, 1.0, 0.0);
glEnd() ;
glDisable(GL_TEXTURE_2D);
glPopMatrix() ;
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D , texture2) ;
glBegin(GL_QUADS);
glTexCoord2d(0,1);glVertex3f(0.2+move, 0.0, 0.0);
glTexCoord2d(1,1);glVertex3f(0.0+move, 0.0, 0.0);
glTexCoord2d(1,0);glVertex3f(0.0+move, 0.2, 0.0);
glTexCoord2d(0,0);glVertex3f(0.2+move, 0.2, 0.0);
glEnd();
glDisable(GL_TEXTURE_2D);
glPopMatrix() ;
glutSwapBuffers() ;
}
void Timer(int value)
{
glutPostRedisplay(); // Post re-paint request to activate display()
glutTimerFunc(refreshMills, Timer, 0); // next Timer call milliseconds later
}