#include "utils.h" unsigned char * DecodeBMP(unsigned char * bmpFileData, int & width, int & height) { if (0x4D42 == *((unsigned short*)bmpFileData)) {//通过前两个字节去判断数据是否是BMP数据 int pixelDataOffset = *((int*)(bmpFileData + 10));//取出位图像素数据的偏移地址、 width = *((int*)(bmpFileData + 18));//获取图片宽度 height = *((int*)(bmpFileData + 22));//获取图片宽度 /*读取图片内容*/ unsigned char* pixelData = bmpFileData + pixelDataOffset; for (int i = 0; i < width * height * 3; i += 3) { unsigned char temp = pixelData[i]; //BGR 转换成 RGB pixelData[i] = pixelData[i + 2]; pixelData[i + 2] = temp; } return pixelData; } return nullptr; } GLuint CreateTexture2D(unsigned char * pixelData, int width, int height, GLenum type) { GLuint texture; glGenTextures(1, &texture); //绑定纹理 glBindTexture(GL_TEXTURE_2D, texture); //设置纹理放大/缩小时候数据采集的算法 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);//使用线性过滤 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);//使用线性过滤 //设置当纹理坐标大于1.0时候的数据采集方式 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); //数据上传到显卡 glTexImage2D(GL_TEXTURE_2D, 0, type, width, height, 0, type, GL_UNSIGNED_BYTE, pixelData); //释放纹理 glBindTexture(GL_TEXTURE_2D, 0); return texture; } GLuint CreateTexture2DFromBMP(const char * bmpPath) { int nFileSize = 0; unsigned char* bmpFileContent = LoadFileContent(bmpPath, nFileSize); if (bmpFileContent == nullptr) { return 0; } int bmpWidth = 0; int bmpHeight = 0; unsigned char* pixelData = DecodeBMP(bmpFileContent, bmpWidth, bmpHeight); if (bmpWidth == 0) { delete bmpFileContent; return 0; } GLuint textrue = CreateTexture2D(pixelData, bmpWidth, bmpHeight, GL_RGB); return textrue; } GLuint CreateDisplayList(std::function foo) { GLuint displayList = glGenLists(1);//申请一个显示列表 glNewList(displayList, GL_COMPILE); foo(); glEndList(); return displayList; }