static GLint imagewidth;
static GLint imageheight;
static GLint pixellength;
static GLubyte* pixeldata;
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
//绘制像素
glDrawPixels(imagewidth, imageheight, GL_BGR_EXT, GL_UNSIGNED_BYTE, pixeldata);
glutSwapBuffers();
}
int main(int argc, char* argv[])
{
FILE* pfile = fopen("1.bmp", "rb");
if (pfile == 0) exit(0);
//读取图像大小
fseek(pfile, 0x0012, SEEK_SET);
fread(&imagewidth, sizeof(imagewidth), 1, pfile);
fread(&imageheight, sizeof(imageheight), 1, pfile);
//计算像素数据长度
pixellength = imagewidth * 3;
while (pixellength % 4 != 0)pixellength++;
pixellength *= imageheight;
//读取像素数据
pixeldata = (GLubyte*)malloc(pixellength);
if (pixeldata == 0) exit(0);
fseek(pfile, 54, SEEK_SET);
fread(pixeldata, pixellength, 1, pfile);
fclose(pfile);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
cout << pixellength;
glutInitWindowSize(500, 500);
glutCreateWindow("显示bmp");
glutDisplayFunc(display);
glutMainLoop();
free(pixeldata);
return 0;
}