1 //参照FreeImage官网给出的CTextrueManager写的加载函数 2 //官方给的例子是用opengl3.0以下的旧GL写的,没有使用glGenerateMipmap(GL_TEXTURE_2D)来产生mipmap 3 //在GL3.0以后,渲染时默认是开启了MIPMAP的,如果不调用glGenerateMipmap来生成MIPMAP,则渲染出的贴图一片黑色 //另外要注意freeimage的颜色分量顺序是gbr,不知为什么,故这里fmt应传GL_BGR 4 GLuint CResMgr::LoadTextrue(const char* texFile, GLenum fmt, GLuint ifmt, GLuint level, GLuint border) { 5 FREE_IMAGE_FORMAT fifmt = FreeImage_GetFileType(texFile, 0); 6 if (fifmt == FIF_UNKNOWN) { 7 fifmt = FreeImage_GetFIFFromFilename(texFile); 8 if (fifmt == FIF_UNKNOWN) { 9 xsMsg(texFile, "图片加载失败:未知的格式", MB_ICONERROR);10 return 0;11 }12 }13 14 if (!FreeImage_FIFSupportsReading(fifmt)) {15 xMsg(texFile, "freeImage不支持该文件格式的读取", MB_ICONERROR);16 return 0;17 }18 19 FIBITMAP* dib = FreeImage_Load(fifmt, texFile);20 if (!dib) {21 xMsg(texFile, "freeImage_load失败", MB_ICONERROR);22 return 0;23 }24 25 BYTE* bits = FreeImage_GetBits(dib);26 GLuint width = FreeImage_GetWidth(dib);27 GLuint height = FreeImage_GetHeight(dib);28 29 if (!bits || width == 0 || height == 0) {30 xMsg(texFile, "freeImage_getbits, getwidth, getheight-failed", MB_ICONERROR);31 return 0;32 }33 34 GLuint texId;35 glGenTextures(1, &texId);36 glBindTexture(GL_TEXTURE_2D, texId);37 glTexImage2D(GL_TEXTURE_2D, level, ifmt, width, height, border, fmt, GL_UNSIGNED_BYTE, bits);38 39 glGenerateMipmap(GL_TEXTURE_2D); //注意,不加这句渲染出的贴图是一片黑色,40 FreeImage_Unload(dib);41 return texId;42 }