正如標題所示,我一直在加載.tga文件時遇到問題。我一直在使用this作爲參考,據我所知(除了使用C++函數而不是c函數),我正在做同樣的事情。我確實得到了一個紋理,但它應該是一個亂碼版本,我不確定爲什麼。請原諒所有錯誤,我只是想讓它工作。加載Targa文件的問題文件C++
頁眉:
struct TGA
{
GLuint Load(const char* filename);
unsigned char header_[12];
unsigned char bpp_;
unsigned char id_;
unsigned short width_;
unsigned short height_;
unsigned char* data_;
};
.cpp的:
GLuint TGA::Load(const char* filename)
{
width_ = 0; height_ = 0;
bpp_ = 32; id_ = 8;
data_ = 0;
std::ifstream file;
file.open(filename, std::ios::binary | std::ios::ate);
if(file.fail())
{
std::cout<<filename<<" could not be opened."<<std::endl;
return 0;
}
file.seekg(0, std::ios::beg);
file.read((char*)header_, sizeof(unsigned char)*12);
file.read((char*)&width_, sizeof(unsigned short));
file.read((char*)&height_, sizeof(unsigned short));
file.read((char*)&bpp_, sizeof(unsigned char));
file.read((char*)&id_, sizeof(unsigned char));
int imageSize = width_ * height_;
std::cout<<imageSize<<std::endl;
data_ = new unsigned char[imageSize * 3]; //3 means RGB
file.read((char*)data_, imageSize * 3);
file.close();
GLuint texture;
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width_, height_, 0, GL_BGRA, GL_UNSIGNED_BYTE, data_);
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);
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, width_, height_, GL_RGBA, GL_UNSIGNED_BYTE, data_);
delete [] data_;
return texture;
}
不是一個答案..但.. ..確保填充和一次讀取!如果你想讓它變得可移植,請不要忘記endianess .. – 2012-01-28 17:06:12
閱讀文件頭後是什麼'bpp_'? (弗拉德:我相信TGA不像BMP那樣填充)。 – user7116 2012-01-28 17:07:37
bpp應該是每像素或像素深度的位數。 RGB圖像爲24。我試着檢查各個位的添加/對齊,但它們似乎沒有問題。我不確定如何在閱讀數據後將其解析爲一大塊。 – Sullivan 2012-01-28 17:11:57