对于我的OpenGL项目,我正在进行txture映射。如果我加载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。然后问题解决了。但我想加载大文件。问题是什么?我怎么解决这个问题?我在Wİndows8上使用Visual Studio 2013。
我正在初始化图像:
Image* image = loadBMP("yer.bmp");
还有loadBMP功能:
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;
}
答案 0 :(得分:0)
我解决了我的问题。问题是因为我使用RGBA作为内部格式我告诉opengl每像素使用4个字节。然而它实际上是3。
glTexImage2D(GL_TEXTURE_2D,
0,
GL_RGBA,
image->width,
image->height,
0,
GL_RGB,//The change is here
GL_UNSIGNED_BYTE,
image->pixels);
当我做出这个改变时,它起作用了。