2014-07-09 4 views
1

내 OpenGL 프로젝트의 경우 txture 매핑을 수행하고 있습니다. 내가 1,000킬로바이트 TGA 파일을로드하면 아무 문제가 없다,하지만 난 3,000킬로바이트 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으로 조정합니다. 그런 다음 문제가 해결되었습니다. 하지만 큰 파일도로드하고 싶습니다. 문제가 무엇입니까? 이 문제를 어떻게 해결할 수 있습니까? 나는 창에서 Visual Studio 2013를 사용하고 8

내가 초기화하고있어 같은 이미지 :

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

어떻게'image'와'tgaFile'을 초기화합니까? 'imageSize' 란 무엇입니까? 귀하의 질문에 정보가 충분하지 않습니다. – user657267

+0

디버거를 사용하여 프로그램을 단계별로 실행하고'glTexImage2D '를 호출하기 직전에'image'를 살펴보십시오. – user657267

답변

0

나는 나의 문제를 해결했다. 문제는 RGBA를 내부 형식으로 사용하고 있기 때문에 픽셀 당 4 바이트를 사용하도록 opengl에 지시하고 있기 때문입니다. 그러나 실제로 3입니다.

glTexImage2D(GL_TEXTURE_2D, 
0, 
GL_RGBA, 
image->width, 
image->height, 
0, 
GL_RGB,//The change is here 
GL_UNSIGNED_BYTE, 
image->pixels); 

이 변경 작업을 수행하면 효과가있었습니다.