GLint TextureManager::generateTextureFromPNG(char* url) {
// 下载图片
DownloadResources dr;
initDownloadResource(&dr, url);
download_image(&dr);
CxImage image((BYTE*) dr.memoeryBuff, dr.size, CXIMAGE_FORMAT_PNG);
// TODO:这里能先释放dr的内存吗
if (image.IsValid()) {
logInfo(“load image ok %s:”, url);
} else {
logError(“load image error %s:”, url);
}
int width = image.GetWidth();
int height = image.GetHeight();
GLuint ids[1];
glGenTextures(1, ids);
LOGI(“gl texture id:%d”, ids[0]);
glBindTexture(GL_TEXTURE_2D, ids[0]);
BYTE* glimagedata = NULL;
if (image.AlphaIsValid()) {
long t = width * height * 4;
// 有alpha通道
if (!image.Encode2RGBA(glimagedata, t)) {
LOGE(“image encode 2 rgba error.%s”, image.GetLastError());
}
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA,
GL_UNSIGNED_BYTE, glimagedata);
checkGlError(“generate texture”);
} else {
LOGE(“image is not support alpha.”);
}
image.FreeMemory(glimagedata); //释放内存空间
freeDownloadResource(&dr); // 释放掉下载的文件占用的内存
return ids[0];
}
0 条评论。