gpt4 book ai didi

c++ - 加载 "big"纹理文件时出现段错误

转载 作者:太空宇宙 更新时间:2023-11-04 04:42:33 25 4
gpt4 key购买 nike

对于我的 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");

还有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);

}

并加载TGA函数:

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;
}

最佳答案

我解决了我的问题。问题是因为我使用 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);

当我进行此更改时,它起作用了。

关于c++ - 加载 "big"纹理文件时出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24665409/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com