gpt4 book ai didi

C++ OpenGL TGA 加载失败

转载 作者:太空宇宙 更新时间:2023-11-04 14:19:20 24 4
gpt4 key购买 nike

我一直在学习有关加载 TGA 文件的基本 OpenGl 教程,以用作 3d 对象的纹理。我已经能够从 TGA header 加载数据,但是当我尝试加载实际图像数据时,它失败了。我不确定哪里出了问题。这是我的纹理加载类:

头文件:

    struct TGA_Header 
{
GLbyte ID_Length;
GLbyte ColorMapType;
GLbyte ImageType;
// Color map specifications
GLbyte firstEntryIndex[2];
GLbyte colorMapLength[2];
GLbyte colorMapEntrySize;

//image specification
GLshort xOrigin;
GLshort yOrigin;
GLshort ImageWidth;
GLshort ImageHeight;
GLbyte PixelDepth;
GLbyte ImageDescriptor;
};

class Texture
{
public:
Texture(string in_filename, string in_name = "");
~Texture();

public:
unsigned short width;
unsigned short height;
unsigned int length;
unsigned char type;
unsigned char *imageData;
unsigned int bpp;
unsigned int texID;

string name;

static vector<Texture *> textures;

private:
bool loadTGA(string filename);
bool createTexture(unsigned char *imageData, int width, int height, int type);

void swap(unsigned char * ori, unsigned char * dest, GLint size);
void flipImage(unsigned char * image, bool flipHorizontal, bool flipVertical, GLushort width, GLushort height, GLbyte bpp);
};

这是cpp中的加载TGA函数:

bool Texture::loadTGA(string filename)
{
TGA_Header TGAheader;

ifstream file( filename.data(), std::ios::in, std::ios::binary );

//make sure the file was opened properly
if (!file.is_open() )
return false;

if( !file.read( (char *)&TGAheader, sizeof(TGAheader) ) )
return false;


//make sure the image is of a type we can handle
if( TGAheader.ImageType != 2 )
return false;

width = TGAheader.ImageWidth;
height = TGAheader.ImageHeight;
bpp = TGAheader.PixelDepth;

if( width < 0 || // if the width or height is less than 0, than
height <= 0 || // the image is corrupt
(bpp != 24 && bpp != 32) ) // make sure we are of the correct bit depth
{
return false;
}

//check for an alpha channel
GLuint type = GL_RGBA;
if ( bpp == 24 )
type = GL_RGB;

GLuint bytesPerPixel = bpp / 8;

//allocate memory for the TGA so we can read it
GLuint imageSize = width * height * bytesPerPixel;
imageData = new GLubyte[imageSize];

if ( imageData == NULL )
return false;

//make sure we are in the correct position to load the image data
file.seekg(-imageSize, std::ios::end);

// if something when wrong, make sure we free up the memory
//NOTE: It never gets past this point. The conditional always fails.
if ( !file.read( (char *)imageData, imageSize ) )
{
delete imageData;

return false;
}

//more code is down here, but it doesnt matter because it does not pass the above function
}

好像加载了一些数据,但是一直返回失败。任何关于为什么的帮助将不胜感激。抱歉,如果它变得有点罗嗦,但我不确定什么是重要的或不重要的。

更新:所以,我只是重写了函数。我使用的 ifsteam 似乎是问题的原因。具体来说,它会尝试加载比我输入的多得多的数据字节。我不知道行为的原因,但我在下面列出了我的功能代码。谢谢大家的帮助。

最佳答案

问题可能取决于不支持压缩 TGA 的 TGA 算法。

确保您没有压缩 TGA 并且 TGA 顺序(不太重要)位于左下原点。

我通常使用 GIMP,此时,取消选中 RLE 压缩并放置左下对齐。

关于C++ OpenGL TGA 加载失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8846525/

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