Here is my situation: I need to preload 2000 images and display them in sequence to be an animation in 60 fps. Currently, I am using OpenGL to load bmp files, but due to memory limit, I can only preload up to 500+ images. How can I solve this problem? I can so far come up with two directions of solutions: First, maybe I can load 8 bit bmp images to save memory. But I have difficulty in using glDrawPixels. Secondly, if possible can I load jpeg directly? Thanks for any advice!
The reason for not using video is that I need to change to animation speed by skipping one or more images as you can see in the code (imgCount+=stp; // stp means how many images to escape. it can make video faster). And in my animation, frame rate is important, FPS lower than 50 shows flickering.
Here is the code:
void Frame::LoadBMP(void){
FILE *in;
in=fopen(file,"rb");//open file
if(in==NULL){
exit(0);
}
fread(&(this->bmfh),sizeof(BITMAPFILEHEADER),1,in);//read bmp file header
fread(&(this->bmih),sizeof(BITMAPINFOHEADER),1,in);//read bmp infomation header
colours=new RGBQUAD[bmih.biBitCount];
fread(colours,sizeof(RGBQUAD),bmih.biBitCount,in);//read bmp colour table
size=bmfh.bfSize-bmfh.bfOffBits;
tempPixelData=new GLubyte[size];
if(tempPixelData==NULL) {
fclose(in);
}
fread(tempPixelData,sizeof(GLubyte),size,in);//read bmp image data
fclose(in);
}
and I will display the sequence of images, the display code:
void display(void){
static clock_t start=clock();
static clock_t end=clock();
CurrtempPixelData=msFrame[offset]->tempPixelData;
glEnable(GL_ALPHA_TEST);
glEnable(GL_BLEND);
glDrawPixels(frWidth, frHeight, GL_RGBA, GL_UNSIGNED_BYTE, msFrame[offset]->tempPixelData);
for(int i=0;i<m;i++){
clock_t c=clock();
}
glutSwapBuffers();
imgCount+=stp; // stp means how many images to escape. it can make video faster.
offset=imgCount%numFrame;
glutPostRedisplay();
}
You should not use glDrawPixels
, it is deprecated functionality. The best way to do it would probably be drawing a screen-sized quad (-1,-1 => 1,1 without any matrix transform) that you texture with these images.
For the textures you can specify several internal formats in glTexImage2D and similar functions. For example, you could use the GL_R3_G3_B2
format to get your 8-bit size, but could as well use the compressed formats like S3TC. You could for example pass COMPRESSED_SRGB_S3TC_DXT1_EXT
, which should reduce your image size to 4 bits per pixel, likely at better quality than the 8 bit format. You cannot use JPEG as a compression format in OpenGL (it's too complex).
Finally, why do you want to do this through OpenGL? blitting an image to a regular window will likely give you well enough performance. Then you could even store your image sequence as video and just blit the decoded frames. It's very unlikely you will ever get memory problems in this case.
Maybe you don't need to have 2000 images and display them at 60fps at all? Stable 25fps is just enough for any movie.
I encourage you to rethink your original problem and come up with a better suited solution (video, animation, vectors, maybe something else)
As for original question:
If you need images only once - put them to memory when you need them and discard them right after displaying.
Use DXT packed images. With a slight degrade in quality you get a constant x4/x8 compression ratio.
OpenGL is not very good at working with paletted textures these days (many vendors have poor implementations). But you can implement that with shaders.
来源:https://stackoverflow.com/questions/11980790/how-can-i-load-8-bit-bmp-with-opengl