【发布时间】:2014-08-31 04:06:34
【问题描述】:
对于我的 OpenGL 项目,我正在做纹理映射。如果我加载 1000 kb tga 文件没有问题,但是当我尝试加载 3000 kb tga 文件时,我在他的行出现分段错误:
tgaFile.data = new unsigned char[imageSize];
然后我尝试加载 BMP 文件。我在这一行遇到分段错误:
glTexImage2D(GL_TEXTURE_2D,
0,
GL_RGBA,
image->width,
image->height,
0,
GL_RGBA,
GL_UNSIGNED_BYTE,
image->pixels); //at this line I get segmentation fault
然后我将 BMP 文件的大小从 512*512 调整为 256*256。然后问题就解决了。但我也想加载大文件。问题是什么?我该如何解决这个问题?我在 Windows 8 上使用 Visual Studio 2013。
我正在像这样初始化图像:
Image* image = loadBMP("yer.bmp");
同时加载BMP函数:
Image* loadBMP(const char* filename) { ifstream input; input.open(filename, ifstream::binary); assert(!input.fail() || !"Could not find file"); char buffer[2]; input.read(buffer, 2); assert(buffer[0] == 'B' && buffer[1] == 'M' || !"Not a bitmap file"); input.ignore(8); int dataOffset = readInt(input); //Read the header int headerSize = readInt(input); int width; int height; switch (headerSize) { case 40: //V3 width = readInt(input); height = readInt(input); input.ignore(2); assert(readShort(input) == 24 || !"Image is not 24 bits per pixel"); assert(readShort(input) == 0 || !"Image is compressed"); break; case 12: //OS/2 V1 width = readShort(input); height = readShort(input); input.ignore(2); assert(readShort(input) == 24 || !"Image is not 24 bits per pixel"); break; case 64: //OS/2 V2 assert(!"Can't load OS/2 V2 bitmaps"); break; case 108: //Windows V4 assert(!"Can't load Windows V4 bitmaps"); break; case 124: //Windows V5 assert(!"Can't load Windows V5 bitmaps"); break; default: assert(!"Unknown bitmap format"); } //Read the data int bytesPerRow = ((width * 3 + 3) / 4) * 4 - (width * 3 % 4); int size = bytesPerRow * height; auto_array<char> pixels(new char[size]); input.seekg(dataOffset, ios_base::beg); input.read(pixels.get(), size); //Get the data into the right format auto_array<char> pixels2(new char[width * height * 3]); for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { for (int c = 0; c < 3; c++) { pixels2[3 * (width * y + x) + c] = pixels[bytesPerRow * y + 3 * x + (2 - c)]; } } } input.close(); return new Image(pixels2.release(), width, height);}
和loadTGA函数:
bool loadTGA(const char *filename, STGA& tgaFile)
{
FILE *file;
unsigned char type[4];
unsigned char info[6];
file = fopen(filename, "rb");
if (!file)
return false;
fread (&type, sizeof (char), 3, file);
fseek (file, 12, SEEK_SET);
fread (&info, sizeof (char), 6, file);
//image type either 2 (color) or 3 (greyscale)
if (type[1] != 0 || (type[2] != 2 && type[2] != 3))
{
fclose(file);
return false;
}
tgaFile.width = info[0] + info[1] * 256;
tgaFile.height = info[2] + info[3] * 256;
tgaFile.byteCount = info[4] / 8;
if (tgaFile.byteCount != 3 && tgaFile.byteCount != 4) {
fclose(file);
return false;
}
long imageSize = tgaFile.width * tgaFile.height
* tgaFile.width * tgaFile.byteCount;
//allocate memory for image data
tgaFile.data = new unsigned char[imageSize];
//read in image data
fread(tgaFile.data, sizeof(unsigned char), imageSize, file);
//close file
fclose(file);
return true;
}
【问题讨论】:
-
你是如何初始化
image和tgaFile的?imageSize是什么?您的问题中几乎没有足够的信息。 -
使用调试器单步调试程序并在调用
glTexImage2D之前查看image。
标签: c++ c opengl segmentation-fault texture-mapping