【发布时间】:2015-11-22 21:53:19
【问题描述】:
我已经尝试了一段时间,网上没有任何地方可以为我的问题提供有效的解决方案。我看过教程、mdsn 和这个网站,但什么也没找到。 我有一个位图加载器:
void GLImage::LoadTexture(const char* filename) //load 24bit bitmap images
{
unsigned int texture;
unsigned char info[54]; //the header
FILE * file;
file = fopen(filename, "rb"); //open the file
fread(info, sizeof(unsigned char), 54, file); //read the header for the bmp file
//get image width and height from header
int width = *(int*)&info[18];
int height = *(int*)&info[22];
int size = 3 * width * height; //3 bytes for the colour
unsigned char* data = new unsigned char[size]; //where the image information is located
fread(data, sizeof(unsigned char), size, file); //read the image and save to data
fclose(file);//close the file
for (int i = 0; i < size; i += 3) //save pixel data to data
{
unsigned char tmp = data[i];
data[i] = data[i + 2];
data[i + 2] = tmp;
}
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
this->tex = texture;
delete[] data;
}
效果很好。 现在我正试图让它工作,但使用 C++ 中的 OpenFileDialog,而不是表单应用程序版本(这真的很简单)。
我有一个类然后可以打开文件,并会得到正确的文件路径。但是,将文件类型保存为 tchar 而不是 char。这意味着我的位图加载器不会允许它。 有什么办法吗
A) 获取 OpenFileDialog 以将文件路径作为字符获取。
或
B) 将 tchar 转换为 char。
【问题讨论】:
-
你的纹理加载器坏了。它无法打开文件名包含当前代码页之外的字符的纹理文件。您需要使用 Unicode。如果你想坚持
const char*,你可以传递一个UTF-8编码的字符串,并在调用_wfopen之前将其转换为UTF-16。当然,GetOpenFileNameW 返回后,您还必须从 UTF-16 转换为 UTF-8。 -
您从哪里获得 BMP 文件阅读器代码?说真的,你在那里使用的这段代码一次又一次地弹出,但它是错误的、不安全的,不应该使用。谁先写了它,就需要受到纪律处分。 BMP 是一种棘手的文件格式野兽,它允许许多事情和一些事情,例如反向扫描线转换实际上会使您在那里使用的代码崩溃。这是一个 BMP 文件的集合,其中包含各种类型的文件:entropymine.com/jason/bmpsuite/bmpsuite/html/bmpsuite.html – 唯一可能在不崩溃的情况下读取的文件是
g/rgb24.bmp -
朋友不要让朋友自己写BMP文件解析器。
-
扩展 datenwolf 的评论,Windows 内置了一个 BMP 解码器。它被称为Windows Imaging Component (WIC)。