如何加载8位bmp与OpenGL? [英] How can I load 8 bit bmp with OpenGL?

查看:160
本文介绍了如何加载8位bmp与OpenGL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是我的情况:我需要预先加载2000个图像,并按顺序显示它们是60 fps的动画。目前,我使用OpenGL加载bmp文件,但由于内存限制,我只能预加载高达500+图像。我如何解决这个问题?我可以到目前为止有两个方向的解决方案:首先,也许我可以加载8位bmp图像,以节省内存。但我在使用 glDrawPixels 时遇到困难。其次,如果可能的话我可以直接加载jpeg吗?感谢任何建议!

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!

不使用视频的原因是,我需要改变动画的速度通过跳过一个或多个图像,你可以在代码中看到(imgCount + = stp; // stp表示要逃离的图片数量,它可以使视频更快)。在我的动画中,帧速率很重要,FPS低于50显示闪烁。

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.

以下是代码:

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();

}


推荐答案

不使用 glDrawPixels ,它是不推荐的功能。最好的办法是绘制一个屏幕尺寸的四边形(-1,-1 => 1,1,没有任何矩阵变换)。

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.

对于纹理,您可以在 glTexImage2D 和类似功能中指定多种内部格式。例如,您可以使用 GL_R3_G3_B2 格式来获取您的8位大小,但也可以使用压缩格式,例如 S3TC 。例如,您可以传递 COMPRESSED_SRGB_S3TC_DXT1_EXT ,这应该将您的图像大小减少到每像素4位,可能质量比8位格式更好。你不能在OpenGL中使用JPEG作为压缩格式(它太复杂了)。

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).

最后,你为什么要通过OpenGL做这个?将图像拼接到常规窗口可能会给你足够好的性能。然后你甚至可以存储你的图像序列作为视频,只是blit解码的帧。在这种情况下,你不太可能会遇到内存问题。

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.

这篇关于如何加载8位bmp与OpenGL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆