代码例子:
void drawPixels() {
FILE *pPixelsFile = 0;
pPixelsFile = fopen("d:\\grab.bmp", "rb");
if(pPixelsFile == 0) {
exit(0);
}
fseek(pPixelsFile, 0x0012, SEEK_SET);
fread(&gImageWidth, sizeof(gImageWidth), 1, pPixelsFile);
fread(&gImageHeigh, sizeof(gImageHeigh), 1, pPixelsFile);
gImageLength = gImageWidth * 3;
while( gImageLength % 4 != 0 ) { // 补充数据,直到i是的倍数
++gImageLength;
}
gImageLength = gImageLength * gImageHeigh;
g_spPixelsBuffer = (GLubyte *)malloc(gImageLength);
if(g_spPixelsBuffer == 0) {
fclose(pPixelsFile);
exit(0);
}
memset(g_spPixelsBuffer, 0, gImageLength);
fseek(pPixelsFile, 54, SEEK_SET);//文件的头部信息,不是图的pixels
fread(g_spPixelsBuffer, gImageLength, 1, pPixelsFile);
fclose(pPixelsFile);
}
void displayShape() {
glDrawPixels(gImageWidth, gImageHeigh, GL_BGR_EXT, GL_UNSIGNED_BYTE, g_spPixelsBuffer);
glutSwapBuffers();
}
int main(int argc, char *argv[]) {
drawPixels();//首先读取文件中的pixels
glutInit(&argc, argv);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(400, 400);
glutCreateWindow("Five-Pointed Star");
glutDisplayFunc(displayShape);
glutSpecialUpFunc(handleMessage);
glutReshapeFunc(reShape);
glutIdleFunc(idleFunc);
glutMainLoop();//后面free(g_spPixelsBuffer)
return 0;
}