#include<GLTools.h>
#include <GL/glut.h>
#include"windows.h"
#include <stdio.h>
#include <stdlib.h>
#define FileName "SeaAndTree.bmp"
static GLint ImageWidth; //BMP图像宽
static GLint ImageHeight; //BMP图像高
static GLint PixelLength; //图像数据长度
static GLubyte* PixelData;
void ReadBmpFile()
{
// 打开文件
FILE* pFile;
pFile=_fsopen("SeaAndTree.bmp", "rb", 64);
if (pFile == 0)
MessageBox(NULL, L"file", L"fileshibai", MB_OK);
// 读取图象的宽高信息
fseek(pFile, 0x0012, SEEK_SET);
fread(&ImageWidth, sizeof(ImageWidth), 1, pFile);
fread(&ImageHeight, sizeof(ImageHeight), 1, pFile);
if (ImageWidth == 0)
MessageBox(NULL, L"ImageWidth", L"ImageWidth", MB_OK);
// 计算像素数据长度,不足4位补齐4位
PixelLength = ImageWidth * 3;
while (PixelLength % 4 != 0)
++PixelLength;
PixelLength *= ImageHeight;
if (PixelLength == 0)
MessageBox(NULL, L"PixelLength", L"PixelLength", MB_OK);
// 读取像素数据
PixelData = (GLubyte*)malloc(100);
//if (PixelData == 0)
//MessageBox(NULL, L"file", L"PixelData", MB_OK);
fseek(pFile, 54, SEEK_SET); //跳过文件头和信息头
fread(PixelData, PixelLength, 1, pFile);
// 关闭文件
fclose(pFile);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glPixelZoom(0.5f, 0.5f);
glRasterPos2f(-0.5, -0.5);
// 绘制像素
glDrawPixels(ImageWidth, ImageHeight,
GL_BGR_EXT, GL_UNSIGNED_BYTE, PixelData);
glutSwapBuffers();
}
int main(int argc, char* argv[])
{
ReadBmpFile(); //读取BMP图像数据到内存,并获取图像的宽高信息
// 初始化GLUT并运行
gltSetWorkingDirectory(argv[0]);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_STENCIL);
glutInitWindowPosition(100, 100);
glutInitWindowSize(ImageWidth, ImageHeight);
glutCreateWindow(FileName);
glutDisplayFunc(&display);
glutMainLoop();
// 释放内存
free(PixelData);
return 0;
}